2021-02-08 12:58:32 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
const VERSION = "2.13.3";
|
2021-02-08 12:58:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Some “list” response that can be paginated have a different response structure
|
|
|
|
*
|
|
|
|
* They have a `total_count` key in the response (search also has `incomplete_results`,
|
|
|
|
* /installation/repositories also has `repository_selection`), as well as a key with
|
|
|
|
* the list of the items which name varies from endpoint to endpoint.
|
|
|
|
*
|
|
|
|
* Octokit normalizes these responses so that paginated results are always returned following
|
|
|
|
* the same structure. One challenge is that if the list response has only one page, no Link
|
|
|
|
* header is provided, so this header alone is not sufficient to check wether a response is
|
|
|
|
* paginated or not.
|
|
|
|
*
|
|
|
|
* We check if a "total_count" key is present in the response data, but also make sure that
|
|
|
|
* a "url" property is not, as the "Get the combined status for a specific ref" endpoint would
|
|
|
|
* otherwise match: https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref
|
|
|
|
*/
|
|
|
|
function normalizePaginatedListResponse(response) {
|
|
|
|
const responseNeedsNormalization = "total_count" in response.data && !("url" in response.data);
|
|
|
|
if (!responseNeedsNormalization) return response; // keep the additional properties intact as there is currently no other way
|
|
|
|
// to retrieve the same information.
|
|
|
|
|
|
|
|
const incompleteResults = response.data.incomplete_results;
|
|
|
|
const repositorySelection = response.data.repository_selection;
|
|
|
|
const totalCount = response.data.total_count;
|
|
|
|
delete response.data.incomplete_results;
|
|
|
|
delete response.data.repository_selection;
|
|
|
|
delete response.data.total_count;
|
|
|
|
const namespaceKey = Object.keys(response.data)[0];
|
|
|
|
const data = response.data[namespaceKey];
|
|
|
|
response.data = data;
|
|
|
|
|
|
|
|
if (typeof incompleteResults !== "undefined") {
|
|
|
|
response.data.incomplete_results = incompleteResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof repositorySelection !== "undefined") {
|
|
|
|
response.data.repository_selection = repositorySelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.data.total_count = totalCount;
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
function iterator(octokit, route, parameters) {
|
|
|
|
const options = typeof route === "function" ? route.endpoint(parameters) : octokit.request.endpoint(route, parameters);
|
|
|
|
const requestMethod = typeof route === "function" ? route : octokit.request;
|
|
|
|
const method = options.method;
|
|
|
|
const headers = options.headers;
|
|
|
|
let url = options.url;
|
|
|
|
return {
|
|
|
|
[Symbol.asyncIterator]: () => ({
|
2021-06-04 22:19:14 +08:00
|
|
|
async next() {
|
|
|
|
if (!url) return {
|
|
|
|
done: true
|
|
|
|
};
|
|
|
|
const response = await requestMethod({
|
2021-02-08 12:58:32 +08:00
|
|
|
method,
|
|
|
|
url,
|
|
|
|
headers
|
|
|
|
});
|
2021-06-04 22:19:14 +08:00
|
|
|
const normalizedResponse = normalizePaginatedListResponse(response); // `response.headers.link` format:
|
|
|
|
// '<https://api.github.com/users/aseemk/followers?page=2>; rel="next", <https://api.github.com/users/aseemk/followers?page=2>; rel="last"'
|
|
|
|
// sets `url` to undefined if "next" URL is not present or `link` header is not set
|
|
|
|
|
|
|
|
url = ((normalizedResponse.headers.link || "").match(/<([^>]+)>;\s*rel="next"/) || [])[1];
|
|
|
|
return {
|
|
|
|
value: normalizedResponse
|
|
|
|
};
|
2021-02-08 12:58:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function paginate(octokit, route, parameters, mapFn) {
|
|
|
|
if (typeof parameters === "function") {
|
|
|
|
mapFn = parameters;
|
|
|
|
parameters = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gather(octokit, [], iterator(octokit, route, parameters)[Symbol.asyncIterator](), mapFn);
|
|
|
|
}
|
|
|
|
|
|
|
|
function gather(octokit, results, iterator, mapFn) {
|
|
|
|
return iterator.next().then(result => {
|
|
|
|
if (result.done) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
let earlyExit = false;
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
earlyExit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
results = results.concat(mapFn ? mapFn(result.value, done) : result.value.data);
|
|
|
|
|
|
|
|
if (earlyExit) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gather(octokit, results, iterator, mapFn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
const composePaginateRest = Object.assign(paginate, {
|
|
|
|
iterator
|
|
|
|
});
|
|
|
|
|
|
|
|
const paginatingEndpoints = ["GET /app/installations", "GET /applications/grants", "GET /authorizations", "GET /enterprises/{enterprise}/actions/permissions/organizations", "GET /enterprises/{enterprise}/actions/runner-groups", "GET /enterprises/{enterprise}/actions/runner-groups/{runner_group_id}/organizations", "GET /enterprises/{enterprise}/actions/runner-groups/{runner_group_id}/runners", "GET /enterprises/{enterprise}/actions/runners", "GET /enterprises/{enterprise}/actions/runners/downloads", "GET /events", "GET /gists", "GET /gists/public", "GET /gists/starred", "GET /gists/{gist_id}/comments", "GET /gists/{gist_id}/commits", "GET /gists/{gist_id}/forks", "GET /installation/repositories", "GET /issues", "GET /marketplace_listing/plans", "GET /marketplace_listing/plans/{plan_id}/accounts", "GET /marketplace_listing/stubbed/plans", "GET /marketplace_listing/stubbed/plans/{plan_id}/accounts", "GET /networks/{owner}/{repo}/events", "GET /notifications", "GET /organizations", "GET /orgs/{org}/actions/permissions/repositories", "GET /orgs/{org}/actions/runner-groups", "GET /orgs/{org}/actions/runner-groups/{runner_group_id}/repositories", "GET /orgs/{org}/actions/runner-groups/{runner_group_id}/runners", "GET /orgs/{org}/actions/runners", "GET /orgs/{org}/actions/runners/downloads", "GET /orgs/{org}/actions/secrets", "GET /orgs/{org}/actions/secrets/{secret_name}/repositories", "GET /orgs/{org}/blocks", "GET /orgs/{org}/credential-authorizations", "GET /orgs/{org}/events", "GET /orgs/{org}/failed_invitations", "GET /orgs/{org}/hooks", "GET /orgs/{org}/installations", "GET /orgs/{org}/invitations", "GET /orgs/{org}/invitations/{invitation_id}/teams", "GET /orgs/{org}/issues", "GET /orgs/{org}/members", "GET /orgs/{org}/migrations", "GET /orgs/{org}/migrations/{migration_id}/repositories", "GET /orgs/{org}/outside_collaborators", "GET /orgs/{org}/projects", "GET /orgs/{org}/public_members", "GET /orgs/{org}/repos", "GET /orgs/{org}/team-sync/groups", "GET /orgs/{org}/teams", "GET /orgs/{org}/teams/{team_slug}/discussions", "GET /orgs/{org}/teams/{team_slug}/discussions/{discussion_number}/comments", "GET /orgs/{org}/teams/{team_slug}/discussions/{discussion_number}/comments/{comment_number}/reactions", "GET /orgs/{org}/teams/{team_slug}/discussions/{discussion_number}/reactions", "GET /orgs/{org}/teams/{team_slug}/invitations", "GET /orgs/{org}/teams/{team_slug}/members", "GET /orgs/{org}/teams/{team_slug}/projects", "GET /orgs/{org}/teams/{team_slug}/repos", "GET /orgs/{org}/teams/{team_slug}/team-sync/group-mappings", "GET /orgs/{org}/teams/{team_slug}/teams", "GET /projects/columns/{column_id}/cards", "GET /projects/{project_id}/collaborators", "GET /projects/{project_id}/columns", "GET /repos/{owner}/{repo}/actions/artifacts", "GET /repos/{owner}/{repo}/actions/runners", "GET /repos/{owner}/{repo}/actions/runners/downloads", "GET /repos/{owner}/{repo}/actions/runs", "GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts", "GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs", "GET /repos/{owner}/{repo}/actions/secrets", "GET /repos/{owner}/{repo}/actions/workflows", "GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs", "GET /repos/{owner}/{repo}/assignees", "GET /repos/{owner}/{repo}/branches", "GET /repos/{owner}/{repo}/check-runs/{check_run_id}/annotations", "GET /repos/{owner}/{repo}/check-suites/{check_suite_id}/check-runs", "GET /repos/{owner}/{repo}/code-scanning/alerts", "GET /repos/{owner}/{repo}/code-scanning/alerts/{alert_number}/instances", "GET /repos/{owner}/{repo}/code-scanning/analyses", "GET /repos/{owner}/{repo}/collaborators", "GET /repos/{owner}/{repo}/comments", "GET /repos/{owner}/{repo}/comments/{comment_id}/reactions", "GET /repos/{owner}/{repo}/commits", "GET /repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head", "GET /repos/{owner}/{repo}/commits/{commit_sha}/comments", "GET /repos/{owner}/{repo}/commits/{commit_sha}/pulls", "GET /repos/{owner}/{repo}/commits/{ref}/check-runs", "GET /repos/{owner}/{repo}/commits/{ref}/check-suites", "GET /repos/{owner}/{repo}/commit
|
|
|
|
|
|
|
|
function isPaginatingEndpoint(arg) {
|
|
|
|
if (typeof arg === "string") {
|
|
|
|
return paginatingEndpoints.includes(arg);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* @param octokit Octokit instance
|
|
|
|
* @param options Options passed to Octokit constructor
|
|
|
|
*/
|
|
|
|
|
|
|
|
function paginateRest(octokit) {
|
|
|
|
return {
|
|
|
|
paginate: Object.assign(paginate.bind(null, octokit), {
|
|
|
|
iterator: iterator.bind(null, octokit)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
paginateRest.VERSION = VERSION;
|
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
exports.composePaginateRest = composePaginateRest;
|
|
|
|
exports.isPaginatingEndpoint = isPaginatingEndpoint;
|
2021-02-08 12:58:32 +08:00
|
|
|
exports.paginateRest = paginateRest;
|
2021-06-04 22:19:14 +08:00
|
|
|
exports.paginatingEndpoints = paginatingEndpoints;
|
2021-02-08 12:58:32 +08:00
|
|
|
//# sourceMappingURL=index.js.map
|