-
-
Notifications
You must be signed in to change notification settings - Fork 339
220 lines (192 loc) · 8.77 KB
/
cherry-pick.yml
File metadata and controls
220 lines (192 loc) · 8.77 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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Cherry Pick to Release Branch
on:
issue_comment:
types: [created]
permissions:
contents: write
issues: write
pull-requests: write
jobs:
cherry-pick:
runs-on: ubuntu-latest
if: |
github.event.issue.pull_request != null &&
github.event.issue.pull_request.merged_at != null &&
(github.event.comment.body == '/cherry-pick' ||
startsWith(github.event.comment.body, '/cherry-pick '))
steps:
- name: Check permission
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENTER: ${{ github.event.comment.user.login }}
run: |
PR_DATA=$(gh pr view "${{ github.event.issue.number }}" \
--repo "${{ github.repository }}" \
--json author,reviews,mergedBy)
if ! echo "$PR_DATA" | jq -e --arg user "$COMMENTER" '
.author.login == $user or
(.mergedBy.login // "") == $user or
([ .reviews[].author.login ] | index($user)) != null
' > /dev/null; then
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
-f body="@${COMMENTER} ⛔ You don't have permission to trigger a cherry-pick. Only the PR author, reviewers, or the person who merged the PR can use this command."
exit 1
fi
- name: Parse command
id: parse
env:
COMMENT_BODY: ${{ github.event.comment.body }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENTER: ${{ github.event.comment.user.login }}
run: |
# Extract required branch argument
TARGET_BRANCH=$(echo "$COMMENT_BODY" | sed 's|^/cherry-pick[[:space:]]*||' | xargs)
if [ -z "$TARGET_BRANCH" ]; then
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
-f body="@${COMMENTER} Please specify a target release branch, e.g. \`/cherry-pick v0.9.9-release\`."
exit 1
fi
echo "target_branch=$TARGET_BRANCH" >> "$GITHUB_OUTPUT"
- name: Get PR details
id: pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER="${{ github.event.issue.number }}"
REPO="${{ github.repository }}"
PR_DATA=$(gh pr view "$PR_NUMBER" --repo "$REPO" \
--json mergeCommit,title)
MERGE_COMMIT=$(echo "$PR_DATA" | jq -r '.mergeCommit.oid // empty')
PR_TITLE=$(echo "$PR_DATA" | jq -r '.title')
echo "merge_commit=$MERGE_COMMIT" >> "$GITHUB_OUTPUT"
echo "pr_title=$PR_TITLE" >> "$GITHUB_OUTPUT"
- name: Validate target branch
id: branch
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_TARGET: ${{ steps.parse.outputs.target_branch }}
COMMENTER: ${{ github.event.comment.user.login }}
run: |
TARGET="$INPUT_TARGET"
# Verify the specified branch exists
if ! gh api "/repos/${{ github.repository }}/branches/$TARGET" > /dev/null 2>&1; then
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
-f body="@${COMMENTER} ❌ Cannot cherry-pick: Branch \`$TARGET\` does not exist in this repository."
exit 1
fi
echo "target=$TARGET" >> "$GITHUB_OUTPUT"
- name: Post acknowledgement comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TARGET_BRANCH: ${{ steps.branch.outputs.target }}
COMMENTER: ${{ github.event.comment.user.login }}
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
-f "body=@${COMMENTER} 🍒 Attempting to cherry-pick to \`${TARGET_BRANCH}\`…"
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Cherry-pick commit
id: cherry_pick
env:
MERGE_COMMIT: ${{ steps.pr.outputs.merge_commit }}
TARGET_BRANCH: ${{ steps.branch.outputs.target }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
NEW_BRANCH="cherry-pick-${PR_NUMBER}-to-${TARGET_BRANCH}"
git fetch origin "$TARGET_BRANCH"
git checkout -b "$NEW_BRANCH" "origin/$TARGET_BRANCH"
if git cherry-pick -x "$MERGE_COMMIT"; then
echo "success=true" >> "$GITHUB_OUTPUT"
else
echo "success=false" >> "$GITHUB_OUTPUT"
git cherry-pick --abort || true
fi
echo "new_branch=$NEW_BRANCH" >> "$GITHUB_OUTPUT"
- name: Push branch and create PR
if: steps.cherry_pick.outputs.success == 'true'
id: create_pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NEW_BRANCH: ${{ steps.cherry_pick.outputs.new_branch }}
TARGET_BRANCH: ${{ steps.branch.outputs.target }}
PR_NUMBER: ${{ github.event.issue.number }}
PR_TITLE: ${{ steps.pr.outputs.pr_title }}
COMMENTER: ${{ github.event.comment.user.login }}
COMMENT_URL: ${{ github.event.comment.html_url }}
run: |
git push origin "$NEW_BRANCH"
PR_BODY=$(printf 'Cherry-pick of #%s to `%s`.\n\nTriggered by @%s via [comment](%s).' \
"$PR_NUMBER" "$TARGET_BRANCH" "$COMMENTER" "$COMMENT_URL")
PR_URL=$(gh pr create \
--repo "${{ github.repository }}" \
--base "$TARGET_BRANCH" \
--head "$NEW_BRANCH" \
--title "[$TARGET_BRANCH] $PR_TITLE" \
--body "$PR_BODY")
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
- name: Comment success
if: steps.cherry_pick.outputs.success == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TARGET_BRANCH: ${{ steps.branch.outputs.target }}
NEW_PR_URL: ${{ steps.create_pr.outputs.pr_url }}
COMMENTER: ${{ github.event.comment.user.login }}
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
-f "body=@${COMMENTER} ✅ Cherry-pick to \`${TARGET_BRANCH}\` succeeded! New PR created: ${NEW_PR_URL}"
- name: Comment conflict
if: steps.cherry_pick.outputs.success == 'false'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MERGE_COMMIT: ${{ steps.pr.outputs.merge_commit }}
TARGET_BRANCH: ${{ steps.branch.outputs.target }}
PR_NUMBER: ${{ github.event.issue.number }}
COMMENTER: ${{ github.event.comment.user.login }}
run: |
MANUAL_STEPS="git fetch origin"
MANUAL_STEPS+=$'\n'"git checkout -b cherry-pick-${PR_NUMBER}-to-${TARGET_BRANCH} origin/${TARGET_BRANCH}"
MANUAL_STEPS+=$'\n'"git cherry-pick -x ${MERGE_COMMIT}"
MANUAL_STEPS+=$'\n'"# resolve conflicts, then:"
MANUAL_STEPS+=$'\n'"git cherry-pick --continue"
MANUAL_STEPS+=$'\n'"git push origin cherry-pick-${PR_NUMBER}-to-${TARGET_BRANCH}"
CONFLICT_BODY=$(printf '@%s ⚠️ Cherry-pick to `%s` failed due to conflicts. Please resolve them manually:\n\n```bash\n%s\n```\n\nThen open a PR from `cherry-pick-%s-to-%s` to `%s`.' \
"$COMMENTER" "$TARGET_BRANCH" "$MANUAL_STEPS" "$PR_NUMBER" "$TARGET_BRANCH" "$TARGET_BRANCH")
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
-f "body=$CONFLICT_BODY"
exit 1