Skip to content

[FIX] Notification 관련 CI workflow 에러 해결 #3

[FIX] Notification 관련 CI workflow 에러 해결

[FIX] Notification 관련 CI workflow 에러 해결 #3

Workflow file for this run

name: PR Slack Notification
on:
pull_request:
types: [opened, reopened]
permissions:
pull-requests: read
jobs:
notify_reviewers:
runs-on: ubuntu-latest
steps:
- name: Check required secrets
run: |
if [ -z "${{ secrets.SLACK_PR_CREATE_WEBHOOK_URL }}" ]; then
echo "Error: SLACK_PR_CREATE_WEBHOOK_URL is not set"
exit 1
fi
if [ -z "${{ secrets.SLACK_IDS }}" ]; then
echo "Error: SLACK_IDS is not set"
exit 1
fi
- name: Set environment variables
run: |
echo "SLACK_WEBHOOK_URL=${{ secrets.SLACK_PR_CREATE_WEBHOOK_URL }}" >> $GITHUB_ENV
echo 'SLACK_IDS=${{ toJson(secrets.SLACK_IDS) }}' >> $GITHUB_ENV
shell: bash
- name: Send Slack notification for new PR
run: |
PR_TITLE="${{ github.event.pull_request.title }}"
PR_URL="${{ github.event.pull_request.html_url }}"
REVIEWERS='${{ toJson(github.event.pull_request.requested_reviewers.*.login) }}'
if [ -z "$REVIEWERS" ] || [ "$REVIEWERS" == "null" ]; then
echo "No reviewers requested for this PR"
exit 0
fi
echo "Reviewers: $REVIEWERS"
parse_slack_ids() {
echo "$SLACK_IDS" | jq -r 'to_entries | map("\(.key):\(.value)") | .[]'
}
reviewers=$(echo "$REVIEWERS" | jq -r '.[]')
mentions=""
for reviewer in $reviewers; do
slack_id=$(parse_slack_ids | grep "^$reviewer:" | cut -d':' -f2)
if [ -n "$slack_id" ]; then
mentions="$mentions <@$slack_id>"
else
mentions="$mentions $reviewer"
echo "Warning: No Slack ID found for GitHub user $reviewer" >&2
fi
done
echo "Mentions: $mentions"
if [ -n "$mentions" ]; then
message="$mentions 님, 새로운 PR이 생성되었습니다: <$PR_URL|$PR_TITLE>"
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$message\"}" \
"$SLACK_WEBHOOK_URL")
if [ "$response" = "200" ]; then
echo "Successfully sent Slack notification"
else
echo "Error: Failed to send Slack notification. HTTP status code: $response" >&2
exit 1
fi
else
echo "No reviewers to notify"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}