Skip to content

Commit b6c6bfb

Browse files
committed
feat(engine.js): add jira step
add a new prompt step for jira id input
1 parent 353bf42 commit b6c6bfb

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# cz-conventional-changelog-with-jiraid-detection
22

3-
[![npm version](https://img.shields.io/npm/v/cz-conventional-changelog-with-jiraid-detection.svg?style=flat-square)](https://www.npmjs.org/package/cz-conventional-changelog-with-jiraid-detection)
4-
[![npm downloads](https://img.shields.io/npm/dm/cz-conventional-changelog-with-jiraid-detection.svg?style=flat-square)](http://npm-stat.com/charts.html?package=cz-conventional-changelog-with-jiraid-detection&from=2020-09-16)
3+
[![npm version](https://img.shields.io/npm/v/cz-conventional-changelog-with-jiraid-detection.svg?style=flat)](https://www.npmjs.org/package/cz-conventional-changelog-with-jiraid-detection)
4+
[![npm downloads](https://img.shields.io/npm/dm/cz-conventional-changelog-with-jiraid-detection.svg?style=flat)](http://npm-stat.com/charts.html?package=cz-conventional-changelog-with-jiraid-detection&from=2020-09-16)
55

6-
A fork of [cz-conventional-changelog-with-jiraid](https://github.com/commitizen/cz-conventional-changelog). Prompts for [conventional changelog](https://github.com/conventional-changelog/conventional-changelog) standard with an extra step to insert Jira ID. Jira ID is auto-detected from your branch name by default.
6+
A fork of [cz-conventional-changelog](https://github.com/commitizen/cz-conventional-changelog).
7+
8+
Prompts for [conventional changelog](https://github.com/conventional-changelog/conventional-changelog) standard with an extra step to insert Jira ID. Jira ID is auto-detected from your branch name by default.
9+
10+
Jira ID in the format of /^[A-Z]+-[0-9]+\$/ will be automatically detected from the current branch name and be displayed as default at the prompt step. The step is optional so you can skip it if you do not have a Jira ID.
711

812
## Configuration
913

@@ -44,13 +48,13 @@ Like commitizen, you specify the configuration of cz-conventional-changelog thro
4448

4549
The following environment varibles can be used to override any default configuration or package.json based configuration.
4650

47-
* CZ_TYPE = defaultType
48-
* CZ_SCOPE = defaultScope
49-
* CZ_SUBJECT = defaultSubject
50-
* CZ_BODY = defaultBody
51-
* CZ_MAX_HEADER_WIDTH = maxHeaderWidth
52-
* CZ_MAX_LINE_WIDTH = maxLineWidth
51+
- CZ_TYPE = defaultType
52+
- CZ_SCOPE = defaultScope
53+
- CZ_SUBJECT = defaultSubject
54+
- CZ_BODY = defaultBody
55+
- CZ_MAX_HEADER_WIDTH = maxHeaderWidth
56+
- CZ_MAX_LINE_WIDTH = maxLineWidth
5357

5458
### Commitlint
5559

56-
If using the [commitlint](https://github.com/conventional-changelog/commitlint) js library, the "maxHeaderWidth" configuration property will default to the configuration of the "header-max-length" rule instead of the hard coded value of 100. This can be ovewritten by setting the 'maxHeaderWidth' configuration in package.json or the CZ_MAX_HEADER_WIDTH environment variable.
60+
If using the [commitlint](https://github.com/conventional-changelog/commitlint) js library, the "maxHeaderWidth" configuration property will default to the configuration of the "header-max-length" rule instead of the hard coded value of 100. This can be ovewritten by setting the 'maxHeaderWidth' configuration in package.json or the CZ_MAX_HEADER_WIDTH environment variable.

engine.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var wrap = require('word-wrap');
44
var map = require('lodash.map');
55
var longest = require('longest');
66
var chalk = require('chalk');
7+
var branch = require('git-branch');
78

89
var filter = function(array) {
910
return array.filter(function(x) {
@@ -23,7 +24,10 @@ var maxSummaryLength = function(options, answers) {
2324

2425
var filterSubject = function(subject, disableSubjectLowerCase) {
2526
subject = subject.trim();
26-
if (!disableSubjectLowerCase && subject.charAt(0).toLowerCase() !== subject.charAt(0)) {
27+
if (
28+
!disableSubjectLowerCase &&
29+
subject.charAt(0).toLowerCase() !== subject.charAt(0)
30+
) {
2731
subject =
2832
subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
2933
}
@@ -47,6 +51,11 @@ module.exports = function(options) {
4751
};
4852
});
4953

54+
// detect jira id in branch name
55+
var branchName = branch.sync() || '';
56+
var jiraIdFound = branchName.match(/[A-Z]+-[0-9]+/) || [];
57+
var defaultJiraId = jiraIdFound.shift();
58+
5059
return {
5160
// When a user runs `git cz`, prompter will
5261
// be executed. We pass you cz, which currently
@@ -75,6 +84,22 @@ module.exports = function(options) {
7584
choices: choices,
7685
default: options.defaultType
7786
},
87+
{
88+
type: 'input',
89+
name: 'jira',
90+
message:
91+
"JIRA ID (e.g. WEB-12345): (press enter to skip if you don't have one)",
92+
default: defaultJiraId,
93+
validate: function(jira) {
94+
if (!jira) {
95+
return true;
96+
}
97+
return /^[A-Z]+-[0-9]+$/.test(jira);
98+
},
99+
filter: function(jira) {
100+
return jira ? jira.toUpperCase() : '';
101+
}
102+
},
78103
{
79104
type: 'input',
80105
name: 'scope',
@@ -99,7 +124,10 @@ module.exports = function(options) {
99124
},
100125
default: options.defaultSubject,
101126
validate: function(subject, answers) {
102-
var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase);
127+
var filteredSubject = filterSubject(
128+
subject,
129+
options.disableSubjectLowerCase
130+
);
103131
return filteredSubject.length == 0
104132
? 'subject is required'
105133
: filteredSubject.length <= maxSummaryLength(options, answers)
@@ -111,7 +139,10 @@ module.exports = function(options) {
111139
' characters.';
112140
},
113141
transformer: function(subject, answers) {
114-
var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase);
142+
var filteredSubject = filterSubject(
143+
subject,
144+
options.disableSubjectLowerCase
145+
);
115146
var color =
116147
filteredSubject.length <= maxSummaryLength(options, answers)
117148
? chalk.green
@@ -202,6 +233,9 @@ module.exports = function(options) {
202233
// Hard limit this line in the validate
203234
var head = answers.type + scope + ': ' + answers.subject;
204235

236+
// jira id will be injected into body
237+
var jiraid = answers.jira ? wrap(answers.jira, wrapOptions) : false;
238+
205239
// Wrap these lines at options.maxLineWidth characters
206240
var body = answers.body ? wrap(answers.body, wrapOptions) : false;
207241

@@ -214,7 +248,7 @@ module.exports = function(options) {
214248

215249
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;
216250

217-
commit(filter([head, body, breaking, issues]).join('\n\n'));
251+
commit(filter([head, jiraid, body, breaking, issues]).join('\n\n'));
218252
});
219253
}
220254
};

engine.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var chalk = require('chalk');
33
var engine = require('./engine');
44
var mock = require('mock-require');
55
var semver = require('semver');
6+
var branch = require('git-branch');
67

78
var types = require('conventional-commit-types').types;
89

@@ -46,8 +47,19 @@ var longIssuesSplit =
4647
.trim() +
4748
'\n' +
4849
longIssues.slice(defaultOptions.maxLineWidth * 2, longIssues.length).trim();
50+
var jira = 'AAA-11223';
4951

5052
describe('commit message', function() {
53+
it('should append jira id on top of commmit body', () => {
54+
expect(
55+
commitMessage({
56+
type,
57+
subject,
58+
jira,
59+
body
60+
})
61+
).to.equal(`${type}: ${subject}\n\n${jira}\n\n${body}`);
62+
});
5163
it('only header w/ out scope', function() {
5264
expect(
5365
commitMessage({
@@ -282,6 +294,12 @@ describe('validation', function() {
282294
});
283295

284296
describe('defaults', function() {
297+
it('Jira id default', () => {
298+
const branchName = branch.sync() || '';
299+
const jiraIdMatch = branchName.match(/[A-Z]+-[0-9]+/) || [];
300+
const jiraId = jiraIdMatch.shift(); // stirng or undefined ;
301+
expect(questionDefault('jira')).to.equal(jiraId);
302+
});
285303
it('defaultType default', function() {
286304
expect(questionDefault('type')).to.be.undefined;
287305
});
@@ -373,6 +391,9 @@ describe('transformation', function() {
373391
});
374392

375393
describe('filter', function() {
394+
it('uppercase jira', () => {
395+
expect(questionFilter('jira', 'web-12345')).to.equal('WEB-12345');
396+
});
376397
it('lowercase scope', () =>
377398
expect(questionFilter('scope', 'HelloMatt')).to.equal('hellomatt'));
378399
it('lowerfirst subject trimmed and trailing dots striped', () =>

0 commit comments

Comments
 (0)