-
Notifications
You must be signed in to change notification settings - Fork 455
/
releaseNotes.mjs
122 lines (107 loc) · 3.58 KB
/
releaseNotes.mjs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Octokit } from '@octokit/core';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { toString } from 'mdast-util-to-string';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
const OWNER = 'vuestorefront';
const REPO = 'storefront-ui';
const octokit = new Octokit({ auth: process.env['GITHUB_TOKEN'] });
const BumpLevels = {
dep: 0,
patch: 1,
minor: 2,
major: 3,
};
// Implementation copied from changeset and its generation release notes https://github.com/changesets/action/blob/main/src/utils.ts#L37
function getChangelogEntry(changelog, version) {
let ast = unified().use(remarkParse).parse(changelog);
let highestLevel = BumpLevels.dep;
let nodes = ast.children;
let headingStartInfo;
let endIndex;
for (let i = 0; i < nodes.length; i++) {
let node = nodes[i];
if (node.type === 'heading') {
let stringified = toString(node);
let match = stringified.toLowerCase().match(/(major|minor|patch)/);
if (match !== null) {
let level = BumpLevels[match[0]];
highestLevel = Math.max(level, highestLevel);
}
if (headingStartInfo === undefined && stringified === version) {
headingStartInfo = {
index: i,
depth: node.depth,
};
continue;
}
if (endIndex === undefined && headingStartInfo !== undefined && headingStartInfo.depth === node.depth) {
endIndex = i;
break;
}
}
}
if (headingStartInfo) {
ast.children = ast.children.slice(headingStartInfo.index + 1, endIndex);
}
return {
content: unified().use(remarkStringify).stringify(ast),
highestLevel: highestLevel,
};
}
const data = await octokit.request(`GET /repos/${OWNER}/${REPO}/commits`, {
owner: OWNER,
repo: REPO,
sha: 'v2',
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
const commitSha = data.data[0].sha;
const commitBody = await octokit.request(`GET /repos/${OWNER}/${REPO}/commits/${commitSha}`, {
owner: OWNER,
repo: REPO,
ref: commitSha,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
const allChangelogFiles = commitBody.data.files.filter((file) => file.filename.endsWith('CHANGELOG.md'));
if (!allChangelogFiles.length) {
console.log('No CHANGELOG file has been found');
} else {
await Promise.all(
allChangelogFiles.map(async (changelogFile) => {
const changelogFilePath = changelogFile.filename;
const updatedPackageDir = changelogFilePath.replace('/CHANGELOG.md', '');
const packageJsonPath = join(updatedPackageDir, 'package.json');
const changelogContent = readFileSync(changelogFilePath, 'utf-8');
const packageJsonContent = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const pkgName = packageJsonContent.name;
const pkgVersion = packageJsonContent.version;
const tagName = `${pkgName}@${pkgVersion}`;
let changelogEntry = getChangelogEntry(changelogContent, pkgVersion);
try {
await octokit.request(`POST /repos/${OWNER}/${REPO}/releases`, {
owner: OWNER,
repo: REPO,
tag_name: tagName,
target_commitish: commitSha,
name: tagName,
body: changelogEntry.content,
draft: false,
prerelease: pkgVersion.includes('-'),
generate_release_notes: false,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
} catch (e) {
console.log(e);
}
console.log(`Release notes for ${tagName} has been created 🎉`);
}),
);
}