From 18c23e8a8669fbf5275e52c30451952341fbe29d Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 15:46:55 +0800 Subject: [PATCH 01/10] feat(revert resolver): record the reverted hashes --- src/git.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/git.ts b/src/git.ts index 3c32d687..d4b47845 100644 --- a/src/git.ts +++ b/src/git.ts @@ -24,6 +24,7 @@ export interface GitCommit extends RawGitCommit { references: Reference[]; authors: GitCommitAuthor[]; isBreaking: boolean; + revertedHashes: string[]; } export async function getLastGitTag() { @@ -93,6 +94,7 @@ const ConventionalCommitRegex = const CoAuthoredByRegex = /co-authored-by:\s*(?.+)(<(?.+)>)/gim; const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm; const IssueRE = /(#\d+)/gm; +const RevertHashRegex = /This reverts commit (?[A-F0-9]{40})./gm; export function parseGitCommit( commit: RawGitCommit, @@ -126,6 +128,13 @@ export function parseGitCommit( // Remove references and normalize description = description.replace(PullRequestRE, "").trim(); + // Extract the reverted hashes. + const revertedHashes = [] + const matchedHashes = commit.body.matchAll(RevertHashRegex) + for (const matchedHash of matchedHashes) { + revertedHashes.push(matchedHash.groups.match) + } + // Find all authors const authors: GitCommitAuthor[] = [commit.author]; for (const match of commit.body.matchAll(CoAuthoredByRegex)) { @@ -143,6 +152,7 @@ export function parseGitCommit( scope, references, isBreaking, + revertedHashes, }; } From f4a3d72887235958b72a2d38baaf8c1238cefa25 Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 15:49:32 +0800 Subject: [PATCH 02/10] refactor(cli): move the function for filtering commits --- src/cli.ts | 13 +++++-------- src/git.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 8c8193b2..e5b2950f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,7 +4,7 @@ import { existsSync, promises as fsp } from "node:fs"; import consola from "consola"; import mri from "mri"; import { execa } from "execa"; -import { getGitDiff, parseCommits } from "./git"; +import { getGitDiff, parseCommits, filterCommits } from "./git"; import { loadChangelogConfig } from "./config"; import { generateMarkDown } from "./markdown"; import { bumpVersion } from "./semver"; @@ -27,15 +27,12 @@ async function main() { 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 = parseCommits(rawCommits, config) + const filteredCommits = filterCommits(commits, config) // Bump version optionally if (args.bump || args.release) { - const newVersion = await bumpVersion(commits, config); + const newVersion = await bumpVersion(filteredCommits, config); if (!newVersion) { consola.error("Unable to bump version based on changes."); process.exit(1); @@ -44,7 +41,7 @@ async function main() { } // Generate markdown - const markdown = await generateMarkDown(commits, config); + const markdown = await generateMarkDown(filteredCommits, config); // Show changelog in CLI unless bumping or releasing const displayOnly = !args.bump && !args.release; diff --git a/src/git.ts b/src/git.ts index d4b47845..9650e051 100644 --- a/src/git.ts +++ b/src/git.ts @@ -156,6 +156,14 @@ export function parseGitCommit( }; } +export function filterCommits (commits: GitCommit[], config: ChangelogConfig): GitCommit[] { + return commits.filter( + (c) => + config.types[c.type] && + !(c.type === "chore" && c.scope === "deps" && !c.isBreaking) + ); +} + async function execCommand(cmd: string, args: string[]) { const { execa } = await import("execa"); const res = await execa(cmd, args); From f670ae31796937031941f94e672bfebe0b2f43c4 Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 16:33:19 +0800 Subject: [PATCH 03/10] feat(revert resolver): resolve the reverted commits Closes #11 --- src/git.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index 9650e051..90275a79 100644 --- a/src/git.ts +++ b/src/git.ts @@ -27,6 +27,11 @@ export interface GitCommit extends RawGitCommit { revertedHashes: string[]; } +export interface RevertPair { + shortRevertingHash: string + revertedHash: string +} + export async function getLastGitTag() { const r = await execCommand("git", [ "--no-pager", @@ -157,11 +162,45 @@ export function parseGitCommit( } export function filterCommits (commits: GitCommit[], config: ChangelogConfig): GitCommit[] { - return commits.filter( + const commitsWithNoDeps = commits.filter( (c) => config.types[c.type] && !(c.type === "chore" && c.scope === "deps" && !c.isBreaking) ); + + let resolvedCommits: GitCommit[] = [] + let revertWatchList: RevertPair[] = [] + for (const commit of commitsWithNoDeps) { + // Include the reverted hashes in the watch list + if (commit.revertedHashes.length > 0) { + revertWatchList.push(...commit.revertedHashes.map(revertedHash => ({ + revertedHash, + shortRevertingHash: commit.shortHash + } as RevertPair))) + } + + // Find the commits which revert the current commit being evaluated + const shortRevertingHashes = revertWatchList.filter( + pair => pair.revertedHash.startsWith(commit.shortHash) + ).map(pair => pair.shortRevertingHash) + + if (shortRevertingHashes.length > 0) { + // Remove commits that reverts this current commit + resolvedCommits = resolvedCommits.filter( + resolvedCommit => !shortRevertingHashes.includes(resolvedCommit.shortHash) + ) + + // Unwatch reverting hashes that has been resolved + revertWatchList = revertWatchList.filter( + watchedRevert => !shortRevertingHashes.includes(watchedRevert.shortRevertingHash) + ) + } else { + // If the current commit is known not to have been reverted, put it to resolved commits. + resolvedCommits = [...resolvedCommits, commit] + } + } + + return resolvedCommits } async function execCommand(cmd: string, args: string[]) { From be95029b67e713e7a00954f9e6236da8d4a5015b Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 17:02:57 +0800 Subject: [PATCH 04/10] test: make test to guarantee expected behavior --- test/filter_commits.test.ts | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 test/filter_commits.test.ts diff --git a/test/filter_commits.test.ts b/test/filter_commits.test.ts new file mode 100644 index 00000000..5467de8c --- /dev/null +++ b/test/filter_commits.test.ts @@ -0,0 +1,86 @@ +import { it, describe, expect } from 'vitest' +import type { ChangelogConfig } from '../src/config' +import { filterCommits, GitCommit } from '../src/git' + +describe('automatic resolving of revert commits', () => { + const tests: ({ 'name': string, input: GitCommit[], output: GitCommit[] })[] = [ + { + name: 'retain revert commits if the commit they revert is from previous version', + input: [ + { + type: 'example', + scope: '', + shortHash: 'a12345', + revertedHashes: ['b12345'] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: ['d12345'] + } as unknown as GitCommit + ], + output: [ + { + type: 'example', + scope: '', + shortHash: 'a12345', + revertedHashes: ['b12345'] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: ['d12345'] + } as unknown as GitCommit + ] + }, + { + name: 'remove revert commits if the commit they revert is within current version', + input: [ + { + type: 'example', + scope: '', + shortHash: 'a12345', + revertedHashes: ['b12345'] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'b12345', + revertedHashes: [] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: [] + } as unknown as GitCommit + ], + output: [ + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: [] + } as unknown as GitCommit + ] + } + ] + + const config: ChangelogConfig = { + types: { + example: { title: 'Example' } + }, + scopeMap: undefined, + github: '', + from: '', + to: '' + } + + for (const test of tests) { + it(test.name, () => { + expect(filterCommits(test.input, config)).toStrictEqual(test.output) + }) + } +}) From ce46c98bf444c090043ae292081bc80bc5b5897e Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 19:17:33 +0800 Subject: [PATCH 05/10] test: add missing properties --- test/filter_commits.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/filter_commits.test.ts b/test/filter_commits.test.ts index 5467de8c..b666ff48 100644 --- a/test/filter_commits.test.ts +++ b/test/filter_commits.test.ts @@ -75,7 +75,9 @@ describe('automatic resolving of revert commits', () => { scopeMap: undefined, github: '', from: '', - to: '' + to: '', + cwd: '', + output: '' } for (const test of tests) { From b6a607390b754035b9ea63629e5c684a0096ab5f Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 19:39:52 +0800 Subject: [PATCH 06/10] chore: move tests to main test file --- test/filter_commits.test.ts | 88 ----------------------------- test/git.test.ts | 107 +++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 89 deletions(-) delete mode 100644 test/filter_commits.test.ts diff --git a/test/filter_commits.test.ts b/test/filter_commits.test.ts deleted file mode 100644 index b666ff48..00000000 --- a/test/filter_commits.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { it, describe, expect } from 'vitest' -import type { ChangelogConfig } from '../src/config' -import { filterCommits, GitCommit } from '../src/git' - -describe('automatic resolving of revert commits', () => { - const tests: ({ 'name': string, input: GitCommit[], output: GitCommit[] })[] = [ - { - name: 'retain revert commits if the commit they revert is from previous version', - input: [ - { - type: 'example', - scope: '', - shortHash: 'a12345', - revertedHashes: ['b12345'] - } as unknown as GitCommit, - { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: ['d12345'] - } as unknown as GitCommit - ], - output: [ - { - type: 'example', - scope: '', - shortHash: 'a12345', - revertedHashes: ['b12345'] - } as unknown as GitCommit, - { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: ['d12345'] - } as unknown as GitCommit - ] - }, - { - name: 'remove revert commits if the commit they revert is within current version', - input: [ - { - type: 'example', - scope: '', - shortHash: 'a12345', - revertedHashes: ['b12345'] - } as unknown as GitCommit, - { - type: 'example', - scope: '', - shortHash: 'b12345', - revertedHashes: [] - } as unknown as GitCommit, - { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: [] - } as unknown as GitCommit - ], - output: [ - { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: [] - } as unknown as GitCommit - ] - } - ] - - const config: ChangelogConfig = { - types: { - example: { title: 'Example' } - }, - scopeMap: undefined, - github: '', - from: '', - to: '', - cwd: '', - output: '' - } - - for (const test of tests) { - it(test.name, () => { - expect(filterCommits(test.input, config)).toStrictEqual(test.output) - }) - } -}) diff --git a/test/git.test.ts b/test/git.test.ts index e0d50fbb..411a2e1f 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -1,9 +1,12 @@ import { describe, expect, test } from "vitest"; +import { GitCommit } from '../src/git' +import type { ChangelogConfig } from '../src/config' import { generateMarkDown, getGitDiff, loadChangelogConfig, parseCommits, + filterCommits, } from "../src"; describe("git", () => { @@ -130,6 +133,7 @@ describe("git", () => { "value": "3828bda", }, ], + "revertedHashes": [], "scope": "release", "shortHash": "3828bda", "type": "chore", @@ -152,6 +156,7 @@ describe("git", () => { "value": "20e622e", }, ], + "revertedHashes": [], "scope": "scope", "shortHash": "20e622e", "type": "fix", @@ -166,6 +171,7 @@ describe("git", () => { "value": "6fc5087", }, ], + "revertedHashes": [], "scope": "release", "shortHash": "6fc5087", "type": "chore", @@ -184,6 +190,7 @@ describe("git", () => { "value": "c0febf1", }, ], + "revertedHashes": [], "scope": "", "shortHash": "c0febf1", "type": "feat", @@ -198,6 +205,7 @@ describe("git", () => { "value": "f4f42a3", }, ], + "revertedHashes": [], "scope": "release", "shortHash": "f4f42a3", "type": "chore", @@ -212,6 +220,7 @@ describe("git", () => { "value": "648ccf1", }, ], + "revertedHashes": [], "scope": "", "shortHash": "648ccf1", "type": "fix", @@ -226,6 +235,7 @@ describe("git", () => { "value": "5451f18", }, ], + "revertedHashes": [], "scope": "", "shortHash": "5451f18", "type": "feat", @@ -240,6 +250,7 @@ describe("git", () => { "value": "8796cf1", }, ], + "revertedHashes": [], "scope": "", "shortHash": "8796cf1", "type": "chore", @@ -254,6 +265,7 @@ describe("git", () => { "value": "c210976", }, ], + "revertedHashes": [], "scope": "", "shortHash": "c210976", "type": "chore", @@ -272,6 +284,7 @@ describe("git", () => { "value": "a80e372", }, ], + "revertedHashes": [], "scope": "deps", "shortHash": "a80e372", "type": "chore", @@ -312,7 +325,99 @@ describe("git", () => { ### ❤️ Contributors - - Pooya Parsa " + - Pooya Parsa ([@pi0](http://github.com/pi0))" `); }); + + test("filterCommits should retain reverts from previous version", async () => { + const inputLog = [ + { + type: 'example', + scope: '', + shortHash: 'a12345', + revertedHashes: ['b12345'] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: ['d12345'] + } as unknown as GitCommit + ]; + const config: ChangelogConfig = { + types: { + example: { title: 'Example' } + }, + scopeMap: undefined, + github: '', + from: '', + to: '', + cwd: '', + output: '' + } + + const resolvedLog = filterCommits(inputLog, config) + expect(resolvedLog).toStrictEqual( + [ + { + type: 'example', + scope: '', + shortHash: 'a12345', + revertedHashes: ['b12345'] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: ['d12345'] + } as unknown as GitCommit + ] + ); + }); + + test("filterCommits should remove reverts from upcoming version", async () => { + const inputLog = [ + { + type: 'example', + scope: '', + shortHash: 'a12345', + revertedHashes: ['b12345'] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'b12345', + revertedHashes: [] + } as unknown as GitCommit, + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: [] + } as unknown as GitCommit + ]; + const config: ChangelogConfig = { + types: { + example: { title: 'Example' } + }, + scopeMap: undefined, + github: '', + from: '', + to: '', + cwd: '', + output: '' + } + + const resolvedLog = filterCommits(inputLog, config) + expect(resolvedLog).toStrictEqual( + [ + { + type: 'example', + scope: '', + shortHash: 'c12345', + revertedHashes: [] + } as unknown as GitCommit + ] + ); + }); }); From 7c10548ba5cc686b71876f2b38df1523aac24294 Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 19:48:34 +0800 Subject: [PATCH 07/10] fix: use lowercase letters to match the hash characters --- src/git.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index 90275a79..b17a0d00 100644 --- a/src/git.ts +++ b/src/git.ts @@ -99,7 +99,7 @@ const ConventionalCommitRegex = const CoAuthoredByRegex = /co-authored-by:\s*(?.+)(<(?.+)>)/gim; const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm; const IssueRE = /(#\d+)/gm; -const RevertHashRegex = /This reverts commit (?[A-F0-9]{40})./gm; +const RevertHashRegex = /This reverts commit (?[a-f0-9]{40})./gm; export function parseGitCommit( commit: RawGitCommit, From 125450410556e4ca0ba4e5a4cbd9bd1c0c9fcaad Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sun, 19 Feb 2023 19:53:26 +0800 Subject: [PATCH 08/10] fix(revert resolver): correct the group name --- src/git.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/git.ts b/src/git.ts index b17a0d00..2807a4d6 100644 --- a/src/git.ts +++ b/src/git.ts @@ -99,7 +99,7 @@ const ConventionalCommitRegex = const CoAuthoredByRegex = /co-authored-by:\s*(?.+)(<(?.+)>)/gim; const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm; const IssueRE = /(#\d+)/gm; -const RevertHashRegex = /This reverts commit (?[a-f0-9]{40})./gm; +const RevertHashRE = /This reverts commit (?[a-f0-9]{40})./gm; export function parseGitCommit( commit: RawGitCommit, @@ -135,9 +135,9 @@ export function parseGitCommit( // Extract the reverted hashes. const revertedHashes = [] - const matchedHashes = commit.body.matchAll(RevertHashRegex) + const matchedHashes = commit.body.matchAll(RevertHashRE) for (const matchedHash of matchedHashes) { - revertedHashes.push(matchedHash.groups.match) + revertedHashes.push(matchedHash.groups.hash) } // Find all authors From 805d93df90382e72319653f2a8e0a7c12a1ca0b6 Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Mon, 20 Feb 2023 21:04:44 +0800 Subject: [PATCH 09/10] chore: tidy the code --- src/cli.ts | 4 +- src/git.ts | 58 ++++++++++++--------- test/git.test.ts | 132 +++++++++++++++++++++++------------------------ 3 files changed, 100 insertions(+), 94 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index d5ad05bb..78dc41ac 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -27,8 +27,8 @@ async function main() { const rawCommits = await getGitDiff(config.from, config.to); // Parse commits as conventional commits - const commits = parseCommits(rawCommits, config) - const filteredCommits = filterCommits(commits, config) + const commits = parseCommits(rawCommits, config); + const filteredCommits = filterCommits(commits, config); // Bump version optionally if (args.bump || args.release) { diff --git a/src/git.ts b/src/git.ts index 2807a4d6..addab2ef 100644 --- a/src/git.ts +++ b/src/git.ts @@ -28,8 +28,8 @@ export interface GitCommit extends RawGitCommit { } export interface RevertPair { - shortRevertingHash: string - revertedHash: string + shortRevertingHash: string; + revertedHash: string; } export async function getLastGitTag() { @@ -99,7 +99,7 @@ const ConventionalCommitRegex = const CoAuthoredByRegex = /co-authored-by:\s*(?.+)(<(?.+)>)/gim; const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm; const IssueRE = /(#\d+)/gm; -const RevertHashRE = /This reverts commit (?[a-f0-9]{40})./gm; +const RevertHashRE = /This reverts commit (?[\da-f]{40})./gm; export function parseGitCommit( commit: RawGitCommit, @@ -134,10 +134,10 @@ export function parseGitCommit( description = description.replace(PullRequestRE, "").trim(); // Extract the reverted hashes. - const revertedHashes = [] - const matchedHashes = commit.body.matchAll(RevertHashRE) + const revertedHashes = []; + const matchedHashes = commit.body.matchAll(RevertHashRE); for (const matchedHash of matchedHashes) { - revertedHashes.push(matchedHash.groups.hash) + revertedHashes.push(matchedHash.groups.hash); } // Find all authors @@ -161,46 +161,56 @@ export function parseGitCommit( }; } -export function filterCommits (commits: GitCommit[], config: ChangelogConfig): GitCommit[] { +export function filterCommits( + commits: GitCommit[], + config: ChangelogConfig +): GitCommit[] { const commitsWithNoDeps = commits.filter( (c) => - config.types[c.type] && - !(c.type === "chore" && c.scope === "deps" && !c.isBreaking) + config.types[c.type] && + !(c.type === "chore" && c.scope === "deps" && !c.isBreaking) ); - let resolvedCommits: GitCommit[] = [] - let revertWatchList: RevertPair[] = [] + let resolvedCommits: GitCommit[] = []; + let revertWatchList: RevertPair[] = []; for (const commit of commitsWithNoDeps) { // Include the reverted hashes in the watch list if (commit.revertedHashes.length > 0) { - revertWatchList.push(...commit.revertedHashes.map(revertedHash => ({ - revertedHash, - shortRevertingHash: commit.shortHash - } as RevertPair))) + revertWatchList.push( + ...commit.revertedHashes.map( + (revertedHash) => + ({ + revertedHash, + shortRevertingHash: commit.shortHash, + } as RevertPair) + ) + ); } // Find the commits which revert the current commit being evaluated - const shortRevertingHashes = revertWatchList.filter( - pair => pair.revertedHash.startsWith(commit.shortHash) - ).map(pair => pair.shortRevertingHash) + const shortRevertingHashes = revertWatchList + .filter((pair) => pair.revertedHash.startsWith(commit.shortHash)) + .map((pair) => pair.shortRevertingHash); if (shortRevertingHashes.length > 0) { // Remove commits that reverts this current commit resolvedCommits = resolvedCommits.filter( - resolvedCommit => !shortRevertingHashes.includes(resolvedCommit.shortHash) - ) + (resolvedCommit) => + !shortRevertingHashes.includes(resolvedCommit.shortHash) + ); // Unwatch reverting hashes that has been resolved revertWatchList = revertWatchList.filter( - watchedRevert => !shortRevertingHashes.includes(watchedRevert.shortRevertingHash) - ) + (watchedRevert) => + !shortRevertingHashes.includes(watchedRevert.shortRevertingHash) + ); } else { // If the current commit is known not to have been reverted, put it to resolved commits. - resolvedCommits = [...resolvedCommits, commit] + resolvedCommits = [...resolvedCommits, commit]; } } - return resolvedCommits + return resolvedCommits; } async function execCommand(cmd: string, args: string[]) { diff --git a/test/git.test.ts b/test/git.test.ts index 411a2e1f..8930a1a6 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "vitest"; -import { GitCommit } from '../src/git' -import type { ChangelogConfig } from '../src/config' +import { GitCommit } from "../src/git"; +import type { ChangelogConfig } from "../src/config"; import { generateMarkDown, getGitDiff, @@ -329,95 +329,91 @@ describe("git", () => { `); }); - test("filterCommits should retain reverts from previous version", async () => { + test("filterCommits should retain reverts from previous version", () => { const inputLog = [ { - type: 'example', - scope: '', - shortHash: 'a12345', - revertedHashes: ['b12345'] + type: "example", + scope: "", + shortHash: "a12345", + revertedHashes: ["b12345"], } as unknown as GitCommit, { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: ['d12345'] - } as unknown as GitCommit + type: "example", + scope: "", + shortHash: "c12345", + revertedHashes: ["d12345"], + } as unknown as GitCommit, ]; const config: ChangelogConfig = { types: { - example: { title: 'Example' } + example: { title: "Example" }, }, scopeMap: undefined, - github: '', - from: '', - to: '', - cwd: '', - output: '' - } + github: "", + from: "", + to: "", + cwd: "", + output: "", + }; - const resolvedLog = filterCommits(inputLog, config) - expect(resolvedLog).toStrictEqual( - [ - { - type: 'example', - scope: '', - shortHash: 'a12345', - revertedHashes: ['b12345'] - } as unknown as GitCommit, - { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: ['d12345'] - } as unknown as GitCommit - ] - ); + const resolvedLog = filterCommits(inputLog, config); + expect(resolvedLog).toStrictEqual([ + { + type: "example", + scope: "", + shortHash: "a12345", + revertedHashes: ["b12345"], + } as unknown as GitCommit, + { + type: "example", + scope: "", + shortHash: "c12345", + revertedHashes: ["d12345"], + } as unknown as GitCommit, + ]); }); - test("filterCommits should remove reverts from upcoming version", async () => { + test("filterCommits should remove reverts from upcoming version", () => { const inputLog = [ { - type: 'example', - scope: '', - shortHash: 'a12345', - revertedHashes: ['b12345'] + type: "example", + scope: "", + shortHash: "a12345", + revertedHashes: ["b12345"], } as unknown as GitCommit, { - type: 'example', - scope: '', - shortHash: 'b12345', - revertedHashes: [] + type: "example", + scope: "", + shortHash: "b12345", + revertedHashes: [], } as unknown as GitCommit, { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: [] - } as unknown as GitCommit + type: "example", + scope: "", + shortHash: "c12345", + revertedHashes: [], + } as unknown as GitCommit, ]; const config: ChangelogConfig = { types: { - example: { title: 'Example' } + example: { title: "Example" }, }, scopeMap: undefined, - github: '', - from: '', - to: '', - cwd: '', - output: '' - } + github: "", + from: "", + to: "", + cwd: "", + output: "", + }; - const resolvedLog = filterCommits(inputLog, config) - expect(resolvedLog).toStrictEqual( - [ - { - type: 'example', - scope: '', - shortHash: 'c12345', - revertedHashes: [] - } as unknown as GitCommit - ] - ); + const resolvedLog = filterCommits(inputLog, config); + expect(resolvedLog).toStrictEqual([ + { + type: "example", + scope: "", + shortHash: "c12345", + revertedHashes: [], + } as unknown as GitCommit, + ]); }); }); From 2e119191e86e7514150bee4b72e90909af514560 Mon Sep 17 00:00:00 2001 From: Kenneth Trecy Tobias Date: Sat, 4 Mar 2023 11:37:59 +0800 Subject: [PATCH 10/10] chore: re-apply the changes to filter commits --- src/commands/default.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index 29cf44de..bcd05002 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -9,6 +9,7 @@ import { parseCommits, bumpVersion, generateMarkDown, + filterCommits, } from ".."; import { githubRelease } from "./github"; @@ -35,6 +36,7 @@ export default async function defaultMain(args: Argv) { config.types[c.type] && !(c.type === "chore" && c.scope === "deps" && !c.isBreaking) ); + const filteredCommits = filterCommits(commits, config); // Bump version optionally if (args.bump || args.release) { @@ -46,7 +48,7 @@ export default async function defaultMain(args: Argv) { } else if (args.patch) { type = "patch"; } - const newVersion = await bumpVersion(commits, config, { type }); + const newVersion = await bumpVersion(filteredCommits, config, { type }); if (!newVersion) { consola.error("Unable to bump version based on changes."); process.exit(1); @@ -55,7 +57,7 @@ export default async function defaultMain(args: Argv) { } // Generate markdown - const markdown = await generateMarkDown(commits, config); + const markdown = await generateMarkDown(filteredCommits, config); // Show changelog in CLI unless bumping or releasing const displayOnly = !args.bump && !args.release;