|
| 1 | +name: Auto Merge Integration Data PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - synchronize |
| 9 | + - ready_for_review |
| 10 | + - labeled |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: auto-merge-integration-data-${{ github.event.pull_request.number }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + approve-and-auto-merge: |
| 21 | + if: >- |
| 22 | + github.event.pull_request.head.repo.full_name == github.repository && |
| 23 | + contains(github.event.pull_request.labels.*.name, ':octocat: auto-merge') && |
| 24 | + contains(github.event.pull_request.body, 'gh-aw-workflow-id: update-integration-data') && |
| 25 | + !github.event.pull_request.draft |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Generate GitHub App token |
| 29 | + id: app-token |
| 30 | + uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 # v3 |
| 31 | + with: |
| 32 | + app-id: ${{ secrets.ASPIRE_BOT_APP_ID }} |
| 33 | + private-key: ${{ secrets.ASPIRE_BOT_PRIVATE_KEY }} |
| 34 | + owner: ${{ github.repository_owner }} |
| 35 | + repositories: ${{ github.event.repository.name }} |
| 36 | + github-api-url: ${{ github.api_url }} |
| 37 | + permission-contents: write |
| 38 | + permission-pull-requests: write |
| 39 | + - name: Approve PR and enable squash auto-merge |
| 40 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 41 | + with: |
| 42 | + github-token: ${{ steps.app-token.outputs.token }} |
| 43 | + script: | |
| 44 | + const pullNumber = context.payload.pull_request.number; |
| 45 | + const { owner, repo } = context.repo; |
| 46 | +
|
| 47 | + const pr = (await github.rest.pulls.get({ |
| 48 | + owner, |
| 49 | + repo, |
| 50 | + pull_number: pullNumber |
| 51 | + })).data; |
| 52 | +
|
| 53 | + if (pr.draft) { |
| 54 | + core.info(`Pull request #${pullNumber} is still a draft; skipping.`); |
| 55 | + return; |
| 56 | + } |
| 57 | +
|
| 58 | + const viewer = await github.graphql(`query ViewerLogin { |
| 59 | + viewer { |
| 60 | + login |
| 61 | + } |
| 62 | + }`); |
| 63 | + const reviews = await github.paginate(github.rest.pulls.listReviews, { |
| 64 | + owner, |
| 65 | + repo, |
| 66 | + pull_number: pullNumber, |
| 67 | + per_page: 100 |
| 68 | + }); |
| 69 | +
|
| 70 | + const alreadyApproved = reviews.some(review => |
| 71 | + review.user?.login === viewer.viewer.login && review.state === 'APPROVED'); |
| 72 | +
|
| 73 | + if (!alreadyApproved) { |
| 74 | + await github.rest.pulls.createReview({ |
| 75 | + owner, |
| 76 | + repo, |
| 77 | + pull_number: pullNumber, |
| 78 | + event: 'APPROVE', |
| 79 | + body: 'Automatically approving the workflow-generated integration data update.' |
| 80 | + }); |
| 81 | + core.info(`Approved pull request #${pullNumber} as ${viewer.viewer.login}.`); |
| 82 | + } else { |
| 83 | + core.info(`Pull request #${pullNumber} is already approved by ${viewer.viewer.login}.`); |
| 84 | + } |
| 85 | +
|
| 86 | + const currentMergeMethod = pr.auto_merge?.merge_method ?? null; |
| 87 | +
|
| 88 | + if (currentMergeMethod === 'squash') { |
| 89 | + core.info(`Pull request #${pullNumber} already has squash auto-merge enabled.`); |
| 90 | + return; |
| 91 | + } |
| 92 | +
|
| 93 | + if (currentMergeMethod) { |
| 94 | + await github.graphql( |
| 95 | + `mutation DisableAutoMerge($pullRequestId: ID!) { |
| 96 | + disablePullRequestAutoMerge(input: { pullRequestId: $pullRequestId }) { |
| 97 | + clientMutationId |
| 98 | + } |
| 99 | + }`, |
| 100 | + { pullRequestId: pr.node_id } |
| 101 | + ); |
| 102 | +
|
| 103 | + core.info(`Disabled existing ${currentMergeMethod} auto-merge setting.`); |
| 104 | + } |
| 105 | +
|
| 106 | + await github.graphql( |
| 107 | + `mutation EnableAutoMerge($pullRequestId: ID!) { |
| 108 | + enablePullRequestAutoMerge(input: { pullRequestId: $pullRequestId, mergeMethod: SQUASH }) { |
| 109 | + pullRequest { |
| 110 | + number |
| 111 | + autoMergeRequest { |
| 112 | + mergeMethod |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }`, |
| 117 | + { pullRequestId: pr.node_id } |
| 118 | + ); |
| 119 | +
|
| 120 | + core.info(`Enabled squash auto-merge for pull request #${pullNumber}.`); |
| 121 | +
|
| 122 | + - name: Invalidate GitHub App token |
| 123 | + if: always() && steps.app-token.outputs.token != '' |
| 124 | + env: |
| 125 | + GITHUB_SERVER_URL: ${{ github.server_url }} |
| 126 | + TOKEN: ${{ steps.app-token.outputs.token }} |
| 127 | + run: | |
| 128 | + echo "Revoking GitHub App installation token..." |
| 129 | + GH_HOST="${GITHUB_SERVER_URL#https://}" |
| 130 | + GH_HOST="${GH_HOST#http://}" |
| 131 | + export GH_HOST |
| 132 | + gh api \ |
| 133 | + --hostname "$GH_HOST" \ |
| 134 | + --method DELETE \ |
| 135 | + -H "Authorization: token $TOKEN" \ |
| 136 | + "/installation/token" || echo "Token revoke may already be expired." |
| 137 | +
|
| 138 | + echo "Token invalidation step complete." |
0 commit comments