diff --git a/lib/paginate.js b/lib/paginate.js new file mode 100644 index 0000000..cb8d7e0 --- /dev/null +++ b/lib/paginate.js @@ -0,0 +1,11 @@ +module.exports = async function paginate(responsePromise, callback) { + let response = await responsePromise; + let collection = await callback(response); + + while (this.hasNextPage(response)) { + response = await this.getNextPage(response); + collection = collection.concat(await callback(response)); + } + + return collection; +} diff --git a/lib/robot.js b/lib/robot.js index 9256f8f..bb188f6 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -35,7 +35,7 @@ class Robot { const github = new GitHubApi({debug: process.env.LOG_LEVEL === 'trace'}); github.authenticate({type: 'token', token: token.token}); - return rateLimitedClient(github); + return probotEnhancedClient(github); } log(...args) { @@ -43,6 +43,14 @@ class Robot { } } +function probotEnhancedClient(github) { + github = rateLimitedClient(github); + + github.paginate = require('./lib/paginate'); + + return github; +} + // Hack client to only allow one request at a time with a 1s delay // https://github.com/mikedeboer/node-github/issues/526 function rateLimitedClient(github) {