-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget-schow-schedules.js
114 lines (101 loc) · 3.16 KB
/
get-schow-schedules.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
import { inspect } from "util";
import core from "@actions/core";
import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat.js";
import utc from "dayjs/plugin/utc.js";
import timezone from "dayjs/plugin/timezone.js";
dayjs.extend(customParseFormat);
dayjs.extend(utc);
dayjs.extend(timezone);
if (process.env.GITHUB_ACTIONS && process.env.NODE_ENV !== "test") {
// Create Octokit constructor with .paginate API and custom user agent
const MyOctokit = Octokit.plugin(paginateRest).defaults({
userAgent: "gr2m-helpdesk",
});
const octokit = new MyOctokit({
auth: process.env.GITHUB_TOKEN,
});
run(process.env, octokit, core);
}
/**
* @param {object} env
* @param {Octokit} octokit
* @param {core} core
*/
export async function run(env, octokit, core) {
// load open issues with the `show` label
const showIssues = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues",
{
owner: "gr2m",
repo: "helpdesk",
labels: "show",
state: "open",
per_page: 100,
}
);
const upcomingShowsCrons = showIssues
.map((issue) => {
const dayString = issue.body
.match(/📅.*/)
.pop()
.replace(/📅\s*/, "")
.replace(/^\w+, /, "")
.trim();
const timeString = issue.body
.match(/🕐[^(\r\n]+/)
.pop()
.replace(/🕐\s*/, "")
.replace("Pacific Time", "")
.trim();
// workaround: cannot parse "June 3, 2021 1:00pm" but can parse "June 3, 2021 12:00pm"
// workaround: cannot set default timezone, so parse the date/time string first, then use `.tz()` with the expected date/time format
let timeStringWithoutAmPm = timeString.replace(/(am|pm)\b/, "");
const tmp = dayjs(
[dayString, timeStringWithoutAmPm].join(" "),
// "MMMM D, YYYY H:mma", // see workaround
"MMMM D, YYYY H:mm",
true
);
let hours = parseInt(timeStringWithoutAmPm, 10);
if (hours < 9) {
timeStringWithoutAmPm = timeStringWithoutAmPm.replace(
hours,
hours + 12
);
}
let time = dayjs.tz(
tmp.format("YYYY-MM-DD HH:mm"),
"America/Los_Angeles"
);
// see workaround above. Parsing am/pm is not working
if (time.get("hour") < 8) {
time = time.add(12, "hours");
}
if (time.toISOString() < dayjs().toISOString())
// ignore open issues for shows that are in the past
return;
return {
start: time
.subtract(time.utcOffset() + 3, "minutes")
.format("m H D M [*]"),
announcement: time
.subtract(time.utcOffset() + 33, "minutes")
.format("m H D M [*]"),
};
})
.filter(Boolean);
core.info(
`CRON schedule for upcoming shows is: ${inspect(upcomingShowsCrons)}`
);
core.setOutput(
"schedule_start",
upcomingShowsCrons.map((schedule) => schedule.start).join("\n")
);
core.setOutput(
"schedule_announcement",
upcomingShowsCrons.map((schedule) => schedule.announcement).join("\n")
);
}