From 8f4af861114cfb5422364ed85346087b573f9a36 Mon Sep 17 00:00:00 2001 From: Thomas Lamant <10244927+tmlmt@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:56:17 +0200 Subject: [PATCH 1/8] feat: add option to include chore(deps) commits --- src/commands/default.ts | 7 ++----- src/config.ts | 2 ++ src/git.ts | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index 02728d5..b111f07 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -8,6 +8,7 @@ import { getGitDiff, getCurrentGitStatus, parseCommits, + filterParsedCommits, bumpVersion, generateMarkDown, BumpVersionOptions, @@ -41,11 +42,7 @@ export default async function defaultMain(args: Argv) { const rawCommits = await getGitDiff(config.from, config.to); // Parse commits as conventional commits - const commits = parseCommits(rawCommits, config).filter( - (c) => - config.types[c.type] && - !(c.type === "chore" && c.scope === "deps" && !c.isBreaking) - ); + const commits = filterParsedCommits(parseCommits(rawCommits, config), config); // Shortcut for canary releases if (args.canary) { diff --git a/src/config.ts b/src/config.ts index 7b6359f..31c535b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,6 +14,7 @@ export interface ChangelogConfig { from: string; to: string; newVersion?: string; + excludeChoreDeps: boolean; signTags?: boolean; output: string | boolean; publish: { @@ -53,6 +54,7 @@ const getDefaultConfig = () => cwd: null, from: "", to: "", + excludeChoreDeps: true, output: defaultOutput, scopeMap: {}, tokens: { diff --git a/src/git.ts b/src/git.ts index df2c00d..27472cd 100644 --- a/src/git.ts +++ b/src/git.ts @@ -97,6 +97,22 @@ export function parseCommits( .filter(Boolean); } +export function filterParsedCommits( + commits: GitCommit[], + config: ChangelogConfig +): GitCommit[] { + return commits.filter( + (c) => + config.types[c.type] && + !( + c.type === "chore" && + config.excludeChoreDeps && + c.scope === "deps" && + !c.isBreaking + ) + ); +} + // https://www.conventionalcommits.org/en/v1.0.0/ // https://regex101.com/r/FSfNvA/1 const ConventionalCommitRegex = From 6ef3a5a78047f85030051d20e933f0a584013169 Mon Sep 17 00:00:00 2001 From: Thomas Lamant <10244927+tmlmt@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:58:11 +0200 Subject: [PATCH 2/8] test: test both possible values of excludeChoreDeps --- test/git.test.ts | 202 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 200 insertions(+), 2 deletions(-) diff --git a/test/git.test.ts b/test/git.test.ts index fde71f4..092e37f 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -4,6 +4,7 @@ import { getGitDiff, loadChangelogConfig, parseCommits, + filterParsedCommits, getRepoConfig, formatReference, } from "../src"; @@ -104,7 +105,7 @@ describe("git", () => { ]); }); - test("parse", async () => { + test("parse with default config", async () => { const COMMIT_FROM = "1cb15d5dd93302ebd5ff912079ed584efcc6703b"; const COMMIT_TO = "3828bda8c45933396ddfa869d671473231ce3c95"; @@ -200,7 +201,204 @@ describe("git", () => { from: COMMIT_FROM, to: COMMIT_TO, }); - const parsed = parseCommits(commits, config); + const parsed = filterParsedCommits(parseCommits(commits, config), config); + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + expect(parsed.map(({ body: _, author: __, authors: ___, ...rest }) => rest)) + .toMatchInlineSnapshot(` + [ + { + "description": "v0.3.5", + "isBreaking": false, + "message": "chore(release): v0.3.5", + "references": [ + { + "type": "hash", + "value": "3828bda", + }, + ], + "scope": "release", + "shortHash": "3828bda", + "type": "chore", + }, + { + "description": "breaking change example, close #123", + "isBreaking": true, + "message": "fix(scope)!: breaking change example, close #123 (#134)", + "references": [ + { + "type": "pull-request", + "value": "#134", + }, + { + "type": "issue", + "value": "#123", + }, + { + "type": "hash", + "value": "20e622e", + }, + ], + "scope": "scope", + "shortHash": "20e622e", + "type": "fix", + }, + { + "description": "v0.3.4", + "isBreaking": false, + "message": "chore(release): v0.3.4", + "references": [ + { + "type": "hash", + "value": "6fc5087", + }, + ], + "scope": "release", + "shortHash": "6fc5087", + "type": "chore", + }, + { + "description": "infer github config from package.json", + "isBreaking": false, + "message": "feat: infer github config from package.json (resolves #37)", + "references": [ + { + "type": "pull-request", + "value": "#37", + }, + { + "type": "hash", + "value": "c0febf1", + }, + ], + "scope": "", + "shortHash": "c0febf1", + "type": "feat", + }, + { + "description": "v0.3.3", + "isBreaking": false, + "message": "chore(release): v0.3.3", + "references": [ + { + "type": "hash", + "value": "f4f42a3", + }, + ], + "scope": "release", + "shortHash": "f4f42a3", + "type": "chore", + }, + { + "description": "consider docs and refactor as semver patch for bump", + "isBreaking": false, + "message": "fix: consider docs and refactor as semver patch for bump", + "references": [ + { + "type": "hash", + "value": "648ccf1", + }, + ], + "scope": "", + "shortHash": "648ccf1", + "type": "fix", + }, + { + "description": "expose \`determineSemverChange\` and \`bumpVersion\`", + "isBreaking": false, + "message": "feat: expose \`determineSemverChange\` and \`bumpVersion\`", + "references": [ + { + "type": "hash", + "value": "5451f18", + }, + ], + "scope": "", + "shortHash": "5451f18", + "type": "feat", + }, + { + "description": "fix typecheck", + "isBreaking": false, + "message": "chore: fix typecheck", + "references": [ + { + "type": "hash", + "value": "8796cf1", + }, + ], + "scope": "", + "shortHash": "8796cf1", + "type": "chore", + }, + { + "description": "update dependencies", + "isBreaking": false, + "message": "chore: update dependencies", + "references": [ + { + "type": "hash", + "value": "c210976", + }, + ], + "scope": "", + "shortHash": "c210976", + "type": "chore", + }, + ] + `); + + const md = await generateMarkDown(parsed, config); + + expect(md).toMatchInlineSnapshot(` + "## 1cb15d5dd93302ebd5ff912079ed584efcc6703b...3828bda8c45933396ddfa869d671473231ce3c95 + + [compare changes](https://github.com/unjs/changelogen/compare/1cb15d5dd93302ebd5ff912079ed584efcc6703b...3828bda8c45933396ddfa869d671473231ce3c95) + + + ### 🚀 Enhancements + + - Expose \`determineSemverChange\` and \`bumpVersion\` ([5451f18](https://github.com/unjs/changelogen/commit/5451f18)) + - Infer github config from package.json ([#37](https://github.com/unjs/changelogen/pull/37)) + + ### 🩹 Fixes + + - Consider docs and refactor as semver patch for bump ([648ccf1](https://github.com/unjs/changelogen/commit/648ccf1)) + - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) + + ### 🏡 Chore + + - Update dependencies ([c210976](https://github.com/unjs/changelogen/commit/c210976)) + - Fix typecheck ([8796cf1](https://github.com/unjs/changelogen/commit/8796cf1)) + - **release:** V0.3.3 ([f4f42a3](https://github.com/unjs/changelogen/commit/f4f42a3)) + - **release:** V0.3.4 ([6fc5087](https://github.com/unjs/changelogen/commit/6fc5087)) + - **release:** V0.3.5 ([3828bda](https://github.com/unjs/changelogen/commit/3828bda)) + + #### ⚠️ Breaking Changes + + - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) + + ### ❤️ Contributors + + - Pooya Parsa ([@pi0](http://github.com/pi0))" + `); + }); + + test("parse including chore(deps)", async () => { + const COMMIT_FROM = "1cb15d5dd93302ebd5ff912079ed584efcc6703b"; + const COMMIT_TO = "3828bda8c45933396ddfa869d671473231ce3c95"; + + const commits = await getGitDiff(COMMIT_FROM, COMMIT_TO); + commits[1].message = + "fix(scope)!: breaking change example, close #123 (#134)"; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + + const config = await loadChangelogConfig(process.cwd(), { + from: COMMIT_FROM, + to: COMMIT_TO, + excludeChoreDeps: false, + }); + const parsed = filterParsedCommits(parseCommits(commits, config), config); expect(parsed.map(({ body: _, author: __, authors: ___, ...rest }) => rest)) .toMatchInlineSnapshot(` From 3b2e970431935377d6e5df70bde03b0ba0d10830 Mon Sep 17 00:00:00 2001 From: Thomas Lamant Date: Sun, 22 Oct 2023 04:26:17 +0200 Subject: [PATCH 3/8] feat!: generalize exclude option to any type and scope --- src/commands/default.ts | 2 + src/config.ts | 4 +- src/git.ts | 30 ++++++++------- test/git.test.ts | 82 +++++++++++++++++------------------------ 4 files changed, 54 insertions(+), 64 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index b111f07..22c1d67 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -25,6 +25,8 @@ export default async function defaultMain(args: Argv) { from: args.from, to: args.to, output: args.output, + exclude: + typeof args.exclude === "string" ? args.exclude.split(",") : (args.exclude === 0 ? [""] : undefined), newVersion: typeof args.r === "string" ? args.r : undefined, }); diff --git a/src/config.ts b/src/config.ts index 31c535b..66642e2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,7 +14,7 @@ export interface ChangelogConfig { from: string; to: string; newVersion?: string; - excludeChoreDeps: boolean; + exclude?: string[]; signTags?: boolean; output: string | boolean; publish: { @@ -54,7 +54,7 @@ const getDefaultConfig = () => cwd: null, from: "", to: "", - excludeChoreDeps: true, + exclude: [], output: defaultOutput, scopeMap: {}, tokens: { diff --git a/src/git.ts b/src/git.ts index 27472cd..a24dcbe 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,4 +1,4 @@ -import type { ChangelogConfig } from "./config"; +import type { ResolvedChangelogConfig } from "./config"; import { execCommand } from "./exec"; export interface GitCommitAuthor { @@ -90,7 +90,7 @@ export async function getGitDiff( export function parseCommits( commits: RawGitCommit[], - config: ChangelogConfig + config: ResolvedChangelogConfig ): GitCommit[] { return commits .map((commit) => parseGitCommit(commit, config)) @@ -99,17 +99,21 @@ export function parseCommits( export function filterParsedCommits( commits: GitCommit[], - config: ChangelogConfig + config: ResolvedChangelogConfig ): GitCommit[] { - return commits.filter( - (c) => - config.types[c.type] && - !( - c.type === "chore" && - config.excludeChoreDeps && - c.scope === "deps" && - !c.isBreaking - ) + // parsing the exclude parameter ('chore(deps)' => ['chore(deps)','chore','(deps)','deps']) + const excludes: RegExpMatchArray[] = config.exclude + ? config.exclude.map((excludeString) => + excludeString.match(/(\*|[a-z]+)(\((.+)\))?/) + ) + : []; + console.log(excludes) + return commits.filter((c) => + config.types[c.type] && !excludes.some((e) => + e && + (e[1] === "*" || c.type === e[1]) && + (c.scope === e[3] || !e[3]) && + !c.isBreaking) ); } @@ -123,7 +127,7 @@ const IssueRE = /(#\d+)/gm; export function parseGitCommit( commit: RawGitCommit, - config: ChangelogConfig + config: ResolvedChangelogConfig ): GitCommit | null { const match = commit.message.match(ConventionalCommitRegex); if (!match) { diff --git a/test/git.test.ts b/test/git.test.ts index 092e37f..8df90dc 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -345,6 +345,24 @@ describe("git", () => { "shortHash": "c210976", "type": "chore", }, + { + "description": "update all non-major dependencies", + "isBreaking": false, + "message": "chore(deps): update all non-major dependencies (#42)", + "references": [ + { + "type": "pull-request", + "value": "#42", + }, + { + "type": "hash", + "value": "a80e372", + }, + ], + "scope": "deps", + "shortHash": "a80e372", + "type": "chore", + }, ] `); @@ -355,36 +373,36 @@ describe("git", () => { [compare changes](https://github.com/unjs/changelogen/compare/1cb15d5dd93302ebd5ff912079ed584efcc6703b...3828bda8c45933396ddfa869d671473231ce3c95) - ### 🚀 Enhancements - - Expose \`determineSemverChange\` and \`bumpVersion\` ([5451f18](https://github.com/unjs/changelogen/commit/5451f18)) - - Infer github config from package.json ([#37](https://github.com/unjs/changelogen/pull/37)) + - Expose \`determineSemverChange\` and \`bumpVersion\` ([5451f18](https://github.com/unjs/changelogen/commit/5451f18)) + - Infer github config from package.json ([#37](https://github.com/unjs/changelogen/pull/37)) ### 🩹 Fixes - - Consider docs and refactor as semver patch for bump ([648ccf1](https://github.com/unjs/changelogen/commit/648ccf1)) - - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) + - Consider docs and refactor as semver patch for bump ([648ccf1](https://github.com/unjs/changelogen/commit/648ccf1)) + - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) ### 🏡 Chore - - Update dependencies ([c210976](https://github.com/unjs/changelogen/commit/c210976)) - - Fix typecheck ([8796cf1](https://github.com/unjs/changelogen/commit/8796cf1)) - - **release:** V0.3.3 ([f4f42a3](https://github.com/unjs/changelogen/commit/f4f42a3)) - - **release:** V0.3.4 ([6fc5087](https://github.com/unjs/changelogen/commit/6fc5087)) - - **release:** V0.3.5 ([3828bda](https://github.com/unjs/changelogen/commit/3828bda)) + - **deps:** Update all non-major dependencies ([#42](https://github.com/unjs/changelogen/pull/42)) + - Update dependencies ([c210976](https://github.com/unjs/changelogen/commit/c210976)) + - Fix typecheck ([8796cf1](https://github.com/unjs/changelogen/commit/8796cf1)) + - **release:** V0.3.3 ([f4f42a3](https://github.com/unjs/changelogen/commit/f4f42a3)) + - **release:** V0.3.4 ([6fc5087](https://github.com/unjs/changelogen/commit/6fc5087)) + - **release:** V0.3.5 ([3828bda](https://github.com/unjs/changelogen/commit/3828bda)) - #### ⚠️ Breaking Changes + #### ⚠️ Breaking Changes - - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) + - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) - ### ❤️ Contributors + ### ❤️ Contributors - Pooya Parsa ([@pi0](http://github.com/pi0))" `); }); - test("parse including chore(deps)", async () => { + test("parse with excluded types and scopes", async () => { const COMMIT_FROM = "1cb15d5dd93302ebd5ff912079ed584efcc6703b"; const COMMIT_TO = "3828bda8c45933396ddfa869d671473231ce3c95"; @@ -396,7 +414,7 @@ describe("git", () => { const config = await loadChangelogConfig(process.cwd(), { from: COMMIT_FROM, to: COMMIT_TO, - excludeChoreDeps: false, + exclude: ["fix","chore(deps)"], }); const parsed = filterParsedCommits(parseCommits(commits, config), config); @@ -485,20 +503,6 @@ describe("git", () => { "shortHash": "f4f42a3", "type": "chore", }, - { - "description": "consider docs and refactor as semver patch for bump", - "isBreaking": false, - "message": "fix: consider docs and refactor as semver patch for bump", - "references": [ - { - "type": "hash", - "value": "648ccf1", - }, - ], - "scope": "", - "shortHash": "648ccf1", - "type": "fix", - }, { "description": "expose \`determineSemverChange\` and \`bumpVersion\`", "isBreaking": false, @@ -541,24 +545,6 @@ describe("git", () => { "shortHash": "c210976", "type": "chore", }, - { - "description": "update all non-major dependencies", - "isBreaking": false, - "message": "chore(deps): update all non-major dependencies (#42)", - "references": [ - { - "type": "pull-request", - "value": "#42", - }, - { - "type": "hash", - "value": "a80e372", - }, - ], - "scope": "deps", - "shortHash": "a80e372", - "type": "chore", - }, ] `); @@ -576,12 +562,10 @@ describe("git", () => { ### 🩹 Fixes - - Consider docs and refactor as semver patch for bump ([648ccf1](https://github.com/unjs/changelogen/commit/648ccf1)) - **scope:** ⚠️ Breaking change example, close #123 ([#134](https://github.com/unjs/changelogen/pull/134), [#123](https://github.com/unjs/changelogen/issues/123)) ### 🏡 Chore - - **deps:** Update all non-major dependencies ([#42](https://github.com/unjs/changelogen/pull/42)) - Update dependencies ([c210976](https://github.com/unjs/changelogen/commit/c210976)) - Fix typecheck ([8796cf1](https://github.com/unjs/changelogen/commit/8796cf1)) - **release:** V0.3.3 ([f4f42a3](https://github.com/unjs/changelogen/commit/f4f42a3)) From ae87d36a91e7fab52349f7f52b15fb1b76b86f48 Mon Sep 17 00:00:00 2001 From: Thomas Lamant Date: Sun, 22 Oct 2023 04:34:36 +0200 Subject: [PATCH 4/8] chore: removing console.log --- src/git.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/git.ts b/src/git.ts index a24dcbe..fbcbd0a 100644 --- a/src/git.ts +++ b/src/git.ts @@ -103,17 +103,20 @@ export function filterParsedCommits( ): GitCommit[] { // parsing the exclude parameter ('chore(deps)' => ['chore(deps)','chore','(deps)','deps']) const excludes: RegExpMatchArray[] = config.exclude - ? config.exclude.map((excludeString) => - excludeString.match(/(\*|[a-z]+)(\((.+)\))?/) - ) - : []; - console.log(excludes) - return commits.filter((c) => - config.types[c.type] && !excludes.some((e) => - e && - (e[1] === "*" || c.type === e[1]) && - (c.scope === e[3] || !e[3]) && - !c.isBreaking) + ? config.exclude.map((excludeString) => + excludeString.match(/(\*|[a-z]+)(\((.+)\))?/) + ) + : []; + return commits.filter( + (c) => + config.types[c.type] && + !excludes.some( + (e) => + e && + (e[1] === "*" || c.type === e[1]) && + (c.scope === e[3] || !e[3]) && + !c.isBreaking + ) ); } From cf6a555e15c28a02e6c476ada09f832b5c39ad80 Mon Sep 17 00:00:00 2001 From: Thomas Lamant Date: Sun, 22 Oct 2023 04:35:03 +0200 Subject: [PATCH 5/8] chore: lint --- src/commands/default.ts | 6 +++++- test/git.test.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index 22c1d67..a513d38 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -26,7 +26,11 @@ export default async function defaultMain(args: Argv) { to: args.to, output: args.output, exclude: - typeof args.exclude === "string" ? args.exclude.split(",") : (args.exclude === 0 ? [""] : undefined), + typeof args.exclude === "string" + ? args.exclude.split(",") + : args.exclude === 0 // eslint-disable-line unicorn/no-nested-ternary + ? [""] + : undefined, newVersion: typeof args.r === "string" ? args.r : undefined, }); diff --git a/test/git.test.ts b/test/git.test.ts index 8df90dc..0423011 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -414,7 +414,7 @@ describe("git", () => { const config = await loadChangelogConfig(process.cwd(), { from: COMMIT_FROM, to: COMMIT_TO, - exclude: ["fix","chore(deps)"], + exclude: ["fix", "chore(deps)"], }); const parsed = filterParsedCommits(parseCommits(commits, config), config); From 3db0bcad47d48b5f418941d6ebb17e944b0a6955 Mon Sep 17 00:00:00 2001 From: Thomas Lamant Date: Sun, 22 Oct 2023 04:40:54 +0200 Subject: [PATCH 6/8] fix: default --- src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 66642e2..9780b14 100644 --- a/src/config.ts +++ b/src/config.ts @@ -54,7 +54,7 @@ const getDefaultConfig = () => cwd: null, from: "", to: "", - exclude: [], + exclude: [""], output: defaultOutput, scopeMap: {}, tokens: { From bb22dd63f0170f7c42f1755656eddd2a8576cbe3 Mon Sep 17 00:00:00 2001 From: Thomas Lamant Date: Sat, 7 Sep 2024 22:40:15 +0200 Subject: [PATCH 7/8] fix: white spaces --- src/commands/default.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index a513d38..2ded230 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -29,8 +29,8 @@ export default async function defaultMain(args: Argv) { typeof args.exclude === "string" ? args.exclude.split(",") : args.exclude === 0 // eslint-disable-line unicorn/no-nested-ternary - ? [""] - : undefined, + ? [""] + : undefined, newVersion: typeof args.r === "string" ? args.r : undefined, }); From c7516768a5d51d8e871b601da5d6173fb28046db Mon Sep 17 00:00:00 2001 From: Thomas Lamant Date: Sat, 7 Sep 2024 22:42:20 +0200 Subject: [PATCH 8/8] chore: remove unused eslint directive --- test/git.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/git.test.ts b/test/git.test.ts index 0423011..42f345f 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -203,7 +203,6 @@ describe("git", () => { }); const parsed = filterParsedCommits(parseCommits(commits, config), config); - // eslint-disable-next-line @typescript-eslint/no-unused-vars expect(parsed.map(({ body: _, author: __, authors: ___, ...rest }) => rest)) .toMatchInlineSnapshot(` [ @@ -409,7 +408,6 @@ describe("git", () => { const commits = await getGitDiff(COMMIT_FROM, COMMIT_TO); commits[1].message = "fix(scope)!: breaking change example, close #123 (#134)"; - // eslint-disable-next-line @typescript-eslint/no-unused-vars const config = await loadChangelogConfig(process.cwd(), { from: COMMIT_FROM,