diff --git a/index.js b/index.js index bee6628..abff423 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,17 @@ var compareFunc = require('compare-func'); var Q = require('q'); var readFile = Q.denodeify(require('fs').readFile); var resolve = require('path').resolve; +var path = require('path'); +var pkgJson = {}; +var gufg = require('github-url-from-git'); +try { + pkgJson = require(path.resolve( + process.cwd(), + './package.json' + )); +} catch (err) { + console.error('no root package.json found'); +} var parserOpts = { headerPattern: /^(\w*)(?:\((.*)\))?\: (.*)$/, @@ -16,6 +27,19 @@ var parserOpts = { revertCorrespondence: ['header', 'hash'] }; +function issueUrl() { + var url = null; + if (pkgJson.repository && pkgJson.repository.url && ~pkgJson.repository.url.indexOf('github.com')) { + var gitUrl = gufg(pkgJson.repository.url); + + if (gitUrl) { + return gitUrl + '/issues/'; + } else { + return url; + } + } +} + var writerOpts = { transform: function(commit) { var discard = true; @@ -56,6 +80,13 @@ var writerOpts = { } if (typeof commit.subject === 'string') { + var url = issueUrl(); + if (url) { + // GitHub issue URLs. + commit.subject = commit.subject.replace(/( ?)#([0-9]+)(\b|^)/g, '$1[#$2](' + url + '$2)$3'); + } + // GitHub user URLs. + commit.subject = commit.subject.replace(/( ?)@([a-zA-Z0-9_]+)(\b|^)/g, '$1[@$2](https://github.com/$2)$3'); commit.subject = commit.subject; } @@ -75,6 +106,7 @@ module.exports = Q.all([ readFile(resolve(__dirname, 'templates/footer.hbs'), 'utf-8') ]) .spread(function(template, header, commit, footer) { + writerOpts.mainTemplate = template; writerOpts.headerPartial = header; writerOpts.commitPartial = commit; diff --git a/package.json b/package.json index cd73bf4..70d2140 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ }, "dependencies": { "compare-func": "^1.3.1", + "github-url-from-git": "^1.4.0", "q": "^1.4.1" } } diff --git a/test.js b/test.js index aa3de17..03590d5 100644 --- a/test.js +++ b/test.js @@ -63,6 +63,38 @@ describe('angular preset', function() { })); }); + it('should replace #[0-9]+ with GitHub issue URL', function(done) { + gitDummyCommit(['feat(awesome): addresses the issue brought up in #133']); + + conventionalChangelogCore({ + config: preset + }) + .on('error', function(err) { + done(err); + }) + .pipe(through(function(chunk) { + chunk = chunk.toString(); + expect(chunk).to.include('[#133](https://github.com/bcoe/conventional-changelog-standard/issues/133)'); + done(); + })); + }); + + it('should replace @username with GitHub user URL', function(done) { + gitDummyCommit(['feat(awesome): issue brought up by @bcoe! on Friday']); + + conventionalChangelogCore({ + config: preset + }) + .on('error', function(err) { + done(err); + }) + .pipe(through(function(chunk) { + chunk = chunk.toString(); + expect(chunk).to.include('[@bcoe](https://github.com/bcoe)'); + done(); + })); + }); + it('should not discard commit if there is BREAKING CHANGE', function(done) { gitDummyCommit(['docs(readme): make it clear', 'BREAKING CHANGE: The Change is huge.']); gitDummyCommit(['style(whitespace): make it easier to read', 'BREAKING CHANGE: The Change is huge.']);