Skip to content

Commit

Permalink
feat: add fallbackTicketNumber (#239)
Browse files Browse the repository at this point in the history
* feat: add fallbackTicketNumber

* chore: add fallbackTicketNumber to readme and index.d.ts
  • Loading branch information
xander-marjoram authored Jul 18, 2024
1 parent 4392762 commit 834deaa
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Here are the options you can set in your `.cz-config.js`:
* **appendBranchNameToCommitMessage**: If you use `cz-customizable` with `cz-customizable-ghooks`, you can get the branch name automatically appended to the commit message. This is done by a commit hook on `cz-customizable-ghooks`. This option has been added on `cz-customizable-ghooks`, v1.3.0. Default value is `true`.
* **ticketNumberPrefix**: {string, default 'ISSUES CLOSED:'}: Set custom prefix for footer ticker number.
* **ticketNumberSuffix**: {string, default ''}: Set custom suffix for footer ticker number.
* **fallbackTicketNumber**: {string, default ''}: Set fallback ticket number which will be used if `ticketNumber` is not provided.
* **breakingPrefix**: {string, default 'BREAKING CHANGE:'}: Set a custom prefix for the breaking change block in commit messages.
* **footerPrefix**: {string, default 'ISSUES CLOSED:'}: Set a custom prefix for the footer block in commit messages. Set to empty string to remove prefix.
* **breaklineChar**: {string, default '|'}: It gets replaced with \n to create the breakline in your commit message. This is supported for fields `body` and `footer` at the moment.
Expand Down
72 changes: 72 additions & 0 deletions __tests__/build-commit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,78 @@ describe('buildCommit()', () => {
});
});

describe('ticket number', () => {
it('subject with ticket number', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
ticketNumber: '123',
};
const options = {};
expect(buildCommit(answersNoScope, options)).toEqual('feat: 123 this is a new feature');
});

it('subject with ticket number prefix but no scope', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
ticketNumber: '123',
};
const options = {
ticketNumberPrefix: 'PREFIX-',
};
expect(buildCommit(answersNoScope, options)).toEqual('feat: PREFIX-123 this is a new feature');
});

it('subject with scope, ticket number and prefix', () => {
const answersNoScope = {
type: 'feat',
scope: 'app',
subject: 'this is a new feature',
ticketNumber: '123',
};
const options = {
ticketNumberPrefix: 'PREFIX-',
};
expect(buildCommit(answersNoScope, options)).toEqual('feat(app): PREFIX-123 this is a new feature');
});

it('subject with no ticket number or scope but with fallback', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
};
const options = {
fallbackTicketNumber: '000',
};
expect(buildCommit(answersNoScope, options)).toEqual('feat: 000 this is a new feature');
});

it('subject with prefix and fallback ticket number', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
};
const options = {
ticketNumberPrefix: 'PREFIX-',
fallbackTicketNumber: '000',
};
expect(buildCommit(answersNoScope, options)).toEqual('feat: PREFIX-000 this is a new feature');
});

it('subject with ticket number and fallback', () => {
const answersNoScope = {
type: 'feat',
subject: 'should not use fallback',
ticketNumber: '123',
};
const options = {
fallbackTicketNumber: '000',
};
expect(buildCommit(answersNoScope, options)).toEqual('feat: 123 should not use fallback');
});
});

describe('type prefix and type suffix', () => {
it('subject with both', () => {
const answersNoScope = {
Expand Down
27 changes: 26 additions & 1 deletion __tests__/questions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,32 @@ describe('cz-customizable', () => {
expect(getQuestion(5).filter('Some subject')).toEqual('some subject');
});

it('subject should be capitilized when config property "upperCaseSubject" is set to true', () => {
it('subject should be capitalized when config property "upperCaseSubject" is set to true', () => {
config = {
upperCaseSubject: true,
};

expect(getQuestion(5).filter('some subject')).toEqual('Some subject');
});

it('message should contain regexp and fallbackTicketNumber if given', () => {
config = {
ticketNumberRegExp: '\\d{1,5}',
fallbackTicketNumber: '12345',
};

expect(getQuestion(4).message).toContain('Enter the ticket number following this pattern');
expect(getQuestion(4).message).toContain(', default: 12345)');
});

it('message should contain fallbackTicketNumber if given', () => {
config = {
fallbackTicketNumber: '12345',
};

expect(getQuestion(4).message).toContain('(default: 12345)');
});

describe('optional fixOverride and allowBreakingChanges', () => {
it('should restrict BREAKING CHANGE question when config property "allowBreakingChanges" specifies array of types', () => {
config = {
Expand Down Expand Up @@ -260,6 +278,13 @@ describe('cz-customizable', () => {
};
expect(getQuestion(4).validate('12345')).toEqual(true);
});
it('valid because fallbackTicketNumber provided', () => {
config = {
isTicketNumberRequired: true,
fallbackTicketNumber: '12345',
};
expect(getQuestion(4).validate('')).toEqual(true);
});
});
});
});
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ declare module "cz-customizable" {
allowBreakingChanges?: string[];
skipQuestions?: string[];
appendBranchNameToCommitMessage?: boolean;
fallbackTicketNumber?: string;
ticketNumberPrefix?: string;
ticketNumberSuffix?:string;
ticketNumberSuffix?: string;
breakingPrefix?: string;
footerPrefix?: string;
subjectLimit?: number;
Expand Down
6 changes: 5 additions & 1 deletion lib/build-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const defaultBreaklineChar = '|';

const addTicketNumber = (ticketNumber, config) => {
if (!ticketNumber) {
return '';
if(!config.fallbackTicketNumber) {
return '';
} else {
ticketNumber = config.fallbackTicketNumber;
}
}

let trimmedTicketNumber = ticketNumber.trim();
Expand Down
8 changes: 6 additions & 2 deletions lib/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const isNotWip = (answers) => answers.type.toLowerCase() !== 'wip';

const isValidateTicketNo = (value, config) => {
if (!value) {
return !config.isTicketNumberRequired;
return !config.isTicketNumberRequired || !!config.fallbackTicketNumber;
}
if (!config.ticketNumberRegExp) {
return true;
Expand Down Expand Up @@ -55,7 +55,11 @@ module.exports = {
messages.scope = messages.scope || '\nDenote the SCOPE of this change (optional):';
messages.customScope = messages.customScope || 'Denote the SCOPE of this change:';
if (!messages.ticketNumber) {
if (config.ticketNumberRegExp) {
if (config.fallbackTicketNumber && config.ticketNumberRegExp) {
messages.ticketNumber = `Enter the ticket number following this pattern (${config.ticketNumberRegExp}, default: ${config.fallbackTicketNumber}):\n`;
} else if (config.fallbackTicketNumber && !config.ticketNumberRegExp) {
messages.ticketNumber = `Enter the ticket number (default: ${config.fallbackTicketNumber}):\n`;
} else if (config.ticketNumberRegExp) {
messages.ticketNumber =
messages.ticketNumberPattern ||
`Enter the ticket number following this pattern (${config.ticketNumberRegExp})\n`;
Expand Down

0 comments on commit 834deaa

Please sign in to comment.