Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions .github/workflows/cla-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ jobs:
PR_JSON=$(GH_TOKEN="$PR_TOKEN" gh api "repos/$REPO/pulls/$PR_NUM")
OPENER=$(jq -r '.user.login' <<< "$PR_JSON")
N_COMMITS=$(jq -r '.commits' <<< "$PR_JSON")
# Inside `if`, a failed `[` is just false, so check that the count is a number first.
case "$N_COMMITS" in
'' | *[!0-9]* | 0)
echo "::error::Could not read this pull request's commit count ($N_COMMITS). A maintainer handles this pull request by hand."
exit 1 ;;
esac
if [ "$N_COMMITS" -gt 100 ]; then
echo "::error::This pull request has $N_COMMITS commits; the CLA check reads only the first 100. Split it or squash it."
exit 1
Expand All @@ -71,21 +77,31 @@ jobs:
echo "opener $OPENER is not exempt"
fi

# Read the commits into a variable: `set -e` can't see a failure inside `< <(...)`, and an empty
# read would leave every commit unchecked. The count must match, since an empty here-string still
# yields one empty line.
COMMIT_AUTHORS=$(GH_TOKEN="$PR_TOKEN" gh api "repos/$REPO/pulls/$PR_NUM/commits?per_page=100" \
--jq '.[] | .author.login // "null"')
N_READ=$(grep -c . <<< "$COMMIT_AUTHORS" || true)
if [ "$N_READ" != "$N_COMMITS" ]; then
echo "::error::Read $N_READ commit authors, but this pull request has $N_COMMITS commits. A maintainer handles this pull request by hand."
exit 1
fi

EXEMPT=" "
REQUIRED=" "
AUTHORS=" "
while read -r AUTHOR; do
if [ "$AUTHOR" = "null" ]; then
echo "a commit author has no GitHub login: CLA required"
REQUIRED="$REQUIRED<none> "
continue
# The action credits such a commit to its committer, who may be allowlisted or may have signed.
echo "::error::A commit's author email is not linked to a GitHub account, so the CLA action would credit the commit to its committer. A maintainer handles this pull request by hand."
exit 1
fi
AUTHORS="$AUTHORS$AUTHOR "
OK=false
if [ "$OPENER_EXEMPT" = "true" ] && { is_named_bot "$AUTHOR" || is_member "$AUTHOR"; }; then OK=true; fi
if [ "$OK" = "true" ]; then EXEMPT="$EXEMPT$AUTHOR "; else REQUIRED="$REQUIRED$AUTHOR "; fi
done < <(GH_TOKEN="$PR_TOKEN" gh api "repos/$REPO/pulls/$PR_NUM/commits?per_page=100" \
--jq '.[] | .author.login // "null"')
done <<< "$COMMIT_AUTHORS"

