forked from eclipse-ecal/ecal
-
Notifications
You must be signed in to change notification settings - Fork 0
189 lines (158 loc) · 8.19 KB
/
cherry-pick-to.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
name: Cherry-pick-to-branch
on:
push:
branches: [ "master" ]
env:
cherry_pick_label_prefix: "cherry-pick-to-"
cherry_pick_pr_success_label: "Auto cherry-pick success ✅"
cherry_pick_pr_failure_label: "Auto cherry-pick failure ⚠️"
use_draft_pr: "true"
jobs:
cherry-pick-from-push:
strategy:
matrix:
target-branch: [ "support/v5.12", "support/v5.13" ]
runs-on: ubuntu-latest
steps:
- name: Setup Variables
id: setup
run: |
echo continue_workflow=true >> $GITHUB_ENV
short_sha=$(echo ${{ github.event.after }} | cut -c1-7)
echo cherry_pick_branch=cherry-pick/$short_sha/${{matrix.target-branch}} >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.CHERRY_PICK_GITHUB_TOKEN }} # This token was created on 2024-03-26 and is valid for 1 year: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/issues/4466#note_1987666
- name: Determine PR number (if any)
id: pr
run: |
pr_number=$(gh pr list --search ${{ github.event.after }} --state merged --json number --jq '.[0].number')
echo "PR number: $pr_number"
echo pr_number=$pr_number >> $GITHUB_OUTPUT
# Set the continue workflow to false, if the pr_number is empty
if [ -z "$pr_number" ]; then
echo continue_workflow=false >> $GITHUB_ENV
fi
env:
GITHUB_TOKEN: ${{ secrets.CHERRY_PICK_GITHUB_TOKEN }} # This token was created on 2024-03-26 and is valid for 1 year: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/issues/4466#note_1987666
- name: Check for a cherry pick label and save whether that is the case
if: ${{ env.continue_workflow == 'true' }}
run: |
# List all labels for the PR number from the last step
labels=$(gh pr view ${{ steps.pr.outputs.pr_number }} --json labels --jq '.labels[].name')
echo "Labels: $labels"
# Check if the list contains the special cherry pick label
if [[ $labels == *"${{ env.cherry_pick_label_prefix }}${{matrix.target-branch}}"* ]]; then
echo "Cherry pick label found"
echo continue_workflow=true >> $GITHUB_ENV
else
echo "Cherry pick label not found"
echo continue_workflow=false >> $GITHUB_ENV
fi
env:
GITHUB_TOKEN: ${{ secrets.CHERRY_PICK_GITHUB_TOKEN }}
- name: Create new branch for cherry picking
if: ${{ env.continue_workflow == 'true' }}
run: |
# Set the git config to the pr_author and pr_author_email
git config user.name '${{ github.event.pusher.name }}'
git config user.email '${{ github.event.pusher.email }}'
git checkout ${{ matrix.target-branch }}
git checkout -b ${{ env.cherry_pick_branch }}
- name: Cherry pick the commit between the before (exclusive) and after (inclusive) commits
id: cherry-pick
if: ${{ env.continue_workflow == 'true' }}
run: |
git cherry-pick ${{ github.event.before }}..${{ github.event.after }}
continue-on-error: true
- name: Create PR description
if: ${{ env.continue_workflow == 'true' }}
id: pr-description
run: |
# Determine original PR title
original_pr_title=$(gh pr view ${{ steps.pr.outputs.pr_number }} --json title --jq '.title')
# Create a title for the new PR
echo pr_title="[CP #${{ steps.pr.outputs.pr_number }} > ${{ matrix.target-branch }}] $original_pr_title" >> $GITHUB_OUTPUT
if [ "${{ steps.cherry-pick.outcome }}" == "success" ]; then
echo pr_label="${{ env.cherry_pick_pr_success_label }}" >> $GITHUB_OUTPUT
echo pr_draft_command="" >> $GITHUB_OUTPUT
# Create multiline pr_description
echo "pr_description<<EOF" >> $GITHUB_OUTPUT
echo "# Cherry-pick" >> $GITHUB_OUTPUT
echo "Cherry-picked PR #${{ steps.pr.outputs.pr_number }} to branch \`${{ matrix.target-branch }}\`." >> $GITHUB_OUTPUT
echo "The cherry-pick was **successful**." >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "Please review the changes and **rebase-merge** if desired." >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo pr_label="${{ env.cherry_pick_pr_failure_label }}" >> $GITHUB_OUTPUT
# Only set the pr_draft_command if the use_draft_pr is true. Otherwise, set it to an empty string.
# Some repos (like personal ones) might not have the draft PR feature enabled.
if [ "${{ env.use_draft_pr }}" == "true" ]; then
echo pr_draft_command="--draft" >> $GITHUB_OUTPUT
else
echo pr_draft_command="" >> $GITHUB_OUTPUT
fi
# Create multiline pr_description
echo "pr_description<<EOF" >> $GITHUB_OUTPUT
echo "# Cherry-pick failed" >> $GITHUB_OUTPUT
echo "Cherry-picked PR #${{ steps.pr.outputs.pr_number }} to branch \`${{ matrix.target-branch }}\`." >> $GITHUB_OUTPUT
echo "The cherry-pick has **failed**." >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "The following files have caused conflicts:" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "\`\`\`bash" >> $GITHUB_OUTPUT
git diff --name-only --diff-filter=U >> $GITHUB_OUTPUT
echo "\`\`\`" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "## Resolving" >> $GITHUB_OUTPUT
echo "Please resolve conflicts manually. You can use this PR and branch to your convenience." >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "\`\`\`bash" >> $GITHUB_OUTPUT
echo "git fetch origin" >> $GITHUB_OUTPUT
echo "git checkout -b local/${{ env.cherry_pick_branch }} origin/${{ matrix.target-branch }}" >> $GITHUB_OUTPUT
echo "git branch -u origin/${{ env.cherry_pick_branch }}" >> $GITHUB_OUTPUT
echo "git cherry-pick ${{ github.event.before }}..${{ github.event.after }}" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "# Resolve conflicts and use" >> $GITHUB_OUTPUT
echo "# git cherry-pick --continue" >> $GITHUB_OUTPUT
echo "# until all conflicts are resolved." >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "git push -f origin HEAD:${{ env.cherry_pick_branch }}" >> $GITHUB_OUTPUT
echo "\`\`\`" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "After resolving all conflicts, **rebase-merge** this PR." >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
env:
GITHUB_TOKEN: ${{ secrets.CHERRY_PICK_GITHUB_TOKEN }}
# If the cherry-pick fails, abort the cherry-pick and create an empty commit on it.
- name: Abort cherry-pick if it failed and add empty commit for PR creation
if: ${{ env.continue_workflow == 'true' && steps.cherry-pick.outcome == 'failure' }}
run: |
git cherry-pick --abort
# Use the bot user for the commit
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
# Create an empty commit
git commit --allow-empty -m "Cherry-pick failed"
git log --oneline -n 5
- name: Push the cherry-pick branch
if: ${{ env.continue_workflow == 'true' }}
run: git push origin ${{ env.cherry_pick_branch }}
- name: create pull request
if: ${{ env.continue_workflow == 'true' }}
run: |
# Check if the label exists in the repository. If not, create it.
gh label list | grep -q "${{ steps.pr-description.outputs.pr_label }}" || gh label create "${{ steps.pr-description.outputs.pr_label }}"
# Create the pull request
gh pr create -B '${{ matrix.target-branch }}' \
-H '${{ env.cherry_pick_branch }}' \
--title '${{ steps.pr-description.outputs.pr_title }}' \
--body '${{ steps.pr-description.outputs.pr_description }}' \
--label '${{ steps.pr-description.outputs.pr_label }}' \
${{ steps.pr-description.outputs.pr_draft_command }}
env:
GITHUB_TOKEN: ${{ secrets.CHERRY_PICK_GITHUB_TOKEN }}