-
Notifications
You must be signed in to change notification settings - Fork 1
79 lines (71 loc) · 3.02 KB
/
pr-review-notification.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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: |
set -e
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 }}"
# Parse SLACK_IDS as JSON
SLACK_IDS_JSON=$(echo "$SLACK_IDS" | jq -r '.' 2>/dev/null || echo "{}")
if [ "$SLACK_IDS_JSON" == "{}" ]; then
echo "Warning: SLACK_IDS is not a valid JSON. Attempting to parse as a string."
SLACK_IDS_JSON=$(echo "$SLACK_IDS" | sed 's/^"//; s/"$//' | jq -R 'split(",") | map(split(":")) | from_entries' 2>/dev/null || echo "{}")
if [ "$SLACK_IDS_JSON" == "{}" ]; then
echo "Error: Failed to parse SLACK_IDS as JSON or string. Please check the format."
exit 1
fi
fi
get_slack_id() {
local github_username="$1"
echo "$SLACK_IDS_JSON" | jq -r --arg user "$github_username" '.[$user] // empty'
}
author_slack_id=$(get_slack_id "$PR_AUTHOR")
reviewer_slack_id=$(get_slack_id "$REVIEWER")
if [ -n "$author_slack_id" ]; then
author_mention="<@$author_slack_id>"
else
author_mention="$PR_AUTHOR"
echo "Warning: No Slack ID found for PR author $PR_AUTHOR" >&2
fi
if [ -n "$reviewer_slack_id" ]; then
reviewer_mention="<@$reviewer_slack_id>"
else
reviewer_mention="$REVIEWER"
echo "Warning: No Slack ID found for reviewer $REVIEWER" >&2
fi
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 [ -n "$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 "Successfully sent Slack notification"
else
echo "Error: Failed to send Slack notification. HTTP status code: $response" >&2
exit 1
fi
else
echo "No notification to send"
fi