-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack-notifier.js
More file actions
1080 lines (979 loc) · 32.9 KB
/
slack-notifier.js
File metadata and controls
1080 lines (979 loc) · 32.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Slack PR Notifier Script - Efficient Version
*
* Key improvements:
* - Delta-based updates: Only changes what's necessary
* - Sanity checks: Verifies final state and recovers if needed
* - Better error handling: slackTs is properly scoped
* - Comprehensive debugging
*/
// Minimal logging control to reduce verbosity (default: errors only)
(function configureLogging() {
try {
const level = (process.env.LOG_LEVEL || "error").toLowerCase();
const levels = { error: 0, warn: 1, info: 2, debug: 3 };
const current = levels[level] ?? 0;
const noop = () => {};
if (current < 3) console.debug = noop;
if (current < 2) console.info = noop;
if (current < 2) console.log = noop; // treat log ~ info
if (current < 1) console.warn = noop;
} catch (_) {
// ignore
}
})();
/**
* Calculate the delta between two sets
*/
function calculateDelta(current, desired) {
const currentSet = new Set(current);
const desiredSet = new Set(desired);
const toAdd = desired.filter((item) => !currentSet.has(item));
const toRemove = current.filter((item) => !desiredSet.has(item));
return { add: toAdd, remove: toRemove };
}
module.exports = async ({ github, context, core }) => {
// Get environment variables
const {
SLACK_BOT_TOKEN,
SLACK_CHANNEL_ID,
PR_NUMBER,
PR_TITLE,
PR_URL,
PR_AUTHOR,
PR_AUTHOR_TYPE,
PR_AUTHOR_AVATAR,
IS_ABANDONED,
WAIT_TIME = "0", // Avoid static waits; rely on API state instead
REACTION_DELAY_MS = "0",
VERIFICATION_DELAY_MS = "0",
} = process.env;
console.log("Starting Slack PR notifier for PR #" + PR_NUMBER);
console.log("Environment:", {
PR_NUMBER,
PR_TITLE,
PR_URL,
PR_AUTHOR,
PR_AUTHOR_TYPE,
IS_ABANDONED,
WAIT_TIME,
SLACK_CHANNEL_ID: SLACK_CHANNEL_ID ? "Set" : "Not set",
SLACK_BOT_TOKEN: SLACK_BOT_TOKEN ? "Set" : "Not set",
});
// If Slack credentials are missing, skip notification but don't fail the run
if (!SLACK_BOT_TOKEN || !SLACK_CHANNEL_ID) {
console.warn(
"Missing Slack credentials; skipping PR notification and continuing."
);
return;
}
// Avoid unconditional waits; only backoff when API indicates instability
const waitTime = parseInt(WAIT_TIME, 10);
if (waitTime > 0) {
console.log(
`Backoff override set: waiting ${waitTime}ms before proceeding...`
);
await new Promise((resolve) => setTimeout(resolve, waitTime));
}
// Initialize slackTs at the top level to avoid reference errors
let slackTs = null;
try {
// Check if repository is public
const { data: repo } = await github.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo,
});
const isPrivateRepo = repo.private;
console.log(`Repository is ${isPrivateRepo ? "private" : "public"}`);
// Get PR labels
let { data: labels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: PR_NUMBER,
});
// Ensure labels is always an array
if (!Array.isArray(labels)) {
console.error("WARNING: Labels response is not an array:", labels);
labels = [];
}
console.log(
`Found ${labels.length} PR labels:`,
labels.map((l) => l.name)
);
// Also check PR merged status directly from GitHub API
// This is more reliable than labels for recently merged PRs
let isPRMerged = false;
try {
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: PR_NUMBER,
});
isPRMerged = pr.merged === true;
if (isPRMerged) {
console.log("PR is merged according to GitHub API");
}
} catch (error) {
console.error("Failed to check PR merged status:", error.message);
}
// Get PR comments to find existing Slack timestamp
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: PR_NUMBER,
});
// Look for existing Slack timestamp in comments
const slackComment = comments.find(
(c) => c.body && c.body.includes("<!-- slack-ts:")
);
if (slackComment) {
const match = slackComment.body.match(/<!-- slack-ts:([0-9.]+) -->/);
if (match) {
slackTs = match[1];
console.log("Found existing Slack timestamp:", slackTs);
}
} else {
console.log("No existing Slack timestamp found");
}
// Check if PR is from a bot - SKIP ALL BOT PRS
if (PR_AUTHOR_TYPE === "Bot") {
console.log(`Skipping notification for bot PR from ${PR_AUTHOR}`);
return;
}
// Check if PR is draft
const isDraft =
Array.isArray(labels) &&
labels.some((label) => label.name === "status:draft");
if (isDraft) {
console.log("Skipping notification for draft PR");
return;
}
// Skip if no existing message and PR is merged
// But allow abandoned PRs to create messages for visibility
if (
!slackTs &&
(labels.some((l) => l.name === "status:merged") || isPRMerged)
) {
console.log(
"Skipping notification for merged PR without existing message"
);
return;
}
// Define label mappings
const statusLabels = [
{ label: "status:draft", text: ":pencil2: Draft" },
{ label: "status:ready-for-review", text: ":mag: Ready for Review" },
{ label: "status:in-review", text: ":eyes: In Review" },
{ label: "qa:running", text: ":hourglass_flowing_sand: QA Running" },
{ label: "qa:failed", text: ":x: QA Failed" },
{ label: "qa:success", text: ":white_check_mark: QA Passed" },
{
label: "status:changes-requested",
text: ":warning: Changes Requested",
},
{ label: "status:approved", text: ":white_check_mark: Approved" },
{ label: "status:on-hold", text: ":pause_button: On Hold" },
{ label: "status:blocked", text: ":octagonal_sign: Blocked" },
{ label: "status:ready-to-merge", text: ":rocket: Ready to Merge" },
{ label: "status:merged", text: ":tada: Merged" },
];
const priorityLabels = [
{ label: "priority:critical", text: ":rotating_light:" },
{ label: "priority:high", text: ":arrow_up:" },
{ label: "priority:medium", text: ":arrow_right:" },
{ label: "priority:low", text: ":arrow_down:" },
];
const categoryLabels = [
{ label: "type:bug", text: ":bug:" },
{ label: "type:feature", text: ":sparkles:" },
{ label: "type:refactor", text: ":recycle:" },
{ label: "type:test", text: ":test_tube:" },
{ label: "type:docs", text: ":books:" },
{ label: "type:chore", text: ":wrench:" },
{ label: "type:style", text: ":art:" },
{ label: "type:perf", text: ":zap:" },
{ label: "type:security", text: ":shield:" },
{ label: "type:breaking", text: ":boom:" },
];
// Build status text from labels
const statusTexts = [];
const activeStatus = statusLabels.find((s) =>
labels.some((l) => l.name === s.label)
);
if (activeStatus) statusTexts.push(activeStatus.text);
const activePriority = priorityLabels.find((p) =>
labels.some((l) => l.name === p.label)
);
if (activePriority) statusTexts.push(activePriority.text);
const activeCategories = categoryLabels.filter((c) =>
labels.some((l) => l.name === c.label)
);
statusTexts.push(...activeCategories.map((c) => c.text));
const statusString =
statusTexts.length > 0 ? statusTexts.join(" ") + " " : "";
// Slack API helper with enhanced debugging and exponential backoff
async function slackApi(method, params, maxRetries = 3) {
let lastError;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
const startTime = Date.now();
if (attempt > 0) {
const backoffTime = Math.min(1000 * Math.pow(2, attempt - 1), 10000); // Max 10 seconds
console.log(
`Retry attempt ${attempt}/${maxRetries} after ${backoffTime}ms backoff`
);
await new Promise((resolve) => setTimeout(resolve, backoffTime));
}
console.log(
`[${new Date().toISOString()}] Calling Slack API: ${method} (attempt ${attempt + 1})`
);
console.log("Request params:", JSON.stringify(params, null, 2));
try {
const response = await fetch(`https://slack.com/api/${method}`, {
method: "POST",
headers: {
Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify(params),
});
const data = await response.json();
const duration = Date.now() - startTime;
if (data.ok) {
console.log(`✓ Slack API ${method} succeeded in ${duration}ms`);
// Log specific useful response data
if (method === "conversations.history" && data.messages) {
console.log(` Found ${data.messages.length} messages`);
}
if (method === "chat.postMessage" && data.ts) {
console.log(` Message posted with timestamp: ${data.ts}`);
}
return data;
} else {
console.error(
`✗ Slack API ${method} failed in ${duration}ms:`,
data.error
);
// Handle specific errors
if (data.error === "missing_scope") {
console.error(
` Required scope for ${method}: Check Slack app permissions`
);
throw new Error(`Slack API error: ${data.error}`); // Don't retry permission errors
}
if (data.error === "not_in_channel") {
console.error(` Bot is not in channel ${SLACK_CHANNEL_ID}`);
throw new Error(`Slack API error: ${data.error}`); // Don't retry channel errors
}
if (data.error === "channel_not_found") {
console.error(` Channel ${SLACK_CHANNEL_ID} not found`);
throw new Error(`Slack API error: ${data.error}`); // Don't retry missing channel
}
if (data.error === "rate_limited") {
const retryAfter = data.retry_after || 60;
console.error(
` Rate limited. Retry after: ${retryAfter} seconds`
);
if (attempt < maxRetries) {
await new Promise((resolve) =>
setTimeout(resolve, retryAfter * 1000)
);
continue; // Retry after rate limit wait
}
}
// For other errors, store and maybe retry
lastError = new Error(`Slack API error: ${data.error}`);
if (attempt === maxRetries) {
throw lastError;
}
}
} catch (error) {
const duration = Date.now() - startTime;
console.error(
`✗ Slack API ${method} failed with exception in ${duration}ms:`,
error.message
);
// Network errors are retryable
if (
error.message.includes("fetch failed") ||
error.message.includes("ECONNRESET")
) {
lastError = error;
if (attempt === maxRetries) {
throw error;
}
continue;
}
// Non-retryable errors
throw error;
}
}
throw lastError || new Error(`Failed after ${maxRetries} retries`);
}
// Escape text for Slack
function escapeText(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
const isMerged =
labels.some((l) => l.name === "status:merged") || isPRMerged;
const isAbandoned = IS_ABANDONED === "true";
const escapedTitle = escapeText(PR_TITLE);
// Build message blocks
let messageBlocks;
if (isMerged) {
messageBlocks = [
{
type: "section",
text: {
type: "mrkdwn",
text: `:tada: ${escapedTitle}`,
},
accessory: {
type: "button",
text: {
type: "plain_text",
text: "View PR",
emoji: false,
},
url: PR_URL,
style: "primary",
},
},
];
} else if (isAbandoned) {
messageBlocks = [
{
type: "section",
text: {
type: "mrkdwn",
text: `:file_folder: ${escapedTitle}`,
},
accessory: {
type: "button",
text: {
type: "plain_text",
text: "View PR",
emoji: false,
},
url: PR_URL,
},
},
];
} else {
// For private repositories, use simplified blocks instead of OpenGraph image
if (isPrivateRepo) {
messageBlocks = [
{
type: "header",
text: {
type: "plain_text",
text: `#${PR_NUMBER} ${escapedTitle}`,
emoji: false,
},
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: `*Repository:*\n${context.repo.owner}/${context.repo.repo}`,
},
{
type: "mrkdwn",
text: `*Author:*\n${PR_AUTHOR}`,
},
],
},
];
// Add status context if available
if (statusString.trim()) {
messageBlocks.push({
type: "context",
elements: [
{
type: "mrkdwn",
text: statusString.trim(),
},
],
});
}
// Add action buttons
messageBlocks.push({
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "View PR",
emoji: false,
},
url: PR_URL,
style: "primary",
},
{
type: "button",
text: {
type: "plain_text",
text: "Files",
emoji: false,
},
url: `${PR_URL}/files`,
},
{
type: "button",
text: {
type: "plain_text",
text: "Checks",
emoji: false,
},
url: `${PR_URL}/checks`,
},
],
});
} else {
// Public repository - use OpenGraph image
let ogImageUrl;
if (!slackTs) {
// New message - use timestamp-based cache busting for QA running
const qaRunning = labels.some((l) => l.name === "qa:running");
const cacheKey = qaRunning ? `qa-${Date.now()}` : Date.now();
ogImageUrl = `https://opengraph.githubassets.com/${cacheKey}/${context.repo.owner}/${context.repo.repo}/pull/${PR_NUMBER}`;
} else {
// Update - use stable URL
ogImageUrl = `https://opengraph.githubassets.com/1/${context.repo.owner}/${context.repo.repo}/pull/${PR_NUMBER}`;
}
messageBlocks = [
{
type: "image",
image_url: ogImageUrl,
alt_text: `PR #${PR_NUMBER}: ${escapedTitle}`,
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "View PR",
emoji: false,
},
url: PR_URL,
style: "primary",
},
{
type: "button",
text: {
type: "plain_text",
text: "Files",
emoji: false,
},
url: `${PR_URL}/files`,
},
{
type: "button",
text: {
type: "plain_text",
text: "Checks",
emoji: false,
},
url: `${PR_URL}/checks`,
},
],
},
];
}
}
// Send or update message with retry logic
async function sendMessage(isNew) {
let result;
let retryCount = 0;
const maxRetries = 2;
while (retryCount <= maxRetries) {
try {
if (isNew) {
result = await slackApi("chat.postMessage", {
channel: SLACK_CHANNEL_ID,
text: `#${PR_NUMBER}: ${escapedTitle}`,
blocks: messageBlocks,
});
} else {
await slackApi("chat.update", {
channel: SLACK_CHANNEL_ID,
ts: slackTs,
text: `#${PR_NUMBER}: ${escapedTitle}`,
blocks: messageBlocks,
});
}
break;
} catch (error) {
if (
error.message.includes("invalid_blocks") &&
retryCount < maxRetries
) {
retryCount++;
await new Promise((resolve) =>
setTimeout(resolve, 1000 * retryCount)
);
} else if (error.message.includes("invalid_blocks")) {
// Fallback to simpler text-only blocks
let fallbackBlocks;
if (isMerged || isAbandoned) {
// For merged/abandoned, keep the simple format
fallbackBlocks = messageBlocks;
} else {
// For active PRs, create a simplified block structure
fallbackBlocks = [
{
type: "section",
text: {
type: "mrkdwn",
text: `*<${PR_URL}|#${PR_NUMBER} ${escapedTitle}>*\n_Author: ${PR_AUTHOR} • Repo: ${context.repo.owner}/${context.repo.repo}_`,
},
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "View PR",
emoji: false,
},
url: PR_URL,
style: "primary",
},
],
},
];
}
if (isNew) {
result = await slackApi("chat.postMessage", {
channel: SLACK_CHANNEL_ID,
text: `#${PR_NUMBER}: ${escapedTitle}`,
blocks: fallbackBlocks,
});
} else {
await slackApi("chat.update", {
channel: SLACK_CHANNEL_ID,
ts: slackTs,
text: `#${PR_NUMBER}: ${escapedTitle}`,
blocks: fallbackBlocks,
});
}
break;
} else {
throw error;
}
}
}
return result;
}
// Handle new message creation
if (!slackTs) {
console.log("Creating new Slack message...");
// Create a lock comment to prevent race conditions
const lockComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: PR_NUMBER,
body: "<!-- slack-creating-lock -->",
});
console.log("Created lock comment with ID:", lockComment.data.id);
// Check again for existing Slack comments (race condition check)
const { data: currentComments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: PR_NUMBER,
});
const existingComment = currentComments.find(
(c) =>
c.body &&
c.body.includes("<!-- slack-ts:") &&
c.id !== lockComment.data.id
);
if (existingComment) {
// Another process already created the message
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: lockComment.data.id,
});
const match = existingComment.body.match(/<!-- slack-ts:([0-9.]+) -->/);
if (match) {
slackTs = match[1];
}
} else {
// Create new Slack message
const result = await sendMessage(true);
if (result && result.ts) {
// Update the lock comment with the Slack timestamp
const commentBody = [
`<!-- slack-ts:${result.ts} -->`,
`To view in Slack, search for: ${result.ts}`,
].join("\n");
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: lockComment.data.id,
body: commentBody,
});
slackTs = result.ts;
} else {
// Clean up lock comment if message creation failed
try {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: lockComment.data.id,
});
} catch (e) {
// Ignore deletion errors
}
}
}
} else {
// Update existing message
await sendMessage(false);
}
// EFFICIENT REACTION MANAGEMENT
if (slackTs) {
await manageReactionsEfficiently(
slackTs,
labels,
isPRMerged,
slackApi,
SLACK_CHANNEL_ID
);
}
} catch (error) {
if (String(error.message).includes("not_authed")) {
console.warn(
"Slack authentication failed; skipping notification without failing run."
);
return;
}
console.error("❌ CRITICAL ERROR in Slack notifier:", error.message);
console.error("Stack trace:", error.stack);
console.error("\n=== EXECUTION CONTEXT ===");
console.error("PR Number:", PR_NUMBER);
console.error("PR Title:", PR_TITLE);
console.error("PR Author:", PR_AUTHOR);
console.error("Author Type:", PR_AUTHOR_TYPE);
console.error("Channel ID:", SLACK_CHANNEL_ID ? "Set" : "Not set");
console.error("Bot Token:", SLACK_BOT_TOKEN ? "Set" : "Not set");
console.error("Slack Timestamp:", slackTs || "Not set");
throw error;
}
console.log("\n=== SLACK NOTIFIER COMPLETED SUCCESSFULLY ===");
console.log(`PR #${PR_NUMBER} processed`);
console.log(`Slack timestamp: ${slackTs || "New message created"}`);
};
/**
* Efficiently manage reactions - only add/remove what's necessary
*/
async function manageReactionsEfficiently(
slackTs,
labels,
isPRMerged,
slackApi,
SLACK_CHANNEL_ID
) {
// Optional small backoff; default to 0 to avoid static waits
const reactionInitDelay = parseInt(process.env.REACTION_DELAY_MS || "0", 10);
if (reactionInitDelay > 0) {
await new Promise((resolve) => setTimeout(resolve, reactionInitDelay));
}
// Define reaction mappings from the ORIGINAL working version
const statusReactions = {
"qa:pending": "hourglass_flowing_sand",
"qa:running": "runner",
"qa:success": "white_check_mark",
"qa:failed": "x",
"status:ready-for-review": "eyes",
"status:approved": "thumbsup",
"status:mergeable": "rocket",
// Note: status:merged is intentionally not mapped - merged PRs should have no reactions
};
// Define mutually exclusive groups (only one can be active at a time)
const exclusiveGroups = {
qa: ["hourglass_flowing_sand", "runner", "white_check_mark", "x"],
status: ["eyes", "thumbsup", "rocket"],
};
// All possible status reactions we manage
const allStatusReactions = new Set(Object.values(statusReactions));
try {
// Get message to ensure it exists
const messages = await slackApi("conversations.history", {
channel: SLACK_CHANNEL_ID,
latest: slackTs,
limit: 1,
inclusive: true,
});
if (!messages.messages || messages.messages.length === 0) {
console.warn("⚠️ No message found in Slack for timestamp:", slackTs);
return;
}
const message = messages.messages[0];
console.log("Message found for reaction management");
// Get current reactions
const currentReactions = (message.reactions || [])
.filter((r) => r.users && r.users.length > 0)
.map((r) => r.name);
console.log(`Current reactions: [${currentReactions.join(", ")}]`);
// Calculate desired reactions based on labels
const desiredReactions = calculateDesiredReactions(
labels,
isPRMerged,
statusReactions,
exclusiveGroups
);
console.log(`Desired reactions: [${desiredReactions.join(", ")}]`);
// Calculate delta (what to add/remove) - only for reactions we manage
const currentManagedReactions = currentReactions.filter((r) =>
allStatusReactions.has(r)
);
const delta = calculateDelta(currentManagedReactions, desiredReactions);
console.log("\n=== EFFICIENT REACTION UPDATE PLAN ===");
console.log(`Reactions to add: [${delta.add.join(", ")}]`);
console.log(`Reactions to remove: [${delta.remove.join(", ")}]`);
// Apply changes
let changesMade = 0;
const errors = [];
// Remove reactions first
for (const reaction of delta.remove) {
try {
console.log(`Removing reaction: ${reaction}`);
await slackApi("reactions.remove", {
channel: SLACK_CHANNEL_ID,
timestamp: slackTs,
name: reaction,
});
changesMade++;
const delay = parseInt(REACTION_DELAY_MS, 10) || 0;
if (delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
} catch (error) {
if (error.message.includes("no_reaction")) {
console.log(
`Reaction ${reaction} was already removed (race condition)`
);
} else {
errors.push(`Failed to remove ${reaction}: ${error.message}`);
console.error(
`Failed to remove reaction ${reaction}:`,
error.message
);
}
}
}
// Add reactions
for (const reaction of delta.add) {
try {
console.log(`Adding reaction: ${reaction}`);
await slackApi("reactions.add", {
channel: SLACK_CHANNEL_ID,
timestamp: slackTs,
name: reaction,
});
changesMade++;
const delay = parseInt(REACTION_DELAY_MS, 10) || 0;
if (delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
} catch (error) {
if (error.message.includes("already_reacted")) {
console.log(`Reaction ${reaction} already exists (race condition)`);
} else {
errors.push(`Failed to add ${reaction}: ${error.message}`);
console.error(`Failed to add reaction ${reaction}:`, error.message);
}
}
}
console.log(`\n✓ Applied ${changesMade} reaction changes`);
// SANITY CHECK: Verify final state if we made changes
if (changesMade > 0 || errors.length > 0) {
await verifySanity(
slackTs,
desiredReactions,
allStatusReactions,
slackApi,
SLACK_CHANNEL_ID
);
}
} catch (error) {
console.error("Failed to manage reactions:", error.message);
console.error("Stack trace:", error.stack);
}
}
/**
* Calculate desired reactions based on labels and rules
*/
function calculateDesiredReactions(
labels,
isPRMerged,
statusReactions,
exclusiveGroups
) {
console.log("\n=== CALCULATING DESIRED REACTIONS ===");
// Check if PR is merged - if so, we want NO reactions
// Check both the label AND the actual PR merge state for reliability
const isMerged = labels.some((l) => l.name === "status:merged") || isPRMerged;
if (isMerged) {
console.log("PR is merged - no reactions should be displayed");
return [];
}
// Map labels to reactions
const mappedReactions = [];
for (const label of labels) {
if (statusReactions[label.name]) {
mappedReactions.push(statusReactions[label.name]);
console.log(
`Label "${label.name}" → reaction "${statusReactions[label.name]}"`
);
}
}
// Apply exclusive group rules
const finalReactions = [];
const groupPriority = {
qa: ["qa:failed", "qa:success", "qa:running", "qa:pending"],
status: [
"status:merged",
"status:mergeable",
"status:approved",
"status:ready-for-review",
],
};
// For each group, pick only the highest priority reaction
for (const [groupName, groupReactions] of Object.entries(exclusiveGroups)) {
const priorityOrder = groupPriority[groupName] || [];
// Find the highest priority label from this group
let selectedReaction = null;
for (const priorityLabel of priorityOrder) {
if (
labels.some((l) => l.name === priorityLabel) &&
statusReactions[priorityLabel]
) {
selectedReaction = statusReactions[priorityLabel];
console.log(
`Selected reaction "${selectedReaction}" for group "${groupName}"`
);
break;
}
}
if (selectedReaction && mappedReactions.includes(selectedReaction)) {
finalReactions.push(selectedReaction);
}
}
// If QA is running or pending, only show QA reactions
const hasQaInProgress = labels.some(
(l) => l.name === "qa:running" || l.name === "qa:pending"
);
if (hasQaInProgress) {
console.log("QA in progress - filtering to only QA reactions");
return finalReactions.filter((r) => exclusiveGroups.qa.includes(r));
}
return finalReactions;
}
/**
* Verify the final state matches our expectations
*/
async function verifySanity(
slackTs,
desiredReactions,
allStatusReactions,
slackApi,
SLACK_CHANNEL_ID
) {
console.log("\n=== SANITY CHECK ===");
// Optional verify backoff; default to 0 to avoid static waits
const verifyDelay = parseInt(process.env.VERIFICATION_DELAY_MS || "0", 10);
if (verifyDelay > 0) {
await new Promise((resolve) => setTimeout(resolve, verifyDelay));
}
try {
// Re-fetch the message
const messages = await slackApi("conversations.history", {
channel: SLACK_CHANNEL_ID,
latest: slackTs,
limit: 1,
inclusive: true,
});
if (!messages.messages || messages.messages.length === 0) {
console.error("❌ SANITY CHECK FAILED: Message disappeared!");
return;
}