Notify Slack of Delayed PRs #139
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: Notify Slack of Delayed PRs | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: "0 9 * * *" | |
| jobs: | |
| notify: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Get delayed PRs (open > 2 days) | |
| id: delayed_prs | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const twoDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000); | |
| const prs = await github.rest.pulls.list({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| state: "open", | |
| per_page: 100, | |
| }); | |
| const delayed = prs.data.filter(pr => new Date(pr.created_at) < twoDaysAgo); | |
| const result = delayed.map(pr => ({ | |
| title: pr.title, | |
| url: pr.html_url, | |
| user: pr.user.login | |
| })); | |
| core.setOutput("delayed", JSON.stringify(result)); | |
| - name: Notify Slack about delayed PRs | |
| if: steps.delayed_prs.outputs.delayed != '[]' | |
| env: | |
| SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} | |
| SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }} | |
| SLACK_USER_MAP: ${{ secrets.SLACK_USER_MAP }} | |
| run: | | |
| echo "Parsing Slack user map" | |
| user_map=$(echo "$SLACK_USER_MAP" | jq -c) | |
| echo "Parsing delayed PRs" | |
| delayed_prs='${{ steps.delayed_prs.outputs.delayed }}' | |
| echo "Composing Slack blocks" | |
| blocks=$(jq -n \ | |
| --argjson prs "$delayed_prs" \ | |
| --argjson userMap "$user_map" \ | |
| ' | |
| [ | |
| { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": ":alarm_clock: *Delayed Pull Request Alert*\nThe following PRs have been open for more than *2 days*:" | |
| } | |
| }, | |
| { "type": "divider" } | |
| ] | |
| + ($prs | map({ | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": ( | |
| "*<\(.url)|\(.title)>*\n" + | |
| "Author: <https://github.com/\(.user)|@\(.user)> (" + | |
| ( | |
| ($userMap[.user] // .user) | |
| | if test("^U[A-Z0-9]+$") | |
| then "<@" + . + ">" | |
| else "`" + . + "`" | |
| end | |
| ) + ")" | |
| ) | |
| } | |
| })) | |
| ') | |
| echo "Sending Slack notification..." | |
| curl -X POST https://slack.com/api/chat.postMessage \ | |
| -H "Authorization: Bearer $SLACK_BOT_TOKEN" \ | |
| -H 'Content-type: application/json' \ | |
| --data "{ | |
| \"channel\": \"$SLACK_CHANNEL_ID\", | |
| \"blocks\": $blocks | |
| }" |