Skip to content

Commit 019e0ac

Browse files
authoredOct 10, 2022
[actions] Add auto cherry-pick actions to release branch (sonic-net#11496)
* [actions] Add github actions to auto cherry-pick prs to release branches * Add README, fix workflow
1 parent 62692f4 commit 019e0ac

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed
 

‎.github/workflows/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Github actions README
2+
This is an introduction about auto-cherry-pick workflow.
3+
take 202205 branch for example:
4+
1. pr_cherrypick_prestep:
5+
```mermaid
6+
graph
7+
Start(Origin PR) --> A{merged?}
8+
A -- NO --> STOP
9+
A -- YES --> A1{Approved<br> for 202205<br> Branch?}
10+
A1 -- NO --> STOP
11+
A1 -- YES --> A2(pr_cherrypick_prestep)
12+
B(pr_cherrypick_prestep)
13+
B --> B1{cherry pick<br>conflict?}
14+
B1 -- YES --> B2(Add tag:<br>Cherry Pick Confclit_202205) --> B3(Add comment:<br>refer author code conflict) --> STOP1(STOP)
15+
B1 -- NO --> B4(Create New PR) -- success --> B5(New PR add tag:<br> automerge) --> B6(New PR add comment:<br>Origin PR link) --> B7(Origin PR add tag:<br>Created PR to 202205 Branch) --> B8(Origin PR add comment:<br>New PR link)
16+
B4 -- fail --> STOP1
17+
```
18+
19+
2. automerge:
20+
```mermaid
21+
graph
22+
Start(PR azp finished successfully) --> A{author:<br>mssonicbld?}
23+
A -- NO --> STOP
24+
A -- YES --> B{tag:<br>automerge?} -- YES --> C(Merge PR)
25+
B -- NO --> STOP
26+
```
27+
28+
3. pr_cherrypick_poststep:
29+
```mermaid
30+
graph
31+
A(PR is Merged) --> B{tag:<br>automerge?}
32+
B -- YES --> B1{author:<br>mssonicbld?}
33+
B1 -- YES --> B2{"title starts:<br>[action] [PR:123]"}
34+
B2 -- YES --> C(Origin PR remove tag:<br> Created PR to 202205 Branch) --> D(Origin PR add tag:<br> Included in 202205 Branch)
35+
B -- NO --> STOP
36+
B1 -- NO --> STOP
37+
B2 -- NO --> STOP
38+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: PostCherryPick
2+
on:
3+
pull_request_target:
4+
types:
5+
- closed
6+
branches:
7+
- '20*'
8+
9+
jobs:
10+
post_cherry_pick:
11+
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'automerge') && github.event.pull_request.head.user.login == 'mssonicbld' && startsWith(github.event.pull_request.title, '[action]')
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Debug
15+
env:
16+
GITHUB_CONTEXT: ${{ toJson(github) }}
17+
run: echo $GITHUB_CONTEXT | jq
18+
- name: Checkout
19+
uses: actions/checkout@v3
20+
with:
21+
persist-credentials: false
22+
- name: Main
23+
env:
24+
GITHUB_CONTEXT: ${{ toJson(github) }}
25+
TOKEN: ${{ secrets.TOKEN }}
26+
run: |
27+
set -e
28+
pr_url=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request._links.html.href")
29+
pr_id=$(echo $GITHUB_CONTEXT | jq -r ".event.number")
30+
base_ref=$(echo $GITHUB_CONTEXT | jq -r ".base_ref")
31+
echo ${TOKEN} | gh auth login --with-token
32+
title=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.title")
33+
origin_pr_id=$(echo $title | grep -Eo "\[action\] \[PR:[0-9]*\]" | grep -Eo "[0-9]*")
34+
origin_pr_url=$(echo $pr_url | sed "s/$pr_id/$origin_pr_id/")
35+
echo =============================
36+
echo pr_url: $pr_url
37+
echo pr_id: $pr_id
38+
echo base_ref: $base_ref
39+
echo title: $title
40+
echo origin_pr_id: $origin_pr_id
41+
echo origin_pr_url: $origin_pr_url
42+
echo =============================
43+
# Add label
44+
if [[ "$origin_pr_id" == "" ]];then
45+
echo "original PR didn't found."
46+
exit 1
47+
fi
48+
gh pr edit $origin_pr_url --add-label "Included in ${base_ref} Branch"
49+
gh pr edit $origin_pr_url --remove-label "Created PR to ${base_ref} Branch,Request for ${base_ref} Branch,Approved for ${base_ref} Branch"
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: PreCherryPick
2+
on:
3+
pull_request_target:
4+
types:
5+
- labeled
6+
- closed
7+
branches:
8+
- master-test
9+
10+
jobs:
11+
pre_cherry_pick:
12+
if: github.event.pull_request.merged == true && ( (github.event.action == 'closed' && contains(join(github.event.pull_request.labels.*.name, ','), 'Approved for 20')) || (github.event.action == 'labeled' && startsWith(github.event.label.name, 'Approved for 20')) )
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 0
19+
persist-credentials: false
20+
- name: Debug
21+
env:
22+
GITHUB_CONTEXT: ${{ toJson(github) }}
23+
run: echo $GITHUB_CONTEXT | jq
24+
- name: Main
25+
env:
26+
GITHUB_CONTEXT: ${{ toJson(github) }}
27+
TOKEN: ${{ secrets.TOKEN }}
28+
run: |
29+
set -e
30+
31+
sha=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.merge_commit_sha")
32+
pr_id=$(echo $GITHUB_CONTEXT | jq -r ".event.number")
33+
pr_url=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request._links.html.href")
34+
repository=$(echo $GITHUB_CONTEXT | jq -r ".repository")
35+
labels=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.labels[].name")
36+
author=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.base.user.login")
37+
branches=$(git branch -a --list 'origin/20????' | awk -F/ '{print$3}' | grep -E "202[0-9]{3}")
38+
if [[ $(echo $GITHUB_CONTEXT | jq -r ".event.action") == "labeled" ]];then
39+
labels=$(echo $GITHUB_CONTEXT | jq -r ".event.label.name")
40+
fi
41+
title=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.title")
42+
echo =============================
43+
echo SHA: $sha
44+
echo PRID: $pr_id
45+
echo pr_url: $pr_url
46+
echo repository: $repository
47+
echo branches: $branches
48+
echo labels:
49+
echo "$labels"
50+
echo ${TOKEN} | gh auth login --with-token
51+
echo author: $author
52+
echo title: $title
53+
echo =============================
54+
55+
git config user.name mssonicbld
56+
git config user.email sonicbld@microsoft.com
57+
git config credential.https://github.com.username mssonicbld
58+
git remote add mssonicbld https://mssonicbld:${TOKEN}@github.com/mssonicbld/sonic-buildimage
59+
git fetch mssonicbld
60+
git remote -vv
61+
62+
cherry_pick(){
63+
set -e
64+
local create_pr=''
65+
while read label
66+
do
67+
echo label: $label
68+
if [[ "$label" == "Approved for $branch Branch" ]];then
69+
create_pr=1
70+
fi
71+
if [[ "$label" == "Created PR to $branch Branch" ]];then
72+
echo "already has tag: Created PR to $branch Branch, return"
73+
return 0
74+
fi
75+
if [[ "$label" == "Included in $branch Branch" ]];then
76+
echo "already has tag: Included in $branch Branch, return"
77+
return 0
78+
fi
79+
if [[ "$label" == "Cherry Pick Conflict_$branch" ]];then
80+
echo "already has tag: Cherry Pick Conflict_$branch, return"
81+
return 0
82+
fi
83+
done <<< "$labels"
84+
85+
if [[ "$create_pr" != "1" ]];then
86+
echo "Didn't find 'Approved for $branch Branch' tag."
87+
return 0
88+
fi
89+
# Begin to cherry-pick PR
90+
git cherry-pick --abort 2>/dev/null || true
91+
git clean -xdff 2>/dev/null || true
92+
git reset HEAD --hard || true
93+
git checkout -b $branch --track origin/$branch
94+
git status | grep "working tree clean"
95+
96+
if ! git cherry-pick $sha;then
97+
echo 'cherry-pick failed.'
98+
git cherry-pick --abort
99+
git status | grep "working tree clean"
100+
# Add label
101+
gh pr edit $pr_url --add-label "Cherry Pick Conflict_$branch"
102+
echo 'Add label "Cherry Pick Conflict_$branch" success'
103+
gh pr comment $pr_url --body "@${author} PR conflicts with $branch branch"
104+
echo 'Add commnet "@${author} PR conflicts with $branch branch"'
105+
else
106+
# Create PR to release branch
107+
git push mssonicbld HEAD:$branch-${pr_id} -f
108+
result=$(gh pr create -R ${repository} -H mssonicbld:$branch-${pr_id} -B $branch -t "[action] [PR:$pr_id] $title" -b '' 2>&1)
109+
echo $result | grep "already exists" && { echo $result; return 0; }
110+
echo $result | grep github.com || { echo $result; return 1; }
111+
new_pr_rul=$(echo $result | grep github.com)
112+
echo new_pr_rul: $new_pr_rul
113+
114+
# Add label to old PR
115+
gh pr edit $pr_url --add-label "Created PR to $branch Branch"
116+
echo Add label Created PR to $branch Branch
117+
# Add comment to old PR
118+
gh pr comment $pr_url --body "Cherry-pick PR to $branch: ${new_pr_rul}"
119+
echo Add comment to old PR
120+
121+
# Add label to new PR
122+
gh pr edit $new_pr_rul --add-label "automerge"
123+
echo Add label automerge to new PR
124+
# Add comment to new PR
125+
gh pr comment $new_pr_rul --body "Original PR: ${pr_url}"
126+
echo Add comment to new PR
127+
fi
128+
}
129+
130+
for branch in $branches
131+
do
132+
echo -------------------------------------------
133+
echo Begin to parse Branch: $branch
134+
cherry_pick
135+
done
136+

0 commit comments

Comments
 (0)
Please sign in to comment.