revert: Use generate-changelog package

This commit is contained in:
Ardalan Amini committed 2020-09-28 21:03:56 +03:30
1 parent bf85019aca
commit 25bed3a982
10 files changed
+126 -145

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
import { GitHub } from "@actions/github/lib/utils";
export async function generate(
octokit: InstanceType<typeof GitHub>,
exclude: string[],
owner: string,
repo: string,
tagRef?: string,
): Promise<string> {
exclude = exclude.map(
(type) => (TYPES as { [type: string]: string | undefined })[type] ?? type,
);
const repoUrl = `https://github.com/${owner}/${repo}`;
const commits: Commits = {};
paginator: for await (const { data } of octokit.paginate.iterator(
octokit.repos.listCommits,
{
per_page: 100,
owner,
repo,
},
)) {
for (const { sha, ...commit } of data) {
if (sha === tagRef) break paginator;
const message = commit.commit.message;
let [, type, category, title, flag] = COMMIT_REGEX.exec(message) || [];
if (!title) continue;
type = trim(type);
type =
(TYPES as { [type: string]: string | undefined })[type] ?? TYPES.other;
category = category ? trim(category) : "";
flag = trim(flag);
if (flag !== "breaking") flag = undefined as never;
title = trim(title).replace(
PR_REGEX,
(match, pull) => `[${match}](${repoUrl}/pull/${pull})`,
);
if (flag) title = title.replace(new RegExp(` ?\\[${flag}\\]$`), "");
title = `${title} ([${sha.slice(0, 8)}](${repoUrl}/commit/${sha}))`;
if (flag) title += ` **[${flag}]**`;
commits[type] = commits[type] ?? {};
commits[type][category] = commits[type][category] ?? [];
commits[type][category].push(title);
}
}
return Object.values(TYPES)
.filter((type) => !exclude.includes(type))
.sort()
.reduce((changelog, type) => {
const typeGroup = commits[type];
if (typeGroup == null) return changelog;
changelog.push(`### ${type}`, "");
const categories = Object.keys(typeGroup).sort();
for (const category of categories) {
const categoryGroup = typeGroup[category];
const defaultCategory = category.length === 0;
if (!defaultCategory) changelog.push(`* **${category}:**`);
const baseLine = defaultCategory ? "" : " ";
for (const title of categoryGroup) {
changelog.push(baseLine + "* " + title);
}
}
changelog.push("");
return changelog;
}, [] as string[])
.join("\n");
}
function trim(value: string): string {
if (value == null) return value;
return value.trim().replace(/ {2,}/g, " ");
}
const COMMIT_REGEX = /^([^)]*)(?:\(([^)]*?)\)|):(.*?(?:\[([^\]]+?)\]|))\s*$/;
const PR_REGEX = /#([1-9]\d*)/g;
const TYPES = {
breaking: "Breaking Changes",
build: "Build System / Dependencies",
ci: "Continuous Integration",
chore: "Chores",
docs: "Documentation Changes",
feat: "New Features",
fix: "Bug Fixes",
other: "Other Changes",
perf: "Performance Improvements",
refactor: "Refactors",
revert: "Reverts",
style: "Code Style Changes",
test: "Tests",
};
interface Commits {
[type: string]: {
[category: string]: string[];
};
}
+5 -14
View File
@@ -1,13 +1,11 @@
import { info, getInput, setOutput, setFailed } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import Changelog from "generate-changelog";
import { generate } from "./changelog";
async function run() {
try {
const token = getInput("token", { required: true });
const exclude = getInput("exclude", { required: false }).split(",");
const allowUnknown =
getInput("allow-unknown", { required: false }) === "true";
const octokit = getOctokit(token);
const {
repo: { owner, repo },
@@ -20,22 +18,15 @@ async function run() {
per_page: 2,
});
let tag: string | undefined;
let tagRef: string | undefined;
if (tags.length > 0) {
if (sha === tags[0].commit.sha) {
if (tags.length > 1) tag = tags[1].name;
} else tag = tags[0].name;
if (tags.length > 1) tagRef = tags[1].commit.sha;
} else tagRef = tags[0].commit.sha;
}
let changelog = await Changelog.generate({
repoUrl: `https://github.com/${owner}/${repo}`,
tag,
exclude,
allowUnknown,
});
changelog = changelog.replace(/^#+ +(\d)/, "#### v$1");
const changelog = await generate(octokit, exclude, owner, repo, tagRef);
info(changelog);