diff --git a/.github/workflows/release-pr.yml b/.github/workflows/release-pr.yml index eb4bd01..5ad815a 100644 --- a/.github/workflows/release-pr.yml +++ b/.github/workflows/release-pr.yml @@ -23,7 +23,7 @@ # `gh pr create` takes a 422, and `set -euo pipefail` turns that into a red # "release PR pipeline failed" alert for a PR that exists and is fine. # -# The ahead-check is kept even though the freeze caller guards on freeze success — +# The tree check is kept even though the freeze caller guards on freeze success — # a manual workflow_dispatch can invoke this workflow directly. # # The PR is created with the `mindsdb-release-train` App token (NOT the default @@ -119,32 +119,43 @@ jobs: BASE: ${{ inputs.base-branch }} run: | set -euo pipefail + # The tree decides. A commit count here would be `main..staging`, which + # on a squash-merge repo counts every commit a release already shipped. if git diff --quiet "origin/${BASE}" "origin/${STAGING}"; then - AHEAD=0 - else - AHEAD=$(git rev-list --count "origin/${BASE}..origin/${STAGING}") - fi - echo "ahead=$AHEAD" >> "$GITHUB_OUTPUT" - if [ "$AHEAD" -eq 0 ]; then + echo "changed=false" >> "$GITHUB_OUTPUT" echo "${STAGING} is up to date with ${BASE} — nothing to release." else - echo "${STAGING} is ${AHEAD} commit(s) ahead of ${BASE}." + echo "changed=true" >> "$GITHUB_OUTPUT" + echo "${STAGING}'s tree differs from ${BASE}'s; the next step lists what no release has shipped." fi - name: Create or update PR - if: steps.check.outputs.ahead != '0' + if: steps.check.outputs.changed == 'true' env: GH_TOKEN: ${{ steps.app-token.outputs.token }} REPO: ${{ github.repository }} - AHEAD: ${{ steps.check.outputs.ahead }} STAGING: ${{ inputs.staging-branch }} BASE: ${{ inputs.base-branch }} FROZEN: ${{ steps.freeze.outputs.frozen }} run: | set -euo pipefail - COMMITS=$(git log --oneline --no-merges "origin/${BASE}..origin/${STAGING}") - CONTRIBUTORS=$(git log --format='%an' "origin/${BASE}..origin/${STAGING}" | sort -u | sed 's/^/- /') + # Only what no release has shipped: `main..staging` keeps every commit a + # squash-merged release shipped, and its list outgrew GitHub's 65,536 + # character body limit. The script says where the list starts and why. + RELEASE=$(python3 .ci-shared/scripts/release_commits.py \ + --repo "${REPO}" --base "${BASE}" --staging "${STAGING}") + COUNT=$(jq -r '.count' <<<"$RELEASE") + COMMITS=$(jq -r '.commits' <<<"$RELEASE") + CONTRIBUTORS=$(jq -r '.contributors' <<<"$RELEASE") + OMITTED=$(jq -r '.omitted' <<<"$RELEASE") + # A cut list links this run, whose summary the script filled with every + # commit. A compare link can't stand in: GitHub fails to render a range + # long enough to need the cut. + MORE_LINE="" + if [ "${OMITTED}" -gt 0 ]; then + MORE_LINE="…and ${OMITTED} more commit(s), cut to fit GitHub's body limit. The [run summary](${GITHUB_SERVER_URL}/${REPO}/actions/runs/${GITHUB_RUN_ID}) lists all ${COUNT}." + fi # Find the PR before writing about it. The body describes whether this is # a draft, and only the PR itself knows that: an existing PR may already @@ -179,12 +190,13 @@ jobs: BODY=$(cat < main` PR. A commit is listed when it is on staging and reachable +from neither main nor that head. With merge-commit releases the head is already +on main, so the list is `main..staging`, exactly as before. With no previous +release, or a head staging no longer contains (staging was rebuilt), it falls +back to `main..staging` and says why. + +The Changes list is cut to a character budget, so a long but correct list (a +long freeze, a first release) still opens the PR. The full list goes to the +run's step summary, and the caller links that run when the list was cut. +GitHub's compare view is no substitute: it fails to render a range that long. + +Run it inside the caller's checkout, with `origin/` and +`origin/` fetched at full depth. It prints one JSON object. +""" + +from __future__ import annotations + +import argparse +import json +import os +import subprocess +import sys +from dataclasses import asdict, dataclass +from typing import Callable, Sequence + +# A command runner, injectable so the tests can answer `gh` without a network. +Runner = Callable[[Sequence[str]], "subprocess.CompletedProcess[str]"] + +# GitHub refuses a PR body over 65,536 characters. The rest of the body (title +# line, contributors, checklist, state line) stays well under 5,000, so the +# commit list gets the remainder. +DEFAULT_MAX_CHARS = 60_000 + +# Enough merged release PRs to find the newest even when `gh pr list` orders +# them by creation rather than by merge time. +RELEASE_LOOKBACK = 20 + + +@dataclass(frozen=True) +class ReleaseCommits: + """The unreleased commits, as the release PR shows them.""" + + since: str # the commit the list starts after: the last release head, or origin/ + since_reason: str # why that start point, for the run log + lookup_failed: bool # the merged-PR lookup errored, so the list fell back to origin/ + count: int # every unreleased non-merge commit, listed or not + commits: str # `git log --oneline` lines, cut to the budget + all_commits: str # every unreleased `git log --oneline` line, uncut, for the run summary + omitted: int # commits cut from `commits` by the budget + contributors: str # "- " lines of every unreleased commit, listed or cut, sorted and unique + + +def _run(argv: Sequence[str]) -> "subprocess.CompletedProcess[str]": + return subprocess.run(list(argv), capture_output=True, text=True, check=False) + + +def _git(runner: Runner, *args: str) -> str: + result = runner(["git", *args]) + if result.returncode != 0: + raise RuntimeError(f"git {' '.join(args)} failed: {result.stderr.strip()}") + return result.stdout + + +def last_release_head(runner: Runner, repo: str, base: str, staging: str) -> str | None: + """The head commit of the newest merged `staging -> base` PR, or None if there is none. + + A failed lookup raises: "no release yet" and "could not ask" lead to the same + fallback, but only the second deserves a warning in the run log. + """ + result = runner( + [ + "gh", "pr", "list", + "--repo", repo, + "--base", base, + "--head", staging, + "--state", "merged", + "--limit", str(RELEASE_LOOKBACK), + "--json", "headRefOid,mergedAt", + ] + ) + if result.returncode != 0: + raise RuntimeError(f"gh pr list failed: {result.stderr.strip()}") + merged = [pr for pr in json.loads(result.stdout or "[]") if pr.get("mergedAt")] + if not merged: + return None + return max(merged, key=lambda pr: pr["mergedAt"])["headRefOid"] + + +def _is_ancestor(runner: Runner, commit: str, of: str) -> bool: + result = runner(["git", "merge-base", "--is-ancestor", commit, of]) + if result.returncode in (0, 1): + return result.returncode == 0 + # 128 and the rest: the commit is not in this clone, so staging cannot contain it. + return False + + +def release_commits( + runner: Runner, + repo: str, + base: str, + staging: str, + max_chars: int = DEFAULT_MAX_CHARS, +) -> ReleaseCommits: + base_ref = f"origin/{base}" + staging_ref = f"origin/{staging}" + + lookup_failed = False + try: + head = last_release_head(runner, repo, base, staging) + except RuntimeError as exc: + head, lookup_failed = None, True + reason = f"could not look up the last release ({exc}); listing {base_ref}..{staging_ref}" + else: + if head is None: + reason = f"no merged {staging} -> {base} PR yet; listing {base_ref}..{staging_ref}" + elif not _is_ancestor(runner, head, staging_ref): + reason = f"last release head {head[:12]} is not on {staging_ref}; listing {base_ref}..{staging_ref}" + head = None + else: + reason = f"last release head {head[:12]}" + + # On staging, and reachable from neither main nor the last release head. + excluded = [f"^{base_ref}"] + ([f"^{head}"] if head else []) + since = head or _git(runner, "rev-parse", base_ref).strip() + + lines = _git(runner, "log", "--oneline", "--no-merges", staging_ref, *excluded).splitlines() + authors = _git(runner, "log", "--format=%an", "--no-merges", staging_ref, *excluded).splitlines() + + kept: list[str] = [] + used = 0 + for line in lines: + if used + len(line) + 1 > max_chars: + break + kept.append(line) + used += len(line) + 1 + + return ReleaseCommits( + since=since, + since_reason=reason, + lookup_failed=lookup_failed, + count=len(lines), + commits="\n".join(kept), + all_commits="\n".join(lines), + omitted=len(lines) - len(kept), + contributors="\n".join(f"- {name}" for name in sorted(set(authors))), + ) + + +def main(argv: Sequence[str] | None = None, runner: Runner = _run) -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("--repo", required=True, help="owner/name, for the merged-PR lookup") + parser.add_argument("--base", required=True, help="the release target branch, e.g. main") + parser.add_argument("--staging", required=True, help="the release source branch, e.g. staging") + parser.add_argument("--max-chars", type=int, default=DEFAULT_MAX_CHARS) + args = parser.parse_args(argv) + + result = release_commits(runner, args.repo, args.base, args.staging, args.max_chars) + level = "warning" if result.lookup_failed else "notice" + print(f"::{level}::Release PR lists from {result.since_reason}: {result.count} commit(s).", file=sys.stderr) + + # The run summary holds every commit, so the PR body can link it when the + # list there was cut. A step summary holds 1 MiB, about 12,000 lines. + summary = os.environ.get("GITHUB_STEP_SUMMARY") + if summary: + with open(summary, "a", encoding="utf-8") as out: + out.write( + f"### Release PR list: {result.count} commit(s)\n\n" + f"Start point: {result.since_reason}.\n\n```\n{result.all_commits}\n```\n" + ) + + print(json.dumps(asdict(result))) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/test_release_commits.py b/tests/test_release_commits.py new file mode 100644 index 0000000..1d2536c --- /dev/null +++ b/tests/test_release_commits.py @@ -0,0 +1,261 @@ +"""Unit tests for the release PR's commit list (``scripts/release_commits.py``). + +Each test builds a real git history in a scratch repository and answers only the +`gh pr list` call from a fake. The history that matters is the squash-merge +release: staging's shipped commits never reach main, so a list built from +`main..staging` grows with every release. The three-release test fails on that +range and passes on the fixed one. +""" + +import importlib.util +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +_PATH = Path(__file__).resolve().parents[1] / "scripts" / "release_commits.py" +_spec = importlib.util.spec_from_file_location("release_commits", _PATH) +release_commits = importlib.util.module_from_spec(_spec) +# A dataclass looks its module up in sys.modules while it is defined. +sys.modules["release_commits"] = release_commits +_spec.loader.exec_module(release_commits) + +REPO = "mindsdb/example" + + +class History: + """A scratch repository with `main` and `staging`, published as `origin/*` refs.""" + + def __init__(self, path: Path): + self.path = path + self.released_heads: list[str] = [] + self.git("init", "-q", "-b", "main") + self.git("config", "user.email", "test@example.com") + self.git("config", "user.name", "Release Bot") + self.git("config", "commit.gpgsign", "false") + self.commit("initial", author="Founder") + self.git("branch", "staging") + self.publish() + + def git(self, *args: str) -> str: + result = subprocess.run( + ["git", *args], cwd=self.path, capture_output=True, text=True, check=True + ) + return result.stdout.strip() + + def commit(self, subject: str, author: str = "Dev One") -> str: + (self.path / f"{subject.replace(' ', '_')}.txt").write_text(subject) + self.git("add", "-A") + self.git("commit", "-q", "-m", subject, "--author", f"{author} <{author.replace(' ', '')}@example.com>") + return self.git("rev-parse", "HEAD") + + def on_staging(self, *subjects: str, author: str = "Dev One") -> None: + self.git("checkout", "-q", "staging") + for subject in subjects: + self.commit(subject, author=author) + self.publish() + + def squash_release(self) -> None: + """Squash staging onto main, then sync main back into staging, as the train does.""" + head = self.git("rev-parse", "staging") + tree = self.git("rev-parse", "staging^{tree}") + main = self.git("rev-parse", "main") + squash = self.git("commit-tree", tree, "-p", main, "-m", "Release: staging -> main") + self.git("update-ref", "refs/heads/main", squash) + self.git("checkout", "-q", "staging") + self.git("merge", "-q", "--no-ff", "main", "-m", "Sync main into staging") + self.released_heads.append(head) + self.publish() + + def merge_release(self) -> None: + """Merge staging into main with a merge commit, then sync main back into staging.""" + head = self.git("rev-parse", "staging") + self.git("checkout", "-q", "main") + self.git("merge", "-q", "--no-ff", "staging", "-m", "Release: staging -> main") + self.git("checkout", "-q", "staging") + self.git("merge", "-q", "--ff-only", "main") + self.released_heads.append(head) + self.publish() + + def publish(self) -> None: + for branch in ("main", "staging"): + self.git("update-ref", f"refs/remotes/origin/{branch}", self.git("rev-parse", branch)) + + def unreleased(self, *excluded: str) -> list[str]: + return self.git("log", "--format=%s", "--no-merges", "origin/staging", *excluded).splitlines() + + +def runner_for(history: History, gh_answer=None, gh_fails=False): + """Run git for real inside the scratch repository and answer `gh pr list` from the history.""" + calls = [] + + def runner(argv): + argv = list(argv) + calls.append(argv) + if argv[0] == "gh": + if gh_fails: + return subprocess.CompletedProcess(argv, 1, "", "HTTP 502") + prs = gh_answer if gh_answer is not None else [ + {"headRefOid": sha, "mergedAt": f"2026-11-{10 + i:02d}T00:00:00Z"} + for i, sha in enumerate(history.released_heads) + ] + return subprocess.CompletedProcess(argv, 0, json.dumps(prs), "") + return subprocess.run(argv, cwd=history.path, capture_output=True, text=True, check=False) + + runner.calls = calls + return runner + + +def subjects(result) -> list[str]: + return [line.split(" ", 1)[1] for line in result.commits.splitlines()] + + +@pytest.fixture +def history(tmp_path): + return History(tmp_path) + + +def test_squash_releases_list_only_what_the_last_release_did_not_ship(history): + for week in range(3): + history.on_staging(f"week {week} a", f"week {week} b") + history.squash_release() + history.on_staging("after release c", "after release d", author="Dev Two") + + # The range the workflow used before: every commit since the first release. + assert len(history.unreleased("^origin/main")) == 8 + + result = release_commits.release_commits(runner_for(history), REPO, "main", "staging") + + assert subjects(result) == ["after release d", "after release c"] + assert result.count == 2 + assert result.omitted == 0 + assert result.since == history.released_heads[-1] + assert result.contributors == "- Dev Two" + assert result.lookup_failed is False + + +def test_merge_commit_releases_list_the_same_commits_as_main_dot_dot_staging(history): + history.on_staging("week 0 a", "week 0 b") + history.merge_release() + history.on_staging("after release c") + + result = release_commits.release_commits(runner_for(history), REPO, "main", "staging") + + assert subjects(result) == history.unreleased("^origin/main") == ["after release c"] + + +def test_a_hotfix_main_already_has_is_not_listed_twice(history): + history.on_staging("week 0 a") + history.squash_release() + history.on_staging("fix x on staging") + # The hotfix lands on main as its own commit, and the sync merges it into staging. + history.git("checkout", "-q", "main") + history.commit("fix x on main") + history.git("checkout", "-q", "staging") + history.git("merge", "-q", "--no-ff", "main", "-m", "Sync main into staging") + history.publish() + + result = release_commits.release_commits(runner_for(history), REPO, "main", "staging") + + assert subjects(result) == ["fix x on staging"] + + +def test_no_previous_release_lists_main_dot_dot_staging(history): + history.on_staging("first a", "first b") + + result = release_commits.release_commits(runner_for(history, gh_answer=[]), REPO, "main", "staging") + + assert result.count == 2 + assert result.since == history.git("rev-parse", "origin/main") + assert "no merged staging -> main PR yet" in result.since_reason + + +def test_a_release_head_staging_no_longer_holds_falls_back_to_main(history): + history.on_staging("week 0 a") + history.squash_release() + history.on_staging("after release b") + gone = [{"headRefOid": "0" * 40, "mergedAt": "2026-11-20T00:00:00Z"}] + + result = release_commits.release_commits(runner_for(history, gh_answer=gone), REPO, "main", "staging") + + assert result.count == 2 + assert "is not on origin/staging" in result.since_reason + assert result.lookup_failed is False + + +def test_a_failed_lookup_falls_back_to_main_and_is_flagged(history): + history.on_staging("week 0 a") + history.squash_release() + history.on_staging("after release b") + + result = release_commits.release_commits(runner_for(history, gh_fails=True), REPO, "main", "staging") + + assert result.lookup_failed is True + assert result.count == 2 + assert "HTTP 502" in result.since_reason + + +def test_the_newest_merged_release_wins_whatever_order_gh_returns(history): + history.on_staging("week 0 a") + history.squash_release() + history.on_staging("week 1 a") + history.squash_release() + history.on_staging("after release b") + older, newer = history.released_heads + answer = [ + {"headRefOid": newer, "mergedAt": "2026-11-15T23:34:06Z"}, + {"headRefOid": older, "mergedAt": "2026-11-09T03:31:12Z"}, + ][::-1] + + result = release_commits.release_commits(runner_for(history, gh_answer=answer), REPO, "main", "staging") + + assert result.since == newer + assert subjects(result) == ["after release b"] + + +def test_a_list_over_the_budget_is_cut_and_counts_what_it_cut(history): + history.on_staging(*[f"change {i:03d} with a reasonably long subject line" for i in range(40)]) + + result = release_commits.release_commits( + runner_for(history, gh_answer=[]), REPO, "main", "staging", max_chars=500 + ) + + assert len(result.commits) <= 500 + assert result.count == 40 + assert result.omitted == 40 - len(result.commits.splitlines()) + assert result.omitted > 0 + + +def test_main_prints_one_json_object(history, capsys, monkeypatch): + # CI sets this for its own steps; the test must not write into that summary. + monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising=False) + history.on_staging("first a") + + assert release_commits.main( + ["--repo", REPO, "--base", "main", "--staging", "staging"], runner=runner_for(history, gh_answer=[]) + ) == 0 + + out = json.loads(capsys.readouterr().out) + assert out["count"] == 1 + assert set(out) == { + "since", "since_reason", "lookup_failed", "count", "commits", "all_commits", "omitted", "contributors" + } + + +def test_a_cut_list_still_reaches_the_run_summary_in_full(history, capsys, monkeypatch, tmp_path_factory): + history.on_staging(*[f"change {i:03d} with a reasonably long subject line" for i in range(40)]) + summary = tmp_path_factory.mktemp("summary") / "step_summary.md" + monkeypatch.setenv("GITHUB_STEP_SUMMARY", str(summary)) + + assert release_commits.main( + ["--repo", REPO, "--base", "main", "--staging", "staging", "--max-chars", "500"], + runner=runner_for(history, gh_answer=[]), + ) == 0 + + out = json.loads(capsys.readouterr().out) + assert out["omitted"] > 0 + written = summary.read_text(encoding="utf-8") + assert "### Release PR list: 40 commit(s)" in written + assert sum("reasonably long subject line" in line for line in written.splitlines()) == 40