Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
971 lines (851 loc) · 32.4 KB
/
Copy pathbackground.js
File metadata and controls
971 lines (851 loc) · 32.4 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
const GITHUB_API = 'https://api.github.com';
const ALARM_NAME = 'pr-check';
// Track seen PR IDs and activity state
let seenPRIds = new Set();
let prState = {}; // { [prId]: { commentCount, reviewState, ciStatus } }
let isInitialized = false;
// fetch() rejects with a TypeError only when the request never completed (offline, sleep, DNS).
// Those are routine for a poller, so they warn; chrome://extensions' Errors page collects only errors.
function logRequestError(message, error) {
if (error instanceof TypeError) console.warn(message, error.message);
else console.error(message, error);
}
// Initialize on service worker start
chrome.runtime.onInstalled.addListener(() => {
console.log('GitHub PR Monitor installed');
initializeExtension();
});
chrome.runtime.onStartup.addListener(() => {
console.log('GitHub PR Monitor starting up');
initializeExtension();
});
// Handle messages from popup/options
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'SETTINGS_UPDATED') {
console.log('Settings updated, reinitializing...');
initializeExtension();
} else if (message.type === 'REFRESH_PRS') {
fetchAndStorePRs().then(() => sendResponse({ success: true }));
return true;
} else if (message.type === 'GET_PRS') {
chrome.storage.local.get(['prs', 'lastUpdated', 'error']).then(sendResponse);
return true;
} else if (message.type === 'UPDATE_BADGE_FILTER') {
// Update badge based on filter
updateBadgeWithFilter(message.filter);
} else if (message.type === 'PR_LIST_DATA') {
fetchPRListData(message.repo, message.numbers, sender)
.then(sendResponse)
.catch(error => sendResponse({ ok: false, error: error.message }));
return true;
}
});
// Handle alarm
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === ALARM_NAME) {
fetchAndStorePRs();
}
});
// Initialize extension
async function initializeExtension() {
const settings = await chrome.storage.sync.get(['token', 'repos', 'repo', 'username', 'pollInterval']);
const repos = settings.repos || (settings.repo ? [{ fullName: settings.repo }] : []);
if (!settings.token || repos.length === 0) {
console.log('Extension not configured yet');
chrome.action.setBadgeText({ text: '?' });
chrome.action.setBadgeBackgroundColor({ color: '#6e7681' });
return;
}
// Load stored state
const stored = await chrome.storage.local.get(['seenPRIds', 'prState']);
if (stored.seenPRIds) {
seenPRIds = new Set(stored.seenPRIds);
}
if (stored.prState) {
prState = stored.prState;
}
// Set up polling alarm
const interval = settings.pollInterval || 2;
await chrome.alarms.clear(ALARM_NAME);
await chrome.alarms.create(ALARM_NAME, {
delayInMinutes: 0.1,
periodInMinutes: interval
});
isInitialized = true;
console.log(`Polling every ${interval} minutes for ${repos.length} repo(s)`);
await fetchAndStorePRs();
}
// Fetch PRs from GitHub
async function fetchAndStorePRs() {
const settings = await chrome.storage.sync.get([
'token', 'repos', 'repo', 'username',
'includeAuthored', 'notificationsEnabled',
'notifyNewPRs', 'notifyComments', 'notifyReviews', 'notifyCIFailure'
]);
const repos = settings.repos || (settings.repo ? [{ fullName: settings.repo }] : []);
if (!settings.token || repos.length === 0 || !settings.username) {
return;
}
try {
// Fetch PRs from all repos
const allPRs = [];
for (const repo of repos) {
const repoPRs = await fetchPRsForRepo(
settings.token,
repo.fullName,
settings.username,
settings.includeAuthored !== false
);
allPRs.push(...repoPRs);
}
// Deduplicate by PR ID
const prMap = new Map();
allPRs.forEach(pr => {
if (!prMap.has(pr.id)) {
prMap.set(pr.id, pr);
} else {
// Merge roles if PR appears multiple times
const existing = prMap.get(pr.id);
existing.isReviewer = existing.isReviewer || pr.isReviewer;
existing.isAssignee = existing.isAssignee || pr.isAssignee;
existing.isAuthor = existing.isAuthor || pr.isAuthor;
}
});
const prs = Array.from(prMap.values());
// Fetch activity data (reviews, CI status) for each PR
await fetchActivityData(settings.token, prs);
// Sort by updated time
prs.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt));
// Detect changes and collect notifications
const notifications = detectChanges(prs, settings);
// Check for closed/merged PRs (PRs that were in our list but are now gone)
const currentPRIds = new Set(prs.map(pr => pr.id));
for (const [prIdStr, state] of Object.entries(prState)) {
const prId = parseInt(prIdStr);
if (!currentPRIds.has(prId) && state.repo) {
const repoNotify = getRepoNotificationSettings(state.repo, settings);
if (repoNotify.status !== false) {
console.log(`PR #${state.number} in ${state.repo}: No longer open (closed/merged)`);
notifications.push({
type: 'status',
pr: {
id: prId,
number: state.number,
title: state.title,
repo: state.repo,
repoOwner: state.repo.split('/')[0],
repoName: state.repo.split('/')[1],
url: state.url
},
status: 'closed',
message: 'PR closed or merged'
});
}
}
}
// Send batched notification if there are updates
console.log(`Notification check: isInitialized=${isInitialized}, notificationsEnabled=${settings.notificationsEnabled}, notifications=${notifications.length}, seenPRIds=${seenPRIds.size}`);
if (isInitialized && settings.notificationsEnabled !== false && notifications.length > 0 && seenPRIds.size > 0) {
await sendBatchedNotification(notifications);
} else if (notifications.length > 0) {
console.log('Notifications detected but not sent:',
!isInitialized ? 'not initialized' :
settings.notificationsEnabled === false ? 'disabled' :
seenPRIds.size === 0 ? 'first run' : 'unknown reason');
}
// Update state
prs.forEach(pr => {
seenPRIds.add(pr.id);
prState[pr.id] = {
commentCount: pr.commentCount,
reviewState: pr.reviewState,
ciStatus: pr.ciStatus,
isDraft: pr.isDraft,
repo: pr.repo,
number: pr.number,
title: pr.title,
url: pr.url
};
});
// Clean up old PR state (PRs no longer in list)
Object.keys(prState).forEach(id => {
if (!currentPRIds.has(parseInt(id))) {
delete prState[id];
}
});
await chrome.storage.local.set({
seenPRIds: Array.from(seenPRIds),
prState
});
// Store PRs
await chrome.storage.local.set({
prs,
lastUpdated: Date.now(),
error: null
});
// Update badge (respecting current filter)
updateBadgeWithCurrentFilter(prs);
console.log(`Found ${prs.length} PRs, ${notifications.length} notifications`);
} catch (error) {
logRequestError('Failed to fetch PRs:', error);
await chrome.storage.local.set({
error: error.message,
lastUpdated: Date.now()
});
updateBadge(-1);
}
}
// Fetch PRs for a single repo
async function fetchPRsForRepo(token, repoFullName, username, includeAuthored) {
const headers = {
'Authorization': `Bearer ${token}`,
'Accept': 'application/vnd.github+json'
};
const [owner, repoName] = repoFullName.split('/');
// Build queries
const queries = [
`type:pr state:open repo:${repoFullName} review-requested:${username}`,
`type:pr state:open repo:${repoFullName} assignee:${username}`
];
if (includeAuthored) {
queries.push(`type:pr state:open repo:${repoFullName} author:${username}`);
}
// Fetch all queries in parallel
const responses = await Promise.all(
queries.map(query =>
fetch(`${GITHUB_API}/search/issues?q=${encodeURIComponent(query)}&per_page=100`, { headers })
)
);
// Check for errors
for (const response of responses) {
if (!response.ok) {
throw new Error(`Failed to fetch PRs from ${repoFullName} (HTTP ${response.status})`);
}
}
const [reviewerData, assigneeData, authorData] = await Promise.all(
responses.map(r => r.json())
);
// Combine and process PRs
const allItems = [
...reviewerData.items,
...assigneeData.items,
...(authorData?.items || [])
];
const prMap = new Map();
for (const pr of allItems) {
if (!prMap.has(pr.id)) {
// Fetch PR details for draft status
const prDetails = await fetchPRDetails(token, pr.pull_request.url);
// Get comment count from PR details (more accurate than search result)
const totalComments = (prDetails.comments || 0) + (prDetails.review_comments || 0);
prMap.set(pr.id, {
id: pr.id,
number: pr.number,
title: pr.title,
url: pr.html_url,
author: pr.user.login,
authorAvatar: pr.user.avatar_url,
isDraft: prDetails.draft || false,
createdAt: pr.created_at,
updatedAt: pr.updated_at,
commentCount: totalComments,
isReviewer: reviewerData.items.some(item => item.id === pr.id),
isAssignee: assigneeData.items.some(item => item.id === pr.id),
isAuthor: authorData?.items?.some(item => item.id === pr.id) || false,
repo: repoFullName,
repoOwner: owner,
repoName: repoName,
// These will be populated by fetchActivityData
reviewState: 'pending',
ciStatus: 'pending',
reviews: [],
headSha: prDetails.head?.sha || null,
// Fallback: mergeable_state from PR details (clean/unstable/blocked/dirty)
mergeableState: prDetails.mergeable_state || null
});
}
}
return Array.from(prMap.values());
}
// Fetch PR details
async function fetchPRDetails(token, prUrl) {
try {
const response = await fetch(prUrl, {
headers: {
'Authorization': `Bearer ${token}`,
'Accept': 'application/vnd.github+json'
}
});
if (!response.ok) {
console.log(`Failed to fetch PR details from ${prUrl}: ${response.status}`);
return { draft: false };
}
const data = await response.json();
console.log(`PR #${data.number} details: comments=${data.comments}, review_comments=${data.review_comments}, mergeable_state=${data.mergeable_state}, mergeable=${data.mergeable}`);
return data;
} catch (error) {
logRequestError('Error fetching PR details:', error);
return { draft: false };
}
}
// Fetch activity data (reviews, CI status) for all PRs
async function fetchActivityData(token, prs) {
const headers = {
'Authorization': `Bearer ${token}`,
'Accept': 'application/vnd.github+json'
};
for (const pr of prs) {
try {
// Fetch reviews
const reviewsResponse = await fetch(
`${GITHUB_API}/repos/${pr.repo}/pulls/${pr.number}/reviews`,
{ headers }
);
if (reviewsResponse.ok) {
const reviews = await reviewsResponse.json();
pr.reviews = reviews;
// Determine overall review state
const latestReviews = {};
reviews.forEach(review => {
if (review.state !== 'COMMENTED' && review.state !== 'PENDING') {
latestReviews[review.user.login] = review.state;
}
});
const states = Object.values(latestReviews);
if (states.includes('CHANGES_REQUESTED')) {
pr.reviewState = 'changes_requested';
} else if (states.includes('APPROVED')) {
pr.reviewState = 'approved';
} else {
pr.reviewState = 'pending';
}
pr.reviewCount = states.length;
pr.approvalCount = states.filter(s => s === 'APPROVED').length;
}
// Fetch CI status if we have a head SHA
// GitHub has two CI systems: Check Runs (newer) and Commit Statuses (older)
// We need to check both for complete coverage
if (pr.headSha) {
let checkRunsStatus = null;
let commitStatus = null;
let totalChecks = 0;
let passedChecks = 0;
// Fetch Workflow Runs (GitHub Actions) - uses Workflows permission
try {
const workflowResponse = await fetch(
`${GITHUB_API}/repos/${pr.repo}/actions/runs?head_sha=${pr.headSha}`,
{ headers }
);
if (workflowResponse.ok) {
const workflowData = await workflowResponse.json();
const workflowRuns = workflowData.workflow_runs || [];
console.log(`PR #${pr.number} workflow-runs:`, workflowRuns.length, workflowRuns.map(r => ({ name: r.name, status: r.status, conclusion: r.conclusion })));
totalChecks += workflowRuns.length;
passedChecks += workflowRuns.filter(r => r.conclusion === 'success').length;
if (workflowRuns.length > 0) {
const hasPending = workflowRuns.some(r =>
r.status === 'in_progress' ||
r.status === 'queued' ||
r.status === 'pending' ||
r.status === 'waiting'
);
const completedRuns = workflowRuns.filter(r => r.status === 'completed');
const hasFailure = completedRuns.some(r =>
r.conclusion === 'failure' ||
r.conclusion === 'timed_out' ||
r.conclusion === 'action_required'
);
const allSuccess = completedRuns.length > 0 && completedRuns.every(r =>
r.conclusion === 'success' ||
r.conclusion === 'skipped' ||
r.conclusion === 'neutral'
);
if (hasFailure) {
checkRunsStatus = 'failure';
} else if (hasPending) {
checkRunsStatus = 'pending';
} else if (allSuccess) {
checkRunsStatus = 'success';
} else if (completedRuns.length === workflowRuns.length) {
// All completed but with other conclusions (cancelled, etc)
checkRunsStatus = 'success';
} else {
checkRunsStatus = 'pending';
}
}
} else {
console.log(`PR #${pr.number} workflow-runs failed:`, workflowResponse.status);
}
} catch (e) {
logRequestError(`PR #${pr.number} workflow-runs error:`, e);
}
// Fetch Commit Status (older API - Jenkins, external CI, etc.)
try {
const statusResponse = await fetch(
`${GITHUB_API}/repos/${pr.repo}/commits/${pr.headSha}/status`,
{ headers }
);
if (statusResponse.ok) {
const statusData = await statusResponse.json();
const statuses = statusData.statuses || [];
console.log(`PR #${pr.number} commit-status:`, statusData.state, statuses.length);
totalChecks += statuses.length;
passedChecks += statuses.filter(s => s.state === 'success').length;
if (statuses.length > 0) {
// Use GitHub's combined state which handles deduplication
commitStatus = statusData.state; // 'success', 'failure', 'pending', 'error'
if (commitStatus === 'error') {
commitStatus = 'failure';
}
}
} else {
console.log(`PR #${pr.number} commit-status failed:`, statusResponse.status);
}
} catch (e) {
logRequestError(`PR #${pr.number} commit-status error:`, e);
}
// Combine both statuses
console.log(`PR #${pr.number} combined: workflowRuns=${checkRunsStatus}, commitStatus=${commitStatus}, mergeableState=${pr.mergeableState}`);
if (checkRunsStatus === 'failure' || commitStatus === 'failure') {
pr.ciStatus = 'failure';
} else if (checkRunsStatus === 'success' || commitStatus === 'success') {
pr.ciStatus = 'success';
} else if (checkRunsStatus === 'pending' || commitStatus === 'pending') {
pr.ciStatus = 'pending';
} else if (checkRunsStatus === null && commitStatus === null) {
// Fallback to mergeable_state from PR details
// clean = all checks passed, unstable = checks failed, blocked = waiting
if (pr.mergeableState === 'clean') {
pr.ciStatus = 'success';
} else if (pr.mergeableState === 'unstable') {
pr.ciStatus = 'failure';
} else if (pr.mergeableState === 'blocked') {
pr.ciStatus = 'pending';
} else if (pr.mergeableState) {
// dirty, unknown, draft, etc. - show as none
pr.ciStatus = 'none';
} else {
pr.ciStatus = 'none';
}
} else {
pr.ciStatus = 'none';
}
pr.ciChecks = totalChecks;
pr.ciPassed = passedChecks;
console.log(`PR #${pr.number} final ciStatus:`, pr.ciStatus);
}
} catch (error) {
logRequestError(`Failed to fetch activity for PR #${pr.number}:`, error);
}
}
}
// Diff stats, reviewers, and latest comment time for the PR list page, in one GraphQL call per page.
// Answers only github.com tabs asking about monitored repos, since this worker holds the token.
async function fetchPRListData(repoFullName, numbers, sender) {
if (!(sender.url ?? sender.tab?.url)?.startsWith('https://github.com/')) {
return { ok: false, error: 'bad-sender' };
}
const settings = await chrome.storage.sync.get(['token', 'repos', 'username']);
if (!settings.token) return { ok: false, error: 'not-configured' };
// URLs aren't case-sensitive on GitHub, so /Acme/Repo/pulls and a stored acme/repo must match
if (!(settings.repos || []).some(r => r.fullName?.toLowerCase() === repoFullName.toLowerCase())) {
return { ok: false, error: 'not-monitored' };
}
const [owner, name] = repoFullName.split('/');
// Numbers are interpolated into GraphQL aliases, so only positive safe integers get through
const prNumbers = [...new Set(numbers.map(Number).filter(n => Number.isSafeInteger(n) && n > 0))];
const prs = {};
// GraphQL caps a query's node count, so batch big lists; a PR list page has 25 rows
for (let i = 0; i < prNumbers.length; i += 100) {
const batch = prNumbers.slice(i, i + 100);
const fields = batch.map(n => `p${n}: pullRequest(number: ${n}) { ...listFields }`).join('\n');
const query = `
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) { ${fields} }
}
fragment listFields on PullRequest {
number additions deletions headRefOid
author { login }
reviewRequests(first: 20) {
nodes { requestedReviewer {
... on User { login avatarUrl }
... on Team { slug avatarUrl }
} }
}
latestReviews(first: 20) { nodes { state commit { oid } author { login avatarUrl } } }
timelineItems(last: 3, itemTypes: [ISSUE_COMMENT, PULL_REQUEST_REVIEW]) {
nodes {
... on IssueComment { createdAt }
... on PullRequestReview { submittedAt }
}
}
}`;
const response = await fetch(`${GITHUB_API}/graphql`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${settings.token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: { owner, name } })
});
if (!response.ok) return { ok: false, error: `http-${response.status}` };
// Partial errors (e.g. a deleted PR number) null out that alias but keep the rest
const { data } = await response.json();
if (!data?.repository) return { ok: false, error: 'repository-unavailable' };
for (const pr of Object.values(data.repository)) {
if (pr) prs[pr.number] = summarizePRForList(pr, owner, settings.username);
}
}
return { ok: true, prs };
}
// Reviewers = pending requests plus everyone who has reviewed, because GitHub drops a reviewer
// from reviewRequests once they submit a review.
function summarizePRForList(pr, owner, username) {
const authorLogin = pr.author?.login;
const reviewers = new Map();
for (const { requestedReviewer: r } of pr.reviewRequests.nodes) {
if (!r) continue;
// Bots and mannequins match neither fragment and come back as {}
if (r.slug) {
reviewers.set(`team:${r.slug}`, { login: r.slug, avatarUrl: r.avatarUrl, isTeam: true, state: 'PENDING', url: `https://github.com/orgs/${owner}/teams/${r.slug}` });
} else if (r.login) {
reviewers.set(r.login, { login: r.login, avatarUrl: r.avatarUrl, isTeam: false, state: 'PENDING', url: `https://github.com/${r.login}` });
}
}
for (const review of pr.latestReviews.nodes) {
const login = review.author?.login;
if (!login || login === authorLogin || reviewers.has(login)) continue;
reviewers.set(login, { login, avatarUrl: review.author.avatarUrl, isTeam: false, state: review.state, url: `https://github.com/${login}` });
}
const commentTimes = pr.timelineItems.nodes
.map(item => item.createdAt || item.submittedAt)
.filter(Boolean)
.sort();
return {
additions: pr.additions,
deletions: pr.deletions,
reviewers: [...reviewers.values()],
needsMyReview: needsMyReview(pr, username),
lastCommentAt: commentTimes.at(-1) || null
};
}
// I'm a pending reviewer, or my latest review asked for changes or commented and commits have
// landed since. Approvals don't count; a rebase or force-push does, as the head commit changes.
function needsMyReview(pr, username) {
if (!username) return false;
const me = username.toLowerCase();
if (pr.author?.login?.toLowerCase() === me) return false;
if (pr.reviewRequests.nodes.some(n => n.requestedReviewer?.login?.toLowerCase() === me)) return true;
const myReview = pr.latestReviews.nodes.find(r => r.author?.login?.toLowerCase() === me);
return ['CHANGES_REQUESTED', 'COMMENTED'].includes(myReview?.state) &&
Boolean(myReview.commit?.oid) && myReview.commit.oid !== pr.headRefOid;
}
// Get notification settings for a specific repo
function getRepoNotificationSettings(repoFullName, settings) {
const repos = settings.repos || [];
const repo = repos.find(r => r.fullName === repoFullName);
if (repo && repo.notifications) {
return repo.notifications;
}
// Fall back to default settings
return {
newPRs: true,
comments: true,
reviews: true,
ciFailure: true,
ciSuccess: true,
readyToMerge: true,
status: true
};
}
// Detect changes and generate notifications
function detectChanges(prs, settings) {
const notifications = [];
for (const pr of prs) {
const oldState = prState[pr.id];
const isNew = !seenPRIds.has(pr.id);
// Get per-repo notification settings
const repoNotify = getRepoNotificationSettings(pr.repo, settings);
// New PR
if (isNew && repoNotify.newPRs) {
notifications.push({
type: 'new_pr',
pr,
message: `New PR: ${pr.title}`
});
continue; // Don't check for other changes on new PRs
}
// If we don't have old state, we can't detect changes - skip but don't treat as new
if (!oldState) {
console.log(`No old state for PR #${pr.number}, skipping change detection`);
continue;
}
// New comments - ensure we compare numbers
const oldComments = oldState.commentCount || 0;
const newComments = pr.commentCount || 0;
console.log(`PR #${pr.number} comment check: old=${oldComments}, new=${newComments}, repoNotify.comments=${repoNotify.comments}`);
if (repoNotify.comments && newComments > oldComments) {
const commentDiff = newComments - oldComments;
console.log(`PR #${pr.number}: ${commentDiff} new comment(s) detected (${oldComments} → ${newComments})`);
notifications.push({
type: 'comment',
pr,
count: commentDiff,
message: `${commentDiff} new comment${commentDiff > 1 ? 's' : ''}`
});
}
// Review state changed
if (repoNotify.reviews &&
oldState.reviewState &&
pr.reviewState !== oldState.reviewState) {
if (pr.reviewState === 'approved') {
console.log(`PR #${pr.number}: Review approved`);
notifications.push({
type: 'review',
pr,
state: 'approved',
message: 'PR approved'
});
} else if (pr.reviewState === 'changes_requested') {
console.log(`PR #${pr.number}: Changes requested`);
notifications.push({
type: 'review',
pr,
state: 'changes_requested',
message: 'Changes requested'
});
}
}
// CI failure
if (repoNotify.ciFailure &&
pr.ciStatus === 'failure' &&
oldState.ciStatus !== 'failure') {
console.log(`PR #${pr.number}: CI failed`);
notifications.push({
type: 'ci_failure',
pr,
message: 'CI failed'
});
}
// CI success
if (repoNotify.ciSuccess !== false &&
pr.ciStatus === 'success' &&
oldState.ciStatus !== 'success' &&
oldState.ciStatus !== undefined) {
console.log(`PR #${pr.number}: CI passed`);
notifications.push({
type: 'ci_success',
pr,
message: 'CI passed'
});
}
// Ready to merge (CI passed AND approved)
if (repoNotify.readyToMerge !== false &&
pr.ciStatus === 'success' &&
pr.reviewState === 'approved' &&
!pr.isDraft) {
// Check if this is newly ready (either CI just passed or review just approved)
const wasReady = oldState.ciStatus === 'success' &&
oldState.reviewState === 'approved' &&
oldState.isDraft === false;
if (!wasReady && oldState.ciStatus !== undefined) {
console.log(`PR #${pr.number}: Ready to merge`);
notifications.push({
type: 'ready_to_merge',
pr,
message: 'Ready to merge'
});
}
}
// Status changes (draft <-> ready)
if (repoNotify.status !== false && oldState.isDraft !== undefined) {
if (oldState.isDraft && !pr.isDraft) {
// Draft -> Ready for review
console.log(`PR #${pr.number}: Now ready for review`);
notifications.push({
type: 'status',
pr,
status: 'ready',
message: 'PR ready for review'
});
} else if (!oldState.isDraft && pr.isDraft) {
// Ready -> Draft (converted back to draft)
console.log(`PR #${pr.number}: Converted to draft`);
notifications.push({
type: 'status',
pr,
status: 'draft',
message: 'PR converted to draft'
});
}
}
}
console.log(`Detected ${notifications.length} notification(s)`);
return notifications;
}
// Store last notification data for click handling
let lastNotificationData = null;
// Send a single batched notification
async function sendBatchedNotification(notifications) {
if (notifications.length === 0) return;
// Count by type
const counts = {
new_pr: 0,
comment: 0,
review: 0,
ci_failure: 0,
status: 0
};
let totalComments = 0;
notifications.forEach(n => {
counts[n.type]++;
if (n.type === 'comment') {
totalComments += n.count;
}
});
// Build notification message
const parts = [];
if (counts.new_pr > 0) parts.push(`${counts.new_pr} new PR${counts.new_pr > 1 ? 's' : ''}`);
if (totalComments > 0) parts.push(`${totalComments} comment${totalComments > 1 ? 's' : ''}`);
if (counts.review > 0) parts.push(`${counts.review} review${counts.review > 1 ? 's' : ''}`);
if (counts.ci_failure > 0) parts.push(`${counts.ci_failure} CI failure${counts.ci_failure > 1 ? 's' : ''}`);
if (counts.ci_success > 0) parts.push(`${counts.ci_success} CI passed`);
if (counts.ready_to_merge > 0) parts.push(`${counts.ready_to_merge} ready to merge`);
if (counts.status > 0) parts.push(`${counts.status} status change${counts.status > 1 ? 's' : ''}`);
// Get unique PRs affected
const affectedPRs = [...new Map(notifications.map(n => [n.pr.id, n.pr])).values()];
// Store for click handling
lastNotificationData = {
notifications,
affectedPRs,
singlePR: affectedPRs.length === 1 ? affectedPRs[0] : null
};
// Store highlighted PRs for popup display
const highlightedPRs = {};
notifications.forEach(n => {
if (!highlightedPRs[n.pr.id]) {
highlightedPRs[n.pr.id] = [];
}
highlightedPRs[n.pr.id].push(n.type);
});
await chrome.storage.local.set({ highlightedPRs });
const totalUpdates = notifications.length;
let title, message;
if (affectedPRs.length === 1) {
// Single PR - show detailed event info
const pr = affectedPRs[0];
const n = notifications[0];
title = `${pr.repoOwner}/${pr.repoName}#${pr.number}`;
// Build detailed message based on event type
switch (n.type) {
case 'new_pr':
message = `New PR by @${pr.author}: ${pr.title}`;
break;
case 'comment':
message = `${n.count} new comment${n.count > 1 ? 's' : ''} on "${pr.title}"`;
break;
case 'review':
if (n.state === 'approved') {
message = `✓ PR approved: ${pr.title}`;
} else {
message = `⚠ Changes requested: ${pr.title}`;
}
break;
case 'ci_failure':
message = `✗ CI failed: ${pr.title}`;
break;
case 'ci_success':
message = `✓ CI passed: ${pr.title}`;
break;
case 'ready_to_merge':
message = `🚀 Ready to merge: ${pr.title}`;
break;
case 'status':
if (n.status === 'ready') {
message = `▶ Ready for review: ${pr.title}`;
} else if (n.status === 'draft') {
message = `◼ Converted to draft: ${pr.title}`;
} else if (n.status === 'closed') {
message = `✓ Closed/Merged: ${pr.title}`;
} else {
message = `Status changed: ${pr.title}`;
}
break;
default:
message = pr.title;
}
} else {
// Multiple PRs
title = `${totalUpdates} PR update${totalUpdates > 1 ? 's' : ''}`;
message = parts.join(' • ');
}
console.log(`Sending notification: "${title}" - "${message}"`);
try {
const notificationId = await chrome.notifications.create('pr-updates', {
type: 'basic',
iconUrl: 'icons/icon128.png',
title,
message,
priority: 2
});
console.log(`Notification created with ID: ${notificationId}`);
} catch (error) {
console.error('Failed to create notification:', error);
}
}
// Update extension badge
async function updateBadge(count) {
if (count < 0) {
await chrome.action.setBadgeText({ text: '!' });
await chrome.action.setBadgeBackgroundColor({ color: '#f85149' });
await chrome.action.setBadgeTextColor({ color: '#ffffff' });
} else if (count === 0) {
await chrome.action.setBadgeText({ text: '' });
} else {
await chrome.action.setBadgeText({ text: count.toString() });
await chrome.action.setBadgeBackgroundColor({ color: '#cf222e' });
await chrome.action.setBadgeTextColor({ color: '#ffffff' });
}
}
// Update badge with filter applied
async function updateBadgeWithFilter(filter) {
const { prs } = await chrome.storage.local.get('prs');
if (!prs || !Array.isArray(prs)) {
updateBadge(0);
return;
}
let filteredPRs = prs;
if (filter === 'mine') {
filteredPRs = prs.filter(pr => pr.isAuthor);
} else if (filter === 'others') {
filteredPRs = prs.filter(pr => !pr.isAuthor);
}
updateBadge(filteredPRs.length);
}
// Update badge respecting current filter
async function updateBadgeWithCurrentFilter(prs) {
const { prFilter } = await chrome.storage.local.get('prFilter');
const filter = prFilter || 'all';
let filteredPRs = prs;
if (filter === 'mine') {
filteredPRs = prs.filter(pr => pr.isAuthor);
} else if (filter === 'others') {
filteredPRs = prs.filter(pr => !pr.isAuthor);
}
updateBadge(filteredPRs.length);
}
// Handle notification click
chrome.notifications.onClicked.addListener(async (notificationId) => {
if (notificationId === 'pr-updates' && lastNotificationData) {
if (lastNotificationData.singlePR) {
// Single PR - open it directly
await chrome.tabs.create({ url: lastNotificationData.singlePR.url });
// Clear highlights since user is going directly to PR
await chrome.storage.local.remove('highlightedPRs');
} else {
// Multiple PRs - open popup in a new tab
const popupUrl = chrome.runtime.getURL('popup/popup.html?from=notification');
await chrome.tabs.create({ url: popupUrl });
}
}
chrome.notifications.clear(notificationId);
});
// Initialize when service worker loads
initializeExtension();