Skip to content

Commit

Permalink
fix: I messed up a little, so this is a working version again
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jul 15, 2022
1 parent 6d20004 commit 19d2d70
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/changelogGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = function(config) {
commitMarkdown += ` **Revert**`;
}

commitMarkdown += ` ${escapeMarkdown(commit.subject)}`;
commitMarkdown += ` ${escapeMarkdown(commit.description)}`;

commitMarkdown += ` (${markdownCommitHash(commit.hash)}`;
if (commit.relatedIssues) {
Expand Down
33 changes: 25 additions & 8 deletions lib/commands/commandCommitMessageHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,31 @@ exports.handler = async function(argv) {
});

if (!conventionalCommit.type) {
console.error(``);
console.error(`[ERROR] Invalid commit message`);
console.error(``);
console.error(` Valid message pattern: <TYPE>[(<SCOPE>)]: <SUBJECT>`);
console.error(` Valid types : ${convention.commitTypes.map(e => `'${e}'`).join(', ')}`);
console.error(` Valid scopes: ${!convention.commitScopes || !convention.commitScopes.length? '*' : convention.commitScopes.map(e => `'${e}'`).join(', ')}`);
console.error(``);
console.error(` To adjust convention edit config file (${argv.config})`);
console.error('[ERROR] Invalid commit message');
console.error(` Valid message pattern: <TYPE>[(<SCOPE>)]: <DESCRIPTION>`);
process.exit(2);
}

let conventionError = false

if (conventionalCommit.type &&
convention.commitTypes && convention.commitTypes.length &&
!convention.commitTypes.includes(conventionalCommit.type)) {
conventionError = true
console.error(`[ERROR] Unexpected commit type: '${conventionalCommit.type}'`);
console.error(` Permitted types : ${convention.commitTypes.map(e => `'${e}'`).join(', ')}`);
}
if (conventionalCommit.scope &&
convention.commitScopes && convention.commitScopes.length &&
!convention.commitScopes.includes(conventionalCommit.scope)) {
conventionError = true
console.error(`[ERROR] Unexpected commit scope: '${conventionalCommit.scope}'`);
console.error(` Permitted scopes: ${!convention.commitScopes || !convention.commitScopes.length ? '*' : convention.commitScopes.map(e => `'${e}'`).join(', ')}`);
}

