2019-08-13 23:36:21 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const ical = require('ical-toolkit');
|
2019-08-25 01:05:08 +03:00
|
|
|
const ghpages = require('gh-pages');
|
2020-12-22 04:44:08 +03:00
|
|
|
const fetch = require('node-fetch');
|
2019-08-13 23:36:21 +03:00
|
|
|
|
2020-12-22 04:44:08 +03:00
|
|
|
const PUBLISHED_URL = 'https://mozilla.github.io/layout-triage';
|
2019-08-24 01:26:52 +03:00
|
|
|
const DIST_DIR = 'dist';
|
2019-08-13 23:36:21 +03:00
|
|
|
const CONFIG_FILE = 'config.json';
|
|
|
|
const HISTORY_FILE = 'history.json';
|
2019-08-24 01:26:52 +03:00
|
|
|
const TRIAGERS_KEY = 'triagers';
|
2019-08-24 02:00:38 +03:00
|
|
|
const ICAL_FILE = 'layout-triage.ics';
|
2019-08-24 01:26:52 +03:00
|
|
|
const INDENT = ' ';
|
|
|
|
const DUTY_START_DATES_KEY = 'duty-start-dates';
|
2019-08-13 23:36:21 +03:00
|
|
|
const CYCLE_LENGTH_DAYS = 7;
|
|
|
|
const DAY_TO_MS = 24 * 60 * 60 * 1000;
|
|
|
|
const CYCLE_LENGTH_MS = CYCLE_LENGTH_DAYS * DAY_TO_MS;
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
/**
|
|
|
|
* Return the parsed results from the config file. Reads file synchronously.
|
|
|
|
*/
|
2019-08-13 23:36:21 +03:00
|
|
|
function readConfig() {
|
2019-08-24 01:26:52 +03:00
|
|
|
return JSON.parse(fs.readFileSync(CONFIG_FILE));
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
/**
|
2019-08-26 00:21:07 +03:00
|
|
|
* Write the given JSON object to the history file. Writes synchronously.
|
2019-08-24 01:26:52 +03:00
|
|
|
* @param {*} json
|
|
|
|
*/
|
|
|
|
function writeToHistory(json) {
|
|
|
|
const data = JSON.stringify(json, undefined, INDENT);
|
2020-12-22 03:01:18 +03:00
|
|
|
fs.writeFileSync(`${DIST_DIR}/${HISTORY_FILE}`, data);
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
/**
|
|
|
|
* Given a date, return the date of the Monday preceding it.
|
|
|
|
* @param {Date} date
|
2019-08-26 00:21:07 +03:00
|
|
|
* @returns
|
2019-08-24 01:26:52 +03:00
|
|
|
*/
|
|
|
|
function getLastMonday(date) {
|
2019-08-25 07:50:05 +03:00
|
|
|
const day = date.getDay() || 7;
|
2021-01-20 01:05:48 +03:00
|
|
|
|
|
|
|
let newDate = new Date(date);
|
2019-08-25 07:50:05 +03:00
|
|
|
if (day !== 1) {
|
2021-01-20 01:05:48 +03:00
|
|
|
newDate.setHours(-24 * (day - 1));
|
2019-08-25 07:50:05 +03:00
|
|
|
}
|
2019-08-24 01:26:52 +03:00
|
|
|
|
2021-01-20 01:05:48 +03:00
|
|
|
return newDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a date, return the date of the Monday following it.
|
|
|
|
*/
|
|
|
|
function getNextMonday(date) {
|
|
|
|
let newDate = getLastMonday(date);
|
|
|
|
newDate.setHours(24 * 7);
|
|
|
|
return newDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a date as YYYY-MM-DD.
|
|
|
|
*/
|
|
|
|
function createDateString(date) {
|
|
|
|
return date.toISOString().replace(/T.*$/, '');
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
function appendDutyCycle({ component, date, triagerName, triagerData }) {
|
|
|
|
const filePath = `${DIST_DIR}/${component}.json`;
|
2020-05-06 00:28:27 +03:00
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
fs.writeFileSync(filePath, '{"triagers":{}, "duty-start-dates":{}}');
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
let data = fs.readFileSync(filePath);
|
|
|
|
const calendar = JSON.parse(data);
|
|
|
|
const triagers = calendar[TRIAGERS_KEY];
|
|
|
|
const dutyStartDates = calendar[DUTY_START_DATES_KEY];
|
|
|
|
if (!dutyStartDates || !triagers) {
|
|
|
|
throw `\nFATAL ERROR: Invalid data in calendar ${component}.json`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!triagers[triagerName]) {
|
|
|
|
triagers[triagerName] = triagerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
dutyStartDates[date] = triagerName;
|
|
|
|
|
2021-01-20 01:05:48 +03:00
|
|
|
data = JSON.stringify(calendar, undefined, INDENT);
|
2019-08-24 01:26:52 +03:00
|
|
|
fs.writeFileSync(filePath, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an array, select n random items from the array,
|
|
|
|
*
|
|
|
|
* @param {} arr The array from which to select items.
|
|
|
|
* @param {*} n The number of items to select.
|
|
|
|
*/
|
2019-08-13 23:36:21 +03:00
|
|
|
function selectRandom(arr, n) {
|
|
|
|
const result = [];
|
|
|
|
const taken = [];
|
|
|
|
let len = arr.length;
|
|
|
|
|
|
|
|
if (n > len) {
|
|
|
|
throw `FATAL ERROR: Cannot select ${n} items from array with length ${arr.length}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
let idx = Math.floor(Math.random() * len);
|
|
|
|
result[n] = arr[idx in taken ? taken[idx] : idx];
|
|
|
|
taken[idx] = --len in taken ? taken[len] : len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
/**
|
|
|
|
* Given a duty cycle history object, return the most recent cycle.
|
|
|
|
*
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params.dutyCycleHistory The duty cycle history as formatted in the history file.
|
|
|
|
*/
|
|
|
|
function getLastDutyCycle({ dutyCycleHistory }) {
|
2019-08-13 23:36:21 +03:00
|
|
|
const dutyDates = Object.keys(dutyCycleHistory).sort();
|
|
|
|
if (dutyDates.length < 1) {
|
2019-08-24 01:26:52 +03:00
|
|
|
return {};
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const lastDutyDate = dutyDates.slice(-1)[0];
|
2019-08-24 01:26:52 +03:00
|
|
|
if (!dutyCycleHistory[lastDutyDate]) {
|
|
|
|
throw `\nFATAL ERROR: Invalid data in history file!`;
|
|
|
|
}
|
2019-08-13 23:36:21 +03:00
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
const lastTriagePair = Object.keys(dutyCycleHistory[lastDutyDate]);
|
|
|
|
return {
|
|
|
|
lastDutyDate,
|
|
|
|
lastTriagePair
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
2019-08-24 01:26:52 +03:00
|
|
|
}
|
2019-08-13 23:36:21 +03:00
|
|
|
|
2019-08-26 00:21:07 +03:00
|
|
|
function generateBugzillaUrl(componentNames) {
|
|
|
|
const prefix = 'https://bugzilla.mozilla.org/buglist.cgi?' +
|
2020-05-06 00:28:27 +03:00
|
|
|
'bug_severity=--' +
|
2019-08-26 00:21:07 +03:00
|
|
|
'&f1=short_desc' +
|
|
|
|
'&bug_type=defect' +
|
|
|
|
'&o1=notsubstring' +
|
|
|
|
'&resolution=---' +
|
2020-05-06 00:28:27 +03:00
|
|
|
'&classification=Client%20Software' +
|
|
|
|
'&classification=Developer%20Infrastructure' +
|
|
|
|
'&classification=Components' +
|
|
|
|
'&classification=Server%20Software' +
|
|
|
|
'&classification=Other' +
|
|
|
|
'&query_format=advanced' +
|
|
|
|
'&chfield=%5BBug%20creation%5D' +
|
|
|
|
'&chfieldfrom=-60d' +
|
|
|
|
'&v1=%5Bmeta%5D' +
|
|
|
|
'&product=Core';
|
2019-08-26 00:21:07 +03:00
|
|
|
return prefix + '&' + componentNames.map(name => `component=${encodeURIComponent(name)}`).join('&')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params.dutyCycleHistory
|
|
|
|
*/
|
|
|
|
function generateIcsFile({ dutyCycleHistory, components }) {
|
2019-08-24 02:00:38 +03:00
|
|
|
const builder = ical.createIcsFileBuilder();
|
|
|
|
|
|
|
|
builder.calname = 'Layout Triage';
|
|
|
|
builder.timezone = 'America/Los_Angeles';
|
|
|
|
builder.tzid = 'America/Los_Angeles';
|
|
|
|
builder.additionalTags = {
|
|
|
|
'REFRESH-INTERVAL': 'VALUE=DURATION:P1H',
|
|
|
|
'X-WR-CALDESC': 'Layout Triage'
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let dutyCycleDate in dutyCycleHistory) {
|
2019-08-25 07:50:05 +03:00
|
|
|
const dutyCycle = dutyCycleHistory[dutyCycleDate];
|
|
|
|
const triagerNames = Object.keys(dutyCycle);
|
2019-08-24 02:00:38 +03:00
|
|
|
const dutyCycleDateMs = new Date(dutyCycleDate).getTime();
|
|
|
|
|
2019-08-26 00:21:07 +03:00
|
|
|
const triager0Components = Array.prototype.concat.apply([], dutyCycle[triagerNames[0]].map(component => components[component]));
|
|
|
|
const triager1Components = Array.prototype.concat.apply([], dutyCycle[triagerNames[1]].map(component => components[component]));
|
2019-08-25 07:50:05 +03:00
|
|
|
|
2019-08-24 02:00:38 +03:00
|
|
|
builder.events.push({
|
|
|
|
start: new Date(dutyCycleDateMs),
|
|
|
|
end: new Date(dutyCycleDateMs + CYCLE_LENGTH_MS),
|
2019-08-25 07:50:05 +03:00
|
|
|
summary: `Triage Duty: ${triagerNames.join(', ')}`,
|
2019-08-26 00:21:07 +03:00
|
|
|
allDay: true,
|
|
|
|
transp: 'TRANSPARENT',
|
|
|
|
description: `<strong>${triagerNames[0]}:</strong> ` +
|
|
|
|
`(<a href="${generateBugzillaUrl(triager0Components)}">Bugzilla Query</a>)` +
|
|
|
|
`<ul>${triager0Components.map(c => `<li>${c}</li>`).join('')}</ul><br>` +
|
|
|
|
`<strong>${triagerNames[1]}:</strong> ` +
|
|
|
|
`(<a href="${generateBugzillaUrl(triager1Components)}">Bugzilla Query</a>)` +
|
|
|
|
`<ul>${triager1Components.map(c => `<li>${c}</li>`).join('')}</ul>`
|
2019-08-24 02:00:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = builder.toString();
|
|
|
|
fs.writeFileSync(`${DIST_DIR}/${ICAL_FILE}`, data);
|
|
|
|
}
|
|
|
|
|
2019-08-25 07:50:05 +03:00
|
|
|
function generateDutyCycle({ dutyCycleHistory, triagers, components }) {
|
2019-08-24 01:26:52 +03:00
|
|
|
let { lastDutyDate, lastTriagePair } = getLastDutyCycle({ dutyCycleHistory })
|
|
|
|
let lastTriagerIdx = -1;
|
2019-08-25 07:50:05 +03:00
|
|
|
const triagerNames = Object.keys(triagers);
|
|
|
|
const componentNames = Object.keys(components);
|
2019-08-24 01:26:52 +03:00
|
|
|
|
|
|
|
if (!lastDutyDate || !Array.isArray(lastTriagePair) || lastTriagePair.length !== 2) {
|
|
|
|
console.warn('No existing duty cycle history. Generating first cycle.');
|
|
|
|
lastDutyDate = createDateString(getLastMonday(new Date()));
|
|
|
|
} else {
|
2019-08-25 07:50:05 +03:00
|
|
|
lastTriagerIdx = triagerNames.indexOf(lastTriagePair[1]);
|
2019-08-24 01:26:52 +03:00
|
|
|
if (lastTriagerIdx === -1) {
|
|
|
|
console.warn(`Unable to find triager named ${lastTriagePair[1]} in config. Starting over from first triager.`);
|
|
|
|
}
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-25 07:50:05 +03:00
|
|
|
const nextTriagerIdx = (lastTriagerIdx + 1) % triagerNames.length;
|
2019-08-13 23:36:21 +03:00
|
|
|
const nextDutyDateMS = new Date(lastDutyDate).getTime() + CYCLE_LENGTH_MS;
|
2019-08-25 07:50:05 +03:00
|
|
|
const nextTriagePair = [triagerNames[nextTriagerIdx], triagerNames[(nextTriagerIdx +1 ) % triagerNames.length]];
|
2019-08-24 01:26:52 +03:00
|
|
|
const nextDutyDate = createDateString(new Date(nextDutyDateMS));
|
2019-08-25 07:50:05 +03:00
|
|
|
const firstComponentSet = selectRandom(componentNames, Math.floor(componentNames.length / 2));
|
|
|
|
const secondComponentSet = componentNames.filter(c => firstComponentSet.indexOf(c) === -1);
|
2019-08-13 23:36:21 +03:00
|
|
|
const dutyCycle = {};
|
|
|
|
dutyCycle[nextTriagePair[0]] = firstComponentSet;
|
|
|
|
dutyCycle[nextTriagePair[1]] = secondComponentSet;
|
|
|
|
|
|
|
|
return {
|
|
|
|
date: nextDutyDate,
|
|
|
|
dutyCycle
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
function runUpdate() {
|
2019-08-25 07:50:05 +03:00
|
|
|
const { triagers, components } = readConfig();
|
2020-12-22 03:01:18 +03:00
|
|
|
const { dutyCycleHistory } = JSON.parse(fs.readFileSync(`${DIST_DIR}/${HISTORY_FILE}`));
|
2019-08-25 07:50:05 +03:00
|
|
|
const { date, dutyCycle } = generateDutyCycle({ dutyCycleHistory, triagers, components });
|
2019-08-13 23:36:21 +03:00
|
|
|
|
2019-08-24 02:00:38 +03:00
|
|
|
function updateJSONCalendars() {
|
|
|
|
const newDutyCycleTriagers = Object.keys(dutyCycle);
|
2019-08-25 07:50:05 +03:00
|
|
|
newDutyCycleTriagers.forEach(triagerName => {
|
|
|
|
const components = dutyCycle[triagerName];
|
2019-08-24 02:00:38 +03:00
|
|
|
components.forEach(component => {
|
2019-08-25 07:50:05 +03:00
|
|
|
appendDutyCycle({ component, date, triagerName, triagerData: triagers[triagerName] });
|
2019-08-24 02:00:38 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:36:21 +03:00
|
|
|
dutyCycleHistory[date] = dutyCycle;
|
2019-08-24 01:26:52 +03:00
|
|
|
|
2019-08-24 02:00:38 +03:00
|
|
|
updateJSONCalendars();
|
|
|
|
writeToHistory({ dutyCycleHistory });
|
2019-08-26 00:21:07 +03:00
|
|
|
generateIcsFile({ dutyCycleHistory, components });
|
2019-08-24 01:26:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all existing data, leaving one duty cycle in the history to serve as a
|
|
|
|
* seed for the next cycle.
|
|
|
|
*/
|
|
|
|
function runReset() {
|
|
|
|
const { components } = readConfig();
|
|
|
|
const resetData = {};
|
|
|
|
resetData[TRIAGERS_KEY] = {};
|
|
|
|
resetData[DUTY_START_DATES_KEY] = {};
|
|
|
|
const resetDataString = JSON.stringify(resetData, undefined, INDENT);
|
|
|
|
|
2019-08-25 07:50:05 +03:00
|
|
|
Object.keys(components).forEach(component => {
|
2019-08-24 01:26:52 +03:00
|
|
|
const filePath = `${DIST_DIR}/${component}.json`;
|
|
|
|
fs.writeFileSync(filePath, resetDataString);
|
|
|
|
});
|
|
|
|
|
|
|
|
writeToHistory({ dutyCycleHistory: {} });
|
2019-08-26 00:21:07 +03:00
|
|
|
generateIcsFile({ dutyCycleHistory: {} });
|
2019-08-13 23:36:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-25 01:05:08 +03:00
|
|
|
function runPublish() {
|
|
|
|
ghpages.publish(DIST_DIR, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error('There was an error during publishing.');
|
|
|
|
} else {
|
|
|
|
console.log('Publish to GitHub was successful.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-22 04:44:08 +03:00
|
|
|
async function runInit() {
|
|
|
|
const { components } = readConfig();
|
|
|
|
|
|
|
|
if (fs.existsSync(DIST_DIR)) {
|
|
|
|
console.error('Cannot initialize while dist/ directory exists.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fs.mkdirSync(DIST_DIR);
|
|
|
|
|
|
|
|
const filenames = [
|
|
|
|
'history.json',
|
|
|
|
...Object.keys(components).map(component => `${component}.json`)
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let filename of filenames) {
|
|
|
|
const url = `${PUBLISHED_URL}/${filename}`;
|
|
|
|
const res = await fetch(url);
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
console.error(`Error fetching ${url}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(`${DIST_DIR}/${filename}`, await res.text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:05:48 +03:00
|
|
|
function runClear() {
|
|
|
|
const { triagers, components } = readConfig();
|
|
|
|
const { dutyCycleHistory } = JSON.parse(fs.readFileSync(`${DIST_DIR}/${HISTORY_FILE}`));
|
|
|
|
const thisWeek = createDateString(getNextMonday(new Date()));
|
|
|
|
|
|
|
|
Object.keys(dutyCycleHistory)
|
|
|
|
.filter(d => d > thisWeek)
|
|
|
|
.forEach(d => delete dutyCycleHistory[d]);
|
|
|
|
|
|
|
|
for (let component in components) {
|
|
|
|
const filePath = `${DIST_DIR}/${component}.json`;
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let calendar = JSON.parse(fs.readFileSync(filePath));
|
|
|
|
Object.keys(calendar[DUTY_START_DATES_KEY])
|
|
|
|
.filter(d => d > thisWeek)
|
|
|
|
.forEach(d => delete calendar[DUTY_START_DATES_KEY][d]);
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, JSON.stringify(calendar, undefined, INDENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
writeToHistory({ dutyCycleHistory });
|
|
|
|
generateIcsFile({ dutyCycleHistory, components });
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:36:21 +03:00
|
|
|
let args = process.argv.slice(2);
|
|
|
|
let command = args.shift();
|
|
|
|
|
2020-12-22 04:44:08 +03:00
|
|
|
if (command != 'init' && !fs.existsSync(DIST_DIR)) {
|
2020-12-22 02:57:45 +03:00
|
|
|
fs.mkdirSync(DIST_DIR);
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:36:21 +03:00
|
|
|
switch (command) {
|
|
|
|
case 'update': {
|
2019-08-24 01:26:52 +03:00
|
|
|
runUpdate();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:05:48 +03:00
|
|
|
case 'clear': {
|
|
|
|
runClear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:26:52 +03:00
|
|
|
case 'reset': {
|
|
|
|
runReset();
|
2019-08-13 23:36:21 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-08-25 01:05:08 +03:00
|
|
|
|
|
|
|
case 'publish': {
|
|
|
|
runPublish();
|
|
|
|
break;
|
|
|
|
}
|
2020-12-22 04:44:08 +03:00
|
|
|
|
|
|
|
case 'init': {
|
|
|
|
runInit();
|
|
|
|
break;
|
|
|
|
}
|
2020-12-22 02:57:45 +03:00
|
|
|
}
|