Files
gentoo-zh/.github/workflows/pkgcheck-comment.yml
T
Zakk 3e38a423ca .github: comment on a rule-breaking pull request instead of closing it
The required_checks ruleset already blocks the merge for everyone: pr-rules
is a required check and the ruleset has no bypass actors. Closing on top of
that only forced a new pull request for a fixable description. Keep one
comment that names the problems, update it on each run, and delete it once
the rules pass. pr-rules now also triggers the comment job, so editing the
description clears it without a push.

Signed-off-by: Zakk <zakk@gentoozh.org>
2026-10-17 19:58:03 +11:00

122 lines
4.5 KiB
YAML

name: pkgcheck-comment
on:
workflow_run:
# pr-rules also reruns on a description edit, which pkgcheck does not
workflows: [pkgcheck, pr-rules]
types: [completed]
permissions:
contents: read
pull-requests: write
actions: read
jobs:
comment:
if: github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v7
- name: check for the report
id: report
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUN_ID: ${{ github.event.workflow_run.id }}
run: |
artifacts=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN_ID/artifacts" --paginate --slurp)
available=$(jq 'any(.[] | .artifacts[]; .name == "pkgcheck-report")' <<< "$artifacts")
echo "available=$available" >> "$GITHUB_OUTPUT"
- name: download the report
if: steps.report.outputs.available == 'true'
uses: actions/download-artifact@v8
with:
name: pkgcheck-report
path: pkgcheck-report
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: post the report
if: steps.report.outputs.available == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
RUN_URL: ${{ github.event.workflow_run.html_url }}
run: scripts/pkgcheck-comment.sh pkgcheck-report
- name: explain why a pull request breaks the pull request rules
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
pulls=$(gh api "repos/$GITHUB_REPOSITORY/pulls?state=open&per_page=100" --paginate --slurp)
pr=$(jq -r --arg sha "$HEAD_SHA" 'first(.[][] | select(.head.sha == $sha) | .number) // empty' <<< "$pulls")
[ -n "$pr" ] || exit 0
bad=$(scripts/commit-rules.sh "$pr")
missing=$(scripts/pr-template.sh "$pr")
marker='<!-- pr-rules -->'
if [ -z "$bad$missing" ]; then
comments=$(gh api "repos/$GITHUB_REPOSITORY/issues/$pr/comments" --paginate --slurp)
jq -r --arg marker "$marker" '.[][] | select(.user.type == "Bot" and (.body | contains($marker))) | .id' <<< "$comments" |
while read -r id; do
gh api -X DELETE "repos/$GITHUB_REPOSITORY/issues/comments/$id" >/dev/null
done
exit 0
fi
echo "This pull request cannot be merged until it follows the rules below." > body.md
if [ -n "$bad" ]; then
cat >> body.md <<EOF
## Commits
These commits break the commit rules:
$bad
### Commit rules
1. **Subject**: start with the package or file the commit changes, a colon and a short summary.
\`\`\`
app-misc/foo: add 1.2.3, drop 1.2.2
app-misc/foo: fix the build with GCC 16
profiles: mask app-misc/foo
foo.eclass: support EAPI 9
\`\`\`
2. **Sign-off**: every commit ends with a \`Signed-off-by\` line.
\`\`\`
Signed-off-by: Your Name <you@example.com>
\`\`\`
\`git commit -s\` and \`pkgdev commit --signoff\` add it for you.
3. **No merge commits**: update the branch with \`git rebase\`, not \`git merge\` or the Update branch button.
### How to fix
\`\`\`
git fetch https://github.com/gentoo-zh/overlay.git master
git rebase -i FETCH_HEAD # mark a commit "reword" to fix its subject
git rebase --signoff FETCH_HEAD # add Signed-off-by to every commit
git push --force-with-lease
\`\`\`
EOF
fi
if [ -n "$missing" ]; then
cat >> body.md <<EOF
## Pull request template
The description lost these checkboxes of the [pull request template](https://github.com/gentoo-zh/overlay/blob/master/.github/pull_request_template.md):
$missing
Write the description above the template and keep the checkboxes. Leave a box unticked when it does not apply.
EOF
fi
cat >> body.md <<EOF
Fix the problems above. The \`pr-rules\` check runs again after a push or an edit of the description, and the pull request can be merged once it passes. See [CONTRIBUTING.en.md](https://github.com/gentoo-zh/overlay/blob/master/CONTRIBUTING.en.md).
$marker
EOF
scripts/upsert-comment.sh "$GITHUB_REPOSITORY" "$pr" "$marker" body.md