Skip to content
This repository was archived by the owner on Feb 5, 2018. It is now read-only.

Commit 2633f73

Browse files
bcoestevemao
authored andcommitted
feat(github): adds github-specific replacements for issues and users
Closes #12
1 parent 6aebb75 commit 2633f73

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ var compareFunc = require('compare-func');
33
var Q = require('q');
44
var readFile = Q.denodeify(require('fs').readFile);
55
var resolve = require('path').resolve;
6+
var path = require('path');
7+
var pkgJson = {};
8+
var gufg = require('github-url-from-git');
9+
try {
10+
pkgJson = require(path.resolve(
11+
process.cwd(),
12+
'./package.json'
13+
));
14+
} catch (err) {
15+
console.error('no root package.json found');
16+
}
617

718
var parserOpts = {
819
headerPattern: /^(\w*)(?:\((.*)\))?\: (.*)$/,
@@ -16,6 +27,19 @@ var parserOpts = {
1627
revertCorrespondence: ['header', 'hash']
1728
};
1829

30+
function issueUrl() {
31+
var url = null;
32+
if (pkgJson.repository && pkgJson.repository.url && ~pkgJson.repository.url.indexOf('github.com')) {
33+
var gitUrl = gufg(pkgJson.repository.url);
34+
35+
if (gitUrl) {
36+
return gitUrl + '/issues/';
37+
} else {
38+
return url;
39+
}
40+
}
41+
}
42+
1943
var writerOpts = {
2044
transform: function(commit) {
2145
var discard = true;
@@ -56,6 +80,13 @@ var writerOpts = {
5680
}
5781

5882
if (typeof commit.subject === 'string') {
83+
var url = issueUrl();
84+
if (url) {
85+
// GitHub issue URLs.
86+
commit.subject = commit.subject.replace(/( ?)#([0-9]+)(\b|^)/g, '$1[#$2](' + url + '$2)$3');
87+
}
88+
// GitHub user URLs.
89+
commit.subject = commit.subject.replace(/( ?)@([a-zA-Z0-9_]+)(\b|^)/g, '$1[@$2](https://github.com/$2)$3');
5990
commit.subject = commit.subject;
6091
}
6192

@@ -75,6 +106,7 @@ module.exports = Q.all([
75106
readFile(resolve(__dirname, 'templates/footer.hbs'), 'utf-8')
76107
])
77108
.spread(function(template, header, commit, footer) {
109+
78110
writerOpts.mainTemplate = template;
79111
writerOpts.headerPartial = header;
80112
writerOpts.commitPartial = commit;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"dependencies": {
3939
"compare-func": "^1.3.1",
40+
"github-url-from-git": "^1.4.0",
4041
"q": "^1.4.1"
4142
}
4243
}

test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ describe('angular preset', function() {
6363
}));
6464
});
6565

66+
it('should replace #[0-9]+ with GitHub issue URL', function(done) {
67+
gitDummyCommit(['feat(awesome): addresses the issue brought up in #133']);
68+
69+
conventionalChangelogCore({
70+
config: preset
71+
})
72+
.on('error', function(err) {
73+
done(err);
74+
})
75+
.pipe(through(function(chunk) {
76+
chunk = chunk.toString();
77+
expect(chunk).to.include('[#133](https://github.com/bcoe/conventional-changelog-standard/issues/133)');
78+
done();
79+
}));
80+
});
81+
82+
it('should replace @username with GitHub user URL', function(done) {
83+
gitDummyCommit(['feat(awesome): issue brought up by @bcoe! on Friday']);
84+
85+
conventionalChangelogCore({
86+
config: preset
87+
})
88+
.on('error', function(err) {
89+
done(err);
90+
})
91+
.pipe(through(function(chunk) {
92+
chunk = chunk.toString();
93+
expect(chunk).to.include('[@bcoe](https://github.com/bcoe)');
94+
done();
95+
}));
96+
});
97+
6698
it('should not discard commit if there is BREAKING CHANGE', function(done) {
6799
gitDummyCommit(['docs(readme): make it clear', 'BREAKING CHANGE: The Change is huge.']);
68100
gitDummyCommit(['style(whitespace): make it easier to read', 'BREAKING CHANGE: The Change is huge.']);

0 commit comments

Comments
 (0)