Skip to content

[WSO2 Release] [Jenkins #2545] [Release 1.8.69] prepare for next deve… #205

[WSO2 Release] [Jenkins #2545] [Release 1.8.69] prepare for next deve…

[WSO2 Release] [Jenkins #2545] [Release 1.8.69] prepare for next deve… #205

# 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: identity-governance
GIT_USERNAME: jenkins-is-staging
PRODUCT_REPOSITORY_FORKED: $GIT_USERNAME'/'${REPOSITORY}
PRODUCT_REPOSITORY_PUBLIC: wso2-extensions/$REPOSITORY
BUILD_NUMBER: ${{github.run_id}}
SYNCHRONIZE_BRANCH_NAME: master_sync_github_action/$BUILD_NUMBER
TARGET_BRANCH_NAME: 2.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}"'"}')