diff --git a/action.yml b/action.yml index e7ee52e..c7f385d 100644 --- a/action.yml +++ b/action.yml @@ -109,11 +109,17 @@ save_file = os.environ.get("INPUT_SAVE_TO_FILE", "") subprocess.run(["git", "fetch", "--tags", "--prune"], capture_output=True) if not from_ref: - code, out = subprocess.run(["git", "describe", "--tags", f"--match={tag_pattern}", "--exact-match", to_ref], capture_output=True, text=True).returncode, "" - if code == 0: - res = subprocess.run(["git", "describe", "--tags", f"--match={tag_pattern}", "--abbrev=0", f"{to_ref}^"], capture_output=True, text=True) + if version: + target_tag = version if version.startswith("v") else f"v{version}" + res = subprocess.run(["git", "describe", "--tags", f"--match={tag_pattern}", "--abbrev=0", f"{target_tag}^"], capture_output=True, text=True) if res.returncode == 0 and res.stdout.strip(): from_ref = res.stdout.strip() + if not from_ref: + code, out = subprocess.run(["git", "describe", "--tags", f"--match={tag_pattern}", "--exact-match", to_ref], capture_output=True, text=True).returncode, "" + if code == 0: + res = subprocess.run(["git", "describe", "--tags", f"--match={tag_pattern}", "--abbrev=0", f"{to_ref}^"], capture_output=True, text=True) + if res.returncode == 0 and res.stdout.strip(): + from_ref = res.stdout.strip() if not from_ref: res = subprocess.run(["git", "describe", "--tags", f"--match={tag_pattern}", "--abbrev=0", to_ref], capture_output=True, text=True) if res.returncode == 0 and res.stdout.strip(): diff --git a/scripts/generate_notes.py b/scripts/generate_notes.py index 679074e..98cb3ce 100755 --- a/scripts/generate_notes.py +++ b/scripts/generate_notes.py @@ -41,7 +41,15 @@ def run_git(args): return 1, "", str(e) -def find_previous_tag(to_ref, tag_pattern): +def find_previous_tag(to_ref, tag_pattern, version=""): + # 0. If a target version is provided (e.g. 1.8.0), check if tag v$VERSION already exists. + # If it does, the commits for this release start from the tag BEFORE v$VERSION: + if version: + target_tag = version if version.startswith("v") else f"v{version}" + code_prev, prev_tag, _ = run_git(["describe", "--tags", f"--match={tag_pattern}", "--abbrev=0", f"{target_tag}^"]) + if code_prev == 0 and prev_tag: + return prev_tag + # 1. Check if to_ref itself is tagged with an exact tag matching tag_pattern: if tag_pattern: code, stdout, _ = run_git(["describe", "--tags", f"--match={tag_pattern}", "--exact-match", to_ref]) @@ -72,12 +80,12 @@ def find_previous_tag(to_ref, tag_pattern): return None -def collect_commits(from_ref, to_ref, tag_pattern, max_commits): +def collect_commits(from_ref, to_ref, tag_pattern, max_commits, version=""): # Ensure tags are up-to-date run_git(["fetch", "--tags", "--prune"]) if not from_ref: - from_ref = find_previous_tag(to_ref, tag_pattern) + from_ref = find_previous_tag(to_ref, tag_pattern, version) commits = "" if from_ref: @@ -179,7 +187,7 @@ def query_llm(args, commits_text): def main(): args = parse_args() - commits = collect_commits(args.from_ref, args.to_ref, args.tag_pattern, args.max_commits) + commits = collect_commits(args.from_ref, args.to_ref, args.tag_pattern, args.max_commits, args.version) if not commits: sys.stderr.write("[action-release-notes] No commits discovered; using fallback release notes.\n")