diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ccc3eed9..6084d20c7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,9 @@ # vendored crates under vendor/ select the packages that read them (scripts/ci/affected.py). # Heavy checks wait for review: a pull request without the `approved-for-ci` label runs only # Select checks, the format and repository checks and a quick compile. Adding the label (after -# review) runs everything above, and every later push to the PR does too. Pushes to main, merge +# review) runs everything above, and every later push to the PR does too, except a push that only +# merges main into a change that already passed (scripts/ci/main_merge.py): it carries that +# result and runs the repository checks. Pushes to main, merge # groups, the nightly run and Run workflow are not pull requests and always run the full suite. # GitHub cannot filter `labeled` events by label name, so any other label starts a run whose jobs # are all skipped; it gets its own concurrency group (below) so it never cancels the real run. @@ -112,6 +114,8 @@ jobs: packages: ${{ steps.paths.outputs.packages }} oracle: ${{ steps.paths.outputs.oracle }} doctests: ${{ steps.paths.outputs.doctests }} + # The passing run a merge of main carries forward (its URL), when this push is only that. + carried: ${{ steps.paths.outputs.carried }} # The fast self-hosted runners are kept for main and the merge queue (the pre-merge gate); # pull requests, same-repo ones included, take the GitHub-hosted or untrusted paths. local: ${{ runner.environment == 'self-hosted' && github.event_name != 'pull_request' }} @@ -235,6 +239,19 @@ jobs: oracle=false goldens=false fi + # A push that only merges main into the change (Merge branch 'main', Update branch) when + # the change itself already passed: nothing new to test here, and main tests the merged + # result after it lands. Over three days, 872 of 1,241 such merges brought in nothing that + # touched the pull request's files, and each restarted the whole run. + carried= + if [ "$EVENT_NAME" = pull_request ] && [ "$native" = true ]; then + carried=$(python3 -I scripts/ci/main_merge.py --base "$BASE_SHA" --head "$HEAD_SHA" --repo "$REPO" | sed -n 's/^carried=//p') + if [ -n "$carried" ]; then + echo "Only a merge of main into a change that passed in $carried: repository checks only." + native=false + goldens=false + fi + fi { echo "native=$native" echo "kernel=$kernel" @@ -242,6 +259,7 @@ jobs: echo "windows=$windows" echo "oracle=$oracle" echo "doctests=$doctests" + echo "carried=$carried" echo "full=$full" echo "quick=$quick" echo "packages=$packages" @@ -285,6 +303,8 @@ jobs: run: cargo xtask web-offline-test - name: Affected-package selection self-test run: python3 -I scripts/ci/test_affected.py + - name: Merge-of-main detection self-test + run: python3 -I scripts/ci/test_main_merge.py - name: Claim and triage bot tests run: node --test scripts/claim/*.test.js scripts/triage/*.test.js @@ -779,6 +799,12 @@ jobs: test "$WINDOWS_FXC" = success test "$WINDOWS_STARTUP" = success fi + - name: Note a carried result + if: needs.changes.outputs.carried != '' + env: + CARRIED: ${{ needs.changes.outputs.carried }} + run: | + echo "This push only merges main into a change that already passed; the heavy checks were not repeated. Passing run: $CARRIED" >> "$GITHUB_STEP_SUMMARY" wasm: name: Compile WebAssembly (manual) diff --git a/scripts/ci/main_merge.py b/scripts/ci/main_merge.py new file mode 100644 index 000000000..35d0504be --- /dev/null +++ b/scripts/ci/main_merge.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Is this pull request push only a merge of main into a change that already passed CI? + + python3 -I scripts/ci/main_merge.py --base BASE_SHA --head HEAD_SHA --repo OWNER/NAME + +Prints `carried=` when all of these hold, else `carried=`: + - HEAD is a merge commit whose second parent is on main (an ancestor of BASE): "Merge branch + 'main'", or GitHub's Update branch button; + - the pull request's own change is the same before and after the merge (equal `git patch-id`), + so the merge resolved no conflict and brought nothing into the change itself; + - a ci.yml run of the first parent (the change before the merge) passed with the test shards. + +Then the run needs only the repository checks: the change was tested, and main tests the merged +result after it lands. Any doubt (an error, an API failure, an unexpected shape) prints +`carried=` and the full checks run. Reads GH_TOKEN from the environment for the API. +""" +import argparse +import json +import os +import subprocess +import sys +import urllib.request + +API = "https://api.github.com" +SHARD_PREFIX = "Native Linux tests" + + +def git(*args): + return subprocess.run(["git", *args], capture_output=True, text=True, check=True).stdout + + +def patch_id(a, b): + """The stable patch id of `git diff a b` ("" for an empty diff).""" + diff = subprocess.run(["git", "diff", "--no-renames", a, b], capture_output=True, check=True).stdout + if not diff.strip(): + return "" + out = subprocess.run(["git", "patch-id", "--stable"], input=diff, capture_output=True, check=True).stdout + return out.split()[0].decode() if out.split() else "" + + +def merge_of_main(base, head): + """The first parent of HEAD when HEAD only merges main into an unchanged change, else None.""" + parents = git("rev-list", "--parents", "-n", "1", head).split()[1:] + if len(parents) != 2: + return None + change, merged = parents + if subprocess.run(["git", "merge-base", "--is-ancestor", merged, base]).returncode != 0: + return None + fork_point = git("merge-base", change, merged).strip() + before, after = patch_id(fork_point, change), patch_id(merged, head) + return change if before and before == after else None + + +def get(url, token): + req = urllib.request.Request(url, headers={"Accept": "application/vnd.github+json", "Authorization": f"Bearer {token}"}) + with urllib.request.urlopen(req, timeout=20) as r: + return json.load(r) + + +def passing_run(repo, sha, token): + """URL of a successful pull request ci.yml run of `sha` whose test shards all passed.""" + runs = get(f"{API}/repos/{repo}/actions/workflows/ci.yml/runs?head_sha={sha}&event=pull_request&status=success&per_page=10", token) + for run in runs.get("workflow_runs", []): + jobs = get(f"{API}/repos/{repo}/actions/runs/{run['id']}/jobs?per_page=100", token).get("jobs", []) + shards = [j for j in jobs if j.get("name", "").startswith(SHARD_PREFIX)] + if shards and all(j.get("conclusion") == "success" for j in shards): + return run.get("html_url", "") + return "" + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--base", required=True) + ap.add_argument("--head", required=True) + ap.add_argument("--repo", required=True) + a = ap.parse_args() + carried = "" + try: + change = merge_of_main(a.base, a.head) + token = os.environ.get("GH_TOKEN", "") + if change and token: + carried = passing_run(a.repo, change, token) + if carried: + print(f"{a.head} merges main into {change}, which passed: {carried}", file=sys.stderr) + except (subprocess.CalledProcessError, OSError, ValueError, KeyError) as e: + print(f"not carried ({e})", file=sys.stderr) + carried = "" + print(f"carried={carried}") + + +if __name__ == "__main__": + main() diff --git a/scripts/ci/test_main_merge.py b/scripts/ci/test_main_merge.py new file mode 100644 index 000000000..3e883e5b7 --- /dev/null +++ b/scripts/ci/test_main_merge.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +"""Self-test for main_merge.py. Run: python3 -I scripts/ci/test_main_merge.py (from the repo root). + +Builds small throwaway git repositories; needs only git. +""" +import os +import subprocess +import sys +import tempfile +import unittest + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import main_merge # noqa: E402 + + +class Repo: + def __init__(self, root): + self.root = root + self.git("init", "-q", "-b", "main") + self.git("config", "user.email", "t@example.com") + self.git("config", "user.name", "t") + self.git("config", "commit.gpgsign", "false") + + def git(self, *args): + return subprocess.run(["git", *args], cwd=self.root, capture_output=True, text=True, check=True).stdout.strip() + + def write(self, path, text): + with open(os.path.join(self.root, path), "w") as f: + f.write(text) + + def commit(self, msg, **files): + for path, text in files.items(): + self.write(path, text) + self.git("add", "-A") + self.git("commit", "-q", "-m", msg) + return self.git("rev-parse", "HEAD") + + +LINES = "".join(f"line {i}\n" for i in range(40)) + + +class MergeOfMain(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.r = Repo(self.tmp.name) + self.r.commit("base", **{"a.txt": LINES, "b.txt": LINES}) + self.r.git("checkout", "-q", "-b", "pr") + self.change = self.r.commit("pr change", **{"a.txt": LINES.replace("line 5\n", "line five\n")}) + self.cwd = os.getcwd() + os.chdir(self.tmp.name) + + def tearDown(self): + os.chdir(self.cwd) + self.tmp.cleanup() + + def main_commit(self, **files): + self.r.git("checkout", "-q", "main") + sha = self.r.commit("main moves", **files) + self.r.git("checkout", "-q", "pr") + return sha + + def merge_main(self): + self.r.git("merge", "-q", "--no-edit", "main") + return self.r.git("rev-parse", "HEAD") + + def test_clean_merge_of_unrelated_main_change_is_carried(self): + base = self.main_commit(**{"b.txt": LINES.replace("line 30\n", "line thirty\n")}) + head = self.merge_main() + self.assertEqual(main_merge.merge_of_main(base, head), self.change) + + def test_conflict_resolution_is_not_carried(self): + base = self.main_commit(**{"a.txt": LINES.replace("line 5\n", "line 5 on main\n")}) + subprocess.run(["git", "merge", "-q", "main"], cwd=self.tmp.name, capture_output=True) + self.r.write("a.txt", LINES.replace("line 5\n", "line five on both\n")) + self.r.git("add", "-A") + self.r.git("commit", "-q", "--no-edit") + head = self.r.git("rev-parse", "HEAD") + self.assertIsNone(main_merge.merge_of_main(base, head)) + + def test_main_change_next_to_the_change_is_not_carried(self): + # Main edits a context line of the pull request's hunk: the change's patch differs. + base = self.main_commit(**{"a.txt": LINES.replace("line 7\n", "line seven\n")}) + head = self.merge_main() + self.assertIsNone(main_merge.merge_of_main(base, head)) + + def test_ordinary_commit_is_not_carried(self): + head = self.r.commit("more", **{"a.txt": LINES.replace("line 5\n", "line 5b\n")}) + base = self.r.git("rev-parse", "main") + self.assertIsNone(main_merge.merge_of_main(base, head)) + + def test_merge_of_another_branch_is_not_carried(self): + base = self.r.git("rev-parse", "main") + self.r.git("checkout", "-q", "-b", "other", "main") + self.r.commit("other work", **{"c.txt": "x\n"}) + self.r.git("checkout", "-q", "pr") + self.r.git("merge", "-q", "--no-edit", "other") + head = self.r.git("rev-parse", "HEAD") + self.assertIsNone(main_merge.merge_of_main(base, head)) + + def test_change_with_empty_diff_is_not_carried(self): + self.r.git("checkout", "-q", "-b", "empty", "main") + base = self.main_commit(**{"b.txt": LINES.replace("line 30\n", "line thirty\n")}) + self.r.git("checkout", "-q", "empty") + self.r.git("merge", "-q", "--no-ff", "--no-edit", "main") + head = self.r.git("rev-parse", "HEAD") + self.assertIsNone(main_merge.merge_of_main(base, head)) + + +if __name__ == "__main__": + unittest.main()