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
132 changes: 132 additions & 0 deletions .github/workflows/pr-review-mention-reusable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Reusable PR Review — Mention Trigger workflow.
# Single source of truth for the org: all review-dispatch logic lives here.
# Repo-level pr-review-mention.yml files are thin caller stubs.
# Standard: https://github.com/petry-projects/.github/blob/main/standards/ci-standards.md
#
# Fires when @petry-review-bot is mentioned in a PR comment / review comment,
# or when donpetry-bot is assigned as a reviewer on a PR. Dispatches a
# repository_dispatch event to petry-projects/.github-private to trigger the
# pr-review agent cascade.
#
# Requires: GH_PAT_WORKFLOWS org secret (classic PAT with repo scope).
name: PR Review — Mention Trigger (Reusable)

on:
workflow_call:
secrets:
GH_PAT_WORKFLOWS:
description: "Classic PAT with repo scope used to post comments and dispatch the review agent"
required: true

Comment thread
don-petry marked this conversation as resolved.
jobs:
handle-mention:
runs-on: ubuntu-latest
permissions:
pull-requests: write
# Fire when:
# (a) donpetry-bot is added as a reviewer on a same-repo PR (fork PRs
# don't receive org secrets so we exclude them), OR
# (b) @petry-review-bot is mentioned in a PR comment / review comment.
#
# Guard each branch against missing fields: requested_reviewer is null for
# team review requests; comment fields don't exist on pull_request events.
if: |
(github.event_name == 'pull_request' &&
github.event.requested_reviewer != null &&
github.event.requested_reviewer.login == 'donpetry-bot' &&
github.event.pull_request.head.repo.full_name == github.repository) ||
(github.event_name != 'pull_request' &&
contains(github.event.comment.body, '@petry-review-bot') &&
(github.event_name == 'pull_request_review_comment' ||
github.event.issue.pull_request != null))

steps:
- name: Check commenter trust level
id: trust
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
run: |
# Only OWNER, MEMBER, and COLLABORATOR can trigger reviews.
# This prevents external contributors or bots from burning review quota.
#
# For review_requested events author_association is absent from the
# sender payload, so query the collaborator permission API instead.
# The endpoint returns a top-level .permission string:
# "admin" | "write" | "read" | "none"
if [ "${{ github.event_name }}" = "pull_request" ]; then
ACTOR="${{ github.event.sender.login }}"
PERM=$(gh api /repos/${{ github.repository }}/collaborators/${{ github.event.sender.login }}/permission \
--jq '.permission' 2>/dev/null || echo "none")
case "$PERM" in
admin|write)
echo "trusted=true" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has permission $PERM — trusted"
;;
*)
echo "trusted=false" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has permission $PERM — not trusted, skipping"
;;
esac
else
ACTOR="${{ github.event.comment.user.login }}"
ASSOC="${{ github.event.comment.author_association }}"
case "$ASSOC" in
OWNER|MEMBER|COLLABORATOR)
echo "trusted=true" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has association $ASSOC — trusted"
;;
*)
echo "trusted=false" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has association $ASSOC — not trusted, skipping"
;;
esac
fi

- name: Resolve PR URL
id: pr
if: steps.trust.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
PR_URL="${{ github.event.pull_request.html_url }}"
elif [ "${{ github.event_name }}" = "pull_request_review_comment" ]; then
PR_URL="${{ github.event.pull_request.html_url }}"
else
# issue_comment: resolve the PR URL from the issue's pull_request link
PR_URL=$(gh api "${{ github.event.issue.pull_request.url }}" --jq '.html_url')
fi
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
echo "PR URL: $PR_URL"

- name: Post acknowledgement comment
if: steps.trust.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
PR_URL: ${{ steps.pr.outputs.pr_url }}
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
ACTOR="${{ github.event.sender.login }}"
MSG="@${ACTOR} assigned me as reviewer — starting a fresh review now. Results will appear in a few minutes."
else
ACTOR="${{ github.event.comment.user.login }}"
MSG="@${ACTOR} I'm on it — starting a fresh review now. Results will appear in a few minutes."
fi
gh pr comment "$PR_URL" --body "$(printf '<!-- pr-review-agent mention-ack -->\n%s' "${MSG}")"

