-
Notifications
You must be signed in to change notification settings - Fork 257
258 lines (238 loc) · 13.2 KB
/
Copy pathtend-mention.yaml
File metadata and controls
258 lines (238 loc) · 13.2 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
# Generated by tend 0.1.3. Regenerate with: uvx tend@latest init
#
# Do not edit this file directly — it will be overwritten on regeneration.
# To customize behavior, edit the relevant skill (for example,
# `running-tend`) in this repo's .claude/skills/ directory, or open an issue at
# https://github.com/max-sixty/tend/issues for changes that need to
# happen upstream in the tend-ci-runner plugin.
name: tend-mention
on:
issues:
types: [edited]
issue_comment:
types: [created, edited]
# Works for same-repo PRs only; secrets unavailable on fork PRs (no _target variant exists)
pull_request_review:
types: [submitted]
# `created` is intentionally absent. Modern GitHub fires *both*
# pull_request_review and pull_request_review_comment for every newly-created
# inline comment (the standalone POST /pulls/PR/comments endpoint, the
# /replies endpoint, the "Add single comment" UI button, and reviews
# submitted with inline comments — all empirically verified). Subscribing to
# `created` would produce a duplicate workflow run that collides on the
# tend-mention-handle-PR concurrency group, with the loser cancelled and
# posted as a CANCELLED check_run on the PR head SHA — which renders the
# PR's statusCheckRollup as FAILURE even though the bot did its job from the
# sibling run. Edits have no sibling event (review submissions don't fire on
# edits), so we still need to listen for `edited` to catch edit-to-summon
# ("@bot" added to an existing comment after the fact).
pull_request_review_comment:
types: [edited]
jobs:
verify:
# Filter out fork PRs for review events — secrets are unavailable there
# (no _target variant exists). The notifications workflow polls for these.
# Skip comments on `tend-outage` issues: the action's Report-failure step
# auto-comments on those when Claude invocation fails, and without this
# guard those comments re-trigger tend-mention during a persistent outage
# (e.g. Anthropic 401), producing a self-sustaining ~1 run/minute loop
# until the outage clears. The prompt's self-loop guard can't help here
# because the model never executes — the action fails before Claude starts.
if: |
(github.event_name == 'issues' &&
contains(github.event.issue.body, '@prql-bot')) ||
(github.event_name == 'issue_comment' &&
!contains(github.event.issue.labels.*.name, 'tend-outage')) ||
(github.event_name == 'pull_request_review_comment' &&
github.event.pull_request.head.repo.full_name == github.repository) ||
(github.event_name == 'pull_request_review' &&
github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-24.04
outputs:
should_run: ${{ steps.check.outputs.should_run }}
steps:
- name: Verify bot engagement
id: check
run: |
# Mentions always run
if [ "$EVENT_NAME" = "issues" ]; then
echo "should_run=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ -n "$COMMENT_BODY" ] && printf '%s\n' "$COMMENT_BODY" | grep -qF '@prql-bot'; then
echo "should_run=true" >> "$GITHUB_OUTPUT"
exit 0
fi
# Undirected bot comments (deploy notifications, CI status) on a
# bot-authored PR otherwise fall through to the PR-author short-circuit
# below and spin up a no-op session — twice per notification when the
# source bot edits its comment. Skip them here.
# TODO: reassess — a quick pass of a small/fast model could replace
# this blanket skip with content-aware filtering (only drop comments
# that have no actionable signal), so we don't miss bot comments that
# actually warrant a response.
if [ "$EVENT_NAME" = "issue_comment" ] && [ "$COMMENT_AUTHOR_TYPE" = "Bot" ]; then
echo "should_run=false" >> "$GITHUB_OUTPUT"
exit 0
fi
# pull_request_review payloads include review.body (checked above)
# but NOT the bodies of inline comments attached to the review.
# Fetch them so a first-contact @-mention inside an inline comment
# is detected on PRs where the bot has no prior engagement.
if [ "$EVENT_NAME" = "pull_request_review" ] && [ -n "$REVIEW_ID" ]; then
if gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$EVENT_PR_NUMBER/reviews/$REVIEW_ID/comments" \
--jq '.[].body' | grep -qF '@prql-bot'; then
echo "should_run=true" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
# Non-mention: check bot engagement
if [ "$EVENT_NAME" = "issue_comment" ]; then
ISSUE_NUMBER="$ISSUE_OR_PR_NUMBER"
if [ -z "$PR_URL" ]; then
if [ "$ISSUE_AUTHOR" = "prql-bot" ]; then
echo "should_run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
if printf '%s\n' "$ISSUE_BODY" | grep -qF '@prql-bot'; then
echo "should_run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
BOT_COMMENTS=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$ISSUE_NUMBER/comments" \
--jq '[.[] | select(.user.login == "prql-bot")] | length')
if [ "$BOT_COMMENTS" -gt "0" ]; then
echo "should_run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
echo "should_run=false" >> "$GITHUB_OUTPUT"; exit 0
fi
PR_NUMBER="$ISSUE_NUMBER"
else
PR_NUMBER="$EVENT_PR_NUMBER"
fi
PR_AUTHOR=$(gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --json author --jq '.author.login')
if [ "$PR_AUTHOR" = "prql-bot" ]; then
echo "should_run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
BOT_REVIEWS=$(gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews" \
--jq '[.[] | select(.user.login == "prql-bot")] | length')
if [ "$BOT_REVIEWS" -gt "0" ]; then
echo "should_run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
BOT_COMMENTS=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" \
--jq '[.[] | select(.user.login == "prql-bot")] | length')
if [ "$BOT_COMMENTS" -gt "0" ]; then
echo "should_run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
echo "should_run=false" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.TEND_BOT_TOKEN }}
EVENT_NAME: ${{ github.event_name }}
COMMENT_BODY: ${{ github.event.comment.body || github.event.review.body }}
COMMENT_AUTHOR_TYPE: ${{ github.event.comment.user.type }}
ISSUE_BODY: ${{ github.event.issue.body }}
ISSUE_OR_PR_NUMBER: ${{ github.event.issue.number }}
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
PR_URL: ${{ github.event.issue.pull_request.url }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
REVIEW_ID: ${{ github.event.review.id }}
- name: React to mention
if: |
steps.check.outputs.should_run == 'true'
&& github.event.comment
&& contains(github.event.comment.body, '@prql-bot')
run: |
gh api "repos/$REPO/issues/comments/$COMMENT_ID/reactions" -f content=eyes 2>/dev/null || \
gh api "repos/$REPO/pulls/comments/$COMMENT_ID/reactions" -f content=eyes 2>/dev/null || true
env:
REPO: ${{ github.repository }}
COMMENT_ID: ${{ github.event.comment.id }}
GITHUB_TOKEN: ${{ secrets.TEND_BOT_TOKEN }}
handle:
needs: verify
if: needs.verify.outputs.should_run == 'true'
concurrency:
group: ${{ github.workflow }}-handle-${{ github.event.issue.number || github.event.pull_request.number }}
cancel-in-progress: false
runs-on: ubuntu-24.04
permissions:
contents: write
pull-requests: write
id-token: write
actions: read
issues: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
fetch-tags: true
token: ${{ secrets.TEND_BOT_TOKEN }}
- uses: ./.github/actions/tend-setup
- name: Check out PR branch
if: |
(github.event_name == 'issue_comment' && github.event.issue.pull_request.url != '') ||
github.event_name == 'pull_request_review_comment' ||
github.event_name == 'pull_request_review'
run: |
PR_STATE=$(gh pr view "$PR_NUMBER" --json state --jq '.state')
if [ "$PR_STATE" = "OPEN" ]; then
gh pr checkout "$PR_NUMBER"
else
echo "::warning::PR is $PR_STATE — staying on default branch"
fi
env:
GITHUB_TOKEN: ${{ secrets.TEND_BOT_TOKEN }}
PR_NUMBER: ${{ github.event_name == 'issue_comment' && github.event.issue.number || github.event.pull_request.number }}
- name: Compute queue delay
id: delay
run: |
if [ -z "$EVENT_TS" ]; then
echo "seconds=" >> "$GITHUB_OUTPUT"
exit 0
fi
event_epoch=$(date -d "$EVENT_TS" +%s)
echo "seconds=$(( $(date +%s) - event_epoch ))" >> "$GITHUB_OUTPUT"
env:
EVENT_TS: ${{ github.event.comment.updated_at || github.event.review.submitted_at || github.event.issue.updated_at }}
- uses: max-sixty/tend@0.1.4
with:
github_token: ${{ secrets.TEND_BOT_TOKEN }}
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
bot_name: prql-bot
model: opus
prompt: >-
${{ steps.delay.outputs.seconds
&& format('This job started {0}s after the triggering event (over ~40s means it was queued). ',
steps.delay.outputs.seconds) || '' }}Before acting,
check recent comments: exit silently if the bot already responded
to the trigger; handle any other unaddressed comments too.
${{ github.event_name == 'issues'
&& format('An issue was updated with a mention of you ({0}). Read it and respond.', github.event.issue.html_url)
|| (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@prql-bot')
&& format('You were mentioned in an inline review comment on PR #{0} ({1}, comment ID {2}). Read the full context, then respond. If changes are requested, make them, commit, and push.', (github.event_name == 'issue_comment' && github.event.issue.number || github.event.pull_request.number), github.event.comment.html_url, github.event.comment.id))
|| (github.event_name == 'pull_request_review_comment'
&& format('An inline review comment was posted on a PR where you previously participated (PR #{0}, {1}, comment ID {2}). Read the full context. Only respond if the comment is directed at you or requests changes.', (github.event_name == 'issue_comment' && github.event.issue.number || github.event.pull_request.number), github.event.comment.html_url, github.event.comment.id))
|| (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@prql-bot')
&& format('A review was submitted on PR #{0} that mentions you ({1}, review ID {2}). Read the review and full context, then respond. If changes were requested, make them, commit, and push.', github.event.pull_request.number, github.event.review.html_url, github.event.review.id))
|| (github.event_name == 'pull_request_review'
&& format('A review was submitted on a PR where you previously participated (PR #{0}, {1}, review ID {2}). Read the review and full context. If the review requests changes or asks questions, respond appropriately. If the review approves or is between humans, exit silently.', github.event.pull_request.number, github.event.review.html_url, github.event.review.id))
|| (contains(github.event.comment.body, '@prql-bot')
&& format('You were mentioned in a comment ({0}). Read the full context and respond. If changes are requested, make them, commit, and push.', github.event.comment.html_url))
|| format('A user commented on an issue/PR where you previously participated ({0}). Read the full context. Only respond if the comment is directed at you, asks a question you can help with, or requests changes you can make. If the conversation is between other participants, exit silently.', github.event.comment.html_url)
}}
- name: Remove eyes reaction
if: |
always()
&& github.event.comment
&& contains(github.event.comment.body, '@prql-bot')
run: |
for KIND in issues pulls; do
REACTION_ID=$(gh api "repos/$REPO/$KIND/comments/$COMMENT_ID/reactions?content=eyes" \
--jq ".[] | select(.user.login == \"$BOT_NAME\") | .id" 2>/dev/null | head -n1)
if [ -n "$REACTION_ID" ]; then
gh api -X DELETE "repos/$REPO/$KIND/comments/$COMMENT_ID/reactions/$REACTION_ID" 2>/dev/null && break
fi
done
env:
REPO: ${{ github.repository }}
COMMENT_ID: ${{ github.event.comment.id }}
BOT_NAME: prql-bot
GITHUB_TOKEN: ${{ secrets.TEND_BOT_TOKEN }}