mirror of
https://github.com/darksaid98/auto-changelog.git
synced 2026-12-02 22:16:06 +00:00
feat: Add semantic versioning compatibility (#47)
* feat: Use regex groups * feat: Add semver input * deps: Add semver v7.3.7 * deps(Dev): Add @types/semver v7.3.9 * feat: Add prerelease output * chore: Update action directory * feat: Log prerelease value * ci(GitHub): Update release workflow * docs: Update README.md * feat: Add ability to detect previous version based on semver comparison * ci(GitHub): fix test workflow
This commit is contained in:
1 parent
de993f2bea
commit
bd78d66436
16 files changed
+226
-53
No files matched your search
+2
-1
@@ -21,7 +21,8 @@ export async function generate(input: ChangelogInputI): Promise<string> {
|
||||
|
||||
const message = commit.commit.message.split("\n")[0];
|
||||
|
||||
let [, type, category, title, flag] = COMMIT_REGEX.exec(message) || [];
|
||||
let { type, category, title, flag } =
|
||||
COMMIT_REGEX.exec(message)?.groups ?? {};
|
||||
|
||||
if (!title) continue;
|
||||
|
||||
|
||||
+12
-1
@@ -1,7 +1,8 @@
|
||||
import type { GitHub } from "@actions/github/lib/utils";
|
||||
import type { SemVer } from "semver";
|
||||
|
||||
export const COMMIT_REGEX =
|
||||
/^([^)]*)(?:\(([^)]*?)\)|):(.*?)(?:\[([^\]]+?)\]|)\s*$/;
|
||||
/^(?<type>[^)]*)(?:\((?<category>[^)]*?)\)|):(?<title>.*?)(?:\[(?<flag>[^\]]+?)\]|)\s*$/;
|
||||
export const PR_REGEX = /#([1-9]\d*)/g;
|
||||
|
||||
export interface TypesI {
|
||||
@@ -19,6 +20,15 @@ export interface LogI {
|
||||
commits: string[];
|
||||
}
|
||||
|
||||
export interface TagInputI {
|
||||
octokit: InstanceType<typeof GitHub>;
|
||||
owner: string;
|
||||
repo: string;
|
||||
sha: string;
|
||||
semver: SemVer | null;
|
||||
prerelease: boolean;
|
||||
}
|
||||
|
||||
export interface ChangelogInputI {
|
||||
octokit: InstanceType<typeof GitHub>;
|
||||
owner: string;
|
||||
@@ -31,4 +41,5 @@ export interface ChangelogInputI {
|
||||
export interface ActionInputsI {
|
||||
commitTypes: TypesI;
|
||||
defaultCommitType: string;
|
||||
semver: boolean;
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,4 @@
|
||||
import { getInput } from "@actions/core";
|
||||
import { getInput, getBooleanInput } from "@actions/core";
|
||||
import Joi from "joi";
|
||||
import YAML from "yaml";
|
||||
import { ActionInputsI, TypesI } from "./constants";
|
||||
@@ -10,6 +10,7 @@ export function getToken(): string {
|
||||
export async function getInputs(): Promise<ActionInputsI> {
|
||||
const commitTypes = YAML.parse(getInput("commit-types", { required: true }));
|
||||
const defaultCommitType = getInput("default-commit-type", { required: true });
|
||||
const semver = getBooleanInput("semver", { required: true });
|
||||
|
||||
return Joi.object<ActionInputsI, true>()
|
||||
.keys({
|
||||
@@ -17,6 +18,7 @@ export async function getInputs(): Promise<ActionInputsI> {
|
||||
.pattern(Joi.string(), Joi.string())
|
||||
.required(),
|
||||
defaultCommitType: Joi.string().required(),
|
||||
semver: Joi.boolean().required(),
|
||||
})
|
||||
.validateAsync({ commitTypes, defaultCommitType });
|
||||
.validateAsync({ commitTypes, defaultCommitType, semver });
|
||||
}
|
||||
+28
-11
@@ -1,5 +1,7 @@
|
||||
import { info, setFailed, setOutput } from "@actions/core";
|
||||
import { context, getOctokit } from "@actions/github";
|
||||
import SemVer from "semver";
|
||||
import { getTagSha } from "./tag.js";
|
||||
import { generate } from "./changelog";
|
||||
import { getInputs, getToken } from "./context";
|
||||
|
||||
@@ -11,22 +13,33 @@ async function run() {
|
||||
const {
|
||||
repo: { owner, repo },
|
||||
sha,
|
||||
ref,
|
||||
} = context;
|
||||
|
||||
const { data: tags } = await octokit.rest.repos.listTags({
|
||||
let semver: SemVer.SemVer | null = null;
|
||||
|
||||
if (inputs.semver) {
|
||||
semver = SemVer.parse(ref, { includePrerelease: true });
|
||||
|
||||
if (semver == null)
|
||||
return setFailed(
|
||||
`Expected a semver compatible ref, got "${ref}" instead.`,
|
||||
);
|
||||
}
|
||||
|
||||
let prerelease = false;
|
||||
|
||||
if (semver != null) prerelease = semver.prerelease.length > 0;
|
||||
|
||||
const tagRef = await getTagSha({
|
||||
octokit,
|
||||
owner,
|
||||
repo,
|
||||
per_page: 2,
|
||||
sha,
|
||||
semver,
|
||||
prerelease,
|
||||
});
|
||||
|
||||
let tagRef: string | undefined;
|
||||
|
||||
if (tags.length > 0) {
|
||||
if (sha === tags[0].commit.sha) {
|
||||
if (tags.length > 1) tagRef = tags[1].commit.sha;
|
||||
} else tagRef = tags[0].commit.sha;
|
||||
}
|
||||
|
||||
const changelog = await generate({
|
||||
octokit,
|
||||
owner,
|
||||
@@ -39,6 +52,10 @@ async function run() {
|
||||
info(changelog);
|
||||
|
||||
setOutput("changelog", changelog);
|
||||
|
||||
info(`prerelease: ${prerelease}`);
|
||||
|
||||
setOutput("prerelease", prerelease);
|
||||
}
|
||||
|
||||
run().catch((error) => setFailed(error.message));
|
||||
run().catch(setFailed);
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import SemVer from "semver";
|
||||
import { TagInputI } from "./constants.js";
|
||||
|
||||
export async function getTagSha(input: TagInputI): Promise<string | undefined> {
|
||||
const { octokit, owner, repo, sha, semver, prerelease } = input;
|
||||
|
||||
for await (const { data } of octokit.paginate.iterator(
|
||||
octokit.rest.repos.listTags,
|
||||
{
|
||||
per_page: 100,
|
||||
owner,
|
||||
repo,
|
||||
},
|
||||
)) {
|
||||
for (const {
|
||||
name,
|
||||
commit: { sha: tagSha },
|
||||
} of data) {
|
||||
if (sha === tagSha) continue;
|
||||
|
||||
if (semver == null) return tagSha;
|
||||
|
||||
const tagSemver = SemVer.parse(name, { includePrerelease: true });
|
||||
|
||||
if (tagSemver == null || semver.compare(tagSemver) <= 0) continue;
|
||||
|
||||
if (tagSemver.prerelease.length > 0 && !prerelease) continue;
|
||||
|
||||
return tagSha;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
Reference in new issue
Block a user