|
| 1 | +name: "Authorize Agentic CI" |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + actions: write |
| 9 | + contents: read |
| 10 | + issues: write |
| 11 | + pull-requests: read |
| 12 | + |
| 13 | +defaults: |
| 14 | + run: |
| 15 | + shell: bash |
| 16 | + |
| 17 | +jobs: |
| 18 | + authorize: |
| 19 | + if: >- |
| 20 | + github.repository_owner == 'NVIDIA-NeMo' |
| 21 | + && github.event.issue.pull_request != null |
| 22 | + && github.event.comment.body == '/authorize-agentic-ci' |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - name: Check commenter permission |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ github.token }} |
| 28 | + COMMENT_AUTHOR: ${{ github.event.comment.user.login }} |
| 29 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 30 | + REPO: ${{ github.repository }} |
| 31 | + run: | |
| 32 | + PERMISSION=$(gh api "repos/${REPO}/collaborators/${COMMENT_AUTHOR}/permission" \ |
| 33 | + --jq '.permission' 2>/dev/null || echo "none") |
| 34 | + echo "Comment author ${COMMENT_AUTHOR} has ${PERMISSION} permission." |
| 35 | +
|
| 36 | + case "$PERMISSION" in |
| 37 | + admin|maintain|write) |
| 38 | + ;; |
| 39 | + *) |
| 40 | + gh issue comment "$PR_NUMBER" --repo "$REPO" --body \ |
| 41 | + "Only maintainers with write access can authorize Agentic CI checks." |
| 42 | + exit 1 |
| 43 | + ;; |
| 44 | + esac |
| 45 | +
|
| 46 | + - name: Load PR metadata |
| 47 | + id: pr |
| 48 | + env: |
| 49 | + GH_TOKEN: ${{ github.token }} |
| 50 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 51 | + REPO: ${{ github.repository }} |
| 52 | + run: | |
| 53 | + PR_JSON=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}") |
| 54 | +
|
| 55 | + PR_AUTHOR=$(printf '%s' "$PR_JSON" | jq -r '.user.login') |
| 56 | + HEAD_REPO=$(printf '%s' "$PR_JSON" | jq -r '.head.repo.full_name') |
| 57 | + HEAD_REF=$(printf '%s' "$PR_JSON" | jq -r '.head.ref') |
| 58 | + HEAD_SHA=$(printf '%s' "$PR_JSON" | jq -r '.head.sha') |
| 59 | + STATE=$(printf '%s' "$PR_JSON" | jq -r '.state') |
| 60 | + PR_BODY=$(printf '%s' "$PR_JSON" | jq -r '.body // ""') |
| 61 | +
|
| 62 | + TRUSTED=false |
| 63 | + printf '%s' "$PR_BODY" > /tmp/pr-body-raw.txt |
| 64 | + # Commit authors can be spoofed; trust only PR metadata GitHub controls. |
| 65 | + if { [ "$PR_AUTHOR" = "github-actions[bot]" ] || [ "$PR_AUTHOR" = "agentic-ci" ]; } && \ |
| 66 | + [ "$HEAD_REPO" = "$REPO" ] && \ |
| 67 | + [[ "$HEAD_REF" == agentic-ci/* ]] && \ |
| 68 | + grep -q '<!-- agentic-ci ' /tmp/pr-body-raw.txt; then |
| 69 | + TRUSTED=true |
| 70 | + fi |
| 71 | +
|
| 72 | + echo "author=${PR_AUTHOR}" >> "$GITHUB_OUTPUT" |
| 73 | + echo "head_ref=${HEAD_REF}" >> "$GITHUB_OUTPUT" |
| 74 | + echo "head_sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT" |
| 75 | + echo "state=${STATE}" >> "$GITHUB_OUTPUT" |
| 76 | + echo "trusted=${TRUSTED}" >> "$GITHUB_OUTPUT" |
| 77 | +
|
| 78 | + - name: Validate Agentic CI PR |
| 79 | + env: |
| 80 | + GH_TOKEN: ${{ github.token }} |
| 81 | + HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 82 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 83 | + REPO: ${{ github.repository }} |
| 84 | + STATE: ${{ steps.pr.outputs.state }} |
| 85 | + TRUSTED: ${{ steps.pr.outputs.trusted }} |
| 86 | + run: | |
| 87 | + if [ "$STATE" != "open" ]; then |
| 88 | + gh issue comment "$PR_NUMBER" --repo "$REPO" --body \ |
| 89 | + "Agentic CI checks were not authorized because this PR is not open." |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + if [ "$TRUSTED" != "true" ]; then |
| 94 | + gh issue comment "$PR_NUMBER" --repo "$REPO" --body \ |
| 95 | + "Agentic CI checks were not authorized because this PR does not match the trusted Agentic CI metadata." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +
|
| 99 | + BLOCKED=$(gh pr diff "$PR_NUMBER" --repo "$REPO" --name-only \ |
| 100 | + | grep -E '^\.github/(workflows|actions|scripts)/' || true) |
| 101 | + if [ -n "$BLOCKED" ]; then |
| 102 | + { |
| 103 | + echo "Agentic CI checks were not authorized because this PR changes privileged workflow files:" |
| 104 | + echo |
| 105 | + printf '%s\n' "$BLOCKED" | sed 's/^/- `/' | sed 's/$/`/' |
| 106 | + } > /tmp/agentic-ci-auth-failed.md |
| 107 | + gh issue comment "$PR_NUMBER" --repo "$REPO" --body-file /tmp/agentic-ci-auth-failed.md |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | +
|
| 111 | + echo "Authorizing checks for ${HEAD_SHA}." |
| 112 | +
|
| 113 | + - name: Dispatch checks |
| 114 | + env: |
| 115 | + GH_TOKEN: ${{ github.token }} |
| 116 | + HEAD_REF: ${{ steps.pr.outputs.head_ref }} |
| 117 | + HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 118 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 119 | + REPO: ${{ github.repository }} |
| 120 | + run: | |
| 121 | + gh workflow run ci.yml --repo "$REPO" --ref "$HEAD_REF" |
| 122 | + gh workflow run agentic-ci-authorized-checks.yml --repo "$REPO" --ref "$HEAD_REF" \ |
| 123 | + -f pr_number="$PR_NUMBER" \ |
| 124 | + -f expected_head_sha="$HEAD_SHA" |
| 125 | +
|
| 126 | + gh issue comment "$PR_NUMBER" --repo "$REPO" --body \ |
| 127 | + "Authorized Agentic CI checks for \`${HEAD_SHA}\`. Launched CI and authorization checks." |
0 commit comments