-
Notifications
You must be signed in to change notification settings - Fork 25
165 lines (147 loc) · 6.36 KB
/
cherry-pick.yml
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
name: Cherry pick
on:
pull_request:
types:
- closed
branches:
- 11.x.x
env:
TARGET_BRANCH: main
FROM_BRANCH: ${{ github.event.pull_request.base.ref }}
jobs:
cherry-pick:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ env.TARGET_BRANCH }}
fetch-depth: 0
token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
- uses: actions/setup-node@v4
id: setup-node
with:
node-version-file: '.nvmrc'
- name: Cache node modules
id: cache
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ steps.setup-node.outputs.node-version }}-modules-${{ hashFiles('package-lock.json') }}
- name: npm install
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci
- name: Cherry pick
id: cherry-pick
run: |
# Set the git user to the author of the merge commit.
git config user.name "$(git log -1 --pretty=format:'%an' ${{ github.event.pull_request.merge_commit_sha }})"
git config user.email "$(git log -1 --pretty=format:'%ae' ${{ github.event.pull_request.merge_commit_sha }})"
# Echo commands to the log.
set -x
# Do not exit on error.
set +e
# Cherry-pick the merge commit into the target branch, which is checked out.
npx skyux-dev cherry-pick \
--baseBranch=${{ env.TARGET_BRANCH }} \
--hash=${{ github.event.pull_request.merge_commit_sha }} \
--skip-confirmation
if [ $? -ne 0 ]; then
echo "CHERRY_PICK_RESULT=failed" >> $GITHUB_ENV
exit 0
fi
# Get the name of the cherry-pick branch.
CHERRY_PICK_BRANCH=$(git branch --show-current)
# Add the cherry-pick branch to the environment.
echo "CHERRY_PICK_BRANCH=${CHERRY_PICK_BRANCH}" >> $GITHUB_ENV
# Push the cherry-pick to a new cherry-pick branch.
git push -u origin ${CHERRY_PICK_BRANCH}
if [ $? -ne 0 ]; then
echo "CHERRY_PICK_RESULT=failed" >> $GITHUB_ENV
exit 0
fi
echo "commit_message=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
- uses: actions/github-script@v7
if: ${{ env.CHERRY_PICK_RESULT != 'failed' }}
with:
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
script: |
const pr = context.payload.pull_request;
const title = ${{ toJson(steps.cherry-pick.outputs.commit_message) }};
let body = `:cherries: Cherry picked from #${pr.number} [${pr.title}](${pr.html_url})`
const prAzureBoardLink = pr.body?.match(/(?<=\[)AB#\d+(?=])/g);
if (prAzureBoardLink) {
body += `\n\n${prAzureBoardLink.join(' \n')} `;
}
github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
head: process.env.CHERRY_PICK_BRANCH,
base: process.env.TARGET_BRANCH,
title,
body
}).then(result => {
console.log(`Created PR #${result.data.number}: ${result.data.html_url}`);
core.exportVariable('PR_URL', result.data.html_url);
core.exportVariable('CHERRY_PICK_RESULT', 'success');
const riskLabels = pr.labels.map(label => label.name).filter(label => label.startsWith('risk level'));
if (riskLabels.length === 0) {
if (pr.labels.find(label => label.name.startsWith('autorelease'))) {
// Changelog PR, so add minimal risk level labels.
riskLabels.push('risk level (author): 1', 'risk level (reviewer): 1');
} else {
// No risk level label found, so don't add any.
return;
}
}
return github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: result.data.number,
labels: riskLabels
});
}).catch(error => {
console.log(error);
core.warning(`Failed to create PR: ${error.message}`);
core.exportVariable('CHERRY_PICK_RESULT', 'failed');
});
- name: Comment on the original PR when cherry-pick is successful
if: ${{ env.CHERRY_PICK_RESULT == 'success' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Cherry-pick successful! :cherries: :tada: See ${{ env.PR_URL }}'
})
- name: Comment on the original PR when cherry-pick fails
if: ${{ env.CHERRY_PICK_RESULT != 'success' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Cherry-pick [failed](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})! :x: Please resolve conflicts and create a new PR.'
})
- name: Notify Slack when cherry-pick fails
if: ${{ env.CHERRY_PICK_RESULT != 'success' }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_TITLE: ':cherries: :x: Cherry-pick failed for “${{ github.event.pull_request.title }} (#${{ github.event.pull_request.number}})”'
SLACK_MESSAGE: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
SLACK_ICON_EMOJI: ':github:'
SLACK_USERNAME: GitHub
#cor-skyux-notifications
SLACK_CHANNEL: C01GY7ZP4HM
SLACK_COLOR: 'fail'
SLACK_FOOTER: 'Blackbaud Sky Build User'
MSG_MINIMAL: 'true'