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

Commit

Permalink
feat: use new api of references and notes
Browse files Browse the repository at this point in the history
Per conventional-changelog-archived-repos/conventional-commits-parser@29bdfe1 and conventional-changelog-archived-repos/conventional-commits-parser@0923ed5.

It is possible to reference an issue in a different repository.

BREAKING CHANGES:

The upstream must use the new api of `references` and `notes`.

`closes` now becomes `references`
The `notes` object is no longer a key-value object but an array of note object, such as
```js
{
  title: 'BREAKING AMEND',
  text: 'some breaking change'
}
```
  • Loading branch information
stevemao committed Apr 2, 2015
1 parent 5aa74d6 commit c34d085
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 115 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ The header has a special format that includes a **type**, a **scope** (optional)

### footer

The footer should contain any information about **Important Notes** (optional) and is also the place to reference GitHub issues that this commit **Closes** (optional).
The footer should contain any information about **Important Notes** (optional) and is also the place to reference GitHub issues that this commit **references** (optional).

```
<important note>
<BLANK LINE>
<closes>
<references>
```

[More details](CONVENTIONS.md)
Expand All @@ -56,22 +56,22 @@ It expects an object mode upstream and the object should look something like thi

{ hash: '9b1aff905b638aa274a5fc8f88662df446d374bd',
header: 'feat(scope): broadcast $destroy event on scope destruction',
body: '',
footer: 'Closes #1',
notes: {},
closes: [ 1 ],
type: 'feat',
scope: 'scope',
subject: 'broadcast $destroy event on scope destruction' }
subject: 'broadcast $destroy event on scope destruction',
body: null,
footer: 'Closes #1',
notes: [],
references: [ { action: 'Closes', repository: null, issue: '1', raw: '#1' } ] }
{ hash: '13f31602f396bc269076ab4d389cfd8ca94b20ba',
header: 'feat(ng-list): Allow custom separator',
body: 'bla bla bla',
footer: 'BREAKING CHANGE: some breaking change',
notes: { 'BREAKING CHANGE': 'some breaking change' },
closes: [],
type: 'feat',
scope: 'ng-list',
subject: 'Allow custom separator' }
subject: 'Allow custom separator',
body: 'bla bla bla',
footer: 'BREAKING CHANGE: some breaking change',
notes: [ { title: 'BREAKING CHANGE', text: 'some breaking change' } ],
references: [] }

Each chunk should be a commit. Json object is also **valid**.

Expand Down Expand Up @@ -164,11 +164,11 @@ Replace with new values in each commit.

Type: `object` Default: `{ 'BREAKING CHANGE': 'BREAKING CHANGES' }`

Replace with new group names. If group name is not present here, it will be ignored.
Replace with new group titles. If a note's title is not in this mapping, the note will be ignored.

##### commitGroupsCompareFn

Type: `function` Default: lexicographical order on `name` field.
Type: `function` Default: lexicographical order on `title` field.

A compare function used to sort commit groups.

Expand All @@ -180,7 +180,7 @@ A compare function used to sort commits.

##### noteGroupsCompareFn

Type : `function` Default: lexicographical order on `name` field.
Type : `function` Default: lexicographical order on `title` field.

A compare function used to sort note groups.

