Skip to content

Commit 09e4b25

Browse files
committed
Update the commit template to receive more options (#88)
* Update the filter otion for the commit to accept all the options * Add options to the commit messages
1 parent 4233ebe commit 09e4b25

File tree

5 files changed

+140
-78
lines changed

5 files changed

+140
-78
lines changed

docs/options.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ You can configure the output of **gren** using templates. Set your own configura
5252
```json
5353
{
5454
"template": {
55-
"commit": "- {{message}}",
55+
"commit": "- [{{message}}]({{url}}) - @{{author}}",
5656
"issue": "- {{labels}} {{name}} [{{text}}]({{url}})",
5757
"label": "[**{{label}}**]",
5858
"noLabel": "closed",
5959
"group": "\n#### {{heading}}\n",
6060
"changelogTitle": "# Changelog\n\n",
61-
"release": "## {{release}} {{date}}\n{{body}}",
61+
"release": "## {{release}} ({{date}})\n{{body}}",
6262
"releaseSeparator": "\n---\n\n"
6363
}
64-
6564
}
6665
```
6766
{% endraw %}

lib/_examples.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ module.exports = {
9090
{
9191
description: 'Otherwise, you can just filter the issues that belong to _a_ milestone',
9292
code: 'gren release --only-milestones'
93+
},
94+
{
95+
name: 'Use commit messages',
96+
description: 'Generate release notes based on commit messages',
97+
code: 'gren release --data-source=commits'
9398
}
9499
],
95100
changelog: [

lib/src/Gren.js

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,16 @@ class Gren {
370370
* @since 0.1.0
371371
* @private
372372
*
373-
* @param {string} message
373+
* @param {Object} commit
374374
*
375375
* @return {string}
376376
*/
377-
_templateCommits(message) {
377+
_templateCommits({ sha, commit: { author: { name }, message, url } }) {
378378
return generate({
379-
message: message
379+
sha,
380+
message: message.split('\n')[0],
381+
url,
382+
author: name
380383
}, this.options.template.commit);
381384
}
382385

@@ -470,64 +473,58 @@ class Gren {
470473
}
471474

472475
/**
473-
* Return a commit messages generated body
476+
* Filter a commit based on the includeMessages option and commit message
474477
*
475-
* @since 0.1.0
478+
* @since 0.10.0
476479
* @private
477480
*
478-
* @param {Array} messages
481+
* @param {Object} commit
479482
*
480-
* @return {string}
483+
* @return {Boolean}
481484
*/
482-
_generateCommitsBody(messages = []) {
483-
const bodyMessages = Array.from(messages);
485+
_filterCommit({ commit: { message } }) {
486+
const messageType = this.options.includeMessages;
487+
const filterMap = {
488+
merges: function(message) {
489+
return message.match(/^merge/i);
490+
},
491+
commits: function(message) {
492+
return !message.match(/^merge/i);
493+
},
494+
all: function() {
495+
return true;
496+
}
497+
};
484498

485-
if (bodyMessages.length === 1) {
486-
bodyMessages.push(null);
499+
if (filterMap[messageType]) {
500+
return filterMap[messageType](message);
487501
}
488502

489-
return bodyMessages
490-
.slice(0, -1)
491-
.filter(message => {
492-
const messageType = this.options.includeMessages;
493-
const filterMap = {
494-
merges: function(message) {
495-
return message.match(/^merge/i);
496-
},
497-
commits: function(message) {
498-
return !message.match(/^merge/i);
499-
},
500-
all: function() {
501-
return true;
502-
}
503-
};
504-
505-
if (filterMap[messageType]) {
506-
return filterMap[messageType](message);
507-
}
508-
509-
return filterMap.commits(message);
510-
})
511-
.map(this._templateCommits.bind(this))
512-
.join('\n');
503+
return filterMap.commits(message);
513504
}
514505

515506
/**
516-
* Transforms the commits to commit messages
507+
* Return a commit messages generated body
517508
*
518509
* @since 0.1.0
519510
* @private
520511
*
521-
* @param {Object[]} commits The array of object containing the commits
512+
* @param {Array} commits
522513
*
523-
* @return {String[]}
514+
* @return {string}
524515
*/
525-
_commitMessages(commits = []) {
526-
if (!Array.isArray(commits)) {
527-
return [];
516+
_generateCommitsBody(commits = []) {
517+
const bodyMessages = Array.from(commits);
518+
519+
if (bodyMessages.length === 1) {
520+
bodyMessages.push(null);
528521
}
529522

530-
return commits.map(({ commit }) => commit && commit.message).filter(Boolean);
523+
return bodyMessages
524+
.slice(0, -1)
525+
.filter(this._filterCommit.bind(this))
526+
.map(this._templateCommits.bind(this))
527+
.join('\n');
531528
}
532529

533530
/**
@@ -551,7 +548,7 @@ class Gren {
551548
};
552549

553550
return this.repo.listCommits(options)
554-
.then(response => this._commitMessages(response.data));
551+
.then(({ data }) => data);
555552
}
556553

557554
/**

lib/src/templates.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"commit": "- {{message}}",
2+
"commit": "- [{{message}}]({{url}}) - @{{author}}",
33
"issue": "- {{labels}} {{name}} [{{text}}]({{url}})",
44
"label": "[**{{label}}**]",
55
"noLabel": "closed",

test/Gren.spec.js

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -267,35 +267,46 @@ describe('Gren', () => {
267267
});
268268
});
269269

270-
describe('_commitMessages', () => {
271-
it('Should return an Array of strings', () => {
272-
assert.isArray(gren._commitMessages([{ commit: { message: 'First body' }}, { commit: {message: 'Second body' }}]), 'Passing the Array of commits');
273-
assert.deepEqual(gren._commitMessages([{ commit: { message: 'First body' }}, { commit: {message: 'Second body' }}]), ['First body', 'Second body'], 'Passing the Array of commits');
274-
});
275-
276-
it('Should return an empty Array', () => {
277-
assert.deepEqual(gren._commitMessages([]), [], 'Passing empty Array');
278-
assert.deepEqual(gren._commitMessages([1, 2, 3]), [], 'Passing invalid Array');
279-
assert.deepEqual(gren._commitMessages(false), [], 'Passing false');
280-
assert.deepEqual(gren._commitMessages(true), [], 'Passing true');
281-
assert.deepEqual(gren._commitMessages(), [], 'No parameters');
282-
assert.deepEqual(gren._commitMessages('string'), [], 'Passing a string');
283-
});
284-
});
285-
286-
describe('_generateCommitsBody, _templateCommits', () => {
270+
describe('_generateCommitsBody, _templateCommits, _filterCommit', () => {
287271
let commitMessages;
288272

289273
before(() => {
274+
gren.options.template.commit = '{{message}} - {{author}}';
275+
290276
commitMessages = [
291-
"First commit",
292-
"This is another commit",
293-
"Merge branch into master: Something else here to be tested",
294-
"This is the last one"
277+
{
278+
commit: {
279+
message: 'First commit',
280+
author: {
281+
name: 'alexcanessa'
282+
}
283+
}
284+
},
285+
{
286+
commit: {
287+
message: 'This is another commit',
288+
author: {
289+
name: 'alexcanessa'
290+
}
291+
}
292+
},
293+
{
294+
commit: {
295+
message: 'Merge branch into master: Something else here to be tested',
296+
author: {
297+
name: 'alexcanessa'
298+
}
299+
}
300+
},
301+
{
302+
commit: {
303+
message: 'This is the last one',
304+
author: {
305+
name: 'alexcanessa'
306+
}
307+
}
308+
}
295309
];
296-
297-
// This makes the test easier
298-
gren.options.template.commit = '{{message}}';
299310
});
300311

301312
it('Should always return a string', () => {
@@ -307,21 +318,71 @@ describe('Gren', () => {
307318
});
308319

309320
it('Should not return the last message', () => {
310-
assert.notInclude(gren._generateCommitsBody(commitMessages), commitMessages.slice(-1)[0], 'Generate the messages');
311-
assert.deepEqual(gren._generateCommitsBody(['One message']), 'One message', 'One message passed');
312-
assert.deepEqual(gren._generateCommitsBody(['One', 'Two']), 'One', 'Two message passed');
313-
assert.deepEqual(gren._generateCommitsBody(['One', 'Two', 'Three']), 'One\nTwo', 'Three message passed');
321+
const lastMessage = commitMessages.slice(-1)[0];
322+
323+
assert.notInclude(gren._generateCommitsBody(commitMessages), `${lastMessage.commit.message} - ${lastMessage.commit.author.name}`, 'Generate the messages');
324+
assert.deepEqual(gren._generateCommitsBody([{
325+
commit: {
326+
message: 'One message',
327+
author: {
328+
name: 'alexcanessa'
329+
}
330+
}
331+
}]), 'One message - alexcanessa', 'One message passed');
332+
assert.deepEqual(gren._generateCommitsBody([{
333+
commit: {
334+
message: 'One',
335+
author: {
336+
name: 'alexcanessa'
337+
}
338+
}
339+
},
340+
{
341+
commit: {
342+
message: 'Two',
343+
author: {
344+
name: 'alexcanessa'
345+
}
346+
}
347+
}]), 'One - alexcanessa', 'Two message passed');
348+
assert.deepEqual(gren._generateCommitsBody([{
349+
commit: {
350+
message: 'One',
351+
author: {
352+
name: 'alexcanessa'
353+
}
354+
}
355+
},
356+
{
357+
commit: {
358+
message: 'Two',
359+
author: {
360+
name: 'alexcanessa'
361+
}
362+
}
363+
},
364+
{
365+
commit: {
366+
message: 'Three',
367+
author: {
368+
name: 'alexcanessa'
369+
}
370+
}
371+
}]), 'One - alexcanessa\nTwo - alexcanessa', 'Three message passed');
314372
});
315373

316374
it('Should only return the messages defined in the options', () => {
317375
gren.options.includeMessages = 'commits';
318-
assert.deepEqual(gren._generateCommitsBody(commitMessages), `${commitMessages[0]}\n${commitMessages[1]}`, 'Using commits as includeMessages');
376+
377+
const messages = msg => `${commitMessages[msg].commit.message} - ${commitMessages[msg].commit.author.name}`;
378+
379+
assert.deepEqual(gren._generateCommitsBody(commitMessages), `${messages(0)}\n${messages(1)}`, 'Using commits as includeMessages');
319380

320381
gren.options.includeMessages = 'all';
321-
assert.deepEqual(gren._generateCommitsBody(commitMessages), `${commitMessages[0]}\n${commitMessages[1]}\n${commitMessages[2]}`, 'Using commits as includeMessages');
382+
assert.deepEqual(gren._generateCommitsBody(commitMessages), `${messages(0)}\n${messages(1)}\n${messages(2)}`, 'Using commits as includeMessages');
322383

323384
gren.options.includeMessages = 'merges';
324-
assert.deepEqual(gren._generateCommitsBody(commitMessages), commitMessages[2], 'Using commits as includeMessages');
385+
assert.deepEqual(gren._generateCommitsBody(commitMessages), messages(2), 'Using commits as includeMessages');
325386
});
326387
});
327388

0 commit comments

Comments
 (0)