Skip to content

Commit 57797f3

Browse files
committed
Bit of refactoring
1 parent 7083ba6 commit 57797f3

File tree

2 files changed

+76
-69
lines changed

2 files changed

+76
-69
lines changed

github-release-notes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
'use strict';
22

3-
var githubReleaseNotes = require('./lib/index');
4-
githubReleaseNotes.init();
3+
var githubReleaseNotes = require('./lib/index');

src/index.js

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
11
'use strict';
22

33
var Github = require('github-api');
4-
var options = getOptions(process.argv);
5-
var token = options.token;
6-
var username = options.username;
7-
var repositoryName = options.repo;
8-
var releasePrefix = options.prefix || '';
9-
var github = new Github({
10-
token: token,
11-
auth: "oauth"
12-
});
13-
var repo = github.getRepo(username, repositoryName);
14-
15-
/**
16-
* Create a literal object of the node module options
17-
*
18-
* @param {Array} args The array of arguments (the module arguments start from index 2)
19-
*
20-
* @return {Object} The object containg the key/value options
21-
*/
22-
function getOptions(args) {
23-
var settings = {};
24-
25-
for(var i=2;i<args.length;i++) {
26-
settings[args[i].split('=')[0].replace('--', '')] = args[i].split('=')[1];
27-
}
28-
29-
return settings;
30-
}
314

