PR Review Slack Notification #2
Workflow file for this run
This file contains 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: PR Review Slack Notification | |
on: | |
pull_request_review: | |
types: [submitted] | |
jobs: | |
notify_pr_author: | |
runs-on: ubuntu-latest | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_PR_REVIEW_WEBHOOK_URL }} | |
SLACK_IDS: ${{ secrets.SLACK_IDS }} | |
steps: | |
- name: PR 리뷰 시 Slack 알림 보내기 | |
run: | | |
PR_TITLE="${{ github.event.pull_request.title }}" | |
PR_URL="${{ github.event.pull_request.html_url }}" | |
PR_AUTHOR="${{ github.event.pull_request.user.login }}" | |
REVIEW_STATE="${{ github.event.review.state }}" | |
REVIEWER="${{ github.event.review.user.login }}" | |
echo "SLACK_IDS 내용:" | |
echo "$SLACK_IDS" | |
# SLACK_IDS를 JSON 문자열로 처리 | |
SLACK_IDS_JSON=$(echo "$SLACK_IDS" | jq -R '.' | jq 'fromjson') | |
if [ $? -ne 0 ]; then | |
echo "오류: SLACK_IDS가 유효한 JSON 형식이 아닙니다. 시크릿 형식을 확인해주세요." >&2 | |
exit 1 | |
fi | |
echo "파싱된 SLACK_IDS:" | |
echo "$SLACK_IDS_JSON" | |
get_slack_id() { | |
echo "$SLACK_IDS_JSON" | jq -r --arg github_user "$1" '.[$github_user] // empty' | |
} | |
author_slack_id=$(get_slack_id "$PR_AUTHOR") | |
reviewer_slack_id=$(get_slack_id "$REVIEWER") | |
author_mention="${author_slack_id:+<@$author_slack_id>}" | |
reviewer_mention="${reviewer_slack_id:+<@$reviewer_slack_id>}" | |
author_mention="${author_mention:-$PR_AUTHOR}" | |
reviewer_mention="${reviewer_mention:-$REVIEWER}" | |
case "$REVIEW_STATE" in | |
"changes_requested") | |
message="$author_mention님, $reviewer_mention님이 PR에 변경을 요청했습니다: <$PR_URL|$PR_TITLE>" | |
;; | |
"commented") | |
message="$author_mention님, $reviewer_mention님이 PR에 댓글을 달았습니다: <$PR_URL|$PR_TITLE>" | |
;; | |
"approved") | |
message="$author_mention님, 축하합니다! $reviewer_mention님이 PR을 승인했습니다: <$PR_URL|$PR_TITLE>" | |
;; | |
esac | |
if [ ! -z "$message" ]; then | |
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 "Slack 알림을 성공적으로 보냈습니다: $message" | |
else | |
echo "오류: Slack 알림 전송에 실패했습니다. HTTP 상태 코드: $response" >&2 | |
exit 1 | |
fi | |
else | |
echo "보낼 알림이 없습니다" | |
fi |