Skip to content

Commit

Permalink
fix: discard BREAKING CHANGE commits with incorrect capitalization
Browse files Browse the repository at this point in the history
This new version of `commit-it` provides a new API and several
improvements to Conventional Commit validation. The main changes
(in term of compliance) are:

- The `BREAKING[- ]CHANGE` footer element MUST be in capital letters
- Any other Conventional Commit element is to be considered as
  case insensitive
  • Loading branch information
Kevin-de-Jong committed Dec 17, 2023
1 parent 99f2b1d commit 12acb5e
Show file tree
Hide file tree
Showing 10 changed files with 1,011 additions and 719 deletions.
686 changes: 442 additions & 244 deletions lib/get/index.js

Large diffs are not rendered by default.

703 changes: 446 additions & 257 deletions lib/main/index.js

Large diffs are not rendered by default.

49 changes: 26 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 4 additions & 14 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as core from "@actions/core";
import * as commitLib from "@dev-build-deploy/commit-it";
import { Commit, ConventionalCommit } from "@dev-build-deploy/commit-it";

import * as assets from "./assets";
import * as branching from "./branching";
Expand All @@ -18,18 +18,8 @@ import * as versioning from "./versioning";
* @returns List of Conventional Commits
* @internal
*/
export function filterConventionalCommits(commits: commitLib.ICommit[]): commitLib.IConventionalCommit[] {
return commits
.map(c => {
try {
return commitLib.getConventionalCommit(c);
} catch (error) {
if (!(error instanceof commitLib.ConventionalCommitError)) throw error;
}

return;
})
.filter(c => c !== undefined) as commitLib.IConventionalCommit[];
export function filterConventionalCommits(commits: Commit[]): ConventionalCommit[] {
return commits.map(c => ConventionalCommit.fromCommit(c)).filter(c => c.isValid);
}

/**
Expand All @@ -45,7 +35,7 @@ export async function run(): Promise<void> {
const versionOverride = core.getInput("version");

let newVersion: versioning.Version;
const commits: commitLib.IConventionalCommit[] = [];
const commits: ConventionalCommit[] = [];

core.startGroup("🔍 Determining increment type");
if (versionOverride) {
Expand Down
8 changes: 5 additions & 3 deletions src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fs from "fs";

import * as core from "@actions/core";
import * as github from "@actions/github";
import { IConventionalCommit } from "@dev-build-deploy/commit-it";
import { ConventionalCommit } from "@dev-build-deploy/commit-it";
import { CalVerIncrement, SemVerIncrement } from "@dev-build-deploy/version-it";
import YAML from "yaml";

Expand Down Expand Up @@ -123,7 +123,7 @@ export async function readChangelogFromFile(file: string): Promise<string> {
* @param commits Conventional Commits part of the Changelog
* @returns Changelog in Markdown format
*/
export async function generateChangelog(versionScheme: VersionScheme, commits: IConventionalCommit[]): Promise<string> {
export async function generateChangelog(versionScheme: VersionScheme, commits: ConventionalCommit[]): Promise<string> {
core.info("📓 Generating Release Notes...");

const isWildcard = (value?: string[]): boolean => isMatch(value, "*");
Expand All @@ -135,6 +135,8 @@ export async function generateChangelog(versionScheme: VersionScheme, commits: I
const changelog = `${title}\n\n${config.changelog?.categories
?.map(category => {
const categoryCommits = commits.filter(commit => {
if (!commit.isValid) return false;
const incrementType = versionScheme.determineIncrementType([commit]);
const hasValidIncrement =
Expand All @@ -157,7 +159,7 @@ export async function generateChangelog(versionScheme: VersionScheme, commits: I
if (categoryCommits.length > 0)
return `### ${category.title}\n\n${categoryCommits
.map(commit => `- ${firstCharToUpperCase(commit.description)}`)
.map(commit => `- ${firstCharToUpperCase(commit.description ?? "")}`)
.join("\n")}\n\n`;
return;
Expand Down
8 changes: 4 additions & 4 deletions src/releasing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export async function getLatestRelease(
* @param ref The git ref to compare against
* @returns List of commits
*/
export async function getChangesSince(ref: string): Promise<commit.ICommit[]> {
export async function getChangesSince(ref: string): Promise<commit.Commit[]> {
const octokit = github.getOctokit(core.getInput("token"));
const commits: Commit[] = [];

Expand All @@ -144,14 +144,14 @@ export async function getChangesSince(ref: string): Promise<commit.ICommit[]> {
commits.push(...response.data.commits);
}

return commits.map(c => commit.getCommit({ hash: c.sha, message: c.commit.message }));
return commits.map(c => commit.Commit.fromString({ hash: c.sha, message: c.commit.message }));
}

/**
* Determines the initial commit in the current branch
* @returns The initial commit
*/
export async function getInitialCommit(): Promise<commit.ICommit> {
export async function getInitialCommit(): Promise<commit.Commit> {
const octokit = github.getOctokit(core.getInput("token"));
const commits: Commit[] = [];

Expand All @@ -162,6 +162,6 @@ export async function getInitialCommit(): Promise<commit.ICommit> {
commits.push(...response.data);
}

const commitMap = commits.map(c => commit.getCommit({ hash: c.sha, message: c.commit.message }));
const commitMap = commits.map(c => commit.Commit.fromString({ hash: c.sha, message: c.commit.message }));
return commitMap[commitMap.length - 1];
}
20 changes: 11 additions & 9 deletions src/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as core from "@actions/core";
import { ConventionalCommitError, IConventionalCommit } from "@dev-build-deploy/commit-it";
import { ConventionalCommit } from "@dev-build-deploy/commit-it";
import { CalVer, SemVer, SemVerIncrement, CalVerIncrement } from "@dev-build-deploy/version-it";

import * as branching from "./branching";
Expand All @@ -24,7 +24,7 @@ export type VersionIncrement = SemVerIncrement | CalVerIncrement;
*/
export abstract class VersionScheme {
abstract defaultConfiguration: IReleaseConfiguration;
abstract determineIncrementType(commits: IConventionalCommit[]): VersionIncrement | undefined;
abstract determineIncrementType(commits: ConventionalCommit[]): VersionIncrement | undefined;
abstract isValid(version: string): boolean;
abstract createVersion(version: string): Version;
abstract initialVersion(): Version;
Expand Down Expand Up @@ -69,15 +69,17 @@ export class SemVerScheme extends VersionScheme {
* @param commits List of commits to determine the increment type for
* @returns Increment type
*/
determineIncrementType(commits: IConventionalCommit[]): SemVerIncrement | undefined {
determineIncrementType(commits: ConventionalCommit[]): SemVerIncrement | undefined {
const typeCount: { [key: string]: number } = { feat: 0, fix: 0 };

for (const commit of commits) {
try {
if (commit.breaking) return branching.getBranch().type === "default" ? "MAJOR" : "PATCH";
typeCount[commit.type]++;
} catch (error) {
if (!(error instanceof ConventionalCommitError)) throw error;
if (!commit.isValid) continue;
if (commit.breaking) return branching.getBranch().type === "default" ? "MAJOR" : "PATCH";

// Implementors of the Conventional Commit specification MUST always treat Conventional Commit elements as non-case sensitive.
const commitType = commit.type?.toLowerCase() ?? "";
if (commitType === "feat" || commitType === "fix") {
typeCount[commitType]++;
}
}

Expand Down Expand Up @@ -131,7 +133,7 @@ export class CalVerScheme extends VersionScheme {
* @returns Increment type
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
determineIncrementType(_commits: IConventionalCommit[]): CalVerIncrement | undefined {
determineIncrementType(_commits: ConventionalCommit[]): CalVerIncrement | undefined {
return branching.getBranch().type === "default" ? "CALENDAR" : "MODIFIER";
}

Expand Down
37 changes: 8 additions & 29 deletions test/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,20 @@ SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>
SPDX-License-Identifier: MIT
*/

import { Commit, ConventionalCommit } from "@dev-build-deploy/commit-it";

import * as action from "../src/action";

describe("Filter Conventional Commits", () => {
test("Filter out non-conventional commits", () => {
const conventionals = action.filterConventionalCommits([
{
hash: "0a0b0c0d",
subject: "feat: add new feature",
},
{
hash: "0a0b0c0d",
subject: "fix: prevent bug",
},
{
hash: "0a0b0c0d",
subject: "Not a conventional commit",
},
Commit.fromString({ hash: "0a0b0c0d", message: "feat: add new feature" }),
Commit.fromString({ hash: "0a0b0c0d", message: "fix: prevent bug" }),
Commit.fromString({ hash: "0a0b0c0d", message: "Not a conventional commit" }),
]);
expect(conventionals).toStrictEqual([
{
hash: "0a0b0c0d",
subject: "feat: add new feature",
breaking: false,
description: "add new feature",
scope: undefined,
type: "feat",
},
{
hash: "0a0b0c0d",
subject: "fix: prevent bug",
breaking: false,
description: "prevent bug",
scope: undefined,
type: "fix",
},
expect(conventionals).toEqual([
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat: add new feature" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "fix: prevent bug" }),
]);
});
});
Loading

0 comments on commit 12acb5e

Please sign in to comment.