-
Notifications
You must be signed in to change notification settings - Fork 544
129 lines (115 loc) · 5.78 KB
/
sync-with-master.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
# This workflow will sync commits from a PR from master to a target branch.
name: Sync PR from master
# on:
# pull_request_target:
# types:
# - closed
# branches:
# - master
env:
REPOSITORY: carbon-identity-framework
GIT_USERNAME: jenkins-is-staging
PRODUCT_REPOSITORY_FORKED: $GIT_USERNAME'/'${REPOSITORY}
PRODUCT_REPOSITORY_PUBLIC: wso2/$REPOSITORY
BUILD_NUMBER: ${{github.run_id}}
SYNCHRONIZE_BRANCH_NAME: master_sync_github_action/$BUILD_NUMBER
TARGET_BRANCH_NAME: 6.0.x
jobs:
create_pr:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cherry pick commits from last PR
run: |
echo "Clean up any existing files"
echo "=========================================================="
rm -rf ${{ env.REPOSITORY }}
echo ""
echo "Cloning: https://github.com/'${{ env.PRODUCT_REPOSITORY_FORKED }}"
echo "=========================================================="
git clone 'https://github.com/'${{ env.PRODUCT_REPOSITORY_FORKED }}'.git'
cd ${{ env.REPOSITORY }}
echo ""
echo 'Add remote: upstream as https://github.com/'${{ env.PRODUCT_REPOSITORY_PUBLIC }}
echo "=========================================================="
git remote add upstream 'https://github.com/'${{ env.PRODUCT_REPOSITORY_PUBLIC }}
echo ""
echo 'Fetching: upstream'
echo "=========================================================="
git fetch upstream
echo ""
echo 'Checking out: upstream:master branch'
echo "=========================================================="
git checkout -b upstream/master remotes/upstream/master
echo ""
echo "Get latest merged PR details"
echo "=========================================================="
LATEST_PR_SHA=$(git log --merges --oneline | grep "Merge pull request" | head -n 1 | cut -d ' ' -f1)
MERGED_PR=$(curl -s "https://api.github.com/search/issues?q=${LATEST_PR_SHA}+type:pr+is:merged+base:master")
PR_NUMBER=$(echo $MERGED_PR | jq -r '.items[0].number')
PR_URL=$(echo $MERGED_PR | jq -r '.items[0].html_url')
PR_TITLE=$(echo $MERGED_PR | jq -r '.items[0].title')
PR_USER=$(echo $MERGED_PR | jq -r '.items[0].user.login')
if [[ "$PR_NUMBER" = null ]]; then
echo "No PR found for commit: $LATEST_PR_SHA"
exit 1
else
echo "PR #$PR_NUMBER found for commit: $LATEST_PR_SHA"
fi
echo ""
echo "Get commits of the PR"
echo "=========================================================="
PR_COMMITS=$(curl -H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${{ env.PRODUCT_REPOSITORY_PUBLIC }}/pulls/$PR_NUMBER/commits")
PR_COMMITS_SHA=$(echo $PR_COMMITS | jq -r '.[].sha')
echo ""
echo 'Checking out: upstream:'${{ env.TARGET_BRANCH_NAME }}' branch'
echo "=========================================================="
git checkout -b ${{ env.SYNCHRONIZE_BRANCH_NAME }} 'remotes/upstream/'${{ env.TARGET_BRANCH_NAME }}
echo ""
echo "Cherry-pick Commits"
echo "=========================================================="
git config --global user.name ${GIT_USERNAME}
for commit in $PR_COMMITS_SHA
do
if [[ $(git show --no-patch --format=%p $commit | wc -w) -gt 1 ]]; then
echo "Skipping merge commit: $commit"
continue
fi
git cherry-pick $commit
done
echo ""
echo 'Push Changes'
echo "=========================================================="
git remote rm origin
git remote add origin 'https://'${{ secrets.PAT }}'@github.com/'${{ env.PRODUCT_REPOSITORY_FORKED }}
git push -u origin ${{ env.SYNCHRONIZE_BRANCH_NAME }}
echo ""
echo 'Send Pull Request'
echo "=========================================================="
TITLE="Merge '${PR_TITLE}' to ${{ env.TARGET_BRANCH_NAME }}"
BODY="[Auto generated]\nThis PR merges ${PR_URL} from the master branch to ${{ env.TARGET_BRANCH_NAME }} branch\nLink : https://github.com/${{ env.PRODUCT_REPOSITORY_PUBLIC }}/actions/runs/${{github.run_id}}"
RESPONSE=$(curl -s -w "%{http_code}" -k -X \
POST https://api.github.com/repos/${{ env.PRODUCT_REPOSITORY_PUBLIC }}/pulls \
-H "Authorization: Bearer "${{ secrets.PAT }}"" \
-H "Content-Type: application/json" \
-d '{ "title": "'"${TITLE}"'","body": "'"${BODY}"'","head": "'"${{ env.GIT_USERNAME }}:${{ env.SYNCHRONIZE_BRANCH_NAME }}"'","base":"'"${{ env.TARGET_BRANCH_NAME }}"'"}')
RESPONSE_BODY=${RESPONSE::-3}
STATUS=$(printf "%s" "$RESPONSE" | tail -c 3)
if [[ $STATUS != "201" ]]; then
echo "Error while creating the PR"
echo $RESPONSE_BODY
exit 1
fi
echo "PR created successfully"
echo $RESPONSE_BODY | jq -r '.html_url'
PR_NUMBER=$(echo "${RESPONSE_BODY}" | jq -r '.number')
echo 'Add comment to the PR'
echo "=========================================================="
COMMENT_BODY="Hi @${PR_USER}, please review and merge this PR."
RESPONSE=$(curl -s -w "%{http_code}" -k -X \
POST https://api.github.com/repos/${{ env.PRODUCT_REPOSITORY_PUBLIC }}/issues/${PR_NUMBER}/comments \
-H "Authorization: Bearer "${{ secrets.PAT }}"" \
-H "Content-Type: application/json" \
-d '{ "body": "'"${COMMENT_BODY}"'"}')