if (conventionError) {
console.error()
console.error(`To adjust convention edit config file (${argv.config})`);
process.exit(2);
}
}
32 changes: 23 additions & 9 deletions lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,25 @@ function defaultConfig() {
const config = {
convention: {
releaseTagGlobPattern: "*",
commitTypes: ['feat', 'fix'],
commitTypes: [
'feat',
'fix',
'perf',
'refactor',
'style',
'test',
'build',
'ops',
'docs',
'chore',
'merge',
],
featureCommitTypes: ['feat'],
commitScopes: null,
issueRegexPattern: null,
},
changelog: {
commitTypes: ['feat', 'fix'],
commitTypes: ['feat', 'fix', 'perf'],
includeInvalidCommits: true,
commitScopes: null,
commitIgnoreRegexPattern: null,
Expand All @@ -77,19 +89,21 @@ function defaultConfig() {
config.convention.msgMergeRegex = /^Merge branch +["'](?<branch>.+)['"]/i;
config.convention.msgRevertRegex = /^Revert +["'](?<subject>.+)['"]/i;
config.convention.bodyRevertRegex = /(?<hash>[^\s]+)\.+?$/im;
config.convention.semanticVersionRegex = /(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<patch>[0-9]+)/;
config.convention.semanticVersionRegex = /(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/;

config.changelog.commitIgnoreRegex = () => RegExp(config.changelog.commitIgnoreRegexPattern || '(?!)');

config.convention.issueRegex = () => RegExp("(?<=(^|\\s))" + (config.convention.issueRegexPattern || '(?!)') + "(?=(\\s|$))",
"mg");
config.convention.issueRegex = () => RegExp(
"(?<=(^|\\s))" + (config.convention.issueRegexPattern || '(?!)') + "(?=(\\s|$))",
"mg"
);

return config;
}

module.exports = {
defaultPath: defaultPath,
templatePath: templatePath,
load: load,
defaultConfig: defaultConfig,
defaultPath,
templatePath,
load,
defaultConfig,
}
53 changes: 19 additions & 34 deletions lib/gitCommitConvention.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,65 +75,50 @@ module.exports = function(convention, commitAnchor = 'HEAD') {
}
delete conventionalCommit.breaking;


// parse issue ids
conventionalCommit.relatedIssues = [commit.subject, commit.body].join("\n\n").match(convention.issueRegex()) || [];

return conventionalCommit;
}

function parseCommitSubject(commit) {
const subject = commit.description;
let conventionalSubject;
const msgMatch = subject.match(convention.msgRegex);
let conventionalSubject = {
...commit,
description: commit.subject,
};
const msgMatch = commit.subject.match(convention.msgRegex);
if (msgMatch) {
conventionalSubject = {
type: msgMatch.groups.type,
scope: msgMatch.groups.scope === '' ? undefined : msgMatch.groups.scope,
breaking: msgMatch.groups.breaking === '!',
description: msgMatch.groups.subject
}
conventionalSubject.type = msgMatch.groups.type;
conventionalSubject.scope = msgMatch.groups.scope === '' ? undefined : msgMatch.groups.scope,
conventionalSubject.breaking = msgMatch.groups.breaking === '!';
conventionalSubject.description = msgMatch.groups.description;
} else {
const msgMergeMatch = subject.match(convention.msgMergeRegex);
const msgMergeMatch = commit.subject.match(convention.msgMergeRegex);
if (msgMergeMatch) {
conventionalSubject = {
type: 'merge',
description: msgMergeMatch.groups.branch
};
conventionalSubject.type = 'merge';
conventionalSubject.description = msgMergeMatch.groups.branch;
} else {
const msgRevertMatch = subject.match(convention.msgRevertRegex);
const msgRevertMatch = commit.subject.match(convention.msgRevertRegex);
if (msgRevertMatch) {
conventionalSubject = parseCommitSubject({
...commit,
description: msgRevertMatch.groups.description
});
conventionalSubject.subject = msgRevertMatch.groups.description;
conventionalSubject = parseCommitSubject(conventionalSubject);
conventionalSubject.revert = !conventionalSubject.revert; // negate revert of revert commit
} else {
console.error(`[WARN] Invalid commit subject format: '${subject}'${commit.hash ? ` (${commit.hash})` : ''}`);
conventionalSubject = {
description: subject
};
console.warn(`[WARN] Invalid commit subject format: '${commit.description}'${commit.hash ? ` (${commit.hash})` : ''}`);
return conventionalSubject;
}
}
}

if (conventionalSubject.type &&
convention.commitTypes && convention.commitTypes.length &&
!convention.commitTypes.includes(conventionalSubject.type)) {
console.error(
`[WARN] Unexpected commit type: '${conventionalSubject.type}'${commit.hash ? ` (${commit.hash})` : ''}`);
return {
subject: subject
}
console.warn(`[WARN] Unexpected commit type: '${conventionalSubject.type}'${commit.hash ? ` (${commit.hash})` : ''}`);
}
if (conventionalSubject.scope &&
convention.commitScopes && convention.commitScopes.length &&
!convention.commitScopes.includes(conventionalSubject.scope)) {
console.error(
`[WARN] Unexpected commit scope: '${conventionalSubject.scope}'${commit.hash ? ` (${commit.hash})` : ''}`);
return {
subject: subject
}
console.warn(`[WARN] Unexpected commit scope: '${conventionalSubject.scope}'${commit.hash ? ` (${commit.hash})` : ''}`);
}

return conventionalSubject;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-conventional-commits",
"version": "1.4.1",
"version": "1.4.2",
"description": "git conventional commits util",
"licence": "GPLv3",
"main": "cli.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const fs = require("fs");
const path = require("path");
const temp = require("tmp-promise");
const execAsync = require("../lib/execAsync");
const execAsync = require("../../lib/execAsync");

const commandChangelog = require("../../lib/commands/commandChangelog");

const commandChangelog = require("../lib/commands/commandChangelog");

beforeEach(async () => {
const gitDir = await temp.dir();
process.chdir(gitDir.path);
const tempDirectory = await temp.dir();
process.chdir(tempDirectory.path);

await execAsync("git init");

const config = {}
fs.writeFileSync("git-conventional-commits.json", JSON.stringify(config), 'utf8');
});

const createSimpleChangelog = async (commitSubject, commitBody) => {
Expand All @@ -30,12 +35,11 @@ const createSimpleChangelog = async (commitSubject, commitBody) => {
if (commitBody) {
gitCmd += ` -m "${commitBody}"`;
}

await execAsync(gitCmd);

// WHEN
await commandChangelog.handler({
config: path.resolve(__dirname, "../git-conventional-commits.default.json"),
config: "./git-conventional-commits.json",
file: changelogFile,
});

Expand Down
24 changes: 12 additions & 12 deletions test/gitCommitConvention.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const Config = require('../lib/commands/config');
const CommitConvention = require('../lib/gitCommitConvention');

// this.parseCommit = parseCommit;
// this.commitLog = getCommitLog;
// this.version = getVersion;

test('parseCommit', async () => {

// GIVEN
Expand All @@ -24,10 +20,11 @@ test('parseCommit', async () => {
// THEN
expect(conventionalCommit).toEqual({
hash: commit.hash,
subject: commit.subject,
body: commit.body,
type: 'feat',
scope: 'ui',
subject: 'new shit',
body: commit.body,
description: 'new shit',
breakingChanges: [],
relatedIssues: []
})
Expand All @@ -52,10 +49,11 @@ test('parseCommit - breaking changes - description', async () => {
// THEN
expect(conventionalCommit).toEqual({
hash: commit.hash,
subject: commit.subject,
body: commit.body,
type: 'feat',
scope: undefined,
subject: 'Ditch support of windows XP',
body: commit.body,
description: 'Ditch support of windows XP',
breakingChanges: ["Ditch support of windows XP"],
relatedIssues: []
})
Expand All @@ -80,10 +78,11 @@ test('parseCommit - breaking changes - body', async () => {
// THEN
expect(conventionalCommit).toEqual({
hash: commit.hash,
subject: commit.subject,
body: commit.body,
type: 'feat',
scope: undefined,
subject: 'new shit',
body: commit.body,
description: 'new shit',
breakingChanges: ["Ditch support of windows XP"],
relatedIssues: []
})
Expand All @@ -109,10 +108,11 @@ test('parseCommit - issue reference', async () => {
// THEN
expect(conventionalCommit).toEqual({
hash: commit.hash,
subject: commit.subject,
body: commit.body,
type: 'feat',
scope: undefined,
subject: 'new shit',
body: commit.body,
description: 'new shit',
breakingChanges: [],
relatedIssues: ['PROJECT-123']
})
Expand Down

0 comments on commit 19d2d70

Please sign in to comment.