|
| 1 | +name: CODEOWNERS Approved - Slack Notification |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + skip-draft: |
| 7 | + description: "Skip notification for draft PRs" |
| 8 | + required: false |
| 9 | + type: boolean |
| 10 | + default: true |
| 11 | + slack-message-prefix: |
| 12 | + description: "Custom prefix for Slack message (emoji + text)" |
| 13 | + required: false |
| 14 | + type: string |
| 15 | + default: ":white_check_mark: CODEOWNERS gate satisfied" |
| 16 | + secrets: |
| 17 | + SLACK_RELEASE_CHANGELOG_WEBHOOK: |
| 18 | + description: "Slack incoming webhook URL" |
| 19 | + required: true |
| 20 | + GH_TOKEN: |
| 21 | + description: "GitHub token with PR read access (uses GITHUB_TOKEN if not provided)" |
| 22 | + required: false |
| 23 | + |
| 24 | +jobs: |
| 25 | + notify: |
| 26 | + name: Notify Slack on CODEOWNERS Approval |
| 27 | + # Only run when the submitted review is an approval |
| 28 | + if: github.event.review.state == 'approved' |
| 29 | + runs-on: ubuntu-latest |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Fetch PR decision and check for duplicate notification |
| 33 | + id: pr |
| 34 | + uses: actions/github-script@v7 |
| 35 | + with: |
| 36 | + github-token: ${{ secrets.GH_TOKEN || github.token }} |
| 37 | + script: | |
| 38 | + const { owner, repo } = context.repo; |
| 39 | + const prNumber = context.payload.pull_request.number; |
| 40 | +
|
| 41 | + const query = `query($owner:String!, $repo:String!, $number:Int!) { |
| 42 | + repository(owner:$owner, name:$repo) { |
| 43 | + pullRequest(number:$number) { |
| 44 | + number |
| 45 | + title |
| 46 | + url |
| 47 | + isDraft |
| 48 | + headRefOid |
| 49 | + baseRefName |
| 50 | + reviewDecision |
| 51 | + } |
| 52 | + } |
| 53 | + }`; |
| 54 | +
|
| 55 | + const data = await github.graphql(query, { owner, repo, number: prNumber }); |
| 56 | + const pr = data.repository.pullRequest; |
| 57 | +
|
| 58 | + core.setOutput("number", String(pr.number)); |
| 59 | + core.setOutput("title", pr.title); |
| 60 | + core.setOutput("url", pr.url); |
| 61 | + core.setOutput("is_draft", pr.isDraft ? "true" : "false"); |
| 62 | + core.setOutput("head_sha", pr.headRefOid); |
| 63 | + core.setOutput("base", pr.baseRefName); |
| 64 | + core.setOutput("review_decision", pr.reviewDecision ?? ""); |
| 65 | +
|
| 66 | + // Dedupe marker per head SHA (so new commits can re-trigger notification) |
| 67 | + const marker = `<!-- codeowners-approved:${pr.headRefOid} -->`; |
| 68 | + core.setOutput("marker", marker); |
| 69 | +
|
| 70 | + // Check if we've already notified for this SHA |
| 71 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 72 | + owner, |
| 73 | + repo, |
| 74 | + issue_number: prNumber, |
| 75 | + per_page: 100, |
| 76 | + }); |
| 77 | +
|
| 78 | + const alreadyNotified = comments.some(c => (c.body || "").includes(marker)); |
| 79 | + core.setOutput("already_notified", alreadyNotified ? "true" : "false"); |
| 80 | +
|
| 81 | + // Log for debugging |
| 82 | + console.log(`PR #${pr.number}: draft=${pr.isDraft}, decision=${pr.reviewDecision}, notified=${alreadyNotified}`); |
| 83 | +
|
| 84 | + - name: Skip notification (conditions not met) |
| 85 | + if: | |
| 86 | + (inputs.skip-draft && steps.pr.outputs.is_draft == 'true') || |
| 87 | + steps.pr.outputs.review_decision != 'APPROVED' || |
| 88 | + steps.pr.outputs.already_notified == 'true' |
| 89 | + run: | |
| 90 | + echo "### Notification Skipped" >> $GITHUB_STEP_SUMMARY |
| 91 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 92 | + echo "| Condition | Value |" >> $GITHUB_STEP_SUMMARY |
| 93 | + echo "|-----------|-------|" >> $GITHUB_STEP_SUMMARY |
| 94 | + echo "| Is Draft | ${{ steps.pr.outputs.is_draft }} |" >> $GITHUB_STEP_SUMMARY |
| 95 | + echo "| Review Decision | ${{ steps.pr.outputs.review_decision }} |" >> $GITHUB_STEP_SUMMARY |
| 96 | + echo "| Already Notified | ${{ steps.pr.outputs.already_notified }} |" >> $GITHUB_STEP_SUMMARY |
| 97 | +
|
| 98 | + - name: Post to Slack |
| 99 | + if: | |
| 100 | + (inputs.skip-draft == false || steps.pr.outputs.is_draft != 'true') && |
| 101 | + steps.pr.outputs.review_decision == 'APPROVED' && |
| 102 | + steps.pr.outputs.already_notified != 'true' |
| 103 | + uses: slackapi/slack-github-action@v2 |
| 104 | + with: |
| 105 | + payload: | |
| 106 | + { |
| 107 | + "text": "${{ inputs.slack-message-prefix }}\n<${{ steps.pr.outputs.url }}|PR #${{ steps.pr.outputs.number }} — ${{ steps.pr.outputs.title }}>\nBase: `${{ steps.pr.outputs.base }}` Head: `${{ steps.pr.outputs.head_sha }}`\nApproved by: `${{ github.event.review.user.login }}`" |
| 108 | + } |
| 109 | + env: |
| 110 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_RELEASE_CHANGELOG_WEBHOOK }} |
| 111 | + |
| 112 | + - name: Add marker comment (dedupe for this SHA) |
| 113 | + if: | |
| 114 | + (inputs.skip-draft == false || steps.pr.outputs.is_draft != 'true') && |
| 115 | + steps.pr.outputs.review_decision == 'APPROVED' && |
| 116 | + steps.pr.outputs.already_notified != 'true' |
| 117 | + uses: actions/github-script@v7 |
| 118 | + with: |
| 119 | + github-token: ${{ secrets.GH_TOKEN || github.token }} |
| 120 | + script: | |
| 121 | + const { owner, repo } = context.repo; |
| 122 | + const prNumber = context.payload.pull_request.number; |
| 123 | + const marker = `${{ steps.pr.outputs.marker }}`; |
| 124 | + const headSha = `${{ steps.pr.outputs.head_sha }}`; |
| 125 | +
|
| 126 | + await github.rest.issues.createComment({ |
| 127 | + owner, |
| 128 | + repo, |
| 129 | + issue_number: prNumber, |
| 130 | + body: `${marker}\n:bell: Slack notified for head SHA \`${headSha}\`.`, |
| 131 | + }); |
| 132 | +
|
| 133 | + - name: Summary (notification sent) |
| 134 | + if: | |
| 135 | + (inputs.skip-draft == false || steps.pr.outputs.is_draft != 'true') && |
| 136 | + steps.pr.outputs.review_decision == 'APPROVED' && |
| 137 | + steps.pr.outputs.already_notified != 'true' |
| 138 | + run: | |
| 139 | + echo "### Slack Notification Sent" >> $GITHUB_STEP_SUMMARY |
| 140 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 141 | + echo "| Field | Value |" >> $GITHUB_STEP_SUMMARY |
| 142 | + echo "|-------|-------|" >> $GITHUB_STEP_SUMMARY |
| 143 | + echo "| PR | [#${{ steps.pr.outputs.number }}](${{ steps.pr.outputs.url }}) |" >> $GITHUB_STEP_SUMMARY |
| 144 | + echo "| Title | ${{ steps.pr.outputs.title }} |" >> $GITHUB_STEP_SUMMARY |
| 145 | + echo "| Base | \`${{ steps.pr.outputs.base }}\` |" >> $GITHUB_STEP_SUMMARY |
| 146 | + echo "| Head SHA | \`${{ steps.pr.outputs.head_sha }}\` |" >> $GITHUB_STEP_SUMMARY |
| 147 | + echo "| Approved By | @${{ github.event.review.user.login }} |" >> $GITHUB_STEP_SUMMARY |
0 commit comments