This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
82 lines (67 loc) · 2.06 KB
/
index.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
const core = require("@actions/core");
const github = require("@actions/github");
if (require.main === module) {
const getListReleases = (token) =>
github.getOctokit(token).repos.listReleases;
run({
getInput: core.getInput,
setOutput: core.setOutput,
listReleases: getListReleases,
context: github.context.repo,
});
}
async function run({
getInput,
setOutput,
listReleases,
context,
}) {
try {
const token = getInput("token");
console.info("Started retrieving releases");
const request = listReleases(token);
const { data } = await request(context);
const { changelog, latest } = getChangelogAndLatest(data, {getInput});
setOutput("changelog", changelog);
setOutput("latest", latest);
} catch (error) {
core.setFailed(error.message);
}
}
function getChangelogAndLatest(releases, {getInput}) {
if (!Array.isArray(releases)) {
throw new Error(
`Expected an array back as response, but got "${typeof releases}"`
);
}
const spacing = "\n\n";
const latest = { tag: null, date: null };
const changelog = releases
.map(({ tag_name, draft, published_at, name, body }) => {
if (draft) {
console.info(`Skipping draft with the name "${name}"`);
return null;
}
const date = new Date(published_at);
if (latest.date == null || date > latest.date) {
latest.date = date;
latest.tag = tag_name;
}
const title = formatTitle(name, getInput);
const description = formatDescription(body, getInput);
return [title, description].filter(Boolean).join(spacing);
})
.filter(Boolean)
.join(spacing);
return { changelog, latest: latest.tag };
}
function formatTitle(replace, getInput) {
return format(getInput("title-template"), "%%TITLE%%", replace);
}
function formatDescription(replace, getInput) {
return format(getInput("description-template"), "%%DESCRIPTION%%", replace);
}
function format(template, find, replace) {
return template.replace(find, replace || "");
}
module.exports = { run, getChangelogAndLatest };