-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (139 loc) · 6.03 KB
/
inline-comments-to-slack.yml
File metadata and controls
152 lines (139 loc) · 6.03 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
name: Notify Slack on Inline Review Comments
on:
pull_request_review_comment:
types:
- created
jobs:
notify-slack:
runs-on: ubuntu-latest
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
steps:
- name: Find PR thread in Slack
id: find-thread
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_TITLE="${{ github.event.pull_request.title }}"
REPO_FULL="${{ github.repository }}"
# Search channel history for the original GitHub app message about this PR
# Look through recent messages for one containing the PR URL or PR reference
CURSOR=""
THREAD_TS=""
for i in 1 2 3 4 5; do
if [ -n "$CURSOR" ]; then
RESPONSE=$(curl -s -H "Authorization: Bearer $SLACK_BOT_TOKEN" \
"https://slack.com/api/conversations.history?channel=$SLACK_CHANNEL_ID&limit=50&cursor=$CURSOR")
else
RESPONSE=$(curl -s -H "Authorization: Bearer $SLACK_BOT_TOKEN" \
"https://slack.com/api/conversations.history?channel=$SLACK_CHANNEL_ID&limit=50")
fi
# Look for messages from the official GitHub app that mention this PR
# The official GitHub app posts messages containing the PR URL like:
# github.com/org/repo/pull/123 or "#123 PR Title"
THREAD_TS=$(echo "$RESPONSE" | jq -r --arg pr_num "#${PR_NUMBER} " --arg pr_title "$PR_TITLE" '
.messages[] |
select(
(.bot_profile.name == "GitHub" or .username == "GitHub") and
(
(.text // "" | contains($pr_num)) or
(.text // "" | contains($pr_title)) or
(.attachments // [] | .[].text // "" | contains($pr_num)) or
(.attachments // [] | .[].text // "" | contains($pr_title)) or
(.blocks // [] | .[].text.text // "" | contains($pr_num)) or
(.blocks // [] | .[].text.text // "" | contains($pr_title))
)
) | .ts' 2>/dev/null | head -1)
if [ -n "$THREAD_TS" ] && [ "$THREAD_TS" != "null" ]; then
break
fi
CURSOR=$(echo "$RESPONSE" | jq -r '.response_metadata.next_cursor // empty')
if [ -z "$CURSOR" ]; then
break
fi
done
if [ -n "$THREAD_TS" ] && [ "$THREAD_TS" != "null" ]; then
echo "thread_ts=$THREAD_TS" >> "$GITHUB_OUTPUT"
echo "Found PR thread: $THREAD_TS"
else
echo "thread_ts=" >> "$GITHUB_OUTPUT"
echo "No existing thread found for PR #$PR_NUMBER"
fi
- name: Post comment to Slack thread
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_TITLE="${{ github.event.pull_request.title }}"
PR_URL="${{ github.event.pull_request.html_url }}"
COMMENT_URL="${{ github.event.comment.html_url }}"
COMMENT_BODY="${{ github.event.comment.body }}"
COMMENT_USER="${{ github.event.comment.user.login }}"
COMMENT_USER_URL="${{ github.event.comment.user.html_url }}"
COMMENT_PATH="${{ github.event.comment.path }}"
COMMENT_LINE="${{ github.event.comment.line }}"
REPO_FULL="${{ github.repository }}"
THREAD_TS="${{ steps.find-thread.outputs.thread_ts }}"
# Build the payload matching the official GitHub app's comment format
# The official app shows: "Comment on pull request by <user>"
# With an attachment containing the PR title and comment body
PAYLOAD=$(jq -n \
--arg channel "$SLACK_CHANNEL_ID" \
--arg thread_ts "$THREAD_TS" \
--arg comment_url "$COMMENT_URL" \
--arg comment_user "$COMMENT_USER" \
--arg comment_user_url "$COMMENT_USER_URL" \
--arg pr_number "$PR_NUMBER" \
--arg pr_title "$PR_TITLE" \
--arg pr_url "$PR_URL" \
--arg comment_body "$COMMENT_BODY" \
--arg comment_path "$COMMENT_PATH" \
--arg comment_line "$COMMENT_LINE" \
--arg repo_full "$REPO_FULL" \
'{
channel: $channel,
unfurl_links: false,
unfurl_media: false,
text: "<\($comment_url)|Comment> on pull request by <\($comment_user_url)|\($comment_user)>",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "<\($comment_url)|Comment> on pull request by <\($comment_user_url)|\($comment_user)>"
}
}
],
attachments: [
{
color: "#24292e",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "<\($pr_url)|#\($pr_number) \($pr_title)>\n`\($comment_path)` (line \($comment_line))\n\($comment_body)"
}
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: "\($repo_full)"
}
]
}
]
}
]
} | if $thread_ts != "" then .thread_ts = $thread_ts else . end')
RESPONSE=$(curl -s -X POST \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"https://slack.com/api/chat.postMessage")
OK=$(echo "$RESPONSE" | jq -r '.ok')
if [ "$OK" != "true" ]; then
echo "Error posting to Slack: $(echo "$RESPONSE" | jq -r '.error')"
exit 1
fi
echo "Comment posted successfully"