-
Notifications
You must be signed in to change notification settings - Fork 226
Add loading of mock scenarios #1641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aeaa4dd
Revert to previously used version of husky
charisk 9561371
Add loading of mock scenarios
charisk 7daa22f
Add some basic tests for getDirectoryNamesInsidePath
charisk e5eef1f
Make matching a more specific
charisk b60b0a5
Shorten code a bit using map
charisk 02c8b01
Use type guards instead of casting
charisk e4be1f4
Added command for unloading a scenario
charisk b658317
Merge branch 'main' into charisk/load-mock-scenario
charisk eb43047
Deal with optional body
charisk 4450542
Merge branch 'main' into charisk/load-mock-scenario
charisk f31eec7
Fix merge issue
charisk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs-extra'; | ||
| import { DefaultBodyType, MockedRequest, rest, RestHandler } from 'msw'; | ||
| import { | ||
| GitHubApiRequest, | ||
| isGetRepoRequest, | ||
| isGetVariantAnalysisRepoRequest, | ||
| isGetVariantAnalysisRepoResultRequest, | ||
| isGetVariantAnalysisRequest, | ||
| isSubmitVariantAnalysisRequest | ||
| } from './gh-api-request'; | ||
|
|
||
| const baseUrl = 'https://api.github.com'; | ||
|
|
||
| export type RequestHandler = RestHandler<MockedRequest<DefaultBodyType>>; | ||
|
|
||
| export async function createRequestHandlers(scenarioDirPath: string): Promise<RequestHandler[]> { | ||
| const requests = await readRequestFiles(scenarioDirPath); | ||
|
|
||
| const handlers = [ | ||
| createGetRepoRequestHandler(requests), | ||
| createSubmitVariantAnalysisRequestHandler(requests), | ||
| createGetVariantAnalysisRequestHandler(requests), | ||
| ...createGetVariantAnalysisRepoRequestHandlers(requests), | ||
| ...createGetVariantAnalysisRepoResultRequestHandlers(requests), | ||
| ]; | ||
|
|
||
| return handlers; | ||
| } | ||
|
|
||
| async function readRequestFiles(scenarioDirPath: string): Promise<GitHubApiRequest[]> { | ||
| const files = await fs.readdir(scenarioDirPath); | ||
|
|
||
| const orderedFiles = files.sort((a, b) => { | ||
| const aNum = parseInt(a.split('-')[0]); | ||
| const bNum = parseInt(b.split('-')[0]); | ||
| return aNum - bNum; | ||
| }); | ||
|
|
||
| const requests: GitHubApiRequest[] = []; | ||
| for (const file of orderedFiles) { | ||
| const filePath = path.join(scenarioDirPath, file); | ||
| const request: GitHubApiRequest = await fs.readJson(filePath, { encoding: 'utf8' }); | ||
| requests.push(request); | ||
| } | ||
|
|
||
| return requests; | ||
| } | ||
|
|
||
| function createGetRepoRequestHandler(requests: GitHubApiRequest[]): RequestHandler { | ||
| const getRepoRequests = requests.filter(isGetRepoRequest); | ||
|
|
||
| if (getRepoRequests.length > 1) { | ||
| throw Error('More than one get repo request found'); | ||
| } | ||
|
|
||
| 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), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function createSubmitVariantAnalysisRequestHandler(requests: GitHubApiRequest[]): RequestHandler { | ||
| const submitVariantAnalysisRequests = requests.filter(isSubmitVariantAnalysisRequest); | ||
|
|
||
| if (submitVariantAnalysisRequests.length > 1) { | ||
| throw Error('More than one submit variant analysis request found'); | ||
| } | ||
|
|
||
| const getRepoRequest = submitVariantAnalysisRequests[0]; | ||
|
|
||
| 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), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function createGetVariantAnalysisRequestHandler(requests: GitHubApiRequest[]): RequestHandler { | ||
| const getVariantAnalysisRequests = requests.filter(isGetVariantAnalysisRequest); | ||
| let requestIndex = 0; | ||
|
|
||
| // During the lifetime of a variant analysis run, there are multiple requests | ||
| // to get the variant analysis. 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}/repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId`, (_req, res, ctx) => { | ||
| const request = getVariantAnalysisRequests[requestIndex]; | ||
|
|
||
| if (requestIndex < getVariantAnalysisRequests.length - 1) { | ||
| // If there are more requests to come, increment the index. | ||
| requestIndex++; | ||
| } | ||
|
|
||
| return res( | ||
| ctx.status(request.response.status), | ||
| ctx.json(request.response.body), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function createGetVariantAnalysisRepoRequestHandlers(requests: GitHubApiRequest[]): RequestHandler[] { | ||
| const getVariantAnalysisRepoRequests = requests.filter(isGetVariantAnalysisRepoRequest); | ||
|
|
||
| return getVariantAnalysisRepoRequests.map(request => rest.get( | ||
| `${baseUrl}/repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId/repositories/${request.request.repositoryId}`, | ||
| (_req, res, ctx) => { | ||
| return res( | ||
| ctx.status(request.response.status), | ||
| ctx.json(request.response.body), | ||
| ); | ||
| })); | ||
| } | ||
|
|
||
| function createGetVariantAnalysisRepoResultRequestHandlers(requests: GitHubApiRequest[]): RequestHandler[] { | ||
| const getVariantAnalysisRepoResultRequests = requests.filter(isGetVariantAnalysisRepoResultRequest); | ||
|
|
||
| return getVariantAnalysisRepoResultRequests.map(request => rest.get( | ||
| `https://objects-origin.githubusercontent.com/codeql-query-console/codeql-variant-analysis-repo-tasks/:variantAnalysisId/${request.request.repositoryId}/*`, | ||
| (_req, res, ctx) => { | ||
| if (request.response.body) { | ||
| return res( | ||
| ctx.status(request.response.status), | ||
| ctx.body(request.response.body), | ||
| ); | ||
| } else { | ||
| return res( | ||
| ctx.status(request.response.status), | ||
| ); | ||
| } | ||
| })); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.