Skip to content

Commit 628bdd9

Browse files
authoredMar 20, 2025··
[CI/CD] Refactor backports flow so that it can determine automatically where to do a backport based on labels (#7976)
This PR changes the command-backport.yml flow so that the branch names are not hardcoded in the pipeline file but will be parsed from the labels. The idea is that there are going to be a label representing each stable branch: - A4-backport-stable2407 - A4-backport-stable2409 - A4-backport-stable2412 - A4-backport-stable2503 If the backport is needed to any of those branches or to all of them, the corresponding label can be set on the PR and the branch name will be parsed from it. The labels need to be created in the repo and adjusted as soon as there is a new release appears or an old one disappears.
1 parent dbebf53 commit 628bdd9

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed
 

‎.github/scripts/common/lib.sh

+20
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ validate_stable_tag() {
508508
}
509509

510510
# Prepare docker stable tag form the polkadot stable tag
511+
#
511512
# input: tag (polkaodot-stableYYMM(-X) or polkadot-stableYYMM(-X)-rcX)
512513
# output: stableYYMM(-X) or stableYYMM(-X)-rcX
513514
prepare_docker_stable_tag() {
@@ -519,3 +520,22 @@ prepare_docker_stable_tag() {
519520
exit 1
520521
fi
521522
}
523+
524+
# Parse names of the branches from the github labels based on the pattern
525+
#
526+
# input: labels (array of lables like ("A3-backport" "RO-silent" "A4-backport-stable2407" "A4-backport-stable2503"))
527+
# output: BRANCHES (array of the branch names)
528+
parse_branch_names_from_backport_labels() {
529+
labels="$1"
530+
BRANCHES=""
531+
532+
for label in $labels; do
533+
if [[ "$label" =~ ^A4-backport-stable[0-9]{4}$ ]]; then
534+
branch_name=$(sed 's/A4-backport-//' <<< "$label")
535+
BRANCHES+=" ${branch_name}"
536+
fi
537+
done
538+
539+
BRANCHES=$(echo "$BRANCHES" | sed 's/^ *//')
540+
echo "$BRANCHES"
541+
}

‎.github/workflows/command-backport.yml

+44-9
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,68 @@ permissions:
1313
actions: write # It may have to backport changes to the CI as well.
1414

1515
jobs:
16-
backport:
17-
name: Backport pull request
16+
check-labels:
1817
runs-on: ubuntu-latest
18+
env:
19+
GH_TOKEN: ${{ github.token }}
20+
outputs:
21+
LABELS: ${{ steps.check_labels.outputs.LABELS}}
22+
found: ${{ steps.check_labels.outputs.found}}
1923

2024
# The 'github.event.pull_request.merged' ensures that it got into master:
2125
if: >
2226
( !startsWith(github.event.pull_request.base.ref, 'stable') ) &&
2327
(
2428
github.event_name == 'pull_request_target' &&
2529
github.event.pull_request.merged &&
26-
github.event.pull_request.base.ref == 'master' &&
27-
contains(github.event.pull_request.labels.*.name, 'A4-needs-backport')
30+
github.event.pull_request.base.ref == 'master'
2831
)
2932
steps:
3033
- uses: actions/checkout@v4
3134

35+
- name: Check for backport labels
36+
id: check_labels
37+
run: |
38+
LABELS=$(gh pr view ${{ github.event.pull_request.number }} --json labels --jq '.labels[].name')
39+
40+
if echo "$LABELS" | grep -q '^A4-backport-stable'; then
41+
echo "found=true" >> $GITHUB_OUTPUT
42+
readarray -t labels_array <<< "$LABELS"
43+
echo "LABELS=${labels_array[@]}" >> $GITHUB_OUTPUT
44+
else
45+
echo "found=false" >> $GITHUB_OUTPUT
46+
fi
47+
48+
49+
backport:
50+
name: Backport pull request
51+
runs-on: ubuntu-latest
52+
needs: [ check-labels ]
53+
if: ${{ needs.check-labels.outputs.found == 'true' }}
54+
steps:
55+
- uses: actions/checkout@v4
56+
57+
- name: Get branches to backport to
58+
id: branches
59+
run: |
60+
. ./.github/scripts/common/lib.sh
61+
62+
LABELS="${{ needs.check-labels.outputs.LABELS }}"
63+
BACKPORT_BRANCHES=$(parse_branch_names_from_backport_labels "$LABELS")
64+
echo "BACKPORT_BRANCHES=${BACKPORT_BRANCHES}" >> $GITHUB_OUTPUT
65+
3266
- name: Generate token
3367
id: generate_token
3468
uses: actions/create-github-app-token@v1
3569
with:
36-
app_id: ${{ secrets.RELEASE_BACKPORT_AUTOMATION_APP_ID }}
37-
private_key: ${{ secrets.RELEASE_BACKPORT_AUTOMATION_APP_PRIVATE_KEY }}
70+
app-id: ${{ secrets.RELEASE_BACKPORT_AUTOMATION_APP_ID }}
71+
private-key: ${{ secrets.RELEASE_BACKPORT_AUTOMATION_APP_PRIVATE_KEY }}
3872

3973
- name: Create backport pull requests
4074
uses: korthout/backport-action@v3
4175
id: backport
4276
with:
43-
target_branches: stable2407 stable2409 stable2412 stable2503
77+
target_branches: ${{ steps.branches.outputs.BACKPORT_BRANCHES }}
4478
merge_commits: skip
4579
github_token: ${{ steps.generate_token.outputs.token }}
4680
pull_description: |
@@ -59,6 +93,7 @@ jobs:
5993
"conflict_resolution": "draft_commit_conflicts"
6094
}
6195
copy_assignees: true
96+
label_pattern: ^A4-backport-stable
6297

6398
- name: Label Backports
6499
if: ${{ steps.backport.outputs.created_pull_numbers != '' }}
@@ -86,11 +121,11 @@ jobs:
86121
const reviewer = '${{ github.event.pull_request.user.login }}';
87122
88123
for (const pullNumber of pullNumbers) {
89-
await github.pulls.requestReviewers({
124+
await github.rest.pulls.requestReviewers({
90125
owner: context.repo.owner,
91126
repo: context.repo.repo,
92127
pull_number: parseInt(pullNumber),
93-
reviewers: [ reviewer ]
128+
reviewers: [reviewer]
94129
});
95130
console.log(`Requested review from ${reviewer} for PR #${pullNumber}`);
96131
}

‎docs/BACKPORT.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ Backports should only be used to fix bugs or security issues - never to introduc
66
## Steps
77

88
1. Fix a bug through a PR that targets `master`.
9-
2. Add label `A4-needs-backport` to the PR.
9+
2. Add label related to the branch to wich to backport changes to the PR.
10+
- `A4-backport-stable2407`
11+
- `A4-backport-stable2409`
12+
- `A4-backport-stable2412`
13+
- `A4-backport-stable2503`
1014
3. Merge the PR into `master`.
1115
4. Wait for the bot to open the backport PR.
1216
5. Ensure the change is audited or does not need audit.

0 commit comments

Comments
 (0)
Please sign in to comment.