ci: add Slack notification for maintainer PRs #936
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: CI | |
| on: | |
| push: | |
| branches: ["main", "v1.x"] | |
| tags: ["v*.*.*"] | |
| pull_request: | |
| branches: ["main", "v1.x"] | |
| permissions: | |
| contents: read | |
| 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_name == 'pull_request' && | |
| !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 two random reviewers (excluding the PR author) | |
| OTHERS=$(echo "$MAINTAINER_MAP" | jq -r --arg author "$PR_AUTHOR" '[.reviewers | keys[] | select(. != $author)] | .[]') | |
| OTHERS_ARRAY=($OTHERS) | |
| if [ ${#OTHERS_ARRAY[@]} -eq 0 ]; then | |
| echo "No other reviewers to notify" | |
| exit 0 | |
| fi | |
| # Shuffle and pick up to 2 | |
| IDX1=$((RANDOM % ${#OTHERS_ARRAY[@]})) | |
| REVIEWER1=${OTHERS_ARRAY[$IDX1]} | |
| SLACK_ID_1=$(echo "$MAINTAINER_MAP" | jq -r --arg user "$REVIEWER1" '.reviewers[$user].slack // empty') | |
| SLACK_ID_2="" | |
| if [ ${#OTHERS_ARRAY[@]} -ge 2 ]; then | |
| IDX2=$(( (IDX1 + 1 + RANDOM % (${#OTHERS_ARRAY[@]} - 1)) % ${#OTHERS_ARRAY[@]} )) | |
| REVIEWER2=${OTHERS_ARRAY[$IDX2]} | |
| SLACK_ID_2=$(echo "$MAINTAINER_MAP" | jq -r --arg user "$REVIEWER2" '.reviewers[$user].slack // empty') | |
| fi | |
| # Post to Slack | |
| if [ -n "$SLACK_WEBHOOK_URL" ] && { [ -n "$SLACK_ID_1" ] || [ -n "$SLACK_ID_2" ]; }; then | |
| 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 uid1 "$SLACK_ID_1" --arg uid2 "$SLACK_ID_2" '{text: $text, user_id: $uid1, user_id_2: $uid2}')" || echo "::warning::Slack notification failed" | |
| fi |