Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

CI: merge-approved needs the licence tick from outside authors (Refs #1371) (#2354)

Eyal Golan85c1c270688f
diff --git a/.github/workflows/merge-approved.yml b/.github/workflows/merge-approved.yml
index 674480d7f..2fcb351e1 100644
--- a/.github/workflows/merge-approved.yml
+++ b/.github/workflows/merge-approved.yml
@@ -6,15 +6,17 @@ name: Merge approved
# token never meets outside code. It starts when CI finishes (workflow_run runs in this repository's context with a
# write token, also for forks), when a review is submitted (for a fork's pull request GitHub gives that run a read-only
# token, so its merge is refused and the next CI or timer run merges), on a timer as a fallback (GitHub runs
-# schedules late or not at all under load), and by hand. It reads only numbers, SHAs and states from the API, never titles or
-# bodies, so no pull-request text reaches the shell.
+# schedules late or not at all under load), and by hand. It reads only numbers, SHAs and states from the API, never titles;
+# the body is read only inside jq, for the licence tick, and never reaches the shell.
#
# A pull request merges only when all of these hold on its current head commit:
# - it targets main, is not a draft, and GitHub reports it MERGEABLE (no conflicts);
# - the maintainer's most recent review is APPROVED and was made on that exact commit (a later push needs a new
# approval);
# - the four CI checks below exist on that commit and each concluded SUCCESS (a first-time contributor's run that is
-# still waiting for approval has no results, so it waits).
+# still waiting for approval has no results, so it waits);
+# - unless the maintainer opened it, its body ticks the template's licence line ("[x] I contribute this under the terms
+# in `CONTRIBUTING.md`", [x] or [X]); an unticked or waived pull request waits for the maintainer to merge it by hand.
# GitHub does not start other workflows from a merge made with this token, so CI on main is not re-run by it.
on:
@@ -50,7 +52,7 @@ jobs:
run: |
set -euo pipefail
gh pr list --state open --base main --limit 100 --json number --jq '.[].number' | while read -r n; do
- ready=$(gh pr view "$n" --json isDraft,mergeable,headRefOid,reviews,statusCheckRollup --jq '
+ ready=$(gh pr view "$n" --json author,body,isDraft,mergeable,headRefOid,reviews,statusCheckRollup --jq '
. as $p
| ([$p.reviews[] | select(.author.login == env.MAINTAINER)] | last) as $r
| (env.REQUIRED_CHECKS | split("|")) as $need
@@ -59,6 +61,8 @@ jobs:
and $r != null and $r.state == "APPROVED" and $r.commit.oid == $p.headRefOid
and ($need | all(. as $x | [$p.statusCheckRollup[] | select(.name == $x)]
| length > 0 and all(.conclusion == "SUCCESS")))
+ and ($p.author.login == env.MAINTAINER
+ or ($p.body // "" | test("\\[[xX]\\] I contribute this under the terms in")))
| if . then $p.headRefOid else "no" end')
if [ "$ready" = "no" ]; then
echo "#$n: not ready"
diff --git a/scripts/oss/tests/test_github_config.py b/scripts/oss/tests/test_github_config.py
index c716720ca..1be0ba4e6 100644
--- a/scripts/oss/tests/test_github_config.py
+++ b/scripts/oss/tests/test_github_config.py
@@ -1,7 +1,11 @@
import json
+import os
import re
+import shutil
+import subprocess
from pathlib import Path
+import pytest
import yaml
ROOT = Path(__file__).resolve().parents[3]
@@ -143,11 +147,41 @@ def test_merge_workflow_never_touches_pull_request_code():
assert wf["permissions"] == {"contents": "write", "pull-requests": "write"}
script = "\n".join(s.get("run", "") for s in steps(wf))
assert not any("uses" in s for s in steps(wf)) and "checkout" not in script
- for field in ("title", "body", "headRefName"):
+ for field in ("title", "headRefName"):
assert field not in script, field
+ # The body is read only by jq for the licence tick; jq prints a SHA or "no", so no body text reaches the shell.
+ assert script.count("body") == 2 and ',body,' in script and '($p.body // "" | test(' in script
assert "--match-head-commit" in script and "$r.commit.oid == $p.headRefOid" in script
+def merge_filter(pr: dict) -> str:
+ # Runs the workflow's own jq program on one `gh pr view --json` object, as gh --jq does.
+ wf = yaml.safe_load(MERGE.read_text(encoding="utf-8"))
+ script = "\n".join(s.get("run", "") for s in steps(wf))
+ program = script.split("statusCheckRollup --jq '", 1)[1].split("')", 1)[0]
+ env = {**os.environ, **{k: wf["jobs"]["merge"]["env"][k] for k in ("MAINTAINER", "REQUIRED_CHECKS")}}
+ return subprocess.run(["jq", "-r", program], input=json.dumps(pr), env=env, capture_output=True, text=True,
+ check=True).stdout.strip()
+
+
[email protected]("author, body, merges", [
+ ("contributor", "- [ ] I contribute this under the terms in `CONTRIBUTING.md`", False),
+ ("contributor", "- [x] I contribute this under the terms in `CONTRIBUTING.md`", True),
+ ("contributor", "- [X] I contribute this under the terms in `CONTRIBUTING.md`", True),
+ ("contributor", "- [x] I contribute this under the repository's licences", False), # the pre-#1371 line
+ ("contributor", None, False),
+ ("eyalgolan", "- [ ] I contribute this under the terms in `CONTRIBUTING.md`", True), # agents never tick it
+])
+def test_merge_workflow_needs_the_licence_tick_from_outside_authors(author, body, merges):
+ if shutil.which("jq") is None:
+ assert not os.environ.get("CI"), "jq is on GitHub's ubuntu runners"
+ pytest.skip("jq not installed")
+ pr = {"author": {"login": author}, "body": body, "isDraft": False, "mergeable": "MERGEABLE", "headRefOid": "abc",
+ "reviews": [{"author": {"login": "eyalgolan"}, "state": "APPROVED", "commit": {"oid": "abc"}}],
+ "statusCheckRollup": [{"name": n, "conclusion": "SUCCESS"} for n in sorted(EXPECTED_JOBS)]}
+ assert merge_filter(pr) == ("abc" if merges else "no")
+
+
def test_merge_workflow_waits_for_every_required_check():
wf = yaml.safe_load(MERGE.read_text(encoding="utf-8"))
need = set(wf["jobs"]["merge"]["env"]["REQUIRED_CHECKS"].split("|"))