|
| 1 | +name: Auto-approve (Copilot) |
| 2 | + |
| 3 | +# Standalone (non-reusable) copy for public repos — public repos cannot call |
| 4 | +# reusable workflows from internal/private repositories. This file mirrors the |
| 5 | +# logic of axeptio/tech-scripts/.github/workflows/auto-approve-copilot.yml. |
| 6 | +# Keep in sync with the canonical template when the reusable workflow is updated. |
| 7 | + |
| 8 | +on: |
| 9 | + pull_request: |
| 10 | + types: [opened, ready_for_review, synchronize, reopened] |
| 11 | + branches: [develop, staging, main, master] |
| 12 | + pull_request_review: |
| 13 | + types: [submitted] |
| 14 | + |
| 15 | +permissions: |
| 16 | + pull-requests: write |
| 17 | + contents: read |
| 18 | + checks: read |
| 19 | + |
| 20 | +concurrency: |
| 21 | + group: >- |
| 22 | + auto-approve-copilot-${{ github.repository }}-${{ |
| 23 | + github.event.pull_request.number || |
| 24 | + github.run_id |
| 25 | + }} |
| 26 | + cancel-in-progress: true |
| 27 | + |
| 28 | +jobs: |
| 29 | + auto-approve: |
| 30 | + name: Auto-approve if Copilot conditions met |
| 31 | + runs-on: ubuntu-latest |
| 32 | + if: >- |
| 33 | + github.event.pull_request != null && |
| 34 | + (github.event.pull_request.base.ref == 'develop' || |
| 35 | + github.event.pull_request.base.ref == 'staging' || |
| 36 | + github.event.pull_request.base.ref == 'main' || |
| 37 | + github.event.pull_request.base.ref == 'master') && |
| 38 | + github.event.pull_request.draft == false && |
| 39 | + github.event.pull_request.head.repo.full_name == github.repository && |
| 40 | + github.actor != 'axeptio-bot' |
| 41 | + env: |
| 42 | + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} |
| 43 | + SLACK_CHANNEL: alert-pr-notifications |
| 44 | + steps: |
| 45 | + - name: Checkout tech-scripts (for decide.js) |
| 46 | + uses: actions/checkout@v4 |
| 47 | + with: |
| 48 | + repository: axeptio/tech-scripts |
| 49 | + ref: master |
| 50 | + path: .tech-scripts |
| 51 | + persist-credentials: false |
| 52 | + token: ${{ secrets.BOT_GITHUB_TOKEN }} |
| 53 | + |
| 54 | + - name: Request axeptio-bot review |
| 55 | + if: >- |
| 56 | + github.event_name == 'pull_request' && |
| 57 | + (github.event.action == 'opened' || github.event.action == 'ready_for_review' || github.event.action == 'reopened') |
| 58 | + uses: actions/github-script@v7 |
| 59 | + with: |
| 60 | + github-token: ${{ secrets.BOT_GITHUB_TOKEN }} |
| 61 | + script: | |
| 62 | + const botLogin = 'axeptio-bot'; |
| 63 | + const { owner, repo } = context.repo; |
| 64 | + const prNumber = context.payload.pull_request.number; |
| 65 | + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber }); |
| 66 | + if (pr.user.login === botLogin) return; |
| 67 | + const alreadyRequested = pr.requested_reviewers.some(r => r.login === botLogin); |
| 68 | + if (!alreadyRequested) { |
| 69 | + await github.rest.pulls.requestReviewers({ |
| 70 | + owner, repo, pull_number: prNumber, |
| 71 | + reviewers: [botLogin], |
| 72 | + }); |
| 73 | + } |
| 74 | +
|
| 75 | + - name: Evaluate and approve |
| 76 | + id: evaluate |
| 77 | + uses: actions/github-script@v7 |
| 78 | + env: |
| 79 | + AUTO_APPROVE_BASE_BRANCH: develop,staging,main,master |
| 80 | + with: |
| 81 | + github-token: ${{ secrets.BOT_GITHUB_TOKEN }} |
| 82 | + script: | |
| 83 | + const decide = require('./.tech-scripts/scripts/auto-approve-copilot/decide.js'); |
| 84 | + await decide({ github, context, core }); |
| 85 | +
|
| 86 | + - name: Notify Slack (approved) |
| 87 | + if: steps.evaluate.outputs.decision == 'approved' && env.SLACK_BOT_TOKEN != '' |
| 88 | + continue-on-error: true |
| 89 | + env: |
| 90 | + REPO: ${{ github.repository }} |
| 91 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 92 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 93 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 94 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 95 | + REASON: ${{ steps.evaluate.outputs.reason }} |
| 96 | + run: | |
| 97 | + payload=$(jq -nc \ |
| 98 | + --arg channel "$SLACK_CHANNEL" \ |
| 99 | + --arg repo "$REPO" \ |
| 100 | + --arg url "$PR_URL" \ |
| 101 | + --arg num "$PR_NUMBER" \ |
| 102 | + --arg title "$PR_TITLE" \ |
| 103 | + --arg author "$PR_AUTHOR" \ |
| 104 | + --arg reason "$REASON" \ |
| 105 | + 'def s: gsub("&";"&")|gsub("<";"<")|gsub(">";">")|gsub("\\|";"|"); |
| 106 | + def test_flag: if ($repo | test("test-only-repo|tech-scripts")) then "[TEST] " else "" end; |
| 107 | + {channel: $channel, |
| 108 | + attachments: [{ |
| 109 | + color: "#36a64f", |
| 110 | + text: (test_flag + "<" + $url + "|" + ($repo | split("/")[-1]) + "#" + $num + "> @" + $author + " — " + ($title|s) + "\n" + ($reason|s)) |
| 111 | + }]}') |
| 112 | + curl -sS -X POST \ |
| 113 | + -H "Authorization: Bearer $SLACK_BOT_TOKEN" \ |
| 114 | + -H 'Content-type: application/json; charset=utf-8' \ |
| 115 | + --data "$payload" \ |
| 116 | + https://slack.com/api/chat.postMessage |
| 117 | +
|
| 118 | + - name: Notify Slack (skipped) |
| 119 | + if: steps.evaluate.outputs.decision == 'skip' && env.SLACK_BOT_TOKEN != '' |
| 120 | + continue-on-error: true |
| 121 | + env: |
| 122 | + REPO: ${{ github.repository }} |
| 123 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 124 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 125 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 126 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 127 | + REASON: ${{ steps.evaluate.outputs.reason }} |
| 128 | + run: | |
| 129 | + payload=$(jq -nc \ |
| 130 | + --arg channel "$SLACK_CHANNEL" \ |
| 131 | + --arg repo "$REPO" \ |
| 132 | + --arg url "$PR_URL" \ |
| 133 | + --arg num "$PR_NUMBER" \ |
| 134 | + --arg title "$PR_TITLE" \ |
| 135 | + --arg author "$PR_AUTHOR" \ |
| 136 | + --arg reason "$REASON" \ |
| 137 | + 'def s: gsub("&";"&")|gsub("<";"<")|gsub(">";">")|gsub("\\|";"|"); |
| 138 | + def test_flag: if ($repo | test("test-only-repo|tech-scripts")) then "[TEST] " else "" end; |
| 139 | + {channel: $channel, |
| 140 | + attachments: [{ |
| 141 | + color: "#e8912d", |
| 142 | + text: (test_flag + "<" + $url + "|" + ($repo | split("/")[-1]) + "#" + $num + "> @" + $author + " — " + ($title|s) + "\nskipped: " + ($reason|s)) |
| 143 | + }]}') |
| 144 | + curl -sS -X POST \ |
| 145 | + -H "Authorization: Bearer $SLACK_BOT_TOKEN" \ |
| 146 | + -H 'Content-type: application/json; charset=utf-8' \ |
| 147 | + --data "$payload" \ |
| 148 | + https://slack.com/api/chat.postMessage |
| 149 | +
|
| 150 | + - name: Notify Slack (error) |
| 151 | + if: failure() && env.SLACK_BOT_TOKEN != '' |
| 152 | + continue-on-error: true |
| 153 | + env: |
| 154 | + REPO: ${{ github.repository }} |
| 155 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 156 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 157 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 158 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 159 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 160 | + run: | |
| 161 | + payload=$(jq -nc \ |
| 162 | + --arg channel "$SLACK_CHANNEL" \ |
| 163 | + --arg repo "$REPO" \ |
| 164 | + --arg url "$PR_URL" \ |
| 165 | + --arg num "$PR_NUMBER" \ |
| 166 | + --arg title "$PR_TITLE" \ |
| 167 | + --arg author "$PR_AUTHOR" \ |
| 168 | + --arg runUrl "$RUN_URL" \ |
| 169 | + 'def s: gsub("&";"&")|gsub("<";"<")|gsub(">";">")|gsub("\\|";"|"); |
| 170 | + def test_flag: if ($repo | test("test-only-repo|tech-scripts")) then "[TEST] " else "" end; |
| 171 | + {channel: $channel, |
| 172 | + attachments: [{ |
| 173 | + color: "#ff0000", |
| 174 | + text: (test_flag + "<" + $url + "|" + ($repo | split("/")[-1]) + "#" + $num + "> @" + $author + " — " + ($title|s) + "\nerror — <" + $runUrl + "|run log>") |
| 175 | + }]}') |
| 176 | + curl -sS -X POST \ |
| 177 | + -H "Authorization: Bearer $SLACK_BOT_TOKEN" \ |
| 178 | + -H 'Content-type: application/json; charset=utf-8' \ |
| 179 | + --data "$payload" \ |
| 180 | + https://slack.com/api/chat.postMessage |