name: pr-commits
on:
pull_request_target:
types: [opened, edited, reopened, synchronize]
permissions: {}
jobs:
pr-commits:
name: pr-commits
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
steps:
- name: every commit is a RK_SCOPEDConventional Commit with clean content
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
NUMBER: ${{ github.event.pull_request.number }}
COUNT: ${{ github.event.pull_request.commits }}
TITLE: ${{ github.event.pull_request.title }}
run: |
printf '%s' "$TITLE" | grep -Eq \
'^chore(\((release|master|main)\))?: (release|v).+$' \
&& exit 0
listing="$(gh api --paginate "repos/$REPO/pulls/$NUMBER/commits" \
--jq '.[] | .sha + " " + (.commit.message | @base64)')"
fail=0
listed=0
while read -r sha encoded; do
[ -n "$sha" ] || continue
listed=$((listed + 1))
message="$(printf '%s' "$encoded" | base64 -d)"
subject="$(printf '%s\n' "$message" | head -n 1)"
commit="$(printf '%s' "$sha" | cut -c 1-12)"
if ! printf '%s' "$subject" | grep -Eq \
'^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)RK_SCOPE_OPEN\((RK_SCOPE_SHAPE)\)RK_SCOPE_CLOSE!?: .+|chore(\((release|master|main)\))?: (release|v).+)$'; then
RK_IF_RELEASE_echo "commit $commit: the subject '$subject' must read type(scope) then a colon and a description, with the scope lowercase letters, digits, and _ . / -"
RK_IF_NO_RELEASE_echo "commit $commit: the subject '$subject' must read type or type(scope) then a colon and a description, with any scope lowercase letters, digits, and _ . / -"
fail=1
fi
#RK_IF_RELEASE_ A message that names a security advisory releases: its type is feat,
#RK_IF_RELEASE_ fix, or perf, or it marks a breaking change. A security update is fix(deps).
RK_IF_RELEASE_unset advisory
RK_IF_RELEASE_printf '%s\n' "$message" | grep -Eq '(^|[^A-Za-z0-9])(RUSTSEC-[0-9]{4}-[0-9]{4}|CVE-[0-9]{4}-[0-9]{4,}|GHSA(-[0-9a-z]{4}){3})' && advisory=yes
RK_IF_RELEASE_printf '%s' "$subject" | grep -Eq '^((feat|fix|perf)(\([^)]*\)){0,1}!{0,1}|[a-z]+(\([^)]*\)){0,1}!): ' && advisory=
RK_IF_RELEASE_printf '%s\n' "$message" | grep -Eq '^BREAKING[ -]CHANGE:' && advisory=
RK_IF_RELEASE_test -z "${advisory:-}" || { echo "commit $commit: the message names a security advisory, so its type must release: feat, fix, or perf, or a breaking change; a security update is fix(deps)"; fail=1; }
while IFS='|' read -r class pattern; do
if printf '%s' "$message" | grep -Eq "$pattern"; then
echo "commit $commit: the message carries a $class finding: a line matches $pattern"
fail=1
fi
done <<'GUARDS'
attribution|[Gg]enerated with \[?Claude
attribution|🤖 Generated with
attribution|[Cc]o-[Aa]uthored-[Bb]y:.*([Cc]laude|[Cc]opilot|Codex|ChatGPT)
attribution|noreply@anthropic\.com
internal-path|(^|[^A-Za-z0-9])\.draft/
GUARDS
done <<<"$listing"
# The forge lists at most 250 commits of a request. A gate that
# judged fewer than the request carries would pass commits it
# never read, so a short listing fails rather than passing.
if [ "$listed" -ne "$COUNT" ]; then
echo "the forge listed $listed of the request's $COUNT commits, and this gate judges every commit or none; split the request"
fail=1
fi
exit "$fail"