- name: Trigger review agent
if: steps.trust.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
PR_URL: ${{ steps.pr.outputs.pr_url }}
run: |
# repository_dispatch requires only Contents: write, not Actions: write.
# pr-review.yml in .github-private listens for "pr-review-mention" and
# extracts client_payload.pr_url to run the cascade.
gh api \
--method POST \
--header "Accept: application/vnd.github+json" \
/repos/petry-projects/.github-private/dispatches \
--field event_type=pr-review-mention \
--field "client_payload[pr_url]=$PR_URL"
echo "Dispatch sent for $PR_URL"
146 changes: 26 additions & 120 deletions .github/workflows/pr-review-mention.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# Deploy this file to petry-projects/.github as:
# .github/workflows/pr-review-mention.yml
# ─────────────────────────────────────────────────────────────────────────────
# SOURCE OF TRUTH: petry-projects/.github/standards/workflows/pr-review-mention.yml
# Standard: petry-projects/.github/standards/ci-standards.md
# Reusable: petry-projects/.github/.github/workflows/pr-review-mention-reusable.yml
#
# Purpose: Trigger the pr-review-agent whenever @petry-review-bot is mentioned
# in a PR comment or review comment, OR when donpetry-bot is assigned
# as a reviewer on a PR within the petry-projects org.
# AGENTS — READ BEFORE EDITING:
# • This file is a THIN CALLER STUB. All review-dispatch logic lives in the
# reusable workflow above.
# • You MAY change: nothing in normal use. NOTE: this file intentionally uses
# a LOCAL ref (`./`) instead of a pinned SHA — this repo IS the source of
# truth, so a local ref is always current. Other repos use @v1
# (see standards/workflows/pr-review-mention.yml).
# • You MUST NOT change: trigger events or the job-level `permissions:` block —
# reusable workflows can be granted no more permissions than the calling job,
# so removing the stanza breaks the reusable's gh API calls.
# • If you need different behaviour, open a PR against the reusable in the
# central repo.
# ─────────────────────────────────────────────────────────────────────────────
#
# Prerequisites:
# Uses GH_PAT_WORKFLOWS (org secret, classic PAT with repo scope) — already
# present in petry-projects org. No additional secret setup required.

# PR Review Mention — thin caller for the org-level reusable.
# To adopt: copy standards/workflows/pr-review-mention.yml to your repo.
# Requires: GH_PAT_WORKFLOWS org secret (already present in petry-projects org).
name: PR Review — Mention Trigger

on:
Expand All @@ -19,116 +30,11 @@ on:
pull_request:
types: [review_requested]

permissions:
pull-requests: write
permissions: {}

jobs:
handle-mention:
runs-on: ubuntu-latest
# Fire when:
# (a) donpetry-bot is added as a reviewer on a same-repo PR (fork PRs
# don't receive org secrets so we exclude them), OR
# (b) @petry-review-bot is mentioned in a PR comment / review comment.
#
# Guard each branch against missing fields: requested_reviewer is null for
# team review requests; comment fields don't exist on pull_request events.
if: |
(github.event_name == 'pull_request' &&
github.event.requested_reviewer != null &&
github.event.requested_reviewer.login == 'donpetry-bot' &&
github.event.pull_request.head.repo.full_name == github.repository) ||
(github.event_name != 'pull_request' &&
contains(github.event.comment.body, '@petry-review-bot') &&
(github.event_name == 'pull_request_review_comment' ||
github.event.issue.pull_request != null))

