feat: Add commit-types & default-commit-type inputs (#15)

* deps: Add `yaml` version `1.10.2`

* deps: Add `joi` version `17.6.0`

* feat: Add `commit-types` & `default-commit-type` inputs

* ci: Update `Test` workflow

* chore: Update `README.md`
This commit is contained in:
Ardalan Amini authored and GitHub committed 2022-03-04 14:17:53 +03:30
1 parent c72a270e90
commit ea01804ccf
12 files changed
+374 -77

No files matched your search

+31 -26
View File
@@ -1,7 +1,8 @@
import { COMMIT_REGEX, InputI, LogsI, PR_REGEX, TYPES } from "./constants";
import { ChangelogInputI, COMMIT_REGEX, LogsI, PR_REGEX } from "./constants";
export async function generate(input: InputI): Promise<string> {
const { octokit, owner, repo, sha, tagRef } = input;
export async function generate(input: ChangelogInputI): Promise<string> {
const { octokit, owner, repo, sha, tagRef, inputs } = input;
const { commitTypes, defaultCommitType } = inputs;
const repoUrl = `https://github.com/${owner}/${repo}`;
const commits: LogsI = {};
@@ -28,7 +29,7 @@ export async function generate(input: InputI): Promise<string> {
if (flag === "ignore") continue;
type = trim(type);
type = TYPES[type] ?? TYPES.other;
type = commitTypes[type] ?? defaultCommitType;
category = category ? trim(category) : "";
@@ -50,38 +51,38 @@ export async function generate(input: InputI): Promise<string> {
}
}
return Object.values(TYPES)
.reduce((changelog, type) => {
const typeGroup = commits[type];
const TYPES = unique([...Object.values(commitTypes), defaultCommitType]);
if (typeGroup == null) return changelog;
return TYPES.reduce((changelog, type) => {
const typeGroup = commits[type];
changelog.push(`### ${type}`, "");
if (typeGroup == null) return changelog;
const categories = Object.keys(typeGroup).sort();
changelog.push(`### ${type}`, "");
for (const category of categories) {
const categoryGroup = typeGroup[category];
const defaultCategory = category.length === 0;
const categories = Object.keys(typeGroup).sort();
if (!defaultCategory) changelog.push(`* **${category}:**`);
for (const category of categories) {
const categoryGroup = typeGroup[category];
const defaultCategory = category.length === 0;
const baseLine = defaultCategory ? "" : " ";
if (!defaultCategory) changelog.push(`* **${category}:**`);
for (const { title, commits } of categoryGroup) {
changelog.push(
`${baseLine}* ${title} (${commits
.map((sha) => `[${sha.slice(0, 8)}](${repoUrl}/commit/${sha})`)
.join(",")})`,
);
}
const baseLine = defaultCategory ? "" : " ";
for (const { title, commits } of categoryGroup) {
changelog.push(
`${baseLine}* ${title} (${commits
.map((sha) => `[${sha.slice(0, 8)}](${repoUrl}/commit/${sha})`)
.join(",")})`,
);
}
}
changelog.push("");
changelog.push("");
return changelog;
}, [] as string[])
.join("\n");
return changelog;
}, [] as string[]).join("\n");
}
function trim(value: string): string {
@@ -89,3 +90,7 @@ function trim(value: string): string {
return value.trim().replace(/ {2,}/g, " ");
}
function unique(value: string[]): string[] {
return [...new Set(value)];
}
+7 -18
View File
@@ -4,23 +4,6 @@ export const COMMIT_REGEX =
/^([^)]*)(?:\(([^)]*?)\)|):(.*?)(?:\[([^\]]+?)\]|)\s*$/;
export const PR_REGEX = /#([1-9]\d*)/g;
export const TYPES: TypesI = {
breaking: "Breaking Changes",
feat: "New Features",
fix: "Bug Fixes",
revert: "Reverts",
perf: "Performance Improvements",
refactor: "Refactors",
deps: "Dependencies",
docs: "Documentation Changes",
style: "Code Style Changes",
build: "Build System",
ci: "Continuous Integration",
test: "Tests",
chore: "Chores",
other: "Other Changes",
};
export interface TypesI {
[type: string]: string;
}
@@ -36,10 +19,16 @@ export interface LogI {
commits: string[];
}
export interface InputI {
export interface ChangelogInputI {
octokit: InstanceType<typeof GitHub>;
owner: string;
repo: string;
sha: string;
tagRef?: string;
inputs: ActionInputsI;
}
export interface ActionInputsI {
commitTypes: TypesI;
defaultCommitType: string;
}
+22
View File
@@ -0,0 +1,22 @@
import { getInput } from "@actions/core";
import Joi from "joi";
import YAML from "yaml";
import { ActionInputsI, TypesI } from "./constants";
export function getToken(): string {
return getInput("token", { required: true });
}
export async function getInputs(): Promise<ActionInputsI> {
const commitTypes = YAML.parse(getInput("commit-types", { required: true }));
const defaultCommitType = getInput("default-commit-type", { required: true });
return Joi.object<ActionInputsI, true>()
.keys({
commitTypes: Joi.object<TypesI>()
.pattern(Joi.string(), Joi.string())
.required(),
defaultCommitType: Joi.string().required(),
})
.validateAsync({ commitTypes, defaultCommitType });
}
+5 -3
View File
@@ -1,11 +1,12 @@
import { getInput, info, setFailed, setOutput } from "@actions/core";
import { info, setFailed, setOutput } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { generate } from "./changelog";
import { getInputs, getToken } from "./context";
async function run() {
const token = getInput("token", { required: true });
const inputs = await getInputs();
const octokit = getOctokit(token);
const octokit = getOctokit(getToken());
const {
repo: { owner, repo },
@@ -32,6 +33,7 @@ async function run() {
repo,
sha,
tagRef,
inputs,
});
info(changelog);