Verify and Merge Submission #101
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Verify and Merge Submission | |
| on: | |
| workflow_run: | |
| workflows: ["Check Author"] | |
| types: [completed] | |
| jobs: | |
| verify: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'workflow_run' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| actions: read | |
| env: | |
| GITHUB_TOKEN: ${{ github.token }} | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Download PR context from triggering run | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: pr-context | |
| github-token: ${{ github.token }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Export PR_NUMBER env | |
| run: | | |
| PR_NUMBER=$(jq -r .number pr-context.json) | |
| PR_HEAD=$(jq -r .head_sha pr-context.json) | |
| echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
| echo "GITHUB_PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
| echo "HEAD_SHA=$PR_HEAD" >> $GITHUB_ENV | |
| echo "GITHUB_HEAD_REF=${{ github.event.workflow_run.head_branch }}" >> $GITHUB_ENV | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.10' | |
| - run: pip install requests | |
| - name: Verify Submission Hash | |
| env: | |
| CORRECT_ANSWERS_JSON: ${{ secrets.CORRECT_ANSWERS_JSON }} | |
| run: python tools/verify_answers.py | |
| - name: Notify PR about incorrect submission | |
| if: failure() | |
| run: | | |
| curl -H "Authorization: token $GITHUB_TOKEN" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| --request POST \ | |
| --data "{\"body\": \"Your submission is incorrect. Please review and submit again.\"}" \ | |
| "https://api.github.com/repos/${{ github.repository }}/issues/${GITHUB_PR_NUMBER}/comments" | |
| - name: Close the PR | |
| if: failure() | |
| run: | | |
| curl -H "Authorization: token $GITHUB_TOKEN" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| --request PATCH \ | |
| --data '{"state":"closed"}' \ | |
| "https://api.github.com/repos/${{ github.repository }}/pulls/${GITHUB_PR_NUMBER}" | |
| - name: Auto-merge PR | |
| if: success() | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| gh pr merge "$GITHUB_PR_NUMBER" \ | |
| --squash \ | |
| --delete-branch \ | |
| --repo "${{ github.repository }}" \ | |
| --match-head-commit "$HEAD_SHA" \ | |
| --auto | |
| echo "PR #$GITHUB_PR_NUMBER queued for auto-merge." | |