steps:
- name: Check commenter trust level
id: trust
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
run: |
# Only OWNER, MEMBER, and COLLABORATOR can trigger reviews.
# This prevents external contributors or bots from burning review quota.
#
# For review_requested events author_association is absent from the
# sender payload, so query the collaborator permission API instead.
# The endpoint returns a top-level .permission string:
# "admin" | "write" | "read" | "none"
if [ "${{ github.event_name }}" = "pull_request" ]; then
ACTOR="${{ github.event.sender.login }}"
PERM=$(gh api /repos/${{ github.repository }}/collaborators/${{ github.event.sender.login }}/permission \
--jq '.permission' 2>/dev/null || echo "none")
case "$PERM" in
admin|write)
echo "trusted=true" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has permission $PERM — trusted"
;;
*)
echo "trusted=false" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has permission $PERM — not trusted, skipping"
;;
esac
else
ACTOR="${{ github.event.comment.user.login }}"
ASSOC="${{ github.event.comment.author_association }}"
case "$ASSOC" in
OWNER|MEMBER|COLLABORATOR)
echo "trusted=true" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has association $ASSOC — trusted"
;;
*)
echo "trusted=false" >> "$GITHUB_OUTPUT"
echo "Actor @$ACTOR has association $ASSOC — not trusted, skipping"
;;
esac
fi

- name: Resolve PR URL
id: pr
if: steps.trust.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
PR_URL="${{ github.event.pull_request.html_url }}"
elif [ "${{ github.event_name }}" = "pull_request_review_comment" ]; then
PR_URL="${{ github.event.pull_request.html_url }}"
else
# issue_comment: resolve the PR URL from the issue's pull_request link
PR_URL=$(gh api "${{ github.event.issue.pull_request.url }}" --jq '.html_url')
fi
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
echo "PR URL: $PR_URL"

- name: Post acknowledgement comment
if: steps.trust.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
PR_URL: ${{ steps.pr.outputs.pr_url }}
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
ACTOR="${{ github.event.sender.login }}"
MSG="@${ACTOR} assigned me as reviewer — starting a fresh review now. Results will appear in a few minutes."
else
ACTOR="${{ github.event.comment.user.login }}"
MSG="@${ACTOR} I'm on it — starting a fresh review now. Results will appear in a few minutes."
fi
gh pr comment "$PR_URL" --body "$(printf '<!-- pr-review-agent mention-ack -->\n%s' "${MSG}")"

- name: Trigger review agent
if: steps.trust.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOWS }}
PR_URL: ${{ steps.pr.outputs.pr_url }}
run: |
# Use repository_dispatch (requires only Contents: write, not Actions: write).
# pr-review.yml listens for type "pr-review-mention" and extracts
# client_payload.pr_url to run the cascade.
gh api \
--method POST \
--header "Accept: application/vnd.github+json" \
/repos/petry-projects/.github-private/dispatches \
--field event_type=pr-review-mention \
--field "client_payload[pr_url]=$PR_URL"
echo "Dispatch sent for $PR_URL"
pr-review-mention:
permissions:
pull-requests: write
uses: ./.github/workflows/pr-review-mention-reusable.yml # local ref — always current
secrets: inherit
3 changes: 2 additions & 1 deletion scripts/compliance-audit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ FINDINGS_FILE="$REPORT_DIR/findings.json"
SUMMARY_FILE="$REPORT_DIR/summary.md"
ISSUES_FILE="$REPORT_DIR/issues.json"

REQUIRED_WORKFLOWS=(ci.yml sonarcloud.yml claude.yml dependabot-automerge.yml dependency-audit.yml agent-shield.yml)
REQUIRED_WORKFLOWS=(ci.yml sonarcloud.yml claude.yml dependabot-automerge.yml dependency-audit.yml agent-shield.yml pr-review-mention.yml)
Comment thread
don-petry marked this conversation as resolved.
# Note: codeql.yml is intentionally NOT in REQUIRED_WORKFLOWS. CodeQL is now
# configured via GitHub-managed default setup (Settings → Code security →
# Code scanning), not a per-repo workflow file. The check_codeql_default_setup
Expand Down Expand Up @@ -665,6 +665,7 @@ check_centralized_workflow_stubs() {
"dependabot-rebase.yml:dependabot-rebase-reusable"
"agent-shield.yml:agent-shield-reusable"
"feature-ideation.yml:feature-ideation-reusable"
"pr-review-mention.yml:pr-review-mention-reusable"
)

# List the repo's workflow directory once instead of probing each file.
Expand Down
Loading