-
Notifications
You must be signed in to change notification settings - Fork 31
146 lines (132 loc) · 5.59 KB
/
label-cherry-pick.yaml
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
name: Label Cherry Pick
on:
pull_request_target:
types:
- labeled
- closed
jobs:
prepare_branch_list:
runs-on: ubuntu-latest
if: |
github.event.pull_request.merged == true &&
github.event.pull_request.merge_commit_sha != null
outputs:
target_branch: ${{ steps.set-branches.outputs.target_branch }}
steps:
- name: Set Branches
id: set-branches
env:
PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
EVENT_ACTION: ${{ github.event.action }}
LABEL_NAME: ${{ github.event.label.name }}
BASE_REF: ${{ github.base_ref }}
run: |
if [[ $EVENT_ACTION == 'closed' ]]; then
# Get a list of the labels from the PR.
labels=$(echo "$PR_LABELS" | jq -r '.[].name')
else
# Or get the label that was added on the merged PR.
labels=$LABEL_NAME
fi
branches=("24.lts.1+" "23.lts.1+" "22.lts.1+" "21.lts.1+" "20.lts.1+" "19.lts.1+" "rc_11" "COBALT_9")
filtered_branches=()
for branch in "${branches[@]}"; do
if [[ $branch == $BASE_REF ]]; then
continue
fi
for label in $labels; do
if [[ $label == "cp-$branch" ]]; then
echo $branch
filtered_branches+=("$branch")
fi
done
done
echo -n "$filtered_branches" | jq -cRs 'split("\n")'
echo "target_branch=$(echo -n "$filtered_branches" | jq -cRs 'split("\n")')" >> $GITHUB_OUTPUT
cherry_pick:
runs-on: ubuntu-latest
needs: prepare_branch_list
if: needs.prepare_branch_list.outputs.target_branch != '[]'
strategy:
matrix:
target_branch: ${{ fromJson(needs.prepare_branch_list.outputs.target_branch) }}
env:
ACCESS_TOKEN: ${{ secrets.CHERRY_PICK_TOKEN }}
REPOSITORY: ${{ github.repository }}
GITHUB_REF: ${{ github.ref }}
MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }}
steps:
- name: Checkout repository
uses: kaidokert/[email protected]
timeout-minutes: 30
with:
ref: ${{ matrix.target_branch }}
fetch-depth: 0
persist-credentials: false
- name: Setup Git
run: |
git config --global user.name "GitHub Release Automation"
git config --global user.email "[email protected]"
- name: Cherry pick merge commit
id: cherry-pick
continue-on-error: true
run: |
git fetch origin ${{ matrix.target_branch }}
set +e
rev_list=`git rev-list --parents -n 1 ${MERGE_COMMIT_SHA}`
hash_count=`wc -w <<< ${rev_list}`
# git rev-list returns a list of hashes: <HEAD_HASH> [<PARENT_HASH>...]
# It's not a merge commit if there are less than three (i.e. less than two parents).
if [ ${hash_count} -lt 3 ]; then
git cherry-pick -x ${MERGE_COMMIT_SHA}
RES=$?
if [ ${RES} -ne 0 ]; then
# If the cherry pick failed due to a merge conflict we can
# add the conflicting file and create the commit anyway.
git add .
git cherry-pick --continue
exit $?
fi
else
# This commit is a merge commit and has multiple parents.
# Select the first parent (-m 1) as the mainline tree.
git cherry-pick -x -m 1 ${MERGE_COMMIT_SHA}
RES=$?
fi
- name: Create Pull Request
id: create-pr
continue-on-error: true
uses: peter-evans/create-pull-request@2b011faafdcbc9ceb11414d64d0573f37c774b04 # v4.2.3
with:
token: ${{ secrets.CHERRY_PICK_TOKEN }}
draft: ${{ steps.cherry-pick.outcome == 'failure' }}
base: ${{ matrix.target_branch }}
branch: "cherry-pick-${{ matrix.target_branch }}-${{ github.event.pull_request.number }}"
committer: GitHub Release Automation <[email protected]>
reviewers: ${{ github.event.pull_request.user.login }}
title: "Cherry pick PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}"
body: |
Refer to the original PR: https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}
${{ github.event.pull_request.body }}
- name: Comment on failure
uses: actions/github-script@v2
with:
github-token: ${{ secrets.CHERRY_PICK_TOKEN }}
script: |
if (!'${{ steps.create-pr.outputs.pull-request-number }}') {
// Comment on the originating PR if creating a cherry pick PR failed.
github.issues.createComment({
issue_number: github.event.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '> [!IMPORTANT]\n> Creating the cherry pick PR failed! Check the log at ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}.'
});
} else if ('${{ steps.cherry-pick.outcome }}' == 'failure') {
// Comment on the new PR if the cherry pick failed.
github.issues.createComment({
issue_number: ${{ steps.create-pr.outputs.pull-request-number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: '> [!IMPORTANT]\n> The cherry-pick failed! Check out the PR branch (${{ matrix.target_branch }}-${{ github.event.pull_request.number }}) and fix the conflicts before proceeding.'
});
}