-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupdate-show-sections-in-readmes.js
106 lines (88 loc) · 2.62 KB
/
update-show-sections-in-readmes.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
import core from "@actions/core";
import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { ReadmeBox } from "readme-box";
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, core, octokit, ReadmeBox);
}
export async function run(env, core, octokit, ReadmeBox) {
// load all open issues with the `show` label
const issues = await octokit.paginate("GET /repos/{owner}/{repo}/issues", {
owner: "gr2m",
repo: "helpdesk",
labels: "show",
state: "all",
per_page: 100,
});
// split up the shows between upcoming (open issue) and past shows (closed issues)
const upcomingShows = [];
const pastShows = [];
for (const issue of issues) {
const [datetime, , title, , guest] = issue.title.split(/ (- |with @)/g);
if (issue.state === "open") {
upcomingShows.push({
datetime,
title,
guest,
url: issue.html_url,
});
} else {
pastShows.push({
datetime,
title,
guest,
url: issue.html_url,
});
}
}
// Create markdown code for both show sectiosn
const upcomingShowsText = upcomingShows
.map(({ datetime, title, url, guest }) => {
if (guest) {
return `- ${datetime} — [${title}](${url}) with [@${guest}](https://github.com/${guest})`;
}
return `- ${datetime} — [${title}](${url})`;
})
.join("\n");
const pastShowsText = pastShows
.map(({ title, url, guest }) => {
if (guest) {
return `- [${title}](${url}) with [@${guest}](https://github.com/${guest})`;
}
return `- [${title}](${url})`;
})
.join("\n");
const markdown = `
## Upcoming shows
${upcomingShowsText}
## Past shows
${pastShowsText}
`;
// update the shows section in gr2m/helpdesk
await ReadmeBox.updateSection(markdown, {
owner: "gr2m",
repo: "helpdesk",
token: env.GITHUB_TOKEN,
section: "helpdesk-shows",
branch: "main",
message: "docs(README): update helpdesk shows",
});
core.info("README updated in gr2m/helpdesk");
// update the shows section in gr2m/gr2m
await ReadmeBox.updateSection(markdown, {
owner: "gr2m",
repo: "gr2m",
token: env.GITHUB_TOKEN,
section: "helpdesk-shows",
branch: "main",
message: "docs(README): update helpdesk shows",
});
core.info("README updated in gr2m/gr2m");
}