-
Notifications
You must be signed in to change notification settings - Fork 736
304 lines (300 loc) · 13.6 KB
/
backport-command.yml
File metadata and controls
304 lines (300 loc) · 13.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
299
300
301
302
303
304
---
name: backport-command
on:
repository_dispatch:
types: [backport-command]
env:
SCRIPT_DIR: "${{ github.workspace }}/.github/workflows/scripts/backport-command"
PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
ORIG_ISSUE_URL: ${{ github.event.client_payload.github.payload.issue.html_url }}
TARGET_ORG: ${{ github.event.client_payload.slash_command.args.named.org }}
TARGET_REPO: ${{ github.event.client_payload.slash_command.args.named.repo }}
ARG1: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 }}
MILESTONE_ARG: ${{ github.event.client_payload.slash_command.args.named.milestone }}
TARGET_FULL_REPO: ${{ github.event.client_payload.slash_command.args.named.org }}/${{ github.event.client_payload.slash_command.args.named.repo }}
permissions:
id-token: write
contents: read
jobs:
# assumptions:
# label "kind/backport" exists
# the TARGET_REPO has been already forked into the bots account
# outputs the source of the comment (PR or issue)
backport-type:
outputs:
commented_on: ${{ steps.get_backport_type.outputs.commented_on }}
backport_branch: ${{ steps.get_backport_type.outputs.backport_branch }}
target_milestone: ${{ steps.get_backport_type.outputs.target_milestone }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.RP_AWS_CRED_REGION }}
role-to-assume: arn:aws:iam::${{ secrets.RP_AWS_CRED_ACCOUNT_ID }}:role/${{ vars.RP_AWS_CRED_BASE_ROLE_NAME }}${{ github.event.repository.name }}
- uses: aws-actions/aws-secretsmanager-get-secrets@v2
with:
secret-ids: |
,sdlc/prod/github/actions_bot_token
parse-json-secrets: true
- name: Get type of backport (issue or PR)
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
CLIENT_PAYLOAD: ${{ toJson(github.event.client_payload) }}
id: get_backport_type
run: $SCRIPT_DIR/get_backport_type.sh
shell: bash
- name: Failed reaction
uses: peter-evans/create-or-update-comment@v4
if: failure()
with:
token: ${{ env.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reactions: "-1"
- name: Post Error
if: failure()
env:
COMMENTED_ON: ${{ steps.get_backport_type.outputs.commented_on }}
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
run: $SCRIPT_DIR/post_error.sh
shell: bash
# creates backport issue if commented on issue, or
# creates backport PR if commented on PR
# eg /backport v21.11.x
type-branch:
needs: backport-type
runs-on: ubuntu-latest
env:
BACKPORT_BRANCH: ${{ needs.backport-type.outputs.backport_branch }}
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.RP_AWS_CRED_REGION }}
role-to-assume: arn:aws:iam::${{ secrets.RP_AWS_CRED_ACCOUNT_ID }}:role/${{ vars.RP_AWS_CRED_BASE_ROLE_NAME }}${{ github.event.repository.name }}
- uses: aws-actions/aws-secretsmanager-get-secrets@v2
with:
secret-ids: |
,sdlc/prod/github/actions_bot_token
parse-json-secrets: true
- name: Get user
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
id: user
run: |
username=$(gh api user --jq .login)
echo "username=$username" >> $GITHUB_OUTPUT
echo "repo=$TARGET_REPO" >> $GITHUB_OUTPUT
echo "email=vbot@redpanda.com" >> $GITHUB_OUTPUT
- name: Get assignees
env:
ASSIGNEES: ${{ toJson(github.event.client_payload.github.payload.issue.assignees) }}
id: assignees
run: echo "assignees=$(echo $ASSIGNEES | jq -r '.[].login' | paste -s -d ',' -)" >> $GITHUB_OUTPUT
- name: Discover and create milestone
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ needs.backport-type.outputs.target_milestone }}
id: create_milestone
run: $SCRIPT_DIR/create_milestone.sh
shell: bash
- name: Create issue
if: needs.backport-type.outputs.commented_on == 'issue'
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ steps.create_milestone.outputs.milestone }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
ORIG_LABELS: ${{ toJson(github.event.client_payload.github.payload.issue.labels) }}
ASSIGNEES: ${{ steps.assignees.outputs.assignees }}
id: create_issue
run: $SCRIPT_DIR/create_issue.sh
shell: bash
- name: Get commits of PR
if: needs.backport-type.outputs.commented_on == 'pr'
env:
BACKPORT_PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
id: backport_commits
run: |
backport_commits=$(gh api "repos/$TARGET_FULL_REPO/pulls/$BACKPORT_PR_NUMBER/commits" --paginate --jq '[.[].sha[0:10]]|join(" ")')
echo "backport_commits=$backport_commits" >> $GITHUB_OUTPUT
- uses: actions/checkout@v4
if: needs.backport-type.outputs.commented_on == 'pr'
with:
repository: ${{ steps.user.outputs.username }}/${{ steps.user.outputs.repo }}
token: ${{ env.ACTIONS_BOT_TOKEN }}
path: ./fork
- name: Backport commits and get details
if: needs.backport-type.outputs.commented_on == 'pr'
continue-on-error: true
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
BACKPORT_COMMITS: ${{ steps.backport_commits.outputs.backport_commits }}
IS_MERGED: ${{ github.event.client_payload.pull_request.merged }}
PR_BASE_BRANCH: ${{ github.event.client_payload.pull_request.base.ref }}
REPO_DEFAULT_BRANCH: ${{ github.event.client_payload.pull_request.base.repo.default_branch }}
GIT_USER: ${{ steps.user.outputs.username }}
GIT_EMAIL: ${{ steps.user.outputs.email }}
id: pr_details
run: $SCRIPT_DIR/pr_details.sh
shell: bash
- name: AI-assisted backport
if: needs.backport-type.outputs.commented_on == 'pr' && steps.pr_details.outcome == 'failure'
id: ai_backport
uses: anthropics/claude-code-action@v1
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
GH_REPO: ${{ env.TARGET_FULL_REPO }}
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
github_token: ${{ env.ACTIONS_BOT_TOKEN }}
allowed_bots: ""
allowed_non_write_users: ""
show_full_output: true
track_progress: false
claude_args: >
--model opus
--max-turns 80
--disallowed-tools "WebFetch,WebSearch"
--allowed-tools "
Bash(git *),
Bash(gh *),
Bash(date *),
Bash(grep *),
Bash(wc *),
Bash(echo *),
Bash(printf *),
Bash(cat *),
Bash(cd *),
Read,
Write,
Edit,
Glob,
Grep,
Agent"
prompt: |
The plain `git cherry-pick` in the prior step failed. Re-do the
backport using the skill at
${{ github.workspace }}/.claude/skills/create-backport-branch/SKILL.md.
Preflight (bash):
cd "${{ github.workspace }}/fork"
git cherry-pick --abort || true
export SKILL_REPORT_FILE="${{ github.workspace }}/.ai-backport-meta/report.md"
mkdir -p "$(dirname "$SKILL_REPORT_FILE")"
Invoke the skill with:
$0 = ${{ env.BACKPORT_BRANCH }} (target — the release branch)
$1 = ${{ env.PR_NUMBER }} (pr-number)
When the skill finishes successfully:
branch=$(git branch --show-current)
git push --set-upstream origin "$branch"
If the skill aborts (modify/delete, architectural unknown, etc.),
exit 1. Do NOT push, do NOT write a report file.
- name: Load AI skill report
id: load_ai_handoff
if: steps.ai_backport.outcome == 'success'
run: |
report="${GITHUB_WORKSPACE}/.ai-backport-meta/report.md"
if [[ ! -s "$report" ]]; then
echo "::error::Skill report file missing or empty at $report (skill probably aborted)"
exit 1
fi
branch=$(cd "${GITHUB_WORKSPACE}/fork" && git branch --list 'ai-backport-pr-*' --sort=-committerdate --format='%(refname:short)' | head -1)
if [[ -z "$branch" ]]; then
echo "::error::Report file written but no ai-backport-pr-* branch found"
exit 1
fi
{
echo "AI_HEAD_BRANCH=$branch"
echo "AI_REPORT_FILE=$report"
} >> "$GITHUB_ENV"
echo "AI branch: $branch"
echo "AI report: $report ($(wc -l < "$report") lines)"
shell: bash
- name: Diagnostic after AI
if: always() && needs.backport-type.outputs.commented_on == 'pr'
run: |
cd "${GITHUB_WORKSPACE}/fork" 2>/dev/null || exit 0
echo "== git log =="
git log --oneline -20 || true
echo "== git status =="
git status || true
echo "== current branch =="
git branch --show-current || true
echo "== remotes =="
git remote -v || true
echo "== AI_HEAD_BRANCH =="
echo "${AI_HEAD_BRANCH:-<unset>}"
shell: bash
- name: Create pull request
if: needs.backport-type.outputs.commented_on == 'pr' && (steps.pr_details.outcome == 'success' || steps.load_ai_handoff.outcome == 'success')
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ steps.create_milestone.outputs.milestone }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
AUTHOR: ${{ github.event.client_payload.pull_request.user.login }}
HEAD_BRANCH: ${{ env.AI_HEAD_BRANCH || steps.pr_details.outputs.head_branch }}
FIXING_ISSUE_URLS: ${{ steps.pr_details.outputs.fixing_issue_urls }}
ORIG_PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
ORIG_PR_URL: ${{ github.event.client_payload.pull_request.html_url }}
GIT_USER: ${{ steps.user.outputs.username }}
id: create_pr
run: $SCRIPT_DIR/create_pr.sh
shell: bash
- name: Label AI-assisted PR
if: steps.load_ai_handoff.outcome == 'success' && steps.create_pr.outcome == 'success'
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
PR_HEAD: ${{ env.AI_HEAD_BRANCH }}
GIT_USER: ${{ steps.user.outputs.username }}
run: |
pr_num=$(gh pr list --repo "$TARGET_FULL_REPO" \
--head "$GIT_USER:$PR_HEAD" --state open \
--json number --jq '.[0].number')
if [[ -z "$pr_num" ]]; then
echo "::warning::Could not find backport PR with head=$GIT_USER:$PR_HEAD"
exit 0
fi
gh pr edit "$pr_num" --repo "$TARGET_FULL_REPO" \
--add-label ai-resolved-conflicts
shell: bash
- name: Add reaction
if: steps.pr_details.outcome == 'success' || steps.load_ai_handoff.outcome == 'success'
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ env.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reactions: hooray
- name: Failed reaction
if: always() && steps.pr_details.outcome == 'failure' && steps.load_ai_handoff.outcome != 'success'
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ env.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reactions: "-1"
- name: Post Error
if: always() && steps.pr_details.outcome == 'failure' && steps.load_ai_handoff.outcome != 'success'
env:
COMMENTED_ON: ${{ needs.backport-type.outputs.commented_on }}
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
run: $SCRIPT_DIR/post_error.sh
shell: bash
- name: Create Issue On Error
if: always() && steps.pr_details.outcome == 'failure' && steps.load_ai_handoff.outcome != 'success'
env:
GITHUB_TOKEN: ${{ env.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ steps.create_milestone.outputs.milestone }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
ORIG_LABELS: ${{ toJson(github.event.client_payload.github.payload.issue.labels) }}
ORIG_ASSIGNEES: ${{ steps.assignees.outputs.assignees }}
CREATE_ISSUE_ON_ERROR: "true"
ASSIGNEES: ${{ github.event.client_payload.pull_request.user.login }}
HEAD_BRANCH: ${{ steps.pr_details.outputs.head_branch }}
GIT_USER: ${{ steps.user.outputs.username }}
BACKPORT_COMMITS: ${{ steps.backport_commits.outputs.backport_commits }}
id: create_issue_on_backport_error
run: $SCRIPT_DIR/create_issue.sh
shell: bash