Skip to content

Commit 70f2c49

Browse files
committed
Merge pull request #1 from alexcanessa/release-notes-tag-release
Release notes tag release
2 parents 5f4f9c5 + 57274c6 commit 70f2c49

File tree

2 files changed

+86
-58
lines changed

2 files changed

+86
-58
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# github-release-notes
2-
Node module which generates a release from the latest tag and compiles release notes based on commit messages between the last two tags.
2+
Node module which generates a release from the latest tag and compiles release notes based on commit messages between the last tag and the latest release.
33

44
## Installation
55

github-release-notes.js

Lines changed: 85 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,28 @@ function getOptions(args) {
3030
}
3131

3232
/**
33-
* Get all the tags of the repo
33+
* Create a release from a given tag (in the options)
3434
*
35-
* @return {Promise}
35+
* @param {Object} options The options to build the release:
36+
* {
37+
* "tag_name": "v1.0.0",
38+
* "target_commitish": "master",
39+
* "name": "v1.0.0",
40+
* "body": "Description of the release",
41+
* "draft": false,
42+
* "prerelease": false
43+
* }
3644
*/
37-
function getAllTags() {
38-
return new Promise(function (resolve, reject) {
39-
repo.listTags(function(err, tags) {
40-
if(err) {
41-
reject(err);
42-
} else {
43-
resolve(tags);
44-
}
45-
});
45+
function makeRelease(releaseOptions) {
46+
repo.makeRelease(releaseOptions, function (err, release) {
47+
if(err) {
48+
console.error(
49+
(JSON.parse(err.request.responseText)).message + '\n'
50+
+ (JSON.parse(err.request.responseText)).errors[0].code
51+
);
52+
} else {
53+
console.info(release.tag_name + ' successfully created!');
54+
}
4655
});
4756
}
4857

@@ -70,6 +79,30 @@ function commitMessages(commits) {
7079
});
7180
}
7281

82+
/**
83+
* Creates the options to make the release
84+
*
85+
* @param {[string]} commitMessages The commit messages to create the release body
86+
*/
87+
function prepareRelease(tags, commitMessages) {
88+
var body = commitMessages.filter(function (message) {
89+
return !message.match('Merge');
90+
}).map(createBody);
91+
92+
body.pop();
93+
94+
var releaseOptions = {
95+
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
100+
};
101+
102+
103+
makeRelease(releaseOptions);
104+
}
105+
73106
/**
74107
* Gets all the commits between two dates
75108
*
@@ -102,10 +135,10 @@ function getCommitsBetweenTwo(since, until) {
102135
* @param {[Object]} tags List of all the tags in the repo
103136
* @return {[Promise]} The promises which returns the dates
104137
*/
105-
function getTagDates(tags) {
106-
return [tags[0], tags[1]].map(function(tag) {
138+
function getTagDates(lastTag, lastRelease) {
139+
return [lastTag, lastRelease].map(function (tag) {
107140
return new Promise(function (resolve, reject) {
108-
repo.getCommit('master', tag.commit.sha, function(err, commit) {
141+
repo.getCommit('master', tag.commit.sha, function (err, commit) {
109142
if(err) {
110143
reject(err);
111144
} else {
@@ -117,63 +150,58 @@ function getTagDates(tags) {
117150
}
118151

119152
/**
120-
* Create a release from a given tag (in the options)
153+
* Get all the tags of the repo
121154
*
122-
* @param {Object} options The options to build the release:
123-
* {
124-
* "tag_name": "v1.0.0",
125-
* "target_commitish": "master",
126-
* "name": "v1.0.0",
127-
* "body": "Description of the release",
128-
* "draft": false,
129-
* "prerelease": false
130-
* }
155+
* @return {Promise}
131156
*/
132-
function makeRelease(releaseOptions) {
133-
repo.makeRelease(releaseOptions, function (err, release) {
134-
if(err) {
135-
console.error(
136-
(JSON.parse(err.request.responseText)).message + '\n'
137-
+ (JSON.parse(err.request.responseText)).errors[0].code
138-
);
139-
} else {
140-
console.info(release.tag_name + 'successfully created!');
141-
}
157+
function getLastTag(releaseTagName) {
158+
return new Promise(function (resolve, reject) {
159+
repo.listTags(function (err, tags) {
160+
if(err) {
161+
reject(err);
162+
} else {
163+
resolve(
164+
tags.filter(function(tag, index) {
165+
return (index === 0 || tag.name === releaseTagName);
166+
})
167+
);
168+
}
169+
});
142170
});
143171
}
144172

145173
/**
146-
* Creates the options to make the release
174+
* Get the latest release
147175
*
148-
* @param {[string]} commitMessages The commit messages to create the release body
176+
* @return {Promise} The promise which resolves the tag name of the release
149177
*/
150-
function prepareRelease(tags, commitMessages) {
151-
var body = commitMessages.filter(function(message) {
152-
return !message.match('Merge');
153-
}).map(createBody);
154-
155-
body.pop();
156-
157-
var releaseOptions = {
158-
tag_name: tags[0].name,
159-
name: releasePrefix + tags[0].name,
160-
body: body.join('\n'),
161-
draft: options.draft || false,
162-
prerelease: options.prerelease || false
163-
};
164-
165-
166-
makeRelease(releaseOptions);
178+
function getLatestRelease() {
179+
return new Promise(function (resolve, reject) {
180+
repo.getLatestRelease(function (err, release) {
181+
if(err) {
182+
reject(err);
183+
} else {
184+
resolve(release.tag_name);
185+
}
186+
});
187+
});
167188
}
168189

169190
/**
170191
* Get All the tags, get the dates, get the commits between those dates and prepeare the release
171192
*/
172193
function init() {
173-
getAllTags().then(function(tags) {
174-
Promise.all(getTagDates(tags))
175-
.then(function(data) {
176-
getCommitsBetweenTwo(data[1], data[0]).then(prepareRelease.bind(null, tags));
194+
getLatestRelease().then(function (releaseTagName) {
195+
getLastTag(releaseTagName).then(function (tags) {
196+
if(tags.length === 1) {
197+
console.error('The latest tag is the latest release!');
198+
return;
199+
}
200+
201+
Promise.all(getTagDates(tags[0], tags[1]))
202+
.then(function (data) {
203+
getCommitsBetweenTwo(data[1], data[0]).then(prepareRelease.bind(null, tags));
204+
});
177205
});
178206
});
179207
}

0 commit comments

Comments
 (0)