|
| 1 | +name: Dependabot Notifications |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["*"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + notify-checks: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + if: github.actor == 'dependabot[bot]' |
| 14 | + steps: |
| 15 | + - name: Get PR Information |
| 16 | + id: get-pr-info |
| 17 | + uses: actions/github-script@v6 |
| 18 | + with: |
| 19 | + script: | |
| 20 | + const { owner, repo } = context.repo; |
| 21 | + const run = context.payload.workflow_run; |
| 22 | + |
| 23 | + // Get PR directly from the workflow run's head SHA |
| 24 | + const response = await github.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 25 | + owner, |
| 26 | + repo, |
| 27 | + commit_sha: run.head_sha |
| 28 | + }); |
| 29 | + |
| 30 | + const pr = response.data[0]; // Get the first associated PR |
| 31 | + |
| 32 | + if (pr) { |
| 33 | + core.exportVariable('PR_TITLE', pr.title); |
| 34 | + core.exportVariable('PR_AUTHOR', pr.user.login); |
| 35 | + core.exportVariable('PR_LINK', pr.html_url); |
| 36 | + core.exportVariable('PR_NUMBER', pr.number.toString()); |
| 37 | + } else { |
| 38 | + core.exportVariable('PR_TITLE', 'Unknown'); |
| 39 | + core.exportVariable('PR_AUTHOR', context.actor); |
| 40 | + core.exportVariable('PR_LINK', `https://github.com/${owner}/${repo}/pulls`); |
| 41 | + core.exportVariable('PR_NUMBER', ''); |
| 42 | + } |
| 43 | + |
| 44 | + // Get check runs for this commit |
| 45 | + const checkRuns = await github.rest.checks.listForRef({ |
| 46 | + owner, |
| 47 | + repo, |
| 48 | + ref: run.head_sha |
| 49 | + }); |
| 50 | + |
| 51 | + // Count different check conclusions |
| 52 | + const stats = checkRuns.data.check_runs.reduce((acc, check) => { |
| 53 | + acc[check.conclusion] = (acc[check.conclusion] || 0) + 1; |
| 54 | + return acc; |
| 55 | + }, {}); |
| 56 | + |
| 57 | + // Create status summary |
| 58 | + const summary = Object.entries(stats) |
| 59 | + .map(([status, count]) => `${count} ${status}`) |
| 60 | + .join(', '); |
| 61 | + |
| 62 | + core.exportVariable('CHECKS_SUMMARY', summary); |
| 63 | + |
| 64 | + // Determine overall status |
| 65 | + const hasFailures = stats.failure > 0; |
| 66 | + const hasSuccess = stats.success > 0; |
| 67 | + const hasCancelled = stats.cancelled > 0; |
| 68 | + |
| 69 | + let overallStatus; |
| 70 | + if (hasFailures) { |
| 71 | + overallStatus = 'failure'; |
| 72 | + } else if (hasCancelled && !hasSuccess) { |
| 73 | + overallStatus = 'cancelled'; |
| 74 | + } else if (hasSuccess) { |
| 75 | + overallStatus = 'success'; |
| 76 | + } else { |
| 77 | + overallStatus = 'unknown'; |
| 78 | + } |
| 79 | + |
| 80 | + // Only set status if this is the last workflow to complete |
| 81 | + const incompleteRuns = await github.rest.actions.listWorkflowRunsForRepo({ |
| 82 | + owner, |
| 83 | + repo, |
| 84 | + head_sha: run.head_sha, |
| 85 | + status: 'in_progress' |
| 86 | + }); |
| 87 | + |
| 88 | + if (incompleteRuns.data.total_count === 0) { |
| 89 | + core.exportVariable('ALL_CHECKS_STATUS', overallStatus); |
| 90 | + core.exportVariable('SHOULD_NOTIFY', 'true'); |
| 91 | + |
| 92 | + // If checks failed and PR exists, close it |
| 93 | + if ((overallStatus === 'failure' || overallStatus === 'cancelled') && pr) { |
| 94 | + await github.rest.pulls.update({ |
| 95 | + owner, |
| 96 | + repo, |
| 97 | + pull_number: pr.number, |
| 98 | + state: 'closed' |
| 99 | + }); |
| 100 | + |
| 101 | + // Add comment explaining why PR was closed |
| 102 | + await github.rest.issues.createComment({ |
| 103 | + owner, |
| 104 | + repo, |
| 105 | + issue_number: pr.number, |
| 106 | + body: `This PR was automatically closed because some checks failed.\nStatus Summary: ${summary}` |
| 107 | + }); |
| 108 | + } |
| 109 | + } else { |
| 110 | + core.exportVariable('SHOULD_NOTIFY', 'false'); |
| 111 | + } |
| 112 | +
|
| 113 | + - name: Send Slack Notification for Success |
| 114 | + if: env.SHOULD_NOTIFY == 'true' && env.ALL_CHECKS_STATUS == 'success' |
| 115 | + id: slack |
| 116 | + |
| 117 | + with: |
| 118 | + channel-id: 'C012YFE3D6D' |
| 119 | + slack-message: | |
| 120 | + Repository: ${{ github.repository }} |
| 121 | + Title: ${{ env.PR_TITLE }} |
| 122 | + Author: ${{ env.PR_AUTHOR }} |
| 123 | + Link: ${{ env.PR_LINK }} |
| 124 | + Status Summary: ${{ env.CHECKS_SUMMARY }} |
| 125 | + env: |
| 126 | + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} |
0 commit comments