forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-release-notes.js
86 lines (76 loc) · 2.2 KB
/
generate-release-notes.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
// ./tools/scripts/generate-release-notes.js
const github = require('@actions/github');
/**
* Generates the release notes for a github release.
*
* Arguments:
* 1 - github_token
* 2 - new version
* 3 - commit SHA of new release
*/
const token = process.argv[2];
const version = process.argv[3];
const comittish = process.argv[4];
const repo = process.env.REPOSITORY_NAME
const owner = process.env.GITHUB_REPOSITORY_OWNER
async function main() {
const client = github.getOctokit(token);
const latestReleaseResponse = await client.request(
'GET /repos/{owner}/{repo}/releases',
{
owner: owner,
repo: repo,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
let previousTag = null;
if (latestReleaseResponse.data) {
for (const responseData of latestReleaseResponse.data) {
if (responseData.draft == false && responseData.prerelease == true) {
previousTag = responseData.tag_name;
break;
}
}
}
const response = await client.request(
'POST /repos/{owner}/{repo}/releases/generate-notes',
{
owner: owner,
repo: repo,
tag_name: version,
previous_tag_name: previousTag,
target_commitish: comittish,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
const noteSections = response.data.body?.split('\n\n');
const trimmedSections = [];
const githubNotesMaxCharLength = 125000;
const maxSectionLength = githubNotesMaxCharLength / noteSections.length;
for (let i = 0; i < noteSections.length; i++) {
if (noteSections[i].length > githubNotesMaxCharLength) {
const lastLineIndex =
noteSections[i].substring(0, maxSectionLength).split('\n').length - 1;
const trimmed =
noteSections[i]
.split('\n')
.slice(0, lastLineIndex - 1)
.join('\n') +
`\n... (+${
noteSections[i].split('\n').length - (lastLineIndex + 1)
} others)`;
trimmedSections.push(trimmed);
continue;
}
trimmedSections.push(noteSections[i]);
}
console.log(trimmedSections.join('\n\n'));
}
main().catch((e) => {
console.error(`Failed generating release notes with error: ${e}`);
process.exit(0);
});