Expand Down Expand Up @@ -258,7 +258,7 @@ It works with [Line Delimited JSON](http://en.wikipedia.org/wiki/Line_Delimited_
If you have commits.ldjson

```js
{"hash":"9b1aff905b638aa274a5fc8f88662df446d374bd","header":"feat(ngMessages): provide support for dynamic message resolution","body":"Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.","footer":"BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.\nCloses #10036\nCloses #9338","notes":{"BREAKING CHANGE":"The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive."},"closes":[10036,9338],"type":"feat","scope":"ngMessages","subject":"provide support for dynamic message resolution"}
{"hash":"9b1aff905b638aa274a5fc8f88662df446d374bd","header":"feat(ngMessages): provide support for dynamic message resolution","type":"feat","scope":"ngMessages","subject":"provide support for dynamic message resolution","body":"Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.","footer":"BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.\nCloses #10036\nCloses #9338","notes":[{"title":"BREAKING CHANGE","text":"The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive."}],"references":[{"action":"Closes","repository":null,"issue":"10036","raw":"#10036"},{"action":"Closes","repository":null,"issue":"9338","raw":"#9338"}]}
```

And you run
Expand Down
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function conventionalCommitsTemplate(version, context, options) {

var stream;
var commits = [];
var allNotes = [];
var notes = [];
var isPatch = semver.patch(version) !== 0;

context = _.extend({
Expand Down Expand Up @@ -42,9 +42,9 @@ function conventionalCommitsTemplate(version, context, options) {
noteGroups: {
'BREAKING CHANGE': 'BREAKING CHANGES'
},
commitGroupsCompareFn: util.getCompareFunction('name'),
commitGroupsCompareFn: util.getCompareFunction('title'),
commitsCompareFn: util.getCompareFunction('scope'),
noteGroupsCompareFn: util.getCompareFunction('name'),
noteGroupsCompareFn: util.getCompareFunction('title'),
notesCompareFn: util.getCompareFunction(),
mainTemplate: fs.readFileSync(__dirname + '/templates/template.hbs', 'utf-8'),
headerPartial: fs.readFileSync(__dirname + '/templates/header.hbs', 'utf-8'),
Expand All @@ -56,11 +56,14 @@ function conventionalCommitsTemplate(version, context, options) {
var commit = util.processCommit(chunk, options.hashLength, options.replacements);

commits.push(commit);
allNotes.push(commit.notes);
notes = notes.concat(commit.notes);

cb();
}, function(cb) {
var compiled = util.compileTemplates(options);
context = _.merge(context, util.getExtraContext(commits, allNotes, options));

context = _.merge(context, util.getExtraContext(commits, notes, options));

this.push(compiled(context));
cb();
});
Expand Down
54 changes: 26 additions & 28 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ function getCommitGroups(groupBy, commits, groupsCompareFn, commitsCompareFn) {
return commit[groupBy] || '';
});

_.forEach(commitGroupsObj, function(commits, name) {
if (name === '') {
name = false;
_.forEach(commitGroupsObj, function(commits, title) {
if (title === '') {
title = false;
}

if (commitsCompareFn) {
commits.sort(commitsCompareFn);
}
commitGroups.push({
name: name,
title: title,
commits: commits
});
});
Expand All @@ -45,38 +45,36 @@ function getCommitGroups(groupBy, commits, groupsCompareFn, commitsCompareFn) {
return commitGroups;
}

function getNoteGroups(allNotes, noteGroups, noteGroupsCompareFn, notesCompareFn) {
function getNoteGroups(notes, noteGroups, noteGroupsCompareFn, notesCompareFn) {
noteGroups = noteGroups || {};
var reGroups = [];
_.forEach(allNotes, function(notes) {
_.forEach(notes, function(note, name) {
name = noteGroups[name];
if (name) {
var titleExists = false;
_.forEach(reGroups, function(group) {
if (group.name === name) {
titleExists = true;
group.notes.push(note);
return false;
}
});

if (!titleExists) {
reGroups.push({
name: name,
notes: [note]
});
var retGroups = [];
_.forEach(notes, function(note) {
var title = noteGroups[note.title];
if (title) {
var titleExists = false;
_.forEach(retGroups, function(group) {
if (group.title === title) {
titleExists = true;
group.notes.push(note.text);
return false;
}
});

if (!titleExists) {
retGroups.push({
title: title,
notes: [note.text]
});
}
});
}
});

reGroups.sort(noteGroupsCompareFn);
_.forEach(reGroups, function(group) {
retGroups.sort(noteGroupsCompareFn);
_.forEach(retGroups, function(group) {
group.notes.sort(notesCompareFn);
});

return reGroups;
return retGroups;
}

function processCommit(chunk, hashLength, replacements) {
Expand Down
2 changes: 1 addition & 1 deletion templates/commit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

{{~!-- commit hash --}} {{#if @root.linkReferences}}([{{hash}}][{{@root.host}}/{{@root.repository}}/{{@root.commit}}/{{hash}}]){{else}}{{hash~}}{{/if}}

{{~!-- commit close --}}{{#if closes}}, closes{{~#each closes}} {{#if @root.linkReferences}}[#{{this}}]({{@root.host}}/{{@root.repository}}/{{@root.issue}}/{{this}}){{else}}#{{this}}{{/if}}{{/each}}{{/if}}
{{~!-- commit references --}}{{#if references}}, closes{{~#each references}} {{#if @root.linkReferences}}[{{#if this.repository}}{{this.repository}}{{/if}}#{{this.issue}}]({{@root.host}}/{{#if this.repository}}{{this.repository}}{{else}}{{@root.repository}}{{/if}}/{{@root.issue}}/{{this.issue}}){{else}}{{#if this.repository}}{{this.repository}}{{/if}}#{{this.issue}}{{/if}}{{/each}}{{/if}}
2 changes: 1 addition & 1 deletion templates/footer.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#each noteGroups}}
### {{name}}
### {{title}}
{{#each notes}}
* {{this}}
{{/each}}
Expand Down
4 changes: 2 additions & 2 deletions templates/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

{{#if commitGroups}}
{{#each commitGroups}}
{{#if name}}
### {{name}}
{{#if title}}
### {{title}}

{{/if}}
{{#each commits}}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/commits.ldjson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"hash":"9b1aff905b638aa274a5fc8f88662df446d374bd","header":"feat(ngMessages): provide support for dynamic message resolution","body":"Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.","footer":"BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.\nCloses #10036\nCloses #9338","notes":{"BREAKING CHANGE":"The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive."},"closes":[10036,9338],"type":"feat","scope":"ngMessages","subject":"provide support for dynamic message resolution"}
{"hash":"9b1aff905b638aa274a5fc8f88662df446d374bd","header":"feat(ngMessages): provide support for dynamic message resolution","type":"feat","scope":"ngMessages","subject":"provide support for dynamic message resolution","body":"Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.","footer":"BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.\nCloses #10036\nCloses #9338","notes":[{"title":"BREAKING CHANGE","text":"The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive."}],"references":[{"action":"Closes","repository":null,"issue":"10036","raw":"#10036"},{"action":"Closes","repository":null,"issue":"9338","raw":"#9338"}]}
53 changes: 33 additions & 20 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,45 @@ describe('conventionalCommitsTemplate', function() {
upstream.write({
hash: '9b1aff905b638aa274a5fc8f88662df446d374bd',
header: 'feat(scope): broadcast $destroy event on scope destruction',
body: 'BREAKING NEWS: breaking news',
footer: 'Closes #1',
notes: {
'BREAKING NEWS': 'breaking news'
},
closes: [1, 2, 3],
type: 'feat',
scope: 'scope',
subject: 'broadcast $destroy event on scope destruction'
subject: 'broadcast $destroy event on scope destruction',
body: null,
footer: 'Closes #1',
notes: [{
title: 'BREAKING NEWS',
text: 'breaking news'
}],
references: [{
action: 'Closes',
repository: null,
issue: '1',
raw: '#1'
}, {
action: 'Closes',
repository: null,
issue: '2',
raw: '#2'
}, {
action: 'Closes',
repository: null,
issue: '3',
raw: '#3'
}]
});
upstream.write({
hash: '13f31602f396bc269076ab4d389cfd8ca94b20ba',
header: 'feat(ng-list): Allow custom separator',
body: 'bla bla bla',
footer: 'BREAKING CHANGE: some breaking change',
notes: {
'BREAKING CHANGE': 'some breaking change'
},
closes: [],
type: 'feat',
scope: 'ng-list',
subject: 'Allow custom separator'
subject: 'Allow custom separator',
body: 'bla bla bla',
footer: 'BREAKING CHANGE: some breaking change',
notes: [{
title: 'BREAKING CHANGE',
text: 'some breaking change'
}],
references: []
});
upstream.end();

Expand All @@ -40,9 +57,7 @@ describe('conventionalCommitsTemplate', function() {

describe('link', function() {
it('should link if host, repository, commit and issue are not truthy', function(done) {
var upstream = getStream();

upstream
getStream()
.pipe(conventionalCommitsTemplate('0.0.1', {
title: 'this is a title',
host: 'https://github.com',
Expand All @@ -59,9 +74,7 @@ describe('conventionalCommitsTemplate', function() {

describe('version', function() {
it('should not error with a valid version', function(done) {
var upstream = getStream();

upstream
getStream()
.pipe(conventionalCommitsTemplate('0.0.1'))
.on('error', function(err) {
done(err);
Expand Down
31 changes: 27 additions & 4 deletions test/partial.commit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,41 @@ describe('partial.commit', function() {
expect(log).to.equal('* **my scope:** my subject ([hash][www.myhost.com/a/b/my commits/hash])\n');
});

it('should generate commit if `closes` is truthy and `linkReferences` is falsy', function() {
templateContext.closes = [1, 2, 3];
it('should generate commit if `references` is truthy and `linkReferences` is falsy', function() {
templateContext.references = [{
issue: 1
}, {
issue: 2
}, {
issue: 3
}];
var log = Handlebars.compile(template)(templateContext);

expect(log).to.equal('* **my scope:** my subject hash, closes #1 #2 #3\n');
});

it('should generate commit if `closes` is truthy and `linkReferences` is truthy', function() {
it('should generate commit if `references` is truthy and `linkReferences` is truthy', function() {
templateContext.linkReferences = true;
templateContext.closes = [1, 2, 3];
templateContext.references = [{
issue: 1
}, {
issue: 2
}, {
issue: 3
}];
var log = Handlebars.compile(template)(templateContext);

expect(log).to.equal('* **my scope:** my subject ([hash][www.myhost.com/a/b/my commits/hash]), closes [#1](www.myhost.com/a/b/my issue/1) [#2](www.myhost.com/a/b/my issue/2) [#3](www.myhost.com/a/b/my issue/3)\n');
});

it('should reference an issue in a different repository', function() {
templateContext.linkReferences = true;
templateContext.references = [{
repository: 'c/d',
issue: 1
}];
var log = Handlebars.compile(template)(templateContext);

expect(log).to.equal('* **my scope:** my subject ([hash][www.myhost.com/a/b/my commits/hash]), closes [c/d#1](www.myhost.com/c/d/my issue/1)\n');
});
});
4 changes: 2 additions & 2 deletions test/partial.footer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ before(function(done) {
beforeEach(function() {
templateContext = {
noteGroups: [{
name: 'my name',
title: 'my name',
notes: ['my note 1', 'my note 2']
}, {
name: 'my other name',
title: 'my other name',
notes: ['my note 3', 'my note 4']
}]
};
Expand Down
2 changes: 1 addition & 1 deletion test/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('template', function() {

it('should generate template if `commitGroups` is truthy and `name` is truthy', function() {
templateContext.commitGroups = [{
name: 'my name',
title: 'my name',
commits: [1, 2]
}];
var log = Handlebars.compile(template)(templateContext);
Expand Down
Loading

0 comments on commit c34d085

Please sign in to comment.