/
main.js
76 lines (61 loc) · 1.91 KB
/
main.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
// @ts-check
/**
*
* @param {import("@actions/core")} core
* @param {{client_payload: import("@octokit/webhooks-types").ReleaseCreatedEvent}} event
*/
export function main(core, event) {
const body = event.client_payload.release.body;
// get separate changelogs
const [ignore_, ...changelogs] = body.split(/^- /gm);
const changelogsByComponent = {};
const subscribedToComponents = process.env.INPUT_COMPONENTS.split(/\s*,\s*/);
for (const changelog of changelogs) {
const lines = changelog.split(/\n\s*/).filter(Boolean);
const lastLine = lines.at(-1);
if (!lastLine?.startsWith("<!-- Changed components:")) {
// ignore changes that didn'd affect any components
continue;
}
// remove last line
lines.pop();
const components = lastLine
.slice("<!-- Changed components:".length, "-->".length * -1)
.trim()
.split(/\s*,\s*/g);
if (components[0] === "_none_") {
continue;
}
for (const component of components) {
if (!subscribedToComponents.includes(component)) continue;
changelogsByComponent[component] = changelogsByComponent[component] || [];
changelogsByComponent[component].push(lines.join("\n"));
}
}
if (Object.keys(changelogsByComponent).length === 0) {
return;
}
core.setOutput("hasComponentChanges", "true");
core.setOutput(
"changelogsByComponent",
JSON.stringify(changelogsByComponent)
);
core.setOutput(
"changelogsByComponentMarkdown",
yamlEscape(toMarkdown(changelogsByComponent, event))
);
}
function toMarkdown(changelogsByComponent, event) {
return (
Object.entries(changelogsByComponent)
.map(([component, changelogs]) => {
return `### ${component}
- ${changelogs.join("\n- ")}`;
})
.join("\n\n") +
`\n\nFull release notes: ${event.client_payload.release.html_url}`
);
}
function yamlEscape(string) {
return string.replace(/"/g, '""');
}