Fix a bug with getting the highest bump type

This commit is contained in:
mitchellhamilton
2019-09-11 15:49:39 +10:00
parent 7d4dde44b2
commit 8db2bfe48d
9 changed files with 9754 additions and 548 deletions
+1 -69
View File
@@ -2,13 +2,9 @@ import * as core from "@actions/core";
import { exec } from "@actions/exec";
import * as github from "@actions/github";
import fs from "fs-extra";
import unified from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
// @ts-ignore
import mdastToString from "mdast-util-to-string";
import getWorkspaces, { Workspace } from "get-workspaces";
import path from "path";
import { getChangelogEntry } from "./utils";
async function execWithOutput(
command: string,
@@ -293,67 +289,3 @@ async function execWithOutput(
console.error(err);
core.setFailed(err.message);
});
enum BumpLevels {
dep = 0,
patch = 1,
minor = 2,
major = 3
}
function getChangelogEntry(changelog: string, version: string) {
let ast = unified()
.use(remarkParse)
.parse(changelog);
let highestLevel = BumpLevels.dep;
let 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++) {
let node = nodes[i];
if (node.type === "heading") {
let stringified: string = mdastToString(node);
let match = stringified.toLowerCase().match(/(major|minor|patch)/);
if (match !== null) {
let 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 &&
node.type === "heading" &&
headingStartInfo.depth === node.depth
) {
endIndex = i;
}
}
if (headingStartInfo) {
ast.children = (ast.children as any).slice(
headingStartInfo.index + 1,
endIndex
);
}
return {
content: unified()
.use(remarkStringify)
.stringify(ast),
highestLevel: highestLevel
};
}