Skip to content

Commit

Permalink
Bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcanessa committed Nov 13, 2015
1 parent 7083ba6 commit 57797f3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 69 deletions.
3 changes: 1 addition & 2 deletions github-release-notes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';

var githubReleaseNotes = require('./lib/index');
githubReleaseNotes.init();
var githubReleaseNotes = require('./lib/index');
142 changes: 75 additions & 67 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,12 @@
'use strict';

var Github = require('github-api');
var options = getOptions(process.argv);
var token = options.token;
var username = options.username;
var repositoryName = options.repo;
var releasePrefix = options.prefix || '';
var github = new Github({
token: token,
auth: "oauth"
});
var repo = github.getRepo(username, repositoryName);

/**
* Create a literal object of the node module options
*
* @param {Array} args The array of arguments (the module arguments start from index 2)
*
* @return {Object} The object containg the key/value options
*/
function getOptions(args) {
var settings = {};

for(var i=2;i<args.length;i++) {
settings[args[i].split('=')[0].replace('--', '')] = args[i].split('=')[1];
}

return settings;
}

/**
* Create a release from a given tag (in the options)
*
* @param {Object} options The options to build the release:
* @param {GithubReleaseNotes} gren The gren object
* @param {Object} releaseOptions The options to build the release:
* {
* "tag_name": "v1.0.0",
* "target_commitish": "master",
Expand All @@ -42,12 +16,13 @@ function getOptions(args) {
* "prerelease": false
* }
*/
function makeRelease(releaseOptions) {
repo.makeRelease(releaseOptions, function (err, release) {
function makeRelease(gren, releaseOptions) {
gren.repo.makeRelease(releaseOptions, function (err, release) {
if(err) {
var responseText = JSON.parse(err.request.responseText);
console.error(
(JSON.parse(err.request.responseText)).message + '\n'
+ (JSON.parse(err.request.responseText)).errors[0].code
responseText.message + '\n'
+ responseText.errors[0].code
);
} else {
console.info(release.tag_name + ' successfully created!');
Expand All @@ -71,55 +46,55 @@ function createBody(message) {
*
* @param {[Object]} commits The array of object containing the commits
*
* @return {[string]}
* @return {Array}
*/
function commitMessages(commits) {
return commits.map(function (commit) {
return commit.commit.message;
return commits.map(function (commitObject) {
return commitObject.commit.message;
});
}

/**
* Creates the options to make the release
*
* @param {[string]} commitMessages The commit messages to create the release body
* @param {GithubReleaseNotes} gren The gren object
* @param {Array} tags The collection of tags
* @param {Array} commitMessages The commit messages to create the release body
*/
function prepareRelease(tags, commitMessages) {
commitMessages.pop();

var body = commitMessages.filter(function (message) {
return !message.match('Merge');
}).map(createBody);
function prepareRelease(gren, tags, commitMessages) {
var body = commitMessages.slice(0, -1).filter(function (message) {
return !message.match(/^merge/i);
}).map(createBody).join('\n');

var releaseOptions = {
tag_name: tags[0].name,
name: releasePrefix + tags[0].name,
body: body.join('\n'),
draft: options.draft || false,
prerelease: options.prerelease || false
name: (gren.options.prefix || '') + tags[0].name,
body: body,
draft: gren.options.draft || false,
prerelease: gren.options.prerelease || false
};


makeRelease(releaseOptions);
makeRelease(gren, releaseOptions);
}

/**
* Gets all the commits between two dates
*
* @param {GithubReleaseNotes} gren The gren object
* @param {string} since The since date in ISO
* @param {string} until The until date in ISO
*
* @return {Promise} The promise which resolves the [Array] commit messages
*/
function getCommitsBetweenTwo(since, until) {
function getCommitsBetweenTwo(gren, since, until) {
var options = {
since: since,
until: until
};

return new Promise(function (resolve, reject) {

repo.getCommits(options, function (err, commits) {
gren.repo.getCommits(options, function (err, commits) {
if(err) {
reject(err);
} else {
Expand All @@ -132,13 +107,15 @@ function getCommitsBetweenTwo(since, until) {
/**
* Get the dates of the last two tags
*
* @param {[Object]} tags List of all the tags in the repo
* @return {[Promise]} The promises which returns the dates
* @param {GithubReleaseNotes} gren The gren object
* @param {Object[]} tags List of all the tags in the repo
*
* @return {Promise[]} The promises which returns the dates
*/
function getTagDates(lastTag, lastRelease) {
function getTagDates(gren, lastTag, lastRelease) {
return [lastTag, lastRelease].map(function (tag) {
return new Promise(function (resolve, reject) {
repo.getCommit('master', tag.commit.sha, function (err, commit) {
gren.repo.getCommit('master', tag.commit.sha, function (err, commit) {
if(err) {
reject(err);
} else {
Expand All @@ -152,11 +129,13 @@ function getTagDates(lastTag, lastRelease) {
/**
* Get all the tags of the repo
*
* @param {GithubReleaseNotes} gren The gren object
*
* @return {Promise}
*/
function getLastTag(releaseTagName) {
function getLastTag(gren, releaseTagName) {
return new Promise(function (resolve, reject) {
repo.listTags(function (err, tags) {
gren.repo.listTags(function (err, tags) {
if(err) {
reject(err);
} else {
Expand All @@ -173,11 +152,13 @@ function getLastTag(releaseTagName) {
/**
* Get the latest release
*
* @param {GithubReleaseNotes} gren The gren object
*
* @return {Promise} The promise which resolves the tag name of the release
*/
function getLatestRelease() {
function getLatestRelease(gren) {
return new Promise(function (resolve, reject) {
repo.getLatestRelease(function (err, release) {
gren.repo.getLatestRelease(function (err, release) {
if(err) {
reject(err);
} else {
Expand All @@ -188,35 +169,62 @@ function getLatestRelease() {
}

/**
* @param {Object} options The options of the module
* Create a literal object of the node module options
*
* @param {Array} args The array of arguments (the module arguments start from index 2)
*
* @return {Object} The object containg the key/value options
*/
function getOptions(args) {
var settings = {};

for(var i=2;i<args.length;i++) {
settings[args[i].split('=')[0].replace('--', '')] = args[i].split('=')[1];
}

return settings;
}

/**
* @param {Object} [options] The options of the module
*
* @constructor
*/
function GithubReleaseNotes(options) {
this.options = options || {};
// Silence is golden
this.options = getOptions(process.argv);

var github = new Github({
token: this.options.token,
auth: "oauth"
});

this.repo = github.getRepo(this.options.username, this.options.repo);
}

/**
* Get All the tags, get the dates, get the commits between those dates and prepeare the release
*/
GithubReleaseNotes.prototype.init = function() {
getLatestRelease().then(function (releaseTagName) {
getLastTag(releaseTagName).then(function (tags) {
var that = this;

getLatestRelease(that).then(function (releaseTagName) {
getLastTag(that, releaseTagName).then(function (tags) {
if(tags.length === 1) {
console.error('The latest tag is the latest release!');
return;
}

Promise.all(getTagDates(tags[0], tags[1]))
Promise.all(getTagDates(that, tags[0], tags[1]))
.then(function (data) {
getCommitsBetweenTwo(data[1], data[0]).then(prepareRelease.bind(null, tags));
getCommitsBetweenTwo(that, data[1], data[0]).then(function(commitMessages) {
prepareRelease(that, tags, commitMessages);
});
});
});
});
};

var githubReleaseNotes = new GithubReleaseNotes();
githubReleaseNotes.init();
var gren = new GithubReleaseNotes();
gren.init();

module.exports = GithubReleaseNotes;

0 comments on commit 57797f3

Please sign in to comment.