forked from mirrors/probot
feat: new github.paginate API, github.paginate.iterator()
This commit is contained in:
parent
fa9a228197
commit
e0868f10b4
|
@ -1,13 +1,46 @@
|
|||
import { AnyResponse } from '@octokit/rest'
|
||||
import { GitHubAPI } from './'
|
||||
import Octokit from '@octokit/rest'
|
||||
|
||||
export function addPagination (octokit: GitHubAPI) {
|
||||
octokit.paginate = paginate.bind(null, octokit)
|
||||
// tslint:disable-next-line
|
||||
const octokitGetNextPage = require('octokit-pagination-methods/lib/get-next-page')
|
||||
// tslint:disable-next-line
|
||||
const octokitHasNextPage = require('octokit-pagination-methods/lib/has-next-page')
|
||||
|
||||
export function addPagination (octokit: Octokit) {
|
||||
const octokitPaginate = octokit.paginate
|
||||
|
||||
octokit.paginate = Object.assign(
|
||||
(...args: any[]) => paginate(octokit, octokitPaginate, args[0], args[1], args[2]),
|
||||
{ iterator: octokit.paginate.iterator }
|
||||
)
|
||||
}
|
||||
|
||||
const defaultCallback = (response: AnyResponse, done?: () => void) => response
|
||||
const defaultCallback = (response: Octokit.AnyResponse, done?: () => void) => response
|
||||
|
||||
async function paginate (octokit: GitHubAPI, responsePromise: any, callback = defaultCallback) {
|
||||
async function paginate (octokit: Octokit, octokitPaginate: Octokit.Paginate, ...args: any[]) {
|
||||
// Until we fully deprecate the old paginate method, we need to check if the
|
||||
// first argument. If it is a promise we return the old function signature
|
||||
if (!args[0].then) {
|
||||
return octokitPaginate(args[0], args[1], args[2])
|
||||
}
|
||||
|
||||
const responsePromise = args[0]
|
||||
const callback = args[1] || defaultCallback
|
||||
|
||||
// Deprecated since 8.0.0
|
||||
// tslint:disable-next-line:no-console
|
||||
console.warn(new Error(`.paginate(promise) is deprecated. Use .paginate(endpointOptions) instead.
|
||||
|
||||
For example, instead of
|
||||
|
||||
context.github.paginate(context.github.issues.getAll(context.repo())
|
||||
|
||||
do
|
||||
|
||||
context.github.paginate(context.github.issues.getAll.endpoint.merge(context.repo())
|
||||
|
||||
Note that when using the new syntax, the responses will be mapped to its data only by default.
|
||||
|
||||
See https://probot.github.io/docs/pagination/`))
|
||||
let collection: any[] = []
|
||||
let getNextPage = true
|
||||
|
||||
|
@ -20,8 +53,8 @@ async function paginate (octokit: GitHubAPI, responsePromise: any, callback = de
|
|||
collection = collection.concat(callback(response, done))
|
||||
|
||||
// eslint-disable-next-line no-unmodified-loop-condition
|
||||
while (getNextPage && octokit.hasNextPage(response)) {
|
||||
response = await octokit.getNextPage(response)
|
||||
while (getNextPage && octokitHasNextPage(response)) {
|
||||
response = await octokitGetNextPage(octokit, response)
|
||||
collection = collection.concat(callback(response, done))
|
||||
}
|
||||
return collection
|
||||
|
|
Loading…
Reference in New Issue