Repository navigation
271 lines (234 loc) · 9.25 KB
/
Copy pathbackport.yml
File metadata and controls
271 lines (234 loc) · 9.25 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
name: Create backport pull requests
on:
pull_request_target:
types: [closed]
branches: [main]
issue_comment:
types: [created]
permissions:
contents: read
issues: read
pull-requests: read
concurrency:
group: backport-${{ github.event.issue.number || github.event.pull_request.number }}
cancel-in-progress: false
jobs:
add-backport-label:
name: Add backport label from comment
if: >-
github.event_name == 'issue_comment' &&
github.event.issue.pull_request != null &&
github.event.issue.state == 'open' &&
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association)
permissions:
contents: read
issues: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Add requested release label
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const command =
/(?:^|\n)\s*\/backport\s+(release-(?:(?:[2-9]|[1-9]\d+)\.\d+|1\.(?:9|10)))\s*$/gm;
const labels = [...context.payload.comment.body.matchAll(command)].map(
match => `backport-${match[1]}`,
);
const uniqueLabels = [...new Set(labels)];
if (uniqueLabels.length === 0) {
core.info('No backport command found in the comment.');
return;
}
for (const label of uniqueLabels) {
try {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label,
color: '1D76DB',
description: 'Release line requested for an automated backport',
});
} catch (error) {
if (error.status !== 422) {
throw error;
}
}
}
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: uniqueLabels,
});
core.info(`Added labels: ${uniqueLabels.join(', ')}`);
backport:
name: Backport merged pull request
if: github.event.pull_request.merged == true && github.repository == 'redhat-developer/rhdh-plugins'
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
with:
egress-policy: audit
- name: Checkout repository
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
fetch-depth: 0
persist-credentials: true
- name: Create backport pull requests
env:
GH_TOKEN: ${{ secrets.RHDH_BOT_TOKEN }}
EVENT_PATH: ${{ github.event_path }}
run: |
set -euo pipefail
pr_number=$(jq -r '.pull_request.number' "$EVENT_PATH")
pr_title=$(jq -r '.pull_request.title' "$EVENT_PATH")
pr_url=$(jq -r '.pull_request.html_url' "$EVENT_PATH")
source_commits=()
while IFS= read -r source_commit; do
source_commits+=("$source_commit")
done < <(
gh api --paginate \
"repos/$GITHUB_REPOSITORY/pulls/$pr_number/commits" \
--jq '.[].sha'
)
if [[ ${#source_commits[@]} -eq 0 ]]; then
echo "No commits found for pull request #$pr_number." >&2
exit 1
fi
release_lines=()
while IFS= read -r release_line; do
release_lines+=("$release_line")
done < <(
jq -r '.pull_request.labels[].name' "$EVENT_PATH" \
| grep -E '^backport-release-((([2-9]|[1-9][0-9]+)\.[0-9]+)|(1\.(9|10)))$' \
| sed 's/^backport-//' \
| sort -u
)
if [[ ${#release_lines[@]} -eq 0 ]]; then
echo 'No backport-release labels found; nothing to do.'
exit 0
fi
if [[ ${#release_lines[@]} -ne 1 ]]; then
echo 'Exactly one backport-release label is required per request.' >&2
exit 1
fi
release_line="${release_lines[0]}"
target_branch="$release_line"
if [[ "$release_line" == release-1.9 || "$release_line" == release-1.10 ]]; then
workspace_labels=()
while IFS= read -r workspace; do
workspace_labels+=("$workspace")
done < <(
jq -r '.pull_request.labels[].name' "$EVENT_PATH" \
| sed -n 's/^workspace\/\([A-Za-z0-9._-]*\)$/\1/p' \
| sort -u
)
if [[ ${#workspace_labels[@]} -ne 1 ]]; then
echo "Exactly one workspace label is required for legacy release '$release_line'." >&2
exit 1
fi
target_branch="$release_line/${workspace_labels[0]}"
fi
git config user.name 'rhdh-bot'
git config user.email 'rhdh-bot@redhat.com'
git fetch origin --prune
process_target() {
local backport_branch="backport/${pr_number}-to-${target_branch}"
local backport_label="backport-${target_branch%%/*}"
local body_file=''
cleanup_branch() {
rm -f "$body_file"
if ! git ls-remote --exit-code origin "refs/heads/$backport_branch" >/dev/null; then
return 0
fi
local existing_pr
if ! existing_pr=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--head "$backport_branch" \
--base "$target_branch" \
--state all \
--json number \
--jq '.[0].number // empty'); then
echo "Unable to determine whether backport PR exists for '$backport_branch'; preserving the branch." >&2
return 1
fi
if [[ -n "$existing_pr" ]]; then
echo "Backport PR #$existing_pr exists; preserving branch '$backport_branch'." >&2
return 0
fi
echo "Removing orphaned backport branch '$backport_branch'." >&2
git push origin --delete "$backport_branch"
}
if ! git ls-remote --exit-code origin "refs/heads/$target_branch" >/dev/null; then
echo "Release branch '$target_branch' does not exist; skipping." >&2
return 0
fi
if git ls-remote --exit-code origin "refs/heads/$backport_branch" >/dev/null; then
existing_pr=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--head "$backport_branch" \
--base "$target_branch" \
--state all \
--json number \
--jq '.[0].number // empty')
if [[ -n "$existing_pr" ]]; then
echo "Backport PR #$existing_pr already exists for '$target_branch'; skipping."
return 0
fi
echo "Branch '$backport_branch' already exists without a backport PR; refusing to overwrite." >&2
return 1
fi
if ! git switch --detach "origin/$target_branch" || \
! git switch --create "$backport_branch"; then
return 1
fi
for source_commit in "${source_commits[@]}"; do
if ! git fetch origin "$source_commit"; then
cleanup_branch
return 1
fi
parent_count=$(git rev-list --parents -n 1 "$source_commit" | wc -w | tr -d ' ')
if [[ "$parent_count" -gt 2 ]]; then
if ! git cherry-pick -m 1 "$source_commit"; then
git cherry-pick --abort || true
cleanup_branch
return 1
fi
elif ! git cherry-pick "$source_commit"; then
git cherry-pick --abort || true
cleanup_branch
return 1
fi
done
if ! git push --set-upstream origin "$backport_branch"; then
cleanup_branch || true
return 1
fi
body_file=$(mktemp)
printf 'This pull request backports [#%s](%s) to `%s`.\n\nPlease resolve any conflicts and complete the normal review before merging.\n' \
"$pr_number" \
"$pr_url" \
"$target_branch" > "$body_file"
if ! gh pr create \
--repo "$GITHUB_REPOSITORY" \
--base "$target_branch" \
--head "$backport_branch" \
--title "Backport #${pr_number}: ${pr_title}" \
--body-file "$body_file" \
--label "$backport_label"; then
cleanup_branch || true
return 1
fi
rm -f "$body_file"
git switch --detach "origin/main"
git branch --delete --force "$backport_branch"
}
if ! process_target; then
echo "Backport target '$target_branch' failed." >&2
exit 1
fi