2022-06-27 16:44:37 +03:00
name : accept-pull-request
on :
issue_comment :
types :
- created
2022-10-10 15:50:19 +03:00
concurrency : accept-pull-request-${{ github.event.issue.number }}
2022-08-31 12:35:35 +03:00
2022-06-27 16:44:37 +03:00
jobs :
accept-pull-request :
2023-05-04 15:46:14 +03:00
if : ${{ github.event.issue.pull_request && (github.event.comment.body == '/accept' || github.event.comment.body == '/accept --force') }}
2022-06-27 16:44:37 +03:00
env :
GITHUB_CONTEXT : ${{ toJson(github) }}
2022-11-14 18:24:52 +03:00
GITHUB_TOKEN : ${{ secrets.HURL_BOT_TOKEN }}
2022-06-27 16:44:37 +03:00
OWNER : ${{ github.repository_owner }}
REPO : ${{ github.event.repository.name }}
PR_NUMBER : ${{ github.event.issue.number }}
COMMENT_USER_LOGIN : ${{ github.event.comment.user.login }}
2023-06-06 14:13:44 +03:00
PR_COMMENT : ${{ github.event.comment.body }}
2022-11-03 18:10:38 +03:00
outputs :
base_ref : ${{ steps.init-all-internal-env-vars.outputs.base_ref }}
new_version_master_snapshot_version : ${{ steps.check-github-release.outputs.new_version_master_snapshot_version }}
is_a_github_release_pr : ${{ steps.check-github-release.outputs.is_a_github_release_pr }}
2022-06-27 16:44:37 +03:00
name : accept-pull-request
runs-on : ubuntu-latest
steps :
2023-04-24 11:14:23 +03:00
- name : Check 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 merge pull request n°${PR_NUMBER}"
else
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
fi
2022-11-03 18:10:38 +03:00
- name : Init all internal env vars
id : init-all-internal-env-vars
2022-06-27 16:44:37 +03:00
run : |
2022-11-03 18:10:38 +03:00
pr_detail_file="pr_detail.json"
gh api repos/"${OWNER}"/"${REPO}"/pulls/"${PR_NUMBER}" > "${pr_detail_file}"
jq . "${pr_detail_file}"
2022-12-06 16:39:15 +03:00
echo "github.event.issue.title: ${{ github.event.issue.title }}"
2022-11-03 18:10:38 +03:00
HEAD_REPO_FULL_NAME=$(jq -rc .head.repo.full_name ${pr_detail_file})
BASE_REPO_FULL_NAME=$(jq -rc .base.repo.full_name ${pr_detail_file})
if [ "${HEAD_REPO_FULL_NAME}" = "${BASE_REPO_FULL_NAME}" ] ; then
HEAD_TYPE=origin
else
HEAD_TYPE=fork
fi
echo "HEAD_REPO_FULL_NAME=${HEAD_REPO_FULL_NAME}" | tee -a "${GITHUB_ENV}"
echo "BASE_REPO_FULL_NAME=${BASE_REPO_FULL_NAME}" | tee -a "${GITHUB_ENV}"
echo "HEAD_TYPE=${HEAD_TYPE}" | tee -a "${GITHUB_ENV}"
echo "HEAD_REF=$(jq -rc .head.ref ${pr_detail_file})" | tee -a "${GITHUB_ENV}"
echo "BASE_REF=$(jq -rc .base.ref ${pr_detail_file})" | tee -a "${GITHUB_ENV}"
echo "PR_STATE=$(jq -rc .state ${pr_detail_file})" | tee -a "${GITHUB_ENV}"
echo "PR_DRAFT=$(jq -rc .draft ${pr_detail_file})" | tee -a "${GITHUB_ENV}"
echo "PR_MERGEABLE=$(jq -rc .mergeable ${pr_detail_file})" | tee -a "${GITHUB_ENV}"
echo "REMAINING_COMMITS_FILE=remaining_commits.txt" | tee -a "${GITHUB_ENV}"
echo "NEW_COMMITS_FILE=new_commits.txt" | tee -a "${GITHUB_ENV}"
echo "base_ref=$(jq -rc .base.ref ${pr_detail_file})" | tee -a "${GITHUB_OUTPUT}"
2022-06-27 16:44:37 +03:00
- name : Checkout repository
uses : actions/checkout@v3
with :
ref : ${{ env.BASE_REF }}
2023-04-12 17:21:40 +03:00
token : ${{ secrets.HURL_BOT_TOKEN }}
2022-06-27 16:44:37 +03:00
fetch-depth : 0
2022-09-20 10:39:51 +03:00
- name : Notify user
run : |
2023-06-06 14:13:44 +03:00
if [[ "${{ env.PR_COMMENT }}" =~ "--force" ]] ; then
2023-04-24 11:14:23 +03:00
order="/accept --force"
else
order="/accept"
fi
2022-11-03 18:10:38 +03:00
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}"
2022-09-20 10:39:51 +03:00
2023-04-12 17:21:40 +03:00
- 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
2022-06-27 16:44:37 +03:00
- name : Check if pull request state is open
run : |
2022-11-03 18:10:38 +03:00
if [ "${{ env.PR_STATE }}" = "open" ] ; then
echo " - ✅ Pull request is ${{ env.PR_STATE }}."
else
2023-04-12 17:21:40 +03:00
comment="❌ Can not merge this pull request because it is not open (actual state=${{ env.PR_STATE }})."
2022-11-03 18:10:38 +03:00
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
2022-06-27 16:44:37 +03:00
- name : Check if pull request is ready
run : |
2022-11-03 18:10:38 +03:00
if [ "${{ env.PR_DRAFT }}" = "false" ] ; then
echo " - ✅ Pull request draft state is ${{ env.PR_DRAFT }}."
else
2023-04-12 17:21:40 +03:00
comment="❌ Can not merge this pull request because it is still in draft."
2022-11-03 18:10:38 +03:00
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
2022-06-27 16:44:37 +03:00
- name : Check if pull request is mergeable
run : |
2022-11-03 18:10:38 +03:00
if [ "${{ env.PR_MERGEABLE }}" = "true" ] ; then
echo " - ✅ Pull request mergeable state is ${{ env.PR_MERGEABLE }}."
else
comment="❌ Pull request is not mergeable, please check unresolved discussions and pull request messages."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
2023-05-04 12:35:10 +03:00
- name : Check GitHub release
2022-11-03 18:10:38 +03:00
id : check-github-release
run : |
if [ $(echo ${{ env.HEAD_REF }} | grep -Ec '^release/') -eq 0 ] ; then
2023-05-04 12:35:10 +03:00
echo " - ✅ Pull request is not about a pending GitHub release."
2022-11-03 18:10:38 +03:00
else
2022-11-08 00:11:31 +03:00
echo "is_a_github_release_pr=true" | tee -a "${GITHUB_OUTPUT}"
2022-11-03 18:10:38 +03:00
tag_version="$(echo ${{ env.HEAD_REF }} | cut --delimiter '/' --field 2)"
2022-11-08 00:11:31 +03:00
major=$(echo "${tag_version}" | cut --delimiter "." --field 1)
minor=$(echo "${tag_version}" | cut --delimiter "." --field 2)
new_minor="$((minor + 1))"
echo "major=${major}, minor=${minor}, new_minor=${new_minor}"
new_version_master_snapshot_version="${major}.${new_minor}.0-SNAPSHOT"
echo "new_version_master_snapshot_version=${new_version_master_snapshot_version}" | tee -a "${GITHUB_OUTPUT}"
2022-11-03 18:10:38 +03:00
gh_result=$(gh api repos/"${OWNER}"/"${REPO}"/releases/tags/"${tag_version}" | tr ":" "=" | tr -d '"' | tr -d " ")
echo "gh_result=${gh_result}"
if [ $(echo "${gh_result}" | grep -Ec "message=NotFound|prerelease=true") -eq 0 ] ; then
comment="✅ Github release ${tag_version} is published."
echo " - ${comment}"
2022-06-27 16:44:37 +03:00
else
2023-04-12 17:21:40 +03:00
comment="👁🗨 Github release ${tag_version} is still in draft/prerelease. Please don't forget to publish it after merging this release PR."
2022-11-03 18:10:38 +03:00
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
2022-06-27 16:44:37 +03:00
fi
2022-11-03 18:10:38 +03:00
fi
2022-11-08 00:11:31 +03:00
- 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
if [ ${exit_code} -eq 1 ] ; then
comment="❌ Rebase check fails. 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
diff_count=$(grep -c ^+ ${{ env.REMAINING_COMMITS_FILE }} || true)
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
2023-04-12 17:21:40 +03:00
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}"
2023-06-06 14:13:44 +03:00
if [[ "${{ env.PR_COMMENT }}" =~ "--force" ]] ; then
2023-05-03 07:52:09 +03:00
order="/accept --force"
else
order="/accept"
fi
2023-05-04 11:25:39 +03:00
comment="🕗 [${order}](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}) is running, please wait for completion."
2023-05-03 07:52:09 +03:00
gh pr comment "${PR_NUMBER}" --body "${comment}"
2023-04-12 17:21:40 +03:00
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 }}
2022-11-08 00:11:31 +03:00
fi
2022-06-27 16:44:37 +03:00
2022-08-05 12:29:02 +03:00
- name : Check if pull request checks are successful
if : ${{ !contains(github.event.comment.body, '--force') }}
run : |
2022-11-03 18:10:38 +03:00
gh pr checks "${PR_NUMBER}" --watch --interval 30 && exit_code=0 || exit_code=$?
if [ "${exit_code}" -eq 0 ] ; then
echo " - ✅ Pull request checks are successful."
else
2023-04-12 17:21:40 +03:00
comment="❌ Some checks are still failing, please fix them before trying to merge this pull request."
2022-11-03 18:10:38 +03:00
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
2023-04-12 17:21:40 +03:00
fi
2022-08-05 12:29:02 +03:00
2022-06-27 16:44:37 +03:00
- name : Check if source branch is rebased from target branch
run : |
2023-04-12 17:21:40 +03:00
git pull
2022-11-03 18:10:38 +03:00
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."
echo " - ${comment}"
gh pr comment "${PR_NUMBER}" --body "${comment}"
exit 1
fi
diff_count=$(grep -c ^+ ${{ env.REMAINING_COMMITS_FILE }} || true)
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
2023-04-12 17:21:40 +03:00
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
2022-11-03 18:10:38 +03:00
fi
2022-06-27 16:44:37 +03:00
- name : Get new commits list
run : |
2022-11-03 18:10:38 +03:00
git log --oneline --cherry origin/${{ env.BASE_REF }}...${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }} | tee ${{ env.NEW_COMMITS_FILE }}
2022-06-27 16:44:37 +03:00
- name : Merge fast forward head ref to base ref
run : |
2022-11-03 18:10:38 +03:00
git merge ${{ env.HEAD_TYPE }}/${{ env.HEAD_REF }} --ff-only && exit_code=0 || exit_code=1
if [ ${exit_code} -eq 0 ] ; then
echo " - ✅ Merge fast forward succeeds."
else
comment="❌ Merge fast forward fails. 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 && exit_code=0 || exit_code=1
if [ ${exit_code} -eq 0 ] ; then
echo " - ✅ Push merge fast forward succeeds."
else
comment="❌ Push merge fast forward fails. 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
2022-06-27 16:44:37 +03:00
- name : Close pull request
run : |
2023-06-06 14:13:44 +03:00
if [[ "${{ env.PR_COMMENT }}" =~ "--force" ]] ; then
2023-04-12 17:21:40 +03:00
comment="✅ Pull request merged without waiting for checks and closed by \`${COMMENT_USER_LOGIN}\` with fast forward merge."
2022-11-03 18:10:38 +03:00
else
2023-04-12 17:21:40 +03:00
comment="✅ Pull request merged and closed by \`${COMMENT_USER_LOGIN}\` with fast forward merge."
2022-11-03 18:10:38 +03:00
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
echo " - ${comment}"
else
2023-03-07 17:50:35 +03:00
comment="❌ A problem occurred when closing pull request and/or deleting branch."
2022-11-03 18:10:38 +03:00
echo " - ${comment}"
#gh pr comment "${PR_NUMBER}" --body "${comment} Please refer to ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} logs."
#exit 1
fi