From 05d6de89adaa3b2a45dddbf1c22b9c8a7e9897c6 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Wed, 4 Mar 2020 12:10:15 -0800 Subject: [PATCH] fix: flatten response from 2nd page --- src/normalize-paginated-list-response.ts | 43 ++++-------------------- 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/src/normalize-paginated-list-response.ts b/src/normalize-paginated-list-response.ts index 20493a4c..bbf1674c 100644 --- a/src/normalize-paginated-list-response.ts +++ b/src/normalize-paginated-list-response.ts @@ -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 ) { - 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