Repository navigation
220 lines (206 loc) · 9.94 KB
/
Copy pathsync-repair.yml
File metadata and controls
220 lines (206 loc) · 9.94 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
name: Sync repair
# Once CI and the AI review finish on a Claude-resolved sync/develop-into-next pull request, lets Claude fix what
# the merge broke and answer the review. See apps/cli/docs/release-process.md#claude-resolved-develop-into-next.
on:
# Starts when the sync branch's checks finish. Every script runs from the default-branch
# checkout, never from the sync branch.
# jactionlint ignore=dangerous-triggers
workflow_run:
workflows:
- Test
- AI Review
- GitHub Scripts CI
- Lint Pull Request
- CodeQL
- run-ci
types:
- completed
# A run started by the job token, such as the dispatched AI review, may not emit `workflow_run`.
schedule:
- cron: "23 * * * *"
workflow_dispatch:
permissions: {}
concurrency:
group: sync-repair-develop-into-next
cancel-in-progress: false
jobs:
repair:
name: Repair with Claude
# AI Review runs are dispatched from the default branch, and their run name carries the PR number, so match the file.
if: >-
github.event_name != 'workflow_run' ||
github.event.workflow_run.head_branch == 'sync/develop-into-next' ||
github.event.workflow_run.path == '.github/workflows/ai-review.yml'
runs-on: ubuntu-latest
timeout-minutes: 75
permissions:
contents: read
pull-requests: read
checks: read
actions: read
outputs:
action: ${{ steps.collect.outputs.action }}
pr: ${{ steps.collect.outputs.pr }}
head: ${{ steps.collect.outputs.head }}
rereview: ${{ steps.publish.outputs.rereview }}
env:
OUTPUT_DIR: ${{ github.workspace }}/sync-agent
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
path: trusted
persist-credentials: false
- name: Load Claude model
id: model
run: echo "model=$(jq -r .claude.opus trusted/.github/ai-models.json)" >> "$GITHUB_OUTPUT"
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version-file: trusted/.bun-version
- name: Collect
id: collect
working-directory: trusted
env:
GH_TOKEN: ${{ github.token }}
run: bun .github/scripts/sync-agent.ts collect
- if: steps.collect.outputs.action == 'repair'
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
path: work
ref: ${{ steps.collect.outputs.head }}
fetch-depth: 0
persist-credentials: false
# Same pinned release and isolated npm config as the AI review workflow.
# jactionlint ignore=adhoc-packages
- name: Install Claude Code CLI
if: steps.collect.outputs.action == 'repair'
run: |
npm install -g \
--userconfig "${RUNNER_TEMP}/sync-npmrc-user" \
--globalconfig "${RUNNER_TEMP}/sync-npmrc-global" \
--registry=https://registry.npmjs.org/ @anthropic-ai/claude-code@2.1.280
- name: Repair
id: repair
if: steps.collect.outputs.action == 'repair'
working-directory: trusted
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
CLAUDE_MODEL: ${{ steps.model.outputs.model }}
WORK_DIR: ${{ github.workspace }}/work
HEAD_SHA: ${{ steps.collect.outputs.head }}
run: |
git -C "$WORK_DIR" config user.name "github-actions[bot]"
git -C "$WORK_DIR" config user.email "41898282+github-actions[bot]@users.noreply.github.com"
bun .github/scripts/sync-agent.ts repair
- id: app-token
if: steps.collect.outputs.action == 'repair' || steps.collect.outputs.action == 'exhausted'
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
with:
client-id: ${{ vars.GH_APP_CLIENT_ID }}
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
# Stops when the branch moved or was deleted since the checks settled; the next completed run starts over. The round
# comment is posted before the push and the replies, so neither failing can hide a round from the limit. The
# re-review comment is posted before the dispatch, so a failed dispatch is never retried.
- name: Publish
id: publish
if: steps.collect.outputs.action == 'repair' || steps.collect.outputs.action == 'exhausted'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR: ${{ steps.collect.outputs.pr }}
HEAD_SHA: ${{ steps.collect.outputs.head }}
CHANGED: ${{ steps.repair.outputs.changed }}
run: |
set -o pipefail
if [ "$(gh pr view "$PR" --repo "$GITHUB_REPOSITORY" --json headRefOid -q .headRefOid)" != "$HEAD_SHA" ]; then
echo "::notice::The sync branch moved since the checks settled; skipping."
exit 0
fi
if [ "$CHANGED" = true ] && ! git -C work diff --quiet "$HEAD_SHA" HEAD -- .github/; then
echo "::error::The repair commit changes .github/; workflow edits must come from a maintainer."
exit 1
fi
gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body-file "$OUTPUT_DIR/comment.md"
if [ "$CHANGED" = true ]; then
git -C work -c core.hooksPath=/dev/null push \
--force-with-lease="refs/heads/sync/develop-into-next:$HEAD_SHA" \
"https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEAD:refs/heads/sync/develop-into-next
gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body-file "$OUTPUT_DIR/rereview.md"
echo "rereview=true" >> "$GITHUB_OUTPUT"
fi
# The workflow suggestions hold the head before any reply counts a withheld fix as answered.
if [ "$CHANGED" != "" ]; then
(cd trusted && REPO_DIR="$GITHUB_WORKSPACE/work" HEAD_SHA="$(git -C "$GITHUB_WORKSPACE/work" rev-parse HEAD)" \
bun .github/scripts/sync-agent.ts suggest)
fi
if [ -f "$OUTPUT_DIR/replies.json" ]; then
jq -c '.[]' "$OUTPUT_DIR/replies.json" | while read -r reply; do
gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR}/comments/$(jq -r .commentId <<<"$reply")/replies" \
-f body="$(jq -r .body <<<"$reply")" > /dev/null
if [ "$(jq -r .resolve <<<"$reply")" = true ]; then
# shellcheck disable=SC2016 # `$threadId` is a GraphQL variable.
gh api graphql -f threadId="$(jq -r .threadId <<<"$reply")" \
-f query='mutation($threadId: ID!) { resolveReviewThread(input: {threadId: $threadId}) { thread { id } } }' > /dev/null
fi
done
fi
# The AI review skips bot pull requests, so the job's own token dispatches it (`actions: write`), kept out of the job
# that runs Claude on the branch.
rereview:
name: Re-run the AI review on the repair
needs: repair
# Runs after a later Publish failure too, since the posted re-review marker already rules out another dispatch.
if: ${{ !cancelled() && needs.repair.outputs.rereview == 'true' }}
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
actions: write
pull-requests: write
steps:
# The re-review marker is already posted, so a failed dispatch is never retried and needs a maintainer.
- name: Dispatch the AI review
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ needs.repair.outputs.pr }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
if ! gh workflow run ai-review.yml --repo "$GITHUB_REPOSITORY" -f pr="$PR"; then
gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" \
--body "@supabase/cli Re-running the AI review on the repair failed; comment \`/ai-review\` or take over ([workflow run](${RUN_URL}))."
exit 1
fi
# A green head both AI review models saw, with every finding fixed or declined and no change request.
# The approval runs fast-forward.yml, which re-checks the head and its checks before moving `next`.
approve:
name: Approve the reviewed sync
needs: repair
if: needs.repair.outputs.action == 'approve'
runs-on: ubuntu-latest
timeout-minutes: 5
permissions: {}
# Holds `AUTO_APPROVE_PR_PAT`; the environment only deploys from the default branch.
environment: auto-approve
steps:
- name: Approve
env:
GH_TOKEN: ${{ secrets.AUTO_APPROVE_PR_PAT }}
PR: ${{ needs.repair.outputs.pr }}
HEAD_SHA: ${{ needs.repair.outputs.head }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
set -euo pipefail
if [ "$(gh pr view "$PR" --repo "$GITHUB_REPOSITORY" --json headRefOid -q .headRefOid)" != "$HEAD_SHA" ]; then
echo "::notice::The sync branch moved since the checks settled; skipping."
exit 0
fi
login="$(gh api user --jq .login)"
# A dismissed approval on this head means a maintainer took it back, so it is never posted again.
previous="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/pulls/${PR}/reviews" \
--jq ".[] | select(.user.login == \"$login\" and .commit_id == \"$HEAD_SHA\" and (.state == \"APPROVED\" or .state == \"DISMISSED\")) | .state")"
if [ -n "$previous" ]; then
echo "::notice::${login} already approved ${HEAD_SHA} (${previous//$'\n'/, })."
exit 0
fi
gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR}/reviews" -f commit_id="$HEAD_SHA" -f event=APPROVE \
-f body="CI is green and the AI review raised nothing the merge introduced, so this approves Claude's resolution of the sync (${RUN_URL})." > /dev/null