Automerge Approve #331
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: Automerge Approve | |
| # Triggered after "Automerge Check" completes successfully. | |
| # workflow_run always executes in the base-repo context with a writable token, | |
| # which is what allows enqueuePullRequest on a protected branch — unlike steps | |
| # running directly inside pull_request_target on opened/synchronize/reopened. | |
| # | |
| # Prerequisites: | |
| # - Repo/org setting "Allow GitHub Actions to create and approve pull requests" | |
| # must be enabled (Settings > Actions > General > Workflow permissions). | |
| on: | |
| workflow_run: | |
| workflows: ["Automerge Check"] | |
| types: [completed] | |
| jobs: | |
| approve-and-queue: | |
| if: github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| contents: write | |
| steps: | |
| - name: Get PR number | |
| id: get-pr | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPO: ${{ github.repository }} | |
| HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | |
| run: | | |
| pr_number=$(gh pr list --repo "$REPO" --state open \ | |
| --json number,headRefOid \ | |
| --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number") | |
| if [ -z "$pr_number" ]; then | |
| echo "No open PR found for SHA $HEAD_SHA; skipping." | |
| echo "skip=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT" | |
| echo "skip=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Check automerge-possible label | |
| id: check-label | |
| if: steps.get-pr.outputs.skip == 'false' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPO: ${{ github.repository }} | |
| PR_NUMBER: ${{ steps.get-pr.outputs.pr_number }} | |
| run: | | |
| has_label=$(gh pr view "$PR_NUMBER" --repo "$REPO" \ | |
| --json labels --jq '.labels[].name | select(. == "automerge-possible")') | |
| if [ -z "$has_label" ]; then | |
| echo "Label \"automerge-possible\" not present; skipping." | |
| echo "skip=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "skip=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Approve PR | |
| if: steps.check-label.outputs.skip == 'false' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| PR_NUMBER: ${{ steps.get-pr.outputs.pr_number }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| gh pr review "$PR_NUMBER" --repo "$REPO" --approve \ | |
| --body "Linter passed all checks — approving for auto-merge." | |
| - name: Add to merge queue | |
| if: steps.check-label.outputs.skip == 'false' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| PR_NUMBER: ${{ steps.get-pr.outputs.pr_number }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| node_id=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json id --jq '.id') | |
| gh api graphql -f query=' | |
| mutation($id: ID!) { | |
| enqueuePullRequest(input: { pullRequestId: $id }) { | |
| mergeQueueEntry { position } | |
| } | |
| }' -f id="$node_id" |