Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions .github/workflows/pr-auto-approve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ permissions:
jobs:
approve:
runs-on: ubuntu-latest
if: ${{github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request'}}
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
steps:
- name: Verify bot identity
run: |
BOT_LOGIN="$(gh api user --jq .login)"
test "$BOT_LOGIN" = "eplus-bot"
env:
GH_TOKEN: ${{secrets.EPLUS_BOT_TOKEN}}
GH_TOKEN: ${{ secrets.EPLUS_BOT_TOKEN }}

- name: Resolve pull request
id: pr
Expand All @@ -38,12 +38,25 @@ jobs:
fi

test -n "$PR_NUMBER"

PR_AUTHOR="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.user.login')"
BOT_LOGIN="$(gh api user --jq .login)"

echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
echo "author=$PR_AUTHOR" >> "$GITHUB_OUTPUT"

if [ "$PR_AUTHOR" = "$BOT_LOGIN" ]; then
echo "::notice::Skipping PR #$PR_NUMBER because '$BOT_LOGIN' cannot approve its own pull request."
echo "should_approve=false" >> "$GITHUB_OUTPUT"
else
echo "should_approve=true" >> "$GITHUB_OUTPUT"
fi
env:
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
GH_TOKEN: ${{secrets.EPLUS_BOT_TOKEN}}
GH_TOKEN: ${{ secrets.EPLUS_BOT_TOKEN }}

- name: Wait for pull request checks
if: ${{ steps.pr.outputs.should_approve == 'true' }}
run: |
for attempt in {1..60}; do
CHECKS="$(gh pr checks "$PR_NUMBER" \
Expand All @@ -68,11 +81,12 @@ jobs:
echo "::error::Timed out waiting for pull request checks to finish."
exit 1
env:
PR_NUMBER: ${{steps.pr.outputs.number}}
APPROVAL_WORKFLOW: ${{github.workflow}}
GH_TOKEN: ${{secrets.EPLUS_BOT_TOKEN}}
PR_NUMBER: ${{ steps.pr.outputs.number }}
APPROVAL_WORKFLOW: ${{ github.workflow }}
GH_TOKEN: ${{ secrets.EPLUS_BOT_TOKEN }}

- name: Approve pull request
if: ${{ steps.pr.outputs.should_approve == 'true' }}
run: |
APPROVED_FOR_HEAD="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews" \
--paginate \
Expand All @@ -83,15 +97,16 @@ jobs:
gh pr review --approve "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --body "$APPROVAL_REVIEW_BODY"
fi
env:
PR_NUMBER: ${{steps.pr.outputs.number}}
PR_NUMBER: ${{ steps.pr.outputs.number }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
APPROVAL_REVIEW_BODY: |
Approved by @eplus-bot after all pull request checks passed.

CI run: ${{ github.event.workflow_run.html_url }}
GH_TOKEN: ${{secrets.EPLUS_BOT_TOKEN}}
GH_TOKEN: ${{ secrets.EPLUS_BOT_TOKEN }}

- name: Upsert CI approval comment
if: ${{ steps.pr.outputs.should_approve == 'true' }}
run: |
COMMENT_ID="$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" \
--paginate \
Expand Down Expand Up @@ -137,11 +152,11 @@ jobs:
-f body="$APPROVAL_COMMENT"
fi
env:
PR_NUMBER: ${{steps.pr.outputs.number}}
PR_NUMBER: ${{ steps.pr.outputs.number }}
COMMENT_MARKER: "<!-- eplus-bot-ci-approval -->"
CI_UPDATED_AT: ${{ github.event.workflow_run.updated_at }}
CI_RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
CI_RUN_URL: ${{ github.event.workflow_run.html_url }}
APPROVAL_RUN_NUMBER: ${{ github.run_number }}
APPROVAL_RUN_ATTEMPT: ${{ github.run_attempt }}
GH_TOKEN: ${{secrets.EPLUS_BOT_TOKEN}}
GH_TOKEN: ${{ secrets.EPLUS_BOT_TOKEN }}