-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcodex-review-ready-for-review.yml
More file actions
298 lines (273 loc) · 9.6 KB
/
Copy pathcodex-review-ready-for-review.yml
File metadata and controls
298 lines (273 loc) · 9.6 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
name: "Cleanup Codex reviews when PR is ready"
on:
pull_request:
types: [ready_for_review]
branches:
- main
- "[0-9]+.[0-9]+.x"
permissions: {}
jobs:
cleanup-codex-review:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- name: Cleanup Codex draft review artifacts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.event.repository.name }}
REPOSITORY: ${{ github.repository }}
CHATGPT_CODEX_GRAPHQL_BOT: chatgpt-codex-connector
CHATGPT_CODEX_REST_BOT: chatgpt-codex-connector[bot]
GITHUB_ACTIONS_BOT: github-actions
GITHUB_ACTIONS_BOT_ALT: github-actions[bot]
LEGACY_GITHUB_ACTIONS_BOT: github_actions
LEGACY_GITHUB_ACTIONS_BOT_ALT: github_actions[bot]
CODEX_REVIEW_BODY: "@codex review"
run: |
set -euo pipefail
# For issue comments, the REST endpoint does not include a reactions object in the default list response schema,
# so the expression falls back to 0 even when someone has reacted with 👍. In PRs where a maintainer thumbs-up'd
# a Codex issue comment to keep it, the ready_for_review cleanup still deletes it; use the reactions endpoint
# or a GraphQL query if thumbs-up is meant to be an opt-out for issue comments.
comments_query="$(cat <<'GRAPHQL'
query($owner: String!, $repo: String!, $pr: Int!, $cursor: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
comments(first: 100, after: $cursor) {
nodes {
id
body
author {
login
}
reactionGroups {
content
users {
totalCount
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
GRAPHQL
)"
delete_comment_mutation="$(cat <<'GRAPHQL'
mutation($id: ID!) {
deleteIssueComment(input: {id: $id}) {
clientMutationId
}
}
GRAPHQL
)"
echo "Deleting matching Codex issue comments from PR ${PR_NUMBER}"
cursor=""
while true; do
cursor_arg=(-F "cursor=null")
if [ -n "${cursor}" ]; then
cursor_arg=(-f "cursor=${cursor}")
fi
response="$(
gh api graphql \
-f "owner=${OWNER}" \
-f "repo=${REPO}" \
-F "pr=${PR_NUMBER}" \
"${cursor_arg[@]}" \
-f "query=${comments_query}"
)"
comment_ids_output="$(
jq -r '
def thumbs_up_count:
([
.reactionGroups[]?
| select(.content == "THUMBS_UP")
| .users.totalCount
] | add // 0);
def is_github_actions_bot:
.author.login == env.GITHUB_ACTIONS_BOT
or .author.login == env.GITHUB_ACTIONS_BOT_ALT
or .author.login == env.LEGACY_GITHUB_ACTIONS_BOT
or .author.login == env.LEGACY_GITHUB_ACTIONS_BOT_ALT;
def is_codex_bot:
.author.login == env.CHATGPT_CODEX_GRAPHQL_BOT
or .author.login == env.CHATGPT_CODEX_REST_BOT;
.data.repository.pullRequest.comments.nodes[]
| select(
(thumbs_up_count == 0)
and (
(is_github_actions_bot and .body == env.CODEX_REVIEW_BODY)
or is_codex_bot
)
)
| .id
' <<< "${response}"
)"
comment_ids=()
if [ -n "${comment_ids_output}" ]; then
mapfile -t comment_ids <<< "${comment_ids_output}"
fi
for comment_id in "${comment_ids[@]}"; do
echo "Deleting issue comment ${comment_id}"
gh api graphql \
-f "query=${delete_comment_mutation}" \
-f "id=${comment_id}" \
> /dev/null
done
has_next_page="$(
jq -r '
.data.repository.pullRequest.comments.pageInfo.hasNextPage
' <<< "${response}"
)"
if [ "${has_next_page}" != "true" ]; then
break
fi
cursor="$(
jq -r '
.data.repository.pullRequest.comments.pageInfo.endCursor
' <<< "${response}"
)"
done
reviews_query="$(cat <<'GRAPHQL'
query($owner: String!, $repo: String!, $pr: Int!, $cursor: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
reviews(first: 50, after: $cursor) {
nodes {
id
isMinimized
body
reactionGroups {
content
users {
totalCount
}
}
comments(first: 100) {
nodes {
id
isMinimized
reactionGroups {
content
users {
totalCount
}
}
author {
login
}
}
}
author {
login
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
GRAPHQL
)"
minimize_mutation="$(cat <<'GRAPHQL'
mutation($subjectId: ID!) {
minimizeComment(
input: {subjectId: $subjectId, classifier: OUTDATED}
) {
minimizedComment {
isMinimized
}
}
}
GRAPHQL
)"
echo "Minimizing matching Codex reviews from PR ${PR_NUMBER}"
cursor=""
while true; do
cursor_arg=(-F "cursor=null")
if [ -n "${cursor}" ]; then
cursor_arg=(-f "cursor=${cursor}")
fi
response="$(
gh api graphql \
-f "owner=${OWNER}" \
-f "repo=${REPO}" \
-F "pr=${PR_NUMBER}" \
"${cursor_arg[@]}" \
-f "query=${reviews_query}"
)"
review_comment_ids_output="$(
jq -r '
def thumbs_up_count:
([
.reactionGroups[]?
| select(.content == "THUMBS_UP")
| .users.totalCount
] | add // 0);
def is_codex_bot:
.author.login == env.CHATGPT_CODEX_GRAPHQL_BOT
or .author.login == env.CHATGPT_CODEX_REST_BOT;
.data.repository.pullRequest.reviews.nodes[] as $review
| (
# Collect comment IDs to minimize
$review.comments.nodes[]
| select(
is_codex_bot
and (.isMinimized | not)
and (thumbs_up_count == 0)
and ($review | thumbs_up_count == 0)
)
| .id
),
(
# Collect review ID to minimize
$review
| select(
is_codex_bot
and (.isMinimized | not)
and (.body | length > 0)
and (thumbs_up_count == 0)
and ([$review.comments.nodes[] | thumbs_up_count] | add == 0)
)
| .id
)
' <<< "${response}"
)"
review_comment_ids=()
if [ -n "${review_comment_ids_output}" ]; then
mapfile -t review_comment_ids <<< "${review_comment_ids_output}"
fi
for review_comment_id in "${review_comment_ids[@]}"; do
echo "Minimizing pull request review artifact ${review_comment_id}"
gh api graphql \
-f "query=${minimize_mutation}" \
-f "subjectId=${review_comment_id}" \
> /dev/null
done
has_next_page="$(
jq -r '
.data.repository.pullRequest.reviews.pageInfo.hasNextPage
' <<< "${response}"
)"
if [ "${has_next_page}" != "true" ]; then
break
fi
cursor="$(
jq -r '
.data.repository.pullRequest.reviews.pageInfo.endCursor
' <<< "${response}"
)"
done