Use new types
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import { ensureDir, writeFile } from "fs-extra";
|
||||
import { join } from "path";
|
||||
|
||||
import { MockedRequest } from "msw";
|
||||
import { SetupServer } from "msw/node";
|
||||
import { IsomorphicResponse } from "@mswjs/interceptors";
|
||||
|
||||
import { Headers } from "headers-polyfill";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
import { DisposableObject } from "../disposable-object";
|
||||
@@ -17,7 +14,7 @@ import {
|
||||
} from "./gh-api-request";
|
||||
|
||||
export class Recorder extends DisposableObject {
|
||||
private readonly allRequests = new Map<string, MockedRequest>();
|
||||
private readonly allRequests = new Map<string, Request>();
|
||||
private currentRecordedScenario: GitHubApiRequest[] = [];
|
||||
|
||||
private _isRecording = false;
|
||||
@@ -112,16 +109,17 @@ export class Recorder extends DisposableObject {
|
||||
return scenarioDirectory;
|
||||
}
|
||||
|
||||
private onRequestStart(request: MockedRequest): void {
|
||||
private onRequestStart(request: Request, requestId: string): void {
|
||||
if (request.headers.has("x-vscode-codeql-msw-bypass")) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.allRequests.set(request.id, request);
|
||||
this.allRequests.set(requestId, request);
|
||||
}
|
||||
|
||||
private async onResponseBypass(
|
||||
response: IsomorphicResponse,
|
||||
response: Response,
|
||||
_: Request,
|
||||
requestId: string,
|
||||
): Promise<void> {
|
||||
const request = this.allRequests.get(requestId);
|
||||
@@ -137,7 +135,7 @@ export class Recorder extends DisposableObject {
|
||||
const gitHubApiRequest = await createGitHubApiRequest(
|
||||
request.url.toString(),
|
||||
response.status,
|
||||
response.body,
|
||||
response.body?.toString() || "",
|
||||
response.headers,
|
||||
);
|
||||
if (!gitHubApiRequest) {
|
||||
@@ -152,7 +150,7 @@ async function createGitHubApiRequest(
|
||||
url: string,
|
||||
status: number,
|
||||
body: string,
|
||||
headers: Headers,
|
||||
headers: globalThis.Headers,
|
||||
): Promise<GitHubApiRequest | undefined> {
|
||||
if (!url) {
|
||||
return undefined;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { join } from "path";
|
||||
import { readdir, readJson, readFile } from "fs-extra";
|
||||
import { DefaultBodyType, MockedRequest, rest, RestHandler } from "msw";
|
||||
import { RequestHandler, rest } from "msw";
|
||||
import {
|
||||
GitHubApiRequest,
|
||||
isAutoModelRequest,
|
||||
@@ -14,8 +14,6 @@ import {
|
||||
|
||||
const baseUrl = "https://api.github.com";
|
||||
|
||||
type RequestHandler = RestHandler<MockedRequest<DefaultBodyType>>;
|
||||
|
||||
export async function createRequestHandlers(
|
||||
scenarioDirPath: string,
|
||||
): Promise<RequestHandler[]> {
|
||||
@@ -82,11 +80,10 @@ function createGetRepoRequestHandler(
|
||||
|
||||
const getRepoRequest = getRepoRequests[0];
|
||||
|
||||
return rest.get(`${baseUrl}/repos/:owner/:name`, (_req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(getRepoRequest.response.status),
|
||||
ctx.json(getRepoRequest.response.body),
|
||||
);
|
||||
return rest.get(`${baseUrl}/repos/:owner/:name`, () => {
|
||||
return new Response(JSON.stringify(getRepoRequest.response.body), {
|
||||
status: getRepoRequest.response.status,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -105,11 +102,10 @@ function createSubmitVariantAnalysisRequestHandler(
|
||||
|
||||
return rest.post(
|
||||
`${baseUrl}/repositories/:controllerRepoId/code-scanning/codeql/variant-analyses`,
|
||||
(_req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(getRepoRequest.response.status),
|
||||
ctx.json(getRepoRequest.response.body),
|
||||
);
|
||||
() => {
|
||||
return new Response(JSON.stringify(getRepoRequest.response.body), {
|
||||
status: getRepoRequest.response.status,
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -127,7 +123,7 @@ function createGetVariantAnalysisRequestHandler(
|
||||
// request, so keep an index of the request and return the appropriate response.
|
||||
return rest.get(
|
||||
`${baseUrl}/repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId`,
|
||||
(_req, res, ctx) => {
|
||||
() => {
|
||||
const request = getVariantAnalysisRequests[requestIndex];
|
||||
|
||||
if (requestIndex < getVariantAnalysisRequests.length - 1) {
|
||||
@@ -135,10 +131,9 @@ function createGetVariantAnalysisRequestHandler(
|
||||
requestIndex++;
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.status(request.response.status),
|
||||
ctx.json(request.response.body),
|
||||
);
|
||||
return new Response(JSON.stringify(request.response.body), {
|
||||
status: request.response.status,
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -152,18 +147,17 @@ function createGetVariantAnalysisRepoRequestHandler(
|
||||
|
||||
return rest.get(
|
||||
`${baseUrl}/repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId/repositories/:repoId`,
|
||||
(req, res, ctx) => {
|
||||
({ request, params }) => {
|
||||
const scenarioRequest = getVariantAnalysisRepoRequests.find(
|
||||
(r) => r.request.repositoryId.toString() === req.params.repoId,
|
||||
(r) => r.request.repositoryId.toString() === params.repoId,
|
||||
);
|
||||
if (!scenarioRequest) {
|
||||
throw Error(`No scenario request found for ${req.url}`);
|
||||
throw Error(`No scenario request found for ${request.url}`);
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.status(scenarioRequest.response.status),
|
||||
ctx.json(scenarioRequest.response.body),
|
||||
);
|
||||
return new Response(JSON.stringify(scenarioRequest.response.body), {
|
||||
status: scenarioRequest.response.status,
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -177,22 +171,23 @@ function createGetVariantAnalysisRepoResultRequestHandler(
|
||||
|
||||
return rest.get(
|
||||
"https://objects-origin.githubusercontent.com/codeql-query-console/codeql-variant-analysis-repo-tasks/:variantAnalysisId/:repoId/*",
|
||||
(req, res, ctx) => {
|
||||
({ request, params }) => {
|
||||
const scenarioRequest = getVariantAnalysisRepoResultRequests.find(
|
||||
(r) => r.request.repositoryId.toString() === req.params.repoId,
|
||||
(r) => r.request.repositoryId.toString() === params.repoId,
|
||||
);
|
||||
if (!scenarioRequest) {
|
||||
throw Error(`No scenario request found for ${req.url}`);
|
||||
throw Error(`No scenario request found for ${request.url}`);
|
||||
}
|
||||
|
||||
if (scenarioRequest.response.body) {
|
||||
return res(
|
||||
ctx.status(scenarioRequest.response.status),
|
||||
ctx.set("Content-Type", scenarioRequest.response.contentType),
|
||||
ctx.body(scenarioRequest.response.body),
|
||||
);
|
||||
return new Response(scenarioRequest.response.body, {
|
||||
status: scenarioRequest.response.status,
|
||||
headers: {
|
||||
"Content-Type": scenarioRequest.response.contentType,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
return res(ctx.status(scenarioRequest.response.status));
|
||||
return new Response(null, { status: scenarioRequest.response.status });
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -207,7 +202,7 @@ function createCodeSearchRequestHandler(
|
||||
// During a code search, there are multiple request to get pages of results. We
|
||||
// need to return different responses for each request, so keep an index of the
|
||||
// request and return the appropriate response.
|
||||
return rest.get(`${baseUrl}/search/code?q=*`, (_req, res, ctx) => {
|
||||
return rest.get(`${baseUrl}/search/code?q=*`, () => {
|
||||
const request = codeSearchRequests[requestIndex];
|
||||
|
||||
if (requestIndex < codeSearchRequests.length - 1) {
|
||||
@@ -215,10 +210,9 @@ function createCodeSearchRequestHandler(
|
||||
requestIndex++;
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.status(request.response.status),
|
||||
ctx.json(request.response.body),
|
||||
);
|
||||
return new Response(JSON.stringify(request.response.body), {
|
||||
status: request.response.status,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -233,7 +227,7 @@ function createAutoModelRequestHandler(
|
||||
// so keep an index of the request and return the appropriate response.
|
||||
return rest.post(
|
||||
`${baseUrl}/repos/github/codeql/code-scanning/codeql/auto-model`,
|
||||
(_req, res, ctx) => {
|
||||
() => {
|
||||
const request = autoModelRequests[requestIndex];
|
||||
|
||||
if (requestIndex < autoModelRequests.length - 1) {
|
||||
@@ -241,10 +235,9 @@ function createAutoModelRequestHandler(
|
||||
requestIndex++;
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.status(request.response.status),
|
||||
ctx.json(request.response.body),
|
||||
);
|
||||
return new Response(JSON.stringify(request.response.body), {
|
||||
status: request.response.status,
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user