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

Commit

Permalink
feat(github): adds github-specific replacements for issues and users
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
bcoe authored and stevemao committed Apr 18, 2016
1 parent 6aebb75 commit 2633f73
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*)(?:\((.*)\))?\: (.*)$/,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"compare-func": "^1.3.1",
"github-url-from-git": "^1.4.0",
"q": "^1.4.1"
}
}
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.']);
Expand Down

0 comments on commit 2633f73

Please sign in to comment.