-
Notifications
You must be signed in to change notification settings - Fork 200
201 lines (179 loc) 路 8.07 KB
/
Copy pathauthorize-agentic-ci.yml
File metadata and controls
201 lines (179 loc) 路 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
name: "Authorize Agentic CI"
on:
issue_comment:
types: [created]
permissions:
actions: write
contents: read
issues: write
pull-requests: read
defaults:
run:
shell: bash
concurrency:
group: authorize-agentic-ci-${{ github.event.issue.number }}
cancel-in-progress: false
jobs:
authorize:
if: >-
github.repository_owner == 'NVIDIA-NeMo'
&& github.event.issue.pull_request != null
&& github.event.comment.body == '/authorize-agentic-ci'
runs-on: ubuntu-latest
steps:
- name: Check commenter permission
env:
GH_TOKEN: ${{ github.token }}
COMMENT_AUTHOR: ${{ github.event.comment.user.login }}
PR_NUMBER: ${{ github.event.issue.number }}
REPO: ${{ github.repository }}
run: |
PERMISSION=$(gh api "repos/${REPO}/collaborators/${COMMENT_AUTHOR}/permission" \
--jq '.permission' 2>/dev/null || echo "none")
echo "Comment author ${COMMENT_AUTHOR} has ${PERMISSION} permission."
case "$PERMISSION" in
admin|maintain|write)
;;
*)
gh api --method POST "repos/${REPO}/issues/${PR_NUMBER}/comments" \
-f body="Only maintainers with write access can authorize Agentic CI checks." >/dev/null || \
echo "::warning::Unable to post permission failure comment."
exit 1
;;
esac
- name: Load PR metadata
id: pr
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.issue.number }}
REPO: ${{ github.repository }}
run: |
PR_JSON=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}")
PR_AUTHOR=$(printf '%s' "$PR_JSON" | jq -r '.user.login')
HEAD_REPO=$(printf '%s' "$PR_JSON" | jq -r '.head.repo.full_name')
HEAD_REF=$(printf '%s' "$PR_JSON" | jq -r '.head.ref')
HEAD_SHA=$(printf '%s' "$PR_JSON" | jq -r '.head.sha')
STATE=$(printf '%s' "$PR_JSON" | jq -r '.state')
PR_BODY=$(printf '%s' "$PR_JSON" | jq -r '.body // ""')
TRUSTED=false
printf '%s' "$PR_BODY" > /tmp/pr-body-raw.txt
# Commit authors can be spoofed; trust only PR metadata GitHub controls.
if [ "$PR_AUTHOR" = "github-actions[bot]" ] && \
[ "$HEAD_REPO" = "$REPO" ] && \
[[ "$HEAD_REF" == agentic-ci/* ]] && \
grep -Eq '<!-- agentic-ci finding=[^[:space:]]+ suite=[^[:space:]]+ -->' /tmp/pr-body-raw.txt; then
TRUSTED=true
fi
echo "author=${PR_AUTHOR}" >> "$GITHUB_OUTPUT"
echo "head_ref=${HEAD_REF}" >> "$GITHUB_OUTPUT"
echo "head_sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT"
echo "state=${STATE}" >> "$GITHUB_OUTPUT"
echo "trusted=${TRUSTED}" >> "$GITHUB_OUTPUT"
- name: Validate Agentic CI PR
env:
COMMENT_ID: ${{ github.event.comment.id }}
GH_TOKEN: ${{ github.token }}
HEAD_SHA: ${{ steps.pr.outputs.head_sha }}
PR_NUMBER: ${{ github.event.issue.number }}
REPO: ${{ github.repository }}
STATE: ${{ steps.pr.outputs.state }}
TRUSTED: ${{ steps.pr.outputs.trusted }}
run: |
comment() {
gh api --method POST "repos/${REPO}/issues/${PR_NUMBER}/comments" \
-f body="$1" >/dev/null || \
echo "::warning::Unable to post authorization failure comment."
}
comment_file() {
tmp=$(mktemp)
trap 'rm -f "$tmp"' RETURN
jq -n --rawfile body "$1" '{body: $body}' > "$tmp"
gh api --method POST "repos/${REPO}/issues/${PR_NUMBER}/comments" \
--input "$tmp" >/dev/null || \
echo "::warning::Unable to post authorization failure comment."
}
if [ "$STATE" != "open" ]; then
comment "Agentic CI checks were not authorized because this PR is not open."
exit 1
fi
if [ "$TRUSTED" != "true" ]; then
comment "Agentic CI checks were not authorized because this PR does not match the trusted Agentic CI metadata."
exit 1
fi
if [ -z "$COMMENT_ID" ]; then
comment "Agentic CI checks were not authorized because the authorization comment ID was missing."
exit 1
fi
COMMENT_FOUND=false
for ATTEMPT in 1 2 3; do
gh api --paginate "repos/${REPO}/issues/${PR_NUMBER}/timeline?per_page=100" \
-H "Accept: application/vnd.github+json" \
--jq '.[] | [.event, ((.id // .sha // "") | tostring)] | @tsv' > /tmp/pr-timeline.tsv
if awk -F '\t' -v comment_id="$COMMENT_ID" '
$1 == "commented" && $2 == comment_id { found = 1 }
END { exit found ? 0 : 1 }
' /tmp/pr-timeline.tsv; then
COMMENT_FOUND=true
break
fi
sleep 2
done
if [ "$COMMENT_FOUND" != "true" ]; then
comment "Agentic CI checks were not authorized because the authorization comment was not found in the PR timeline."
exit 1
fi
HEAD_EVENT_AFTER_COMMENT=$(awk -F '\t' -v comment_id="$COMMENT_ID" '
$1 == "commented" && $2 == comment_id { seen_comment = 1; next }
seen_comment && ($1 == "committed" || $1 == "head_ref_force_pushed" || $1 == "head_ref_deleted" || $1 == "head_ref_restored") {
print $1 " " $2
exit
}
' /tmp/pr-timeline.tsv)
if [ -n "$HEAD_EVENT_AFTER_COMMENT" ]; then
{
echo "Agentic CI checks were not authorized because the PR head changed after the authorization comment."
echo
echo "Latest PR head: \`${HEAD_SHA}\`"
echo "Detected update: \`${HEAD_EVENT_AFTER_COMMENT}\`"
echo
echo "Please review the latest commit and comment \`/authorize-agentic-ci\` again."
} > /tmp/agentic-ci-auth-stale.md
comment_file /tmp/agentic-ci-auth-stale.md
exit 1
fi
BLOCKED=$(gh pr diff "$PR_NUMBER" --repo "$REPO" --name-only \
| grep -E '^\.github/' || true)
if [ -n "$BLOCKED" ]; then
{
echo "Agentic CI checks were not authorized because this PR changes privileged repository files:"
echo
printf '%s\n' "$BLOCKED" | sed 's/^/- `/' | sed 's/$/`/'
} > /tmp/agentic-ci-auth-failed.md
comment_file /tmp/agentic-ci-auth-failed.md
exit 1
fi
for WORKFLOW_PATH in .github/workflows/ci.yml .github/workflows/agentic-ci-authorized-checks.yml; do
MAIN_WORKFLOW_SHA=$(gh api "repos/${REPO}/contents/${WORKFLOW_PATH}?ref=main" --jq '.sha')
HEAD_WORKFLOW_SHA=$(gh api "repos/${REPO}/contents/${WORKFLOW_PATH}?ref=${HEAD_SHA}" --jq '.sha')
if [ "$HEAD_WORKFLOW_SHA" != "$MAIN_WORKFLOW_SHA" ]; then
comment "Agentic CI checks were not authorized because ${WORKFLOW_PATH} does not match main. Update the PR branch and try again."
exit 1
fi
done
echo "Authorizing checks for ${HEAD_SHA}."
- name: Dispatch checks
env:
GH_TOKEN: ${{ github.token }}
HEAD_REF: ${{ steps.pr.outputs.head_ref }}
HEAD_SHA: ${{ steps.pr.outputs.head_sha }}
PR_NUMBER: ${{ github.event.issue.number }}
REPO: ${{ github.repository }}
run: |
gh workflow run ci.yml --repo "$REPO" --ref "$HEAD_REF" \
-f expected_head_sha="$HEAD_SHA"
gh workflow run agentic-ci-authorized-checks.yml --repo "$REPO" --ref "$HEAD_REF" \
-f pr_number="$PR_NUMBER" \
-f expected_head_sha="$HEAD_SHA"
gh api --method POST "repos/${REPO}/issues/${PR_NUMBER}/comments" \
-f body="Authorized Agentic CI checks for \`${HEAD_SHA}\`. Launched CI and authorization checks." >/dev/null || \
echo "::warning::Unable to post authorization confirmation comment."