Sort changelog entries in PRs by highest bump type

This commit is contained in:
mitchellhamilton
2019-09-10 17:01:47 +10:00
parent ab0734a919
commit 7d4dde44b2
2 changed files with 43 additions and 20 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+42 -19
View File
@@ -163,7 +163,7 @@ async function execWithOutput(
await octokit.repos.createRelease({ await octokit.repos.createRelease({
tag_name: `${workspace.name}@${workspace.config.version}`, tag_name: `${workspace.name}@${workspace.config.version}`,
body: changelogEntry, body: changelogEntry.content,
prerelease: workspace.config.version.includes("-"), prerelease: workspace.config.version.includes("-"),
...github.context.repo ...github.context.repo
}); });
@@ -251,11 +251,17 @@ async function execWithOutput(
changelogContents, changelogContents,
pkgJsonContent.version pkgJsonContent.version
); );
return ( return {
`## ${pkgJsonContent.name}@${pkgJsonContent.version}\n\n` + entry highestLevel: entry.highestLevel,
); content:
`## ${pkgJsonContent.name}@${pkgJsonContent.version}\n\n` +
entry.content
};
}) })
)).join("\n ") ))
.sort((a, b) => b.highestLevel - a.highestLevel)
.map(x => x.content)
.join("\n ")
); );
})(); })();
await exec("git", ["add", "."]); await exec("git", ["add", "."]);
@@ -288,11 +294,20 @@ async function execWithOutput(
core.setFailed(err.message); core.setFailed(err.message);
}); });
enum BumpLevels {
dep = 0,
patch = 1,
minor = 2,
major = 3
}
function getChangelogEntry(changelog: string, version: string) { function getChangelogEntry(changelog: string, version: string) {
let ast = unified() let ast = unified()
.use(remarkParse) .use(remarkParse)
.parse(changelog); .parse(changelog);
let highestLevel = BumpLevels.dep;
let nodes = ast.children as Array<any>; let nodes = ast.children as Array<any>;
let headingStartInfo: let headingStartInfo:
| { | {
@@ -304,24 +319,29 @@ function getChangelogEntry(changelog: string, version: string) {
for (let i = 0; i < nodes.length; i++) { for (let i = 0; i < nodes.length; i++) {
let node = nodes[i]; let node = nodes[i];
if ( if (node.type === "heading") {
headingStartInfo === undefined && let stringified: string = mdastToString(node);
node.type === "heading" && let match = stringified.toLowerCase().match(/(major|minor|patch)/);
mdastToString(node) === version if (match !== null) {
) { let level = BumpLevels[match[0] as "major" | "minor" | "patch"];
headingStartInfo = { highestLevel = Math.max(level, highestLevel);
index: i, }
depth: node.depth if (headingStartInfo === undefined && stringified === version) {
}; headingStartInfo = {
continue; index: i,
depth: node.depth
};
continue;
}
} }
if ( if (
endIndex === undefined &&
headingStartInfo !== undefined && headingStartInfo !== undefined &&
node.type === "heading" && node.type === "heading" &&
headingStartInfo.depth === node.depth headingStartInfo.depth === node.depth
) { ) {
endIndex = i; endIndex = i;
break;
} }
} }
if (headingStartInfo) { if (headingStartInfo) {
@@ -330,7 +350,10 @@ function getChangelogEntry(changelog: string, version: string) {
endIndex endIndex
); );
} }
return unified() return {
.use(remarkStringify) content: unified()
.stringify(ast); .use(remarkStringify)
.stringify(ast),
highestLevel: highestLevel
};
} }