-
Notifications
You must be signed in to change notification settings - Fork 0
215 lines (202 loc) · 8.53 KB
/
pr-comment-slack-notify.yml
File metadata and controls
215 lines (202 loc) · 8.53 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
name: PR Slack Notification
on:
pull_request:
types: [opened, reopened]
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
jobs:
notify-slack:
# For issue_comment, only run if the comment is on a pull request
if: >
github.event_name == 'pull_request' ||
github.event_name == 'pull_request_review_comment' ||
(github.event_name == 'issue_comment' && github.event.issue.pull_request)
runs-on: ubuntu-latest
steps:
- name: Send Slack notification
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
EVENT_NAME: ${{ github.event_name }}
COMMENT_BODY: ${{ github.event.comment.body }}
COMMENT_URL: ${{ github.event.comment.html_url }}
COMMENTER: ${{ github.event.comment.user.login }}
COMMENTER_AVATAR: ${{ github.event.comment.user.avatar_url }}
REPO: ${{ github.repository }}
# pull_request event
PR_URL_PR: ${{ github.event.pull_request.html_url }}
PR_TITLE_PR: ${{ github.event.pull_request.title }}
PR_NUMBER_PR: ${{ github.event.pull_request.number }}
PR_AUTHOR_PR: ${{ github.event.pull_request.user.login }}
PR_AUTHOR_AVATAR_PR: ${{ github.event.pull_request.user.avatar_url }}
# pull_request_review_comment event
PR_URL_REVIEW: ${{ github.event.pull_request.html_url }}
PR_TITLE_REVIEW: ${{ github.event.pull_request.title }}
PR_NUMBER_REVIEW: ${{ github.event.pull_request.number }}
# issue_comment event
PR_URL_ISSUE: ${{ github.event.issue.pull_request.html_url }}
PR_TITLE_ISSUE: ${{ github.event.issue.title }}
PR_NUMBER_ISSUE: ${{ github.event.issue.number }}
run: |
# Determine PR details based on event type
if [ "$EVENT_NAME" = "pull_request" ]; then
PR_URL="$PR_URL_PR"
PR_TITLE="$PR_TITLE_PR"
PR_NUMBER="$PR_NUMBER_PR"
elif [ "$EVENT_NAME" = "pull_request_review_comment" ]; then
PR_URL="$PR_URL_REVIEW"
PR_TITLE="$PR_TITLE_REVIEW"
PR_NUMBER="$PR_NUMBER_REVIEW"
else
PR_URL="$PR_URL_ISSUE"
PR_TITLE="$PR_TITLE_ISSUE"
PR_NUMBER="$PR_NUMBER_ISSUE"
fi
REPO_SHORT="${REPO#*/}"
# Ensure bot has joined the channel
curl -s -X POST "https://slack.com/api/conversations.join" \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"channel\": \"$SLACK_CHANNEL_ID\"}" > /dev/null
# Search channel history for an existing thread about this PR
find_thread_ts() {
HISTORY=$(curl -s "https://slack.com/api/conversations.history?channel=$SLACK_CHANNEL_ID&limit=200" \
-H "Authorization: Bearer $SLACK_BOT_TOKEN")
# Look for a message containing this PR's URL
THREAD_TS=$(echo "$HISTORY" | jq -r --arg pr_url "$PR_URL" '
.messages[]
| select(.text != null and (.text | contains($pr_url)))
| .ts' | tail -1)
if [ -n "$THREAD_TS" ] && [ "$THREAD_TS" != "null" ]; then
echo "$THREAD_TS"
fi
}
if [ "$EVENT_NAME" = "pull_request" ]; then
# PR opened/reopened — post a new top-level thread-starting message
PAYLOAD=$(jq -n \
--arg channel "$SLACK_CHANNEL_ID" \
--arg repo_short "$REPO_SHORT" \
--arg pr_url "$PR_URL" \
--arg pr_number "$PR_NUMBER" \
--arg pr_title "$PR_TITLE" \
--arg author "$PR_AUTHOR_PR" \
--arg avatar "$PR_AUTHOR_AVATAR_PR" \
'{
"channel": $channel,
"username": $author,
"icon_url": $avatar,
"text": ("New pull request [" + $repo_short + "#" + $pr_number + "] <" + $pr_url + "|" + $pr_title + "> by " + $author),
"unfurl_links": false,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ("New pull request [" + $repo_short + "#" + $pr_number + "] <" + $pr_url + "|" + $pr_title + "> by " + $author)
}
}
]
}')
curl -s -X POST "https://slack.com/api/chat.postMessage" \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD"
else
# Comment event — try to find existing thread, then reply in it
THREAD_TS=$(find_thread_ts)
# Clean comment body:
# 1. Remove HTML comments (<!-- ... -->)
# 2. Remove GitHub admonitions (> [!TIP], > [!NOTE], etc. and their continuation lines)
# 3. Trim leading/trailing whitespace
CLEANED_BODY=$(echo "$COMMENT_BODY" | perl -0777 -pe '
s/<!--.*?-->//gs;
s/^>\s*\[!(TIP|NOTE|WARNING|CAUTION|IMPORTANT)\]\s*\n(^>.*\n)*//gm;
s/^\s+|\s+$//g;
')
# Truncate to 200 chars
if [ ${#CLEANED_BODY} -gt 200 ]; then
TRUNCATED_BODY="${CLEANED_BODY:0:200}..."
else
TRUNCATED_BODY="$CLEANED_BODY"
fi
if [ -n "$THREAD_TS" ]; then
# Thread reply — just show the comment text, no header
PAYLOAD=$(jq -n \
--arg channel "$SLACK_CHANNEL_ID" \
--arg commenter "$COMMENTER" \
--arg comment_body "$TRUNCATED_BODY" \
--arg avatar "$COMMENTER_AVATAR" \
--arg thread_ts "$THREAD_TS" \
'{
"channel": $channel,
"username": $commenter,
"icon_url": $avatar,
"thread_ts": $thread_ts,
"text": $comment_body,
"unfurl_links": false,
"blocks": [
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_quote",
"elements": [
{
"type": "text",
"text": $comment_body
}
]
}
]
}
]
}')
else
# No thread found — post top-level with full context
PAYLOAD=$(jq -n \
--arg channel "$SLACK_CHANNEL_ID" \
--arg repo_short "$REPO_SHORT" \
--arg pr_url "$PR_URL" \
--arg pr_number "$PR_NUMBER" \
--arg pr_title "$PR_TITLE" \
--arg commenter "$COMMENTER" \
--arg comment_body "$TRUNCATED_BODY" \
--arg avatar "$COMMENTER_AVATAR" \
'{
"channel": $channel,
"username": $commenter,
"icon_url": $avatar,
"text": ($commenter + " commented on [" + $repo_short + "#" + $pr_number + "] <" + $pr_url + "|" + $pr_title + ">"),
"unfurl_links": false,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ($commenter + " commented on [" + $repo_short + "#" + $pr_number + "] <" + $pr_url + "|" + $pr_title + ">")
}
},
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_quote",
"elements": [
{
"type": "text",
"text": $comment_body
}
]
}
]
}
]
}')
fi
curl -s -X POST "https://slack.com/api/chat.postMessage" \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD"
fi