-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·61 lines (49 loc) · 2.4 KB
/
index.js
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
import { GitHubClient } from "./lib/client.js";
import { NotificationReducer } from "./lib/notification-reducer.js";
const DEFAULT_INTERVAL = 60;
let options = process.argv
.slice(2)
.filter(arg => arg.startsWith("--"))
.map(arg => arg.slice(2))
.reduce((acc, arg) => {
const [option, value] = arg.indexOf("=") === -1 ? [arg, true] : arg.split("=");
return Object.assign(acc, { [option.replaceAll(/-([a-z])/gi, (_match, char) => char.toUpperCase())]: value });
}, {});
console.debug(options);
const github = new GitHubClient(options);
const me = await github.me();
const doWork = async () => {
const notifications = await github.notifications();
const reducer = new NotificationReducer({ notifications, me });
reducer.pullRequests = await github.batchRequests(reducer.pullNotifications.map(n => n.subject.url));
// Case 1: notifications for closed PRs => marking as done
if (options.cleanupClosedPrs) {
const notificationsForClosedPRs = reducer.notificationsForClosedPRs;
console.debug("%d notifications for closed PRs, marking as done…", notificationsForClosedPRs.length);
notificationsForClosedPRs.forEach(notification => console.debug(notification.pull_request.html_url));
await github.markAsDone(notificationsForClosedPRs);
}
// Case 2: subscribed but someone else already assigned
if (options.cleanupReassignedPrs) {
const someoneElseAssigned = reducer.notificationsForReassignedPRs;
console.debug("%d notifications for PRs assigned to someone else, unsubscribing…", someoneElseAssigned.length);
someoneElseAssigned.forEach(notification => console.debug(notification.pull_request.html_url));
await github.unsubscribe(someoneElseAssigned);
}
// Case 3: review requested but no reviews pending
if (options.cleanupReviewedPrs) {
const reviewRequestedAndReviewed = reducer.notificationsForReviewedPRs;
console.debug("%d notifications for PRs requesting and gotten reviews, unsubscribing…", reviewRequestedAndReviewed.length);
await github.unsubscribe(reviewRequestedAndReviewed);
}
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
while(true) {
if (await github.hasNewNotifications()) {
await doWork();
} else {
console.debug("No new notifications");
}
const interval = github.pollInterval || DEFAULT_INTERVAL;
await delay(interval * 1000);
}