mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
If the `gh api` call to fetch a commit's verification status fails (rate limit, transient API error), the old code left `verification` empty and reported the commit as unsigned with a blank reason, sending a contributor off to re-sign a commit that may already be signed. Guard the call and annotate an API failure as its own distinct error instead. The end-of-job signing/rebase guidance is now gated on a separate UNSIGNED flag rather than the general FAILED flag, so an API failure no longer triggers the "re-sign and force-push" instructions, which previously contradicted the per-commit "not a signature failure" annotation. Verification lookups are now batched through a single call to the PR-commits endpoint, falling back to the existing per-commit call for any sha it doesn't cover (over its 250-commit cap, or any other reason), so the git-log-derived commit set stays authoritative either way. Follow-up from #3812, flagged by hectorvent in review.
112 lines
5.1 KiB
YAML
112 lines
5.1 KiB
YAML
name: Signed Commits
|
|
|
|
# The repo's branch protection already requires every commit to be signed
|
|
# and verified before a PR can merge, but that only shows up as a generic
|
|
# red status on the merge button - it doesn't say which commit, or why. This
|
|
# job front-runs that with a per-commit annotation in the Checks tab, using
|
|
# the same signal GitHub's own merge gate uses (the commit's `verification`
|
|
# field from the API), so a contributor gets a specific, actionable failure
|
|
# instead of discovering the block only when they try to merge.
|
|
#
|
|
# 'edited' is deliberately left out of the trigger: editing the PR title or
|
|
# body doesn't change which commits are on the branch, so there's nothing
|
|
# new to check.
|
|
|
|
on:
|
|
pull_request:
|
|
types: [opened, synchronize, reopened]
|
|
|
|
permissions:
|
|
contents: read
|
|
pull-requests: read
|
|
|
|
jobs:
|
|
check-signatures:
|
|
name: Commits are signed and verified
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Check commit signatures
|
|
env:
|
|
BASE_SHA: ${{ github.event.pull_request.base.sha }}
|
|
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
|
GH_TOKEN: ${{ github.token }}
|
|
REPO: ${{ github.repository }}
|
|
PR_NUMBER: ${{ github.event.pull_request.number }}
|
|
run: |
|
|
# Local `git log --show-signature` only verifies against keys the
|
|
# runner already trusts, which is nobody's - it would report every
|
|
# commit as unsigned regardless of reality. GitHub's own merge gate
|
|
# checks the commit's `verification` field instead (matched against
|
|
# the key the author uploaded to their GitHub account), so this
|
|
# checks the same field via the API to give the same verdict the
|
|
# merge button will, just earlier and per-commit.
|
|
#
|
|
# Merge commits are skipped via --no-merges, matching the
|
|
# Conventional Commits and AI Attribution jobs.
|
|
#
|
|
# An unresolvable API call is treated as a hard failure here,
|
|
# unlike ai-attribution.yml's classify(), which lets a commit
|
|
# through when it can't resolve an author ("Unresolvable (deleted
|
|
# account, API trouble) is not evidence"). The two jobs weigh the
|
|
# mistake differently: a false positive in AI Attribution accuses a
|
|
# human contributor of being a bot, while an unverifiable signature
|
|
# here only withholds a verdict that branch protection enforces at
|
|
# the merge button anyway - failing closed costs a re-run, not a
|
|
# wrongly blocked contributor.
|
|
|
|
FAILED=0
|
|
UNSIGNED=0
|
|
|
|
# One call gets verification for up to 250 commits (this
|
|
# endpoint's documented cap). The loop below still walks the same
|
|
# git-log range as before, so this is only a lookup cache: any sha
|
|
# it doesn't cover (over the cap, or absent for any other reason)
|
|
# falls back to the per-commit call that already handles API
|
|
# failures. --paginate re-runs --jq per page and concatenates the
|
|
# output, so this emits one JSON object per commit rather than a
|
|
# single array - jq reads that as a stream of values just fine.
|
|
pr_commits=$(gh api "repos/$REPO/pulls/$PR_NUMBER/commits" --paginate --jq '.[] | {sha: .sha, verification: .commit.verification}') || pr_commits=''
|
|
|
|
for sha in $(git log --no-merges --format=%H "$BASE_SHA".."$HEAD_SHA"); do
|
|
subject=$(git show -s --format=%s "$sha")
|
|
|
|
verification=$(echo "$pr_commits" | jq -c --arg sha "$sha" 'select(.sha == $sha) | .verification')
|
|
|
|
if [ -z "$verification" ] && ! verification=$(gh api "repos/$REPO/commits/$sha" --jq '.commit.verification'); then
|
|
echo "::error::Could not check verification status for commit: $sha $subject (GitHub API call failed - this is not a signature failure, re-run the job)"
|
|
FAILED=1
|
|
continue
|
|
fi
|
|
|
|
verified=$(echo "$verification" | jq -r '.verified')
|
|
reason=$(echo "$verification" | jq -r '.reason')
|
|
|
|
if [ "$verified" = "true" ]; then
|
|
echo "ok: $sha $subject"
|
|
else
|
|
echo "::error::Commit is not signed/verified: $sha $subject ($reason)"
|
|
UNSIGNED=1
|
|
FAILED=1
|
|
fi
|
|
done
|
|
|
|
if [ "$UNSIGNED" -ne 0 ]; then
|
|
echo ""
|
|
echo " This repo requires every commit to be GPG or SSH signed and verified"
|
|
echo " by GitHub before a PR can merge - the commit(s) flagged above aren't."
|
|
echo ""
|
|
echo " Set up commit signing (GPG or SSH):"
|
|
echo " https://docs.github.com/en/authentication/managing-commit-signature-verification"
|
|
echo ""
|
|
echo " Then re-sign the commits this PR adds and force-push:"
|
|
echo " git rebase --exec 'git commit --amend --no-edit -S' origin/main"
|
|
echo " git push --force-with-lease"
|
|
fi
|
|
|
|
exit $FAILED
|