Skip to content

Commit b92b064

Browse files
committed
fix(transform): only match semantic version tags
`conventional-changelog-writer` expects that the `version` property of a commit object is a semantically valid version. The previous version of the `transform` function would match against any tag associated with the commit, and assign the first matched tag as the commit's version. If the matched tag is not a semantically valid version number, `conventional-changelog-writer` will ignore the `version` property of the commit object, and not successfully generate a changelog (since it has no version on which to generate the changelog). Therefore we update the `transform` function to only match against semantically valid version tags when assigning the `version` property of a commit object.
1 parent 36b97f3 commit b92b064

4 files changed

Lines changed: 71 additions & 15 deletions

File tree

index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
var assign = require('object-assign');
33
var conventionalChangelog = require('conventional-changelog');
4-
var dateFormat = require('dateformat');
54
var Github = require('github');
65
var gitSemverTags = require('git-semver-tags');
76
var merge = require('lodash.merge');
87
var Q = require('q');
98
var semver = require('semver');
109
var through = require('through2');
10+
var transform = require('./lib/transform');
1111

1212
var github = new Github({
1313
version: '3.0.0'
@@ -40,20 +40,7 @@ function conventionalGithubReleaser(auth, changelogOpts, context, gitRawCommitsO
4040
writerOpts = changelogArgs[4];
4141

4242
changelogOpts = merge({
43-
transform: function(chunk, cb) {
44-
if (typeof chunk.gitTags === 'string') {
45-
var match = /tag:\s*(.+?)[,\)]/gi.exec(chunk.gitTags);
46-
if (match) {
47-
chunk.version = match[1];
48-
}
49-
}
50-
51-
if (chunk.committerDate) {
52-
chunk.committerDate = dateFormat(chunk.committerDate, 'yyyy-mm-dd', true);
53-
}
54-
55-
cb(null, chunk);
56-
},
43+
transform: transform,
5744
releaseCount: 1
5845
}, changelogOpts);
5946

lib/transform.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
var dateFormat = require('dateformat');
4+
var findVersions = require('find-versions');
5+
6+
function transform(chunk, cb) {
7+
if (typeof chunk.gitTags === 'string') {
8+
chunk.version = findVersions(chunk.gitTags)[0];
9+
}
10+
11+
if (chunk.committerDate) {
12+
chunk.committerDate = dateFormat(chunk.committerDate, 'yyyy-mm-dd', true);
13+
}
14+
15+
cb(null, chunk);
16+
}
17+
18+
module.exports = transform;

lib/transform.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
// jshint expr: true
4+
5+
var chai = require('chai');
6+
var transform = require('./transform');
7+
8+
var expect = chai.expect;
9+
10+
describe('transform', function() {
11+
beforeEach(function() {
12+
this.chunk = {
13+
gitTags: '',
14+
};
15+
});
16+
17+
it('should skip semantic version matching when gitTags isn\'t a string', function(done) {
18+
this.chunk.gitTags = undefined;
19+
20+
transform(this.chunk, function(err, chunk) {
21+
expect(chunk.version).to.be.undefined;
22+
done();
23+
});
24+
});
25+
26+
it('should have no version when there are no tags', function(done) {
27+
transform(this.chunk, function(err, chunk) {
28+
expect(chunk.version).to.be.undefined;
29+
done();
30+
});
31+
});
32+
33+
it('should not match invalid semantic version tag', function(done) {
34+
this.chunk.gitTags = ' tag: release-18';
35+
36+
transform(this.chunk, function(err, chunk) {
37+
expect(chunk.version).to.be.undefined;
38+
done();
39+
});
40+
});
41+
42+
it('should match valid semantic version tag', function(done) {
43+
this.chunk.gitTags = ' tag: release-18, tag: 1.1.20';
44+
45+
transform(this.chunk, function(err, chunk) {
46+
expect(chunk.version).to.equal('1.1.20');
47+
done();
48+
});
49+
});
50+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"conventional-changelog": "^1.1.0",
3030
"dateformat": "^1.0.11",
31+
"find-versions": "^2.0.0",
3132
"git-semver-tags": "^1.0.0",
3233
"github": "^0.2.4",
3334
"lodash.merge": "^4.0.2",

0 commit comments

Comments
 (0)