Auto rebase from target branch when /accept on PR

This commit is contained in:
Filipe PINTO 2023-04-12 16:21:40 +02:00 committed by GitHub
parent ea2d08c4bb
commit b0522ff288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,7 @@ jobs:
uses: actions/checkout@v3
with:
ref: ${{ env.BASE_REF }}
token: ${{ secrets.HURL_BOT_TOKEN }}
fetch-depth: 0
- name: Notify user
@ -63,13 +64,30 @@ jobs:
comment="🕗 [${order}](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}) is running, please wait for completion."
gh pr comment "${PR_NUMBER}" --body "${comment}"
- name: Init git bot context
uses: crazy-max/ghaction-import-gpg@v5
with:
gpg_private_key: ${{ secrets.HURL_BOT_GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.HURL_BOT_GPG_PRIVATE_KEY_PASSPHRASE }}
git_committer_name: "hurl-bot"
git_committer_email: "bot@hurl.dev"
git_user_signingkey: true
git_commit_gpgsign: true
- name: Add fork source branch
if: env.HEAD_TYPE == 'fork'
run: |
git remote add fork https://github.com/${{ env.HEAD_REPO_FULL_NAME}}
git remote --verbose
git fetch --all
- name: Check comment user permission
run: |
comment_user_permission=$(gh api repos/"${OWNER}"/"${REPO}"/collaborators/"${COMMENT_USER_LOGIN}"/permission -q .permission)
if [ "${comment_user_permission}" = "admin" ] ; then
echo " - ✅ You have the ${comment_user_permission} permission then you are allowed to accept pull request n°${PR_NUMBER}"
echo " - ✅ You have the ${comment_user_permission} permission then you are allowed to merge pull request n°${PR_NUMBER}"
else
comment="❌ Sorry \`${COMMENT_USER_LOGIN}\`, you are not allowed to accept this pull request because you do not have admin permission (actual permission=${comment_user_permission})."
comment="❌ Sorry \`${COMMENT_USER_LOGIN}\`, you are not allowed to merge this pull request because you do not have admin permission (actual permission=${comment_user_permission})."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
@ -80,7 +98,7 @@ jobs:
if [ "${{ env.PR_STATE }}" = "open" ] ; then
echo " - ✅ Pull request is ${{ env.PR_STATE }}."
else
comment="❌ Can not accept this pull request because it is not open (actual state=${{ env.PR_STATE }})."
comment="❌ Can not merge this pull request because it is not open (actual state=${{ env.PR_STATE }})."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
@ -91,7 +109,7 @@ jobs:
if [ "${{ env.PR_DRAFT }}" = "false" ] ; then
echo " - ✅ Pull request draft state is ${{ env.PR_DRAFT }}."
else
comment="❌ Can not accept this pull request because it is still in draft."
comment="❌ Can not merge this pull request because it is still in draft."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
@ -128,19 +146,12 @@ jobs:
comment="✅ Github release ${tag_version} is published."
echo " - ${comment}"
else
comment=" Github release ${tag_version} is still in draft/prerelease. Please don't forget to publish it after merging this release PR."
comment="👁‍🗨 Github release ${tag_version} is still in draft/prerelease. Please don't forget to publish it after merging this release PR."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
fi
fi
- name: Add fork source branch
if: env.HEAD_TYPE == 'fork'
run: |
git remote add fork https://github.com/${{ env.HEAD_REPO_FULL_NAME}}
git remote --verbose
git fetch --all
- name: Check if source branch is rebased from target branch
run: |
git log --oneline --cherry ${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }}...origin/${{ env.BASE_REF }} > "${{ env.REMAINING_COMMITS_FILE }}" && exit_code=0 || exit_code=1
@ -154,10 +165,35 @@ jobs:
if [ ${diff_count} -eq 0 ] ; then
echo " - ✅ ${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }} is already rebased from ${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}."
else
comment="❌ You have to rebase your \`${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }}\` branch first because there are new commits pending on target \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` branch:<br>$(echo ; sed "s/+/-/g" ${{ env.REMAINING_COMMITS_FILE }})"
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
if [ ${{ env.HEAD_TYPE }} == "fork" ] ; then
comment="❌ You have to rebase \`${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }}\` branch because there are new commits pending on target \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` branch (sorry i can't auto rebase from a fork):<br>$(echo ; sed "s/+/-/g" ${{ env.REMAINING_COMMITS_FILE }})"
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
git fetch
git checkout ${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }}
git switch ${{ env.HEAD_REF }}
git pull
git rebase origin/${{ env.BASE_REF }} && exit_code=0 || exit_code=1
if [ ${exit_code} -eq 1 ] ; then
comment="❌ Auto rebase from \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` fails due to conflicts. Sorry but you have to manage this manually. Please refer to ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} logs."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
git push --force && exit_code=0 || exit_code=1
if [ ${exit_code} -eq 0 ] ; then
comment="🔨 Auto rebase from \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` succeeds, \`${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }}\` now embeds these commits:<br>$(echo ; sed "s/+/-/g" ${{ env.REMAINING_COMMITS_FILE }})"
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
sleep 15
else
comment="❌ Auto rebase from \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` fails on `git push --force`. Please refer to ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} logs."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
fi
git switch ${{ env.BASE_REF }}
fi
- name: Check if pull request checks are successful
@ -167,14 +203,15 @@ jobs:
if [ "${exit_code}" -eq 0 ] ; then
echo " - ✅ Pull request checks are successful."
else
comment="❌ Some checks are still failing, please fix them before trying to /accept this pull request."
comment="❌ Some checks are still failing, please fix them before trying to merge this pull request."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
fi
- name: Check if source branch is rebased from target branch
run: |
git pull
git log --oneline --cherry ${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }}...origin/${{ env.BASE_REF }} > "${{ env.REMAINING_COMMITS_FILE }}" && exit_code=0 || exit_code=1
if [ ${exit_code} -eq 1 ] ; then
comment="❌ Rebase check fails. Please refer to ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} logs."
@ -186,26 +223,23 @@ jobs:
if [ ${diff_count} -eq 0 ] ; then
echo " - ✅ ${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }} is already rebased from ${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}."
else
comment="❌ You have to rebase your \`${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }}\` branch first because there are new commits pending on target \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` branch:<br>$(echo ; sed "s/+/-/g" ${{ env.REMAINING_COMMITS_FILE }})"
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
if [ ${{ env.HEAD_TYPE }} == "fork" ] ; then
comment="❌ New commits have been pushed to \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` since you accept this PR. You have to rebase \`${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }}\` branch by yourself because i can't auto rebase from a fork.<br>Pending commits:<br>$(echo ; sed "s/+/-/g" ${{ env.REMAINING_COMMITS_FILE }})"
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
else
comment="❌ New commits have been pushed to \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` since you accept this PR. You can rebase it by yourself or simply re-accept this PR to execute an auto rebase.<br>Pending commits:<br>$(echo ; sed "s/+/-/g" ${{ env.REMAINING_COMMITS_FILE }})"
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
fi
- name: Get new commits list
run: |
git log --oneline --cherry origin/${{ env.BASE_REF }}...${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }} | tee ${{ env.NEW_COMMITS_FILE }}
- name: Init git bot context
uses: crazy-max/ghaction-import-gpg@v5
with:
gpg_private_key: ${{ secrets.HURL_BOT_GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.HURL_BOT_GPG_PRIVATE_KEY_PASSPHRASE }}
git_committer_name: "hurl-bot"
git_committer_email: "bot@hurl.dev"
git_user_signingkey: true
git_commit_gpgsign: true
- name: Merge fast forward head ref to base ref
run: |
git merge ${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }} --ff-only && exit_code=0 || exit_code=1
@ -230,9 +264,9 @@ jobs:
- name: Close pull request
run: |
if [[ "${{ github.event.comment.body }}" =~ "--force" ]] ; then
comment="✅ Pull request accepted without waiting for checks and closed by \`${COMMENT_USER_LOGIN}\` with fast forward merge."
comment="✅ Pull request merged without waiting for checks and closed by \`${COMMENT_USER_LOGIN}\` with fast forward merge."
else
comment="✅ Pull request accepted and closed by \`${COMMENT_USER_LOGIN}\` with fast forward merge."
comment="✅ Pull request merged and closed by \`${COMMENT_USER_LOGIN}\` with fast forward merge."
fi
gh pr close "${PR_NUMBER}" --delete-branch --comment "${comment}.<br><br>\# List of commits merged from \`${{ env.HEAD_REPO_FULL_NAME}}/${{ env.HEAD_REF }}\` branch into \`${{ env.BASE_REPO_FULL_NAME}}/${{ env.BASE_REF }}\` branch:<br>$(echo ; sed "s/+/-/g" ${{ env.NEW_COMMITS_FILE }})" && exit_code=0 || exit_code=1
if [ ${exit_code} -eq 0 ] ; then