-
Notifications
You must be signed in to change notification settings - Fork 0
/
codefreeze.js
77 lines (65 loc) · 3.53 KB
/
codefreeze.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
'use strict';
const moment = require('moment-timezone');
const validate = require('./validate');
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const timezone = process.env.CODE_FREEZE_TIMEZONE || 'America/Los_Angeles';
const codefreeze = maybeNowOverride => {
validate();
const codeFreezeErrorMessage = (now, begin, end) => `Code freeze in effect: Today (${now.format('MM/DD hh:mm A')}) is between ${begin.format('MM/DD hh:00 A')} and ${end.format('MM/DD hh:00 A')}`;
const codeFreezeOverrideFrozen = process.env.CODE_FREEZE_OVERRIDE === 'frozen';
const codeFreezeOverrideUnfrozen = process.env.CODE_FREEZE_OVERRIDE === 'unfrozen';
const codeFreezeBiWeekly = process.env.CODE_FREEZE_BI_WEEKLY === 'true';
const codeFreezeBiWeeklyOdd = process.env.CODE_FREEZE_BI_WEEKLY_WEEK === 'odd';
const codeFreezeBiWeeklyEven = !codeFreezeBiWeeklyOdd;
const codeFreezeBeginDay = days.indexOf(process.env.CODE_FREEZE_DAY_BEGIN);
const codeFreezeBeginHour = parseInt(process.env.CODE_FREEZE_HOUR_BEGIN, 10);
const codeFreezeEndDay = days.indexOf(process.env.CODE_FREEZE_DAY_END);
const codeFreezeEndHour = parseInt(process.env.CODE_FREEZE_HOUR_END, 10);
const now = moment(maybeNowOverride).tz(timezone);
const codeFreezeBeginMoment = moment(maybeNowOverride).tz(timezone).day(codeFreezeBeginDay).hour(codeFreezeBeginHour);
const codeFreezeBeginMomentLastWeek = moment(codeFreezeBeginMoment).subtract(1, 'weeks');
const codeFreezeEndMoment = moment(maybeNowOverride).tz(timezone).day(codeFreezeEndDay).hour(codeFreezeEndHour);
const codeFreezeEndMomentNextWeek = moment(codeFreezeEndMoment).add(1, 'weeks');
const crossesWeekend = codeFreezeBeginDay > codeFreezeEndDay;
if (codeFreezeOverrideFrozen) {
throw new Error('Code freeze full override in effect. All merges frozen.');
}
if (codeFreezeOverrideUnfrozen) {
return;
}
if (codeFreezeBiWeekly) {
const isWeekOdd = moment(maybeNowOverride).tz(timezone).week() % 2 === 1,
isWeekEven = !isWeekOdd;
if ((codeFreezeBiWeeklyOdd && isWeekEven) || (codeFreezeBiWeeklyEven && isWeekOdd)) {
if (crossesWeekend) {
if (now.isBetween(codeFreezeBeginMomentLastWeek, codeFreezeEndMoment)) {
throw new Error(codeFreezeErrorMessage(now, codeFreezeBeginMomentLastWeek, codeFreezeEndMoment));
}
}
} else {
if (crossesWeekend) {
if (now.isBetween(codeFreezeBeginMoment, codeFreezeEndMomentNextWeek)) {
throw new Error(codeFreezeErrorMessage(now, codeFreezeBeginMoment, codeFreezeEndMomentNextWeek));
}
} else {
if (now.isBetween(codeFreezeBeginMoment, codeFreezeEndMoment)) {
throw new Error(codeFreezeErrorMessage(now, codeFreezeBeginMoment, codeFreezeEndMoment));
}
}
}
} else {
if (crossesWeekend) {
if (now.isBetween(codeFreezeBeginMomentLastWeek, codeFreezeEndMoment)) {
throw new Error(codeFreezeErrorMessage(now, codeFreezeBeginMomentLastWeek, codeFreezeEndMoment));
}
if (now.isBetween(codeFreezeBeginMoment, codeFreezeEndMomentNextWeek)) {
throw new Error(codeFreezeErrorMessage(now, codeFreezeBeginMoment, codeFreezeEndMomentNextWeek));
}
} else {
if (now.isBetween(codeFreezeBeginMoment, codeFreezeEndMoment)) {
throw new Error(codeFreezeErrorMessage(now, codeFreezeBeginMoment, codeFreezeEndMoment));
}
}
}
};
module.exports = codefreeze;