|
| 1 | +name: PR Slack Notification |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, ready_for_review] |
| 6 | + |
| 7 | +jobs: |
| 8 | + notify: |
| 9 | + name: Notify maintainers |
| 10 | + runs-on: ubuntu-latest |
| 11 | + if: >- |
| 12 | + !github.event.pull_request.draft |
| 13 | + permissions: {} |
| 14 | + steps: |
| 15 | + - name: Notify Slack |
| 16 | + env: |
| 17 | + MAINTAINER_MAP: ${{ secrets.MAINTAINER_MAP }} |
| 18 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 19 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 20 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 21 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 22 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 23 | + run: | |
| 24 | + # Check if PR author is on the team (triggers list) |
| 25 | + if ! echo "$MAINTAINER_MAP" | jq -e --arg author "$PR_AUTHOR" '.triggers | index($author)' > /dev/null 2>&1; then |
| 26 | + echo "PR author $PR_AUTHOR is not on the team, skipping notification" |
| 27 | + exit 0 |
| 28 | + fi |
| 29 | +
|
| 30 | + # Pick a random reviewer |
| 31 | + ALL_REVIEWERS=$(echo "$MAINTAINER_MAP" | jq -r '.reviewers | keys[]') |
| 32 | + REVIEWERS_ARRAY=($ALL_REVIEWERS) |
| 33 | + if [ ${#REVIEWERS_ARRAY[@]} -eq 0 ]; then |
| 34 | + echo "No reviewers configured" |
| 35 | + exit 0 |
| 36 | + fi |
| 37 | + REVIEWER=${REVIEWERS_ARRAY[$((RANDOM % ${#REVIEWERS_ARRAY[@]}))]} |
| 38 | + SLACK_ID=$(echo "$MAINTAINER_MAP" | jq -r --arg user "$REVIEWER" '.reviewers[$user].slack') |
| 39 | +
|
| 40 | + # Post to Slack |
| 41 | + SLACK_TEXT="${PR_URL} \"${PR_TITLE}\" by ${PR_AUTHOR}" |
| 42 | + curl -sf -X POST "$SLACK_WEBHOOK_URL" \ |
| 43 | + -H 'Content-Type: application/json' \ |
| 44 | + -d "$(jq -n --arg text "$SLACK_TEXT" --arg user_id "$SLACK_ID" '{text: $text, user_id: $user_id}')" || echo "::warning::Slack notification failed" |
0 commit comments