ci: add Slack notification for maintainer PRs #4222
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: Pull request checks | |
| on: | |
| pull_request: | |
| jobs: | |
| checks: | |
| uses: ./.github/workflows/shared.yml | |
| all-green: | |
| if: always() | |
| needs: [checks] | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # v1.2.2 | |
| with: | |
| jobs: ${{ toJSON(needs) }} | |
| notify: | |
| name: Notify maintainers | |
| runs-on: ubuntu-latest | |
| needs: [all-green] | |
| if: >- | |
| !github.event.pull_request.draft && | |
| needs.all-green.result == 'success' | |
| permissions: {} | |
| steps: | |
| - name: Notify Slack | |
| env: | |
| MAINTAINER_MAP: ${{ secrets.MAINTAINER_MAP }} | |
| SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
| PR_URL: ${{ github.event.pull_request.html_url }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| PR_TITLE: ${{ github.event.pull_request.title }} | |
| PR_AUTHOR: ${{ github.event.pull_request.user.login }} | |
| run: | | |
| # Check if PR author is on the team (triggers list) | |
| if ! echo "$MAINTAINER_MAP" | jq -e --arg author "$PR_AUTHOR" '.triggers | index($author)' > /dev/null 2>&1; then | |
| echo "PR author $PR_AUTHOR is not on the team, skipping notification" | |
| exit 0 | |
| fi | |
| # Pick a random reviewer | |
| ALL_REVIEWERS=$(echo "$MAINTAINER_MAP" | jq -r '.reviewers | keys[]') | |
| REVIEWERS_ARRAY=($ALL_REVIEWERS) | |
| if [ ${#REVIEWERS_ARRAY[@]} -eq 0 ]; then | |
| echo "No reviewers configured" | |
| exit 0 | |
| fi | |
| REVIEWER=${REVIEWERS_ARRAY[$((RANDOM % ${#REVIEWERS_ARRAY[@]}))]} | |
| SLACK_ID=$(echo "$MAINTAINER_MAP" | jq -r --arg user "$REVIEWER" '.reviewers[$user].slack') | |
| # Post to Slack | |
| SLACK_TEXT="${PR_URL} \"${PR_TITLE}\" by ${PR_AUTHOR}" | |
| curl -sf -X POST "$SLACK_WEBHOOK_URL" \ | |
| -H 'Content-Type: application/json' \ | |
| -d "$(jq -n --arg text "$SLACK_TEXT" --arg user_id "$SLACK_ID" '{text: $text, user_id: $user_id}')" || echo "::warning::Slack notification failed" |