-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.ts
180 lines (145 loc) · 5.14 KB
/
process.ts
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
import * as core from "@actions/core";
import * as dayjs from "dayjs";
import { Config, Day, DAYS, validDateFormats } from "./config";
import type { Logger } from "./logger";
const words = /\w/g;
function formatOutput(input: string): string {
let output: string = "";
for (const s of input) {
if (!s.match(words)) {
output += "_";
} else {
output += s;
}
}
output = output.replace(/_+/g, "_");
if (output.endsWith("_")) {
output = output.slice(0, output.length - 1);
}
if (output.startsWith("_")) {
output = output.slice(1);
}
return output.toUpperCase();
}
export function process(
now: dayjs.Dayjs,
config: Config,
logger: Logger,
): boolean {
let anyMatched = false;
for (const schedule of config.schedules) {
if (!schedule.name) {
throw new Error(
`Missing Schedule Name: ${JSON.stringify(schedule, null, 2)}`,
);
}
logger.debug(`Processing "${schedule.name}"`);
if (!("dates" in schedule) && !("days" in schedule)) {
throw new Error(
"A schedule must container either a date or days. Found neither.",
);
}
let matched = false;
if ("dates" in schedule) {
if (!Array.isArray(schedule.dates)) {
throw new Error("The dates field must be an array.");
}
if (schedule.dates.length === 0) {
throw new Error(
"At least one date must be provided in the dates field.",
);
}
for (const date of schedule.dates) {
const start = dayjs(date, validDateFormats, true /* strict parsing */)
.tz(config.timeZone, true)
.add(schedule.startHour, "hour")
.add(schedule.startMinute || 0, "minute")
.add(schedule.startSecond || 0, "second");
const end = dayjs(date, validDateFormats, true /* strict parsing */)
.tz(config.timeZone, true)
.add(schedule.endHour, "hour")
.add(schedule.endMinute || 0, "minute")
.add(schedule.endSecond || 0, "second");
logger.debug(
`Processing "${schedule.name}"."${date}" start=${start} end=${end}`,
);
if (!start.isValid()) {
throw new Error(
`Start date should follow one of the allowed date formats: ${JSON.stringify(validDateFormats, null, 2)}`,
);
}
if (!end.isValid()) {
throw new Error(
`End date should follow one of the allowed date formats: ${JSON.stringify(validDateFormats, null, 2)}`,
);
}
if (now.isSameOrAfter(start) && now.isSameOrBefore(end)) {
matched = true;
logger.debug(`Date matched: now=${now} start=${start} end=${end}`);
} else {
logger.debug(
`Date not matched: now=${now} start=${start} end=${end} nowIsAfterStart=${now.isSameOrAfter(start)} nowIsBeforeEnd=${now.isSameOrBefore(end)}`,
);
}
}
}
if ("days" in schedule) {
if (!Array.isArray(schedule.days)) {
throw new Error("The days field must be an array.");
}
if (schedule.days.length === 0) {
throw new Error("At least one day must be provided in the days field");
}
for (const d of schedule.days) {
const day: Day = d;
if (!day) {
throw new Error(`Expected a day, got: "${day}`);
}
logger.debug(`Processing "${schedule.name}"."${day}"`);
if (!Day[day]) {
throw new Error(
`Unexpected day: "${day}". Acceptable options are: ${JSON.stringify(Day, null, 2)}. Days are case-sensitive.`,
);
}
const wantDay = DAYS.find((d) => d === day);
const gotDay = DAYS[now.day()];
if (wantDay !== gotDay) {
logger.debug(`Day not matched: want=${wantDay} got=${gotDay}`);
continue;
}
logger.debug(`Day matched: want=${wantDay} got=${gotDay}`);
const start = now
.tz(config.timeZone)
.hour(schedule.startHour)
.minute(schedule.startMinute || 0)
.second(schedule.startSecond || 0);
const end = now
.tz(config.timeZone)
.hour(schedule.endHour)
.minute(schedule.endMinute || 0)
.second(schedule.endSecond || 0);
if (now.isSameOrAfter(start) && now.isSameOrBefore(end)) {
matched = true;
logger.debug(`Day matched: now=${now} start=${start} end=${end}`);
} else {
logger.debug(
`Day not matched: now=${now} start=${start} end=${end} nowIsAfterStart=${now.isSameOrAfter(start)} nowIsBeforeEnd=${now.isSameOrBefore(end)}`,
);
}
}
}
if (matched) {
anyMatched = true;
logger.notice(
`The schedule "${schedule.name}" IS matched. You can access it as "steps.{ STEP_ID }.outputs.${formatOutput(schedule.name)}".`,
);
logger.setOutput(formatOutput(schedule.name), true);
} else {
logger.notice(
`The schedule "${schedule.name}" is NOT matched. You can access it as "steps.{ STEP_ID }.outputs.${formatOutput(schedule.name)}".`,
);
logger.setOutput(formatOutput(schedule.name), false);
}
}
return anyMatched;
}