if [ "$OPENER_EXEMPT" = "false" ]; then
case "$AUTHORS" in
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ same release controls as the projects it publishes.
| `maturin-wheels.yml`, `crates.yml`, `docker.yml` | Build and publish artifacts |
| `docker-dev.yml` | `dev` channel images on GHCR (not releases) |
| `publish.yml` | Creates the tag and GitHub release with the release App, after everything is published |
| `cla-reusable.yml` | CLA check. The agreement and signatures live in [`CLA`](https://github.com/blacklanternsecurity/CLA), signatures on its bot-only `signatures` branch. On a pull request opened by a member or named bot, other members and named bots are exempt; on one opened by anyone else, the opener and every commit author must sign, members included. Fails a pull request with more than 100 commits, or one whose outside opener has no commit in it |
| `cla-reusable.yml` | CLA check. The agreement and signatures live in [`CLA`](https://github.com/blacklanternsecurity/CLA), signatures on its bot-only `signatures` branch. On a pull request opened by a member or named bot, other members and named bots are exempt; on one opened by anyone else, the opener and every commit author must sign, members included. Fails, for a maintainer to handle, a pull request with more than 100 commits, one whose commits can't all be read, one with a commit whose author has no GitHub login, or one whose outside opener has no commit in it |

## Using a workflow

Expand Down
48 changes: 42 additions & 6 deletions tests/test_cla_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
case " $MEMBERS " in *" ${2##*/} "*) exit 0 ;; esac
exit 1 ;;
repos/o/r/pulls/7) printf '{"user":{"login":"%s"},"commits":%s}\\n' "$OPENER" "$N_COMMITS" ;;
repos/o/r/pulls/7/commits*) cat "$COMMITS_FILE" ;;
repos/o/r/pulls/7/commits*) [ -n "$COMMITS_FAIL" ] && { echo "HTTP 502" >&2; exit 1; }; cat "$COMMITS_FILE" ;;
*) echo "unexpected: gh $*" >&2; exit 2 ;;
esac
"""


def run_step(tmp_path, opener, members, commits, n_commits=None):
def run_step(tmp_path, opener, members, commits, n_commits=None, commits_fail=False):
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
gh = bin_dir / "gh"
Expand All @@ -51,12 +51,11 @@ def run_step(tmp_path, opener, members, commits, n_commits=None):
"MEMBERS": members,
"N_COMMITS": str(len(commits) if n_commits is None else n_commits),
"COMMITS_FILE": str(commits_file),
"COMMITS_FAIL": "1" if commits_fail else "",
}
result = subprocess.run(["bash", "-c", STEP], cwd=tmp_path, env=env, capture_output=True, text=True)
outputs = dict(line.split("=", 1) for line in output.read_text().splitlines()) if output.exists() else {}
required = sorted(
line.removesuffix(": CLA required") for line in result.stdout.splitlines() if line.endswith(": CLA required") and "no GitHub login" not in line
)
required = sorted(line.removesuffix(": CLA required") for line in result.stdout.splitlines() if line.endswith(": CLA required"))
return result, outputs, required


Expand All @@ -71,7 +70,6 @@ def run_step(tmp_path, opener, members, commits, n_commits=None):
("another external's commit on an external's PR: both sign", "ext", "m1", ["ext", "ext2"], "false", "", ["ext", "ext2"]),
("member commit on an external's PR: member signs too", "ext", "m1", ["ext", "m1"], "false", "", ["ext", "m1"]),
("external's PR with a commit claiming dependabot: must sign", "ext", "m1", ["ext", "dependabot[bot]"], "false", "", ["dependabot[bot]", "ext"]),
("member PR, commit without a GitHub login: CLA required", "m1", "m1", ["null"], "false", "", ["<none>"]),
("membership lookup fails: not a member", "m1", "FAIL", ["m1"], "false", "", ["m1"]),
]

Expand Down Expand Up @@ -103,3 +101,41 @@ def test_external_opener_without_a_commit_fails(tmp_path):
assert result.returncode != 0
assert "The opener (ext) must sign the CLA" in result.stdout
assert outputs == {}


@pytest.mark.parametrize("commits", [["null"], ["null", "m1"], ["m1", "null"], ["ext", "null"]], ids=str)
@pytest.mark.parametrize("opener", ["m1", "ext"])
def test_commit_without_a_github_login_fails(tmp_path, opener, commits):
# The action would credit the commit to its committer, who may be allowlisted or may have signed.
result, outputs, _ = run_step(tmp_path, opener, "m1", commits)
assert result.returncode != 0
assert "not linked to a GitHub account" in result.stdout
assert outputs == {}


def test_failed_commits_read_fails(tmp_path):
result, outputs, _ = run_step(tmp_path, "m1", "m1", ["m1"], commits_fail=True)
assert result.returncode != 0
assert outputs == {}


def test_commit_count_mismatch_fails(tmp_path):
result, outputs, _ = run_step(tmp_path, "m1", "m1", ["m1"], n_commits=2)
assert result.returncode != 0
assert "Read 1 commit authors, but this pull request has 2 commits" in result.stdout
assert outputs == {}


def test_empty_commit_list_fails(tmp_path):
result, outputs, _ = run_step(tmp_path, "m1", "m1", [], n_commits=1)
assert result.returncode != 0
assert "Read 0 commit authors" in result.stdout
assert outputs == {}


@pytest.mark.parametrize("n_commits", ["null", "0"])
def test_unreadable_commit_count_fails(tmp_path, n_commits):
result, outputs, _ = run_step(tmp_path, "m1", "m1", ["m1"], n_commits=n_commits)
assert result.returncode != 0
assert "Could not read this pull request's commit count" in result.stdout
assert outputs == {}
Loading