Skip to content

Commit

Permalink
fix: flatten response from 2nd page
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 4, 2020
1 parent 86f8abc commit 05d6de8
Showing 1 changed file with 7 additions and 36 deletions.
43 changes: 7 additions & 36 deletions src/normalize-paginated-list-response.ts
Expand Up @@ -8,53 +8,24 @@
* 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. For the exceptions with the namespace, a fallback check for the route
* paths has to be added in order to normalize the response. We cannot check for the total_count
* property because it also exists in the response of Get the combined status for a specific ref.
* 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
*/

import { Octokit } from "@octokit/core";

import { OctokitResponse } from "./types";

const REGEX = [
// all search endpoints: https://developer.github.com/v3/search/
/^\/search\//,

// All list endpoints for check suites and check runs
// - https://developer.github.com/v3/checks/runs/#list-check-runs-for-a-specific-ref
// - https://developer.github.com/v3/checks/runs/#list-check-runs-in-a-check-suite
// - https://developer.github.com/v3/checks/suites/#list-check-suites-for-a-specific-ref
/^\/repos\/[^/]+\/[^/]+\/commits\/[^/]+\/(check-runs|check-suites)([^/]|$)/,

// List installation repositories
// - https://developer.github.com/v3/apps/installations/#list-repositories
// - https://developer.github.com/v3/apps/installations/#list-installations-for-a-user
// - https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation
/^\/installation\/repositories$/,
/^\/user\/installations([^/]|$)/,

// - https://developer.github.com/v3/actions/secrets/#list-secrets-for-a-repository
/^\/repos\/[^/]+\/[^/]+\/actions\/secrets$/,

// - https://developer.github.com/v3/actions/workflows/#list-repository-workflows
// - https://developer.github.com/v3/actions/workflow_runs/#list-workflow-runs
/^\/repos\/[^/]+\/[^/]+\/actions\/workflows(\/[^/]+\/runs)?$/,

// - https://developer.github.com/v3/actions/artifacts/#list-workflow-run-artifacts
// - https://developer.github.com/v3/actions/workflow_jobs/#list-jobs-for-a-workflow-run
/^\/repos\/[^/]+\/[^/]+\/actions\/runs(\/[^/]+\/(artifacts|jobs))?$/
];

export function normalizePaginatedListResponse(
octokit: Octokit,
url: string,
response: OctokitResponse<any>
) {
const path = url
.replace(octokit.request.endpoint.DEFAULTS.baseUrl, "")
.replace(/\?.*$/, "");
const responseNeedsNormalization = REGEX.find(regex => regex.test(path));
const responseNeedsNormalization =
"total_count" in response.data && !("url" in response.data);
if (!responseNeedsNormalization) return;

// keep the additional properties intact as there is currently no other way
Expand Down

0 comments on commit 05d6de8

Please sign in to comment.