forked from mozilla-necko/triage-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triage.js
144 lines (119 loc) · 3.99 KB
/
triage.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
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
const fs = require('fs');
const help_string =
`Usage: npm run <command> -- [arguments]
commands
* update: automatically extends the json calendar with triagers using the default list, adds only one cycle
* exempt -- <date>: removes a triager on duty at the specified <date> from the calendar and shifts all triagers
* prepush: updates the ICS generated file with data from the json calendar (called automatically as part of push)
* push: pushes the changes to the repo and makes them public, refreshes the ics file as well`;
const TRIAGE_JSON_FILE = "general-triage.json";
const TRIAGE_ICAL_FILE = "general-triage.ics";
const CYCLE_LENGTH_DAYS = 28;
const DAY_TO_MS = 24 * 60 * 60 * 1000;
const CYCLE_LENGTH_MS = CYCLE_LENGTH_DAYS * DAY_TO_MS;
function readTriage() {
let data = fs.readFileSync(TRIAGE_JSON_FILE);
let triage = JSON.parse(data);
return {
triage,
triagers: triage.triagers,
duties: triage["duty-start-dates"]
};
}
function writeTriage(json) {
let data = JSON.stringify(json, undefined, " ");
fs.writeFileSync(TRIAGE_JSON_FILE, data);
}
function nextTriager(triagers, current_triager) {
let names = Object.keys(triagers);
let index = names.findIndex(t => t === current_triager);
if (index < 0) {
throw `\n**** FATAL: duty-start-dates refers an unexisting triager '${current_triager}'\n`;
}
++index;
if (index >= names.length) {
index = 0;
}
return names[index];
}
function commandUpdate() {
let { triage, triagers, duties } = readTriage();
let last_date = Object.keys(duties).sort().slice(-1);
if (!last_date) {
throw "\n**** UNEXPECTED: The duty calendar is empty\n";
}
[last_date] = last_date;
let last_triager = duties[last_date];
let next_triager = last_triager;
let next_date_ms = new Date(last_date).getTime();
do {
next_date_ms += CYCLE_LENGTH_MS;
next_triager = nextTriager(triagers, next_triager);
let date = new Date(next_date_ms).toISOString().replace(/T.*$/, "");
duties[date] = next_triager;
console.log(`Added: from ${date} duty ${next_triager}`);
} while (next_triager !== last_triager);
writeTriage(triage);
console.log("\nDon't forget to run 'npm run push' to publish the changes\n");
}
function commandPrepush() {
const { triage, triagers, duties } = readTriage();
const ical = require('ical-toolkit');
let builder = ical.createIcsFileBuilder();
builder.calname = "Necko Triage";
builder.timezone = "Europe/Dublin";
builder.tzid = "Europe/Dublin";
builder.additionalTags = {
'REFRESH-INTERVAL': 'VALUE=DURATION:P1H',
'X-WR-CALDESC': 'Necko Triage'
};
for (let duty_date in duties) {
let duty_triager = duties[duty_date];
let duty_date_ms = new Date(duty_date).getTime();
builder.events.push({
start: new Date(duty_date_ms),
end: new Date(duty_date_ms + CYCLE_LENGTH_MS),
summary: `Necko triager: ${duty_triager}`,
allDay: true,
});
}
let data = builder.toString();
fs.writeFileSync(TRIAGE_ICAL_FILE, data);
console.log(`Updated ${TRIAGE_ICAL_FILE}`);
}
function commandExempt(exempt_date) {
let { triage, triagers, duties } = readTriage();
if (!(exempt_date in duties)) {
throw `\n**** ERROR: The start date '${exempt_date}' not found in the duty list\n`;
}
console.log(`Removing '${duties[exempt_date]}' on duty from ${exempt_date} from the duty list`);
exempt_date = new Date(exempt_date);
let dates_to_shift = Object.keys(duties).filter(date => {
return new Date(date).getTime() >= exempt_date;
}).sort();
let date;
while (date = dates_to_shift.shift()) {
let next_date = dates_to_shift[0];
if (!next_date) {
delete duties[date];
} else {
duties[date] = duties[next_date];
}
}
writeTriage(triage);
}
let args = process.argv.slice(2);
let command = args.shift();
switch (command) {
case "update":
commandUpdate();
break;
case "exempt":
commandExempt(args[0]);
break;
case "prepush":
commandPrepush();
break;
default:
console.log(help_string);
}