Skip to content

Commit d3c9f51

Browse files
authored
Add the limit option for pagination (#94)
* Add the limit option for pagination * Add tests for the _listTags and _listReleases functions * Add the limit option to the examples
1 parent 54468dc commit d3c9f51

File tree

5 files changed

+99
-50
lines changed

5 files changed

+99
-50
lines changed

lib/_examples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module.exports = {
8080
{
8181
name: 'Create release notes for all the tags',
8282
description: 'Get the commits or issues closed between the specified tag and the one before.',
83-
code: 'gren release --tags=all'
83+
code: 'gren release --tags=all --limit=200'
8484
},
8585
{
8686
name: 'Work with milestones',

lib/_options.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ module.exports = {
6262
description: 'Write release notes for <new-tag> using data collected until <old-tag>. If only one tag is specified, will use data until the previous tag. To run gren for all the tags, use --tags=*',
6363
action: value => value.split('..')
6464
},
65+
{
66+
short: '-l',
67+
name: 'limit',
68+
valueType: '<number>',
69+
description: 'The limit of tags/releases to get. [30]',
70+
defaultValue: '30'
71+
},
6572
{
6673
short: '-D',
6774
name: 'data-source',

lib/src/Gren.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaults = {
1717
ignoreLabels: false,
1818
ignoreIssuesWith: false,
1919
groupBy: false,
20+
limit: 30,
2021
milestoneMatch: 'Release {{tag_name}}'
2122
};
2223

@@ -150,11 +151,9 @@ class Gren {
150151
const loaded = utils.task(this, 'Updating latest release');
151152

152153
return this.repo.updateRelease(releaseId, releaseOptions)
153-
.then(response => {
154+
.then(({ data: release }) => {
154155
loaded();
155156

156-
const release = response.data;
157-
158157
console.log(chalk.green(`\n${release.name} has been successfully updated!`));
159158
console.log(chalk.blue(`See the results here: ${release.html_url}`));
160159

@@ -184,9 +183,8 @@ class Gren {
184183
const loaded = utils.task(this, 'Preparing the release');
185184

186185
return this.repo.createRelease(releaseOptions)
187-
.then(response => {
186+
.then(({ data: release }) => {
188187
loaded();
189-
const release = response.data;
190188

191189
console.log(chalk.green(`\n${release.name} has been successfully created!`));
192190
console.log(chalk.blue(`See the results here: ${release.html_url}`));
@@ -262,6 +260,18 @@ class Gren {
262260
}).slice(0, 2);
263261
}
264262

263+
/**
264+
* Temporary function for this.repo.listReleases to accept options
265+
*
266+
* @see https://github.com/github-tools/github/pull/485
267+
* @param {Object} options
268+
*
269+
* @return {Promise}
270+
*/
271+
_listTags(options) {
272+
return this.repo._request('GET', `/repos/${this.repo.__fullname}/tags`, options);
273+
}
274+
265275
/**
266276
* Get all the tags of the repo
267277
*
@@ -273,12 +283,12 @@ class Gren {
273283
_getLastTags(releases) {
274284
const loaded = utils.task(this, 'Getting tags');
275285

276-
return this.repo.listTags()
277-
.then(response => {
286+
return this._listTags({
287+
per_page: this.options.limit
288+
})
289+
.then(({ data: tags }) => {
278290
loaded();
279291

280-
const tags = response.data;
281-
282292
if (!tags.length) {
283293
throw chalk.red('Looks like you have no tags! Tag a commit first and then run gren again');
284294
}
@@ -320,6 +330,18 @@ class Gren {
320330
})));
321331
}
322332

333+
/**
334+
* Temporary function for this.repo.listReleases to accept options
335+
*
336+
* @see https://github.com/github-tools/github/pull/485
337+
* @param {Object} options
338+
*
339+
* @return {Promise}
340+
*/
341+
_listReleases(options) {
342+
return this.repo._request('GET', `/repos/${this.repo.__fullname}/releases`, options);
343+
}
344+
323345
/**
324346
* Get all releases
325347
*
@@ -331,12 +353,12 @@ class Gren {
331353
_getListReleases() {
332354
const loaded = utils.task(this, 'Getting the list of releases');
333355

334-
return this.repo.listReleases()
335-
.then(response => {
356+
return this._listReleases({
357+
per_page: this.options.limit
358+
})
359+
.then(({ data: releases }) => {
336360
loaded();
337361

338-
const releases = response.data;
339-
340362
process.stdout.write(releases.length + ' releases found\n');
341363

342364
return releases;

package-lock.json

Lines changed: 34 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/Gren.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,28 @@ describe('Gren', () => {
493493
.catch(err => done(err));
494494
});
495495

496+
it('_listReleases', done => {
497+
gren._listReleases({
498+
per_page: 10
499+
})
500+
.then(({ data: releases }) => {
501+
assert.lengthOf(releases, 10, 'The list of releases is the set one.');
502+
done();
503+
})
504+
.catch(err => done(err));
505+
});
506+
507+
it('_listTags', done => {
508+
gren._listTags({
509+
per_page: 10
510+
})
511+
.then(({ data: tags }) => {
512+
assert.lengthOf(tags, 10, 'The list of tags is the set one.');
513+
done();
514+
})
515+
.catch(err => done(err));
516+
});
517+
496518
it('_getReleaseBlocks', done => {
497519
gren._getReleaseBlocks()
498520
.then(releaseBlocks => {

0 commit comments

Comments
 (0)