Skip to content

Commit 2bd2bbf

Browse files
committed
add test for handle-show-announcement.js
1 parent 4aacd0c commit 2bd2bbf

File tree

3 files changed

+274
-113
lines changed

3 files changed

+274
-113
lines changed

handle-show-announcement.js

+122-113
Original file line numberDiff line numberDiff line change
@@ -12,128 +12,137 @@ dayjs.extend(customParseFormat);
1212
dayjs.extend(utc);
1313
dayjs.extend(timezone);
1414

15-
// Create Octokit constructor with .paginate API and custom user agent
16-
const MyOctokit = Octokit.plugin(paginateRest).defaults({
17-
userAgent: "gr2m-helpdesk",
18-
});
19-
const octokit = new MyOctokit({
20-
auth: process.env.GITHUB_TOKEN,
21-
});
22-
23-
// load open issues with the `show` label
24-
const showIssues = await octokit.paginate("GET /repos/{owner}/{repo}/issues", {
25-
owner: "gr2m",
26-
repo: "helpdesk",
27-
labels: "show",
28-
state: "open",
29-
per_page: 100,
30-
});
31-
32-
const currentShowIssue = showIssues.find((issue) => {
33-
const dayString = issue.body
34-
.match(/📅.*/)
35-
.pop()
36-
.replace(/📅\s*/, "")
37-
.replace(/^\w+, /, "")
38-
.trim();
39-
const timeString = issue.body
40-
.match(/🕐[^(\r\n]+/)
41-
.pop()
42-
.replace(/🕐\s*/, "")
43-
.replace("Pacific Time", "")
44-
.trim();
45-
46-
// workaround: cannot parse "June 3, 2021 1:00pm" but can parse "June 3, 2021 12:00pm"
47-
// workaround: cannot set default timezone, so parse the date/time string first, then use `.tz()` with the expected date/time format
48-
let timeStringWithoutAmPm = timeString.replace(/(am|pm)\b/, "");
49-
50-
let hours = parseInt(timeStringWithoutAmPm, 10);
51-
52-
if (hours < 9) {
53-
timeStringWithoutAmPm = timeStringWithoutAmPm.replace(hours, hours + 12);
54-
}
15+
if (process.env.GITHUB_ACTIONS && process.env.NODE_ENV !== "test") {
16+
// Create Octokit constructor with .paginate API and custom user agent
17+
const MyOctokit = Octokit.plugin(paginateRest).defaults({
18+
userAgent: "gr2m-helpdesk",
19+
});
20+
const octokit = new MyOctokit({
21+
auth: process.env.GITHUB_TOKEN,
22+
});
23+
run(process.env, core, octokit, twitterRequest);
24+
}
5525

56-
const tmp = dayjs(
57-
[dayString, timeStringWithoutAmPm].join(" "),
58-
// "MMMM D, YYYY H:mma", // see workaround
59-
"MMMM D, YYYY H:mm",
60-
true
26+
export async function run(env, core, octokit, twitterRequest) {
27+
// load open issues with the `show` label
28+
const showIssues = await octokit.paginate(
29+
"GET /repos/{owner}/{repo}/issues",
30+
{
31+
owner: "gr2m",
32+
repo: "helpdesk",
33+
labels: "show",
34+
state: "open",
35+
per_page: 100,
36+
}
6137
);
6238

63-
let time = dayjs.tz(tmp.format("YYYY-MM-DD HH:mm"), "America/Los_Angeles");
64-
65-
const showIsWithinRange =
66-
time.subtract(30, "minutes") > dayjs().subtract(25, "minutes") &&
67-
time.subtract(30, "minutes") < dayjs().add(25, "minutes");
68-
return showIsWithinRange;
69-
});
70-
71-
if (!currentShowIssue) {
72-
core.setFailed("No current issue found to comment on");
73-
process.exit(1);
74-
}
75-
76-
const [, , title, , guest] = currentShowIssue.title.split(/ (- |with @)/g);
77-
78-
const currentShow = {
79-
title,
80-
number: currentShowIssue.number,
81-
issue: currentShowIssue,
82-
guest,
83-
url: currentShowIssue.html_url,
84-
};
85-
86-
// add comment on issue
87-
const {
88-
data: { html_url: commentUrl },
89-
} = await octokit.request(
90-
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
91-
{
92-
owner: "gr2m",
93-
repo: "helpdesk",
94-
issue_number: currentShow.number,
95-
body: "Going live in 30 minutes at https://twitch.tv/gregorcodes",
39+
const currentShowIssue = showIssues.find((issue) => {
40+
const dayString = issue.body
41+
.match(/📅.*/)
42+
.pop()
43+
.replace(/📅\s*/, "")
44+
.replace(/^\w+, /, "")
45+
.trim();
46+
const timeString = issue.body
47+
.match(/🕐[^(\r\n]+/)
48+
.pop()
49+
.replace(/🕐\s*/, "")
50+
.replace("Pacific Time", "")
51+
.trim();
52+
53+
// workaround: cannot parse "June 3, 2021 1:00pm" but can parse "June 3, 2021 12:00pm"
54+
// workaround: cannot set default timezone, so parse the date/time string first, then use `.tz()` with the expected date/time format
55+
let timeStringWithoutAmPm = timeString.replace(/(am|pm)\b/, "");
56+
57+
let hours = parseInt(timeStringWithoutAmPm, 10);
58+
59+
if (hours < 9) {
60+
timeStringWithoutAmPm = timeStringWithoutAmPm.replace(hours, hours + 12);
61+
}
62+
63+
const tmp = dayjs(
64+
[dayString, timeStringWithoutAmPm].join(" "),
65+
// "MMMM D, YYYY H:mma", // see workaround
66+
"MMMM D, YYYY H:mm",
67+
true
68+
);
69+
70+
let time = dayjs.tz(tmp.format("YYYY-MM-DD HH:mm"), "America/Los_Angeles");
71+
72+
const showIsWithinRange =
73+
time.subtract(30, "minutes") > dayjs().subtract(25, "minutes") &&
74+
time.subtract(30, "minutes") < dayjs().add(25, "minutes");
75+
76+
return showIsWithinRange;
77+
});
78+
79+
if (!currentShowIssue) {
80+
core.setFailed("No current issue found to comment on");
81+
process.exit(1);
9682
}
97-
);
98-
console.log("Comment created at %s", commentUrl);
9983

100-
// Tweet out that the show is live:
101-
const auth = {
102-
consumerKey: process.env.TWITTER_CONSUMER_KEY,
103-
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
104-
accessTokenKey: process.env.TWITTER_ACCESS_TOKEN_KEY,
105-
accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
106-
};
84+
const [, , title, , guest] = currentShowIssue.title.split(/ (- |with @)/g);
85+
86+
const currentShow = {
87+
title,
88+
number: currentShowIssue.number,
89+
issue: currentShowIssue,
90+
guest,
91+
url: currentShowIssue.html_url,
92+
};
93+
94+
// add comment on issue
95+
const {
96+
data: { html_url: commentUrl },
97+
} = await octokit.request(
98+
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
99+
{
100+
owner: "gr2m",
101+
repo: "helpdesk",
102+
issue_number: currentShow.number,
103+
body: "Going live in 30 minutes at https://twitch.tv/gregorcodes",
104+
}
105+
);
106+
core.info(`Comment created at ${commentUrl}`);
107+
108+
// Tweet out that the show is live:
109+
const auth = {
110+
consumerKey: env.TWITTER_CONSUMER_KEY,
111+
consumerSecret: env.TWITTER_CONSUMER_SECRET,
112+
accessTokenKey: env.TWITTER_ACCESS_TOKEN_KEY,
113+
accessTokenSecret: env.TWITTER_ACCESS_TOKEN_SECRET,
114+
};
107115

108-
const tweetText = `📯 Starting in 30 minutes
116+
const tweetText = `📯 Starting in 30 minutes
109117
110118
💁🏻‍♂️ ${currentShow.title}
111119
🔴 Watch live at https://twitch.tv/gregorcodes
112120
113121
${currentShow.url}`;
114122

115-
const data = await twitterRequest(`POST statuses/update.json`, {
116-
auth,
117-
status: tweetText,
118-
});
119-
const tweetUrl = `https://twitter.com/gr2m/status/${data.id_str}`;
120-
121-
console.log("Tweeted at %s", tweetUrl);
122-
123-
// update TODOs in issue
124-
await octokit.request("PATCH /repos/{owner}/{repo}/issues/{issue_number}", {
125-
owner: "gr2m",
126-
repo: "helpdesk",
127-
issue_number: currentShow.number,
128-
body: currentShow.issue.body
129-
.replace(
130-
/- \[ \] <!-- todo:announcement-tweet --> ([^\n]+)/,
131-
`- [x] <!-- todo:announcement-tweet --> $1 (${tweetUrl})`
132-
)
133-
.replace(
134-
/- \[ \] <!-- todo:announcement-issue-comment --> ([^\n]+)/,
135-
`- [x] <!-- todo:announcement-issue-comment --> $1 (${commentUrl})`
136-
),
137-
});
138-
139-
console.log("TODOs in issue updated: %s", currentShow.url);
123+
const data = await twitterRequest(`POST statuses/update.json`, {
124+
auth,
125+
status: tweetText,
126+
});
127+
const tweetUrl = `https://twitter.com/gr2m/status/${data.id_str}`;
128+
129+
core.info(`Tweeted at ${tweetUrl}`);
130+
131+
// update TODOs in issue
132+
await octokit.request("PATCH /repos/{owner}/{repo}/issues/{issue_number}", {
133+
owner: "gr2m",
134+
repo: "helpdesk",
135+
issue_number: currentShow.number,
136+
body: currentShow.issue.body
137+
.replace(
138+
/- \[ \] <!-- todo:announcement-tweet --> ([^\n]+)/,
139+
`- [x] <!-- todo:announcement-tweet --> $1 (${tweetUrl})`
140+
)
141+
.replace(
142+
/- \[ \] <!-- todo:announcement-issue-comment --> ([^\n]+)/,
143+
`- [x] <!-- todo:announcement-issue-comment --> $1 (${commentUrl})`
144+
),
145+
});
146+
147+
core.info(`TODOs in issue updated: ${currentShow.url}`);
148+
}

test/fixtures/list-issues.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"number": 2,
4+
"title": "📅 8/26 @ 10:00am PT - Creating tests Part II",
5+
"body": "📅 Thursday, August 26, 2021\n🕐 10:00am Pacific Time\n- [ ] <!-- todo:announcement-tweet --> 30 minute announcement tweet\n- [ ] <!-- todo:announcement-issue-comment --> 30 minute announcement comment\n",
6+
"html_url": "<show url #2>"
7+
},
8+
{
9+
"number": 1,
10+
"title": "📅 8/26 @ 10:00am PT - Creating tests",
11+
"body": "📅 Thursday, August 19, 2021\n🕐 10:00am Pacific Time\n- [ ] <!-- todo:announcement-tweet --> 30 minute announcement tweet\n- [ ] <!-- todo:announcement-issue-comment --> 30 minute announcement comment\n",
12+
"html_url": "<show url #1>"
13+
}
14+
]

0 commit comments

Comments
 (0)