-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathrelease.ts
120 lines (102 loc) · 3.45 KB
/
release.ts
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
/**
* The following code is adapted from a fork of the `changesets/actions` repository
* to allow us to generate aggregated changelogs for our monorepo.
* This code can be removed when changesets supports monorepo aggregations.
*/
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable no-console */
import fs from "fs-extra";
import path from "path";
import unified from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
// @ts-ignore
import mdastToString from "mdast-util-to-string";
import { Octokit } from "octokit";
export const BumpLevels = {
dep: 0,
patch: 1,
minor: 2,
major: 3,
} as const;
export function getChangelogEntry(changelog: string, version: string) {
const ast = unified().use(remarkParse).parse(changelog) as any;
let highestLevel: number = BumpLevels.dep;
const nodes = ast.children as Array<any>;
let headingStartInfo:
| {
index: number;
depth: number;
}
| undefined;
let endIndex: number | undefined;
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node.type === "heading") {
const stringified: string = mdastToString(node);
const match = stringified.toLowerCase().match(/(major|minor|patch)/);
if (match !== null) {
const level = BumpLevels[match[0] as "major" | "minor" | "patch"];
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,
};
}
const createAggregatedRelease = async () => {
const octokit = new Octokit({ auth: process.env.CHANGESETS_GITHUB_TOKEN });
const prevRelease = await octokit.rest.repos.getLatestRelease({
owner: "FormidableLabs",
repo: "victory",
});
const pkgFileName = path.join("packages", "victory", "package.json");
const packageJson = JSON.parse(await fs.readFile(pkgFileName, "utf8"));
const changelogFileName = path.join("packages", "victory", "CHANGELOG.md");
const changelog = await fs.readFile(changelogFileName, "utf8");
const changelogEntry = getChangelogEntry(changelog, packageJson.version);
if (!changelogEntry) {
// we can find a changelog but not the entry for this version
// if this is true, something has probably gone wrong
throw new Error(
`Could not find changelog entry for ${packageJson.name}@${packageJson.version}`,
);
}
const tag_name = `v${packageJson.version}`;
const prevTag = prevRelease.data.tag_name;
const link = `Full Changelog: [${prevTag}...${tag_name}](https://github.com/FormidableLabs/victory/compare/${prevTag}...${tag_name})`;
const body = `## What's Changed\n\n${changelogEntry.content}\n\n${link}`;
await octokit.rest.repos.createRelease({
owner: "FormidableLabs",
repo: "victory",
tag_name,
name: tag_name,
body,
prerelease: false,
});
};
(async () => {
await createAggregatedRelease();
})()
.then(() => console.log("Release created!"))
.catch(console.error);