Skip to content

Commit

Permalink
feat: normalize git reference responses
Browse files Browse the repository at this point in the history
closes #21
  • Loading branch information
gr2m committed Nov 5, 2019
1 parent 14e525c commit 2ad4e22
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Import for types only, not a production dependency
import { Octokit } from "@octokit/core";
import { EndpointOptions } from "@octokit/types";
import { EndpointOptions, OctokitResponse } from "@octokit/types";
import { RequestError } from "@octokit/request-error";

import { VERSION } from "./version";
import { isIssueLabelsUpdateOrReplace } from "./is-issue-labels-update-or-replace";
import {
isGetReference,
isListReferences
} from "./is-get-reference-or-list-references";

export function enterpriseCompatibility(octokit: Octokit) {
octokit.hook.wrap(
"request",
(request, options: Required<EndpointOptions>) => {
async (request, options: Required<EndpointOptions>) => {
// see https://github.com/octokit/rest.js/blob/15.x/lib/routes.json#L3046-L3068
if (isIssueLabelsUpdateOrReplace(options)) {
options.data = options.labels;
Expand All @@ -20,6 +25,50 @@ export function enterpriseCompatibility(octokit: Octokit) {
}
return request(options);
}

const isGetReferenceRequest = isGetReference(options);
const isListReferencesRequest = isListReferences(options);
if (isGetReferenceRequest || isListReferencesRequest) {
options.url = options.url.replace(
/\/repos\/([^/]+)\/([^/]+)\/git\/(ref|matching-refs)\/(.*)$/,
"/repos/$1/$2/git/refs/$4"
);
return request(options)
.then((response: OctokitResponse<any>) => {
if (isGetReferenceRequest) {
if (Array.isArray(response.data)) {
throw new RequestError(
`More than one reference found at "${options.url}"`,
404,
{
request: options
}
);
}

// ✅ received single reference
return response;
}

// make sure that
if (!Array.isArray(response.data)) {
response.data = [response.data];
}

return response;
})
.catch((error: RequestError) => {
if (isListReferencesRequest && error.status === 404) {
return {
status: 200,
headers: error.headers,
data: []
};
}

throw error;
});
}
}
);
}
Expand Down
20 changes: 20 additions & 0 deletions src/is-get-reference-or-list-references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EndpointOptions } from "@octokit/types";

const REGEX_IS_GET_REFERENCE_PATH = /\/repos\/[^/]+\/[^/]+\/git\/ref\//;
const REGEX_IS_LIST_REFERENCES_PATH = /\/repos\/[^/]+\/[^/]+\/git\/matching-refs\//;

export function isGetReference({ method, url }: EndpointOptions) {
if (!["GET", "HEAD"].includes(method)) {
return false;
}

return REGEX_IS_GET_REFERENCE_PATH.test(url);
}

export function isListReferences({ method, url }: EndpointOptions) {
if (!["GET", "HEAD"].includes(method)) {
return false;
}

return REGEX_IS_LIST_REFERENCES_PATH.test(url);
}

0 comments on commit 2ad4e22

Please sign in to comment.