325
/**
336
* Create a release from a given tag (in the options)
347
*
35-
* @param {Object} options The options to build the release:
8+
* @param {GithubReleaseNotes} gren The gren object
9+
* @param {Object} releaseOptions The options to build the release:
3610
* {
3711
* "tag_name": "v1.0.0",
3812
* "target_commitish": "master",
@@ -42,12 +16,13 @@ function getOptions(args) {
4216
* "prerelease": false
4317
* }
4418
*/
45-
function makeRelease(releaseOptions) {
46-
repo.makeRelease(releaseOptions, function (err, release) {
19+
function makeRelease(gren, releaseOptions) {
20+
gren.repo.makeRelease(releaseOptions, function (err, release) {
4721
if(err) {
22+
var responseText = JSON.parse(err.request.responseText);
4823
console.error(
49-
(JSON.parse(err.request.responseText)).message + '\n'
50-
+ (JSON.parse(err.request.responseText)).errors[0].code
24+
responseText.message + '\n'
25+
+ responseText.errors[0].code
5126
);
5227
} else {
5328
console.info(release.tag_name + ' successfully created!');
@@ -71,55 +46,55 @@ function createBody(message) {
7146
*
7247
* @param {[Object]} commits The array of object containing the commits
7348
*
74-
* @return {[string]}
49+
* @return {Array}
7550
*/
7651
function commitMessages(commits) {
77-
return commits.map(function (commit) {
78-
return commit.commit.message;
52+
return commits.map(function (commitObject) {
53+
return commitObject.commit.message;
7954
});
8055
}
8156

8257
/**
8358
* Creates the options to make the release
8459
*
85-
* @param {[string]} commitMessages The commit messages to create the release body
60+
* @param {GithubReleaseNotes} gren The gren object
61+
* @param {Array} tags The collection of tags
62+
* @param {Array} commitMessages The commit messages to create the release body
8663
*/
87-
function prepareRelease(tags, commitMessages) {
88-
commitMessages.pop();
89-
90-
var body = commitMessages.filter(function (message) {
91-
return !message.match('Merge');
92-
}).map(createBody);
64+
function prepareRelease(gren, tags, commitMessages) {
65+
var body = commitMessages.slice(0, -1).filter(function (message) {
66+
return !message.match(/^merge/i);
67+
}).map(createBody).join('\n');
9368

9469
var releaseOptions = {
9570
tag_name: tags[0].name,
96-
name: releasePrefix + tags[0].name,
97-
body: body.join('\n'),
98-
draft: options.draft || false,
99-
prerelease: options.prerelease || false
71+
name: (gren.options.prefix || '') + tags[0].name,
72+
body: body,
73+
draft: gren.options.draft || false,
74+
prerelease: gren.options.prerelease || false
10075
};
10176

102-
103-
makeRelease(releaseOptions);
77+
makeRelease(gren, releaseOptions);
10478
}
10579

10680
/**
10781
* Gets all the commits between two dates
10882
*
83+
* @param {GithubReleaseNotes} gren The gren object
10984
* @param {string} since The since date in ISO
11085
* @param {string} until The until date in ISO
11186
*
11287
* @return {Promise} The promise which resolves the [Array] commit messages
11388
*/
114-
function getCommitsBetweenTwo(since, until) {
89+
function getCommitsBetweenTwo(gren, since, until) {
11590
var options = {
11691
since: since,
11792
until: until
11893
};
11994

12095
return new Promise(function (resolve, reject) {
12196

122-
repo.getCommits(options, function (err, commits) {
97+
gren.repo.getCommits(options, function (err, commits) {
12398
if(err) {
12499
reject(err);
125100
} else {
@@ -132,13 +107,15 @@ function getCommitsBetweenTwo(since, until) {
132107
/**
133108
* Get the dates of the last two tags
134109
*
135-
* @param {[Object]} tags List of all the tags in the repo
136-
* @return {[Promise]} The promises which returns the dates
110+
* @param {GithubReleaseNotes} gren The gren object
111+
* @param {Object[]} tags List of all the tags in the repo
112+
*
113+
* @return {Promise[]} The promises which returns the dates
137114
*/
138-
function getTagDates(lastTag, lastRelease) {
115+
function getTagDates(gren, lastTag, lastRelease) {
139116
return [lastTag, lastRelease].map(function (tag) {
140117
return new Promise(function (resolve, reject) {
141-
repo.getCommit('master', tag.commit.sha, function (err, commit) {
118+
gren.repo.getCommit('master', tag.commit.sha, function (err, commit) {
142119
if(err) {
143120
reject(err);
144121
} else {
@@ -152,11 +129,13 @@ function getTagDates(lastTag, lastRelease) {
152129
/**
153130
* Get all the tags of the repo
154131
*
132+
* @param {GithubReleaseNotes} gren The gren object
133+
*
155134
* @return {Promise}
156135
*/
157-
function getLastTag(releaseTagName) {
136+
function getLastTag(gren, releaseTagName) {
158137
return new Promise(function (resolve, reject) {
159-
repo.listTags(function (err, tags) {
138+
gren.repo.listTags(function (err, tags) {
160139
if(err) {
161140
reject(err);
162141
} else {
@@ -173,11 +152,13 @@ function getLastTag(releaseTagName) {
173152
/**
174153
* Get the latest release
175154
*
155+
* @param {GithubReleaseNotes} gren The gren object
156+
*
176157
* @return {Promise} The promise which resolves the tag name of the release
177158
*/
178-
function getLatestRelease() {
159+
function getLatestRelease(gren) {
179160
return new Promise(function (resolve, reject) {
180-
repo.getLatestRelease(function (err, release) {
161+
gren.repo.getLatestRelease(function (err, release) {
181162
if(err) {
182163
reject(err);
183164
} else {
@@ -188,35 +169,62 @@ function getLatestRelease() {
188169
}
189170

190171
/**
191-
* @param {Object} options The options of the module
172+
* Create a literal object of the node module options
173+
*
174+
* @param {Array} args The array of arguments (the module arguments start from index 2)
175+
*
176+
* @return {Object} The object containg the key/value options
177+
*/
178+
function getOptions(args) {
179+
var settings = {};
180+
181+
for(var i=2;i<args.length;i++) {
182+
settings[args[i].split('=')[0].replace('--', '')] = args[i].split('=')[1];
183+
}
184+
185+
return settings;
186+
}
187+
188+
/**
189+
* @param {Object} [options] The options of the module
192190
*
193191
* @constructor
194192
*/
195193
function GithubReleaseNotes(options) {
196-
this.options = options || {};
197-
// Silence is golden
194+
this.options = getOptions(process.argv);
195+
196+
var github = new Github({
197+
token: this.options.token,
198+
auth: "oauth"
199+
});
200+
201+
this.repo = github.getRepo(this.options.username, this.options.repo);
198202
}
199203

200204
/**
201205
* Get All the tags, get the dates, get the commits between those dates and prepeare the release
202206
*/
203207
GithubReleaseNotes.prototype.init = function() {
204-
getLatestRelease().then(function (releaseTagName) {
205-
getLastTag(releaseTagName).then(function (tags) {
208+
var that = this;
209+
210+
getLatestRelease(that).then(function (releaseTagName) {
211+
getLastTag(that, releaseTagName).then(function (tags) {
206212
if(tags.length === 1) {
207213
console.error('The latest tag is the latest release!');
208214
return;
209215
}
210216

211-
Promise.all(getTagDates(tags[0], tags[1]))
217+
Promise.all(getTagDates(that, tags[0], tags[1]))
212218
.then(function (data) {
213-
getCommitsBetweenTwo(data[1], data[0]).then(prepareRelease.bind(null, tags));
219+
getCommitsBetweenTwo(that, data[1], data[0]).then(function(commitMessages) {
220+
prepareRelease(that, tags, commitMessages);
221+
});
214222
});
215223
});
216224
});
217225
};
218226

219-
var githubReleaseNotes = new GithubReleaseNotes();
220-
githubReleaseNotes.init();
227+
var gren = new GithubReleaseNotes();
228+
gren.init();
221229

222230
module.exports = GithubReleaseNotes;

0 commit comments

Comments
 (0)