Skip to content

Commit 834deaa

Browse files
feat: add fallbackTicketNumber (#239)
* feat: add fallbackTicketNumber * chore: add fallbackTicketNumber to readme and index.d.ts
1 parent 4392762 commit 834deaa

6 files changed

Lines changed: 112 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Here are the options you can set in your `.cz-config.js`:
153153
* **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`.
154154
* **ticketNumberPrefix**: {string, default 'ISSUES CLOSED:'}: Set custom prefix for footer ticker number.
155155
* **ticketNumberSuffix**: {string, default ''}: Set custom suffix for footer ticker number.
156+
* **fallbackTicketNumber**: {string, default ''}: Set fallback ticket number which will be used if `ticketNumber` is not provided.
156157
* **breakingPrefix**: {string, default 'BREAKING CHANGE:'}: Set a custom prefix for the breaking change block in commit messages.
157158
* **footerPrefix**: {string, default 'ISSUES CLOSED:'}: Set a custom prefix for the footer block in commit messages. Set to empty string to remove prefix.
158159
* **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.

__tests__/build-commit.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,78 @@ describe('buildCommit()', () => {
4848
});
4949
});
5050

51+
describe('ticket number', () => {
52+
it('subject with ticket number', () => {
53+
const answersNoScope = {
54+
type: 'feat',
55+
subject: 'this is a new feature',
56+
ticketNumber: '123',
57+
};
58+
const options = {};
59+
expect(buildCommit(answersNoScope, options)).toEqual('feat: 123 this is a new feature');
60+
});
61+
62+
it('subject with ticket number prefix but no scope', () => {
63+
const answersNoScope = {
64+
type: 'feat',
65+
subject: 'this is a new feature',
66+
ticketNumber: '123',
67+
};
68+
const options = {
69+
ticketNumberPrefix: 'PREFIX-',
70+
};
71+
expect(buildCommit(answersNoScope, options)).toEqual('feat: PREFIX-123 this is a new feature');
72+
});
73+
74+
it('subject with scope, ticket number and prefix', () => {
75+
const answersNoScope = {
76+
type: 'feat',
77+
scope: 'app',
78+
subject: 'this is a new feature',
79+
ticketNumber: '123',
80+
};
81+
const options = {
82+
ticketNumberPrefix: 'PREFIX-',
83+
};
84+
expect(buildCommit(answersNoScope, options)).toEqual('feat(app): PREFIX-123 this is a new feature');
85+
});
86+
87+
it('subject with no ticket number or scope but with fallback', () => {
88+
const answersNoScope = {
89+
type: 'feat',
90+
subject: 'this is a new feature',
91+
};
92+
const options = {
93+
fallbackTicketNumber: '000',
94+
};
95+
expect(buildCommit(answersNoScope, options)).toEqual('feat: 000 this is a new feature');
96+
});
97+
98+
it('subject with prefix and fallback ticket number', () => {
99+
const answersNoScope = {
100+
type: 'feat',
101+
subject: 'this is a new feature',
102+
};
103+
const options = {
104+
ticketNumberPrefix: 'PREFIX-',
105+
fallbackTicketNumber: '000',
106+
};
107+
expect(buildCommit(answersNoScope, options)).toEqual('feat: PREFIX-000 this is a new feature');
108+
});
109+
110+
it('subject with ticket number and fallback', () => {
111+
const answersNoScope = {
112+
type: 'feat',
113+
subject: 'should not use fallback',
114+
ticketNumber: '123',
115+
};
116+
const options = {
117+
fallbackTicketNumber: '000',
118+
};
119+
expect(buildCommit(answersNoScope, options)).toEqual('feat: 123 should not use fallback');
120+
});
121+
});
122+
51123
describe('type prefix and type suffix', () => {
52124
it('subject with both', () => {
53125
const answersNoScope = {

__tests__/questions.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,32 @@ describe('cz-customizable', () => {
115115
expect(getQuestion(5).filter('Some subject')).toEqual('some subject');
116116
});
117117

118-
it('subject should be capitilized when config property "upperCaseSubject" is set to true', () => {
118+
it('subject should be capitalized when config property "upperCaseSubject" is set to true', () => {
119119
config = {
120120
upperCaseSubject: true,
121121
};
122122

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

126+
it('message should contain regexp and fallbackTicketNumber if given', () => {
127+
config = {
128+
ticketNumberRegExp: '\\d{1,5}',
129+
fallbackTicketNumber: '12345',
130+
};
131+
132+
expect(getQuestion(4).message).toContain('Enter the ticket number following this pattern');
133+
expect(getQuestion(4).message).toContain(', default: 12345)');
134+
});
135+
136+
it('message should contain fallbackTicketNumber if given', () => {
137+
config = {
138+
fallbackTicketNumber: '12345',
139+
};
140+
141+
expect(getQuestion(4).message).toContain('(default: 12345)');
142+
});
143+
126144
describe('optional fixOverride and allowBreakingChanges', () => {
127145
it('should restrict BREAKING CHANGE question when config property "allowBreakingChanges" specifies array of types', () => {
128146
config = {
@@ -260,6 +278,13 @@ describe('cz-customizable', () => {
260278
};
261279
expect(getQuestion(4).validate('12345')).toEqual(true);
262280
});
281+
it('valid because fallbackTicketNumber provided', () => {
282+
config = {
283+
isTicketNumberRequired: true,
284+
fallbackTicketNumber: '12345',
285+
};
286+
expect(getQuestion(4).validate('')).toEqual(true);
287+
});
263288
});
264289
});
265290
});

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ declare module "cz-customizable" {
2121
allowBreakingChanges?: string[];
2222
skipQuestions?: string[];
2323
appendBranchNameToCommitMessage?: boolean;
24+
fallbackTicketNumber?: string;
2425
ticketNumberPrefix?: string;
25-
ticketNumberSuffix?:string;
26+
ticketNumberSuffix?: string;
2627
breakingPrefix?: string;
2728
footerPrefix?: string;
2829
subjectLimit?: number;

lib/build-commit.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const defaultBreaklineChar = '|';
77

88
const addTicketNumber = (ticketNumber, config) => {
99
if (!ticketNumber) {
10-
return '';
10+
if(!config.fallbackTicketNumber) {
11+
return '';
12+
} else {
13+
ticketNumber = config.fallbackTicketNumber;
14+
}
1115
}
1216

1317
let trimmedTicketNumber = ticketNumber.trim();

lib/questions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const isNotWip = (answers) => answers.type.toLowerCase() !== 'wip';
77

88
const isValidateTicketNo = (value, config) => {
99
if (!value) {
10-
return !config.isTicketNumberRequired;
10+
return !config.isTicketNumberRequired || !!config.fallbackTicketNumber;
1111
}
1212
if (!config.ticketNumberRegExp) {
1313
return true;
@@ -55,7 +55,11 @@ module.exports = {
5555
messages.scope = messages.scope || '\nDenote the SCOPE of this change (optional):';
5656
messages.customScope = messages.customScope || 'Denote the SCOPE of this change:';
5757
if (!messages.ticketNumber) {
58-
if (config.ticketNumberRegExp) {
58+
if (config.fallbackTicketNumber && config.ticketNumberRegExp) {
59+
messages.ticketNumber = `Enter the ticket number following this pattern (${config.ticketNumberRegExp}, default: ${config.fallbackTicketNumber}):\n`;
60+
} else if (config.fallbackTicketNumber && !config.ticketNumberRegExp) {
61+
messages.ticketNumber = `Enter the ticket number (default: ${config.fallbackTicketNumber}):\n`;
62+
} else if (config.ticketNumberRegExp) {
5963
messages.ticketNumber =
6064
messages.ticketNumberPattern ||
6165
`Enter the ticket number following this pattern (${config.ticketNumberRegExp})\n`;

0 commit comments

Comments
 (0)