Skip to content

Commit 9894eda

Browse files
authored
Merge pull request #1746 from github/koesie10/remote-queries-tests-tar
Change remote queries to test against submitted data
2 parents 1241ce9 + a4402cb commit 9894eda

File tree

8 files changed

+243
-120
lines changed

8 files changed

+243
-120
lines changed

extensions/ql-vscode/package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ql-vscode/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@
13801380
"@types/sinon-chai": "~3.2.3",
13811381
"@types/stream-chain": "~2.0.1",
13821382
"@types/stream-json": "~1.7.1",
1383+
"@types/tar-stream": "^2.2.2",
13831384
"@types/through2": "^2.0.36",
13841385
"@types/tmp": "^0.1.0",
13851386
"@types/unzipper": "~0.10.1",
@@ -1420,6 +1421,7 @@
14201421
"proxyquire": "~2.1.3",
14211422
"sinon": "~14.0.0",
14221423
"sinon-chai": "~3.5.0",
1424+
"tar-stream": "^2.2.0",
14231425
"through2": "^4.0.2",
14241426
"ts-jest": "^29.0.1",
14251427
"ts-json-schema-generator": "^1.1.2",

extensions/ql-vscode/src/remote-queries/gh-api/gh-api-client.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Credentials } from '../../authentication';
22
import { OctokitResponse } from '@octokit/types/dist-types';
3+
import { RemoteQueriesSubmission } from '../shared/remote-queries';
34
import { VariantAnalysisSubmission } from '../shared/variant-analysis';
45
import {
56
VariantAnalysis,
67
VariantAnalysisRepoTask,
78
VariantAnalysisSubmissionRequest
89
} from './variant-analysis';
910
import { Repository } from './repository';
11+
import { RemoteQueriesResponse, RemoteQueriesSubmissionRequest } from './remote-queries';
1012

1113
export async function submitVariantAnalysis(
1214
credentials: Credentials,
@@ -116,3 +118,31 @@ export async function createGist(
116118
}
117119
return response.data.html_url;
118120
}
121+
122+
export async function submitRemoteQueries(
123+
credentials: Credentials,
124+
submissionDetails: RemoteQueriesSubmission
125+
): Promise<RemoteQueriesResponse> {
126+
const octokit = await credentials.getOctokit();
127+
128+
const { ref, language, repositories, repositoryLists, repositoryOwners, queryPack, controllerRepoId } = submissionDetails;
129+
130+
const data: RemoteQueriesSubmissionRequest = {
131+
ref,
132+
language,
133+
repositories,
134+
repository_lists: repositoryLists,
135+
repository_owners: repositoryOwners,
136+
query_pack: queryPack,
137+
};
138+
139+
const response: OctokitResponse<RemoteQueriesResponse> = await octokit.request(
140+
'POST /repositories/:controllerRepoId/code-scanning/codeql/queries',
141+
{
142+
controllerRepoId,
143+
data
144+
}
145+
);
146+
147+
return response.data;
148+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export interface RemoteQueriesSubmissionRequest {
2+
ref: string;
3+
language: string;
4+
repositories?: string[];
5+
repository_lists?: string[];
6+
repository_owners?: string[];
7+
query_pack: string;
8+
}
9+
10+
export interface RemoteQueriesResponse {
11+
workflow_run_id: number,
12+
errors?: {
13+
invalid_repositories?: string[],
14+
repositories_without_database?: string[],
15+
private_repositories?: string[],
16+
cutoff_repositories?: string[],
17+
cutoff_repositories_count?: number,
18+
},
19+
repositories_queried: string[],
20+
}

extensions/ql-vscode/src/remote-queries/run-remote-query.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import * as cli from '../cli';
1818
import { logger } from '../logging';
1919
import { getActionBranch, getRemoteControllerRepo, isVariantAnalysisLiveResultsEnabled, setRemoteControllerRepo } from '../config';
2020
import { ProgressCallback, UserCancellationException } from '../commandRunner';
21-
import { OctokitResponse, RequestError } from '@octokit/types/dist-types';
21+
import { RequestError } from '@octokit/types/dist-types';
2222
import { RemoteQuery } from './remote-query';
2323
import { RemoteQuerySubmissionResult } from './remote-query-submission-result';
2424
import { QueryMetadata } from '../pure/interface-types';
2525
import { getErrorMessage, REPO_REGEX } from '../pure/helpers-pure';
2626
import { pluralize } from '../pure/word';
2727
import * as ghApiClient from './gh-api/gh-api-client';
28+
import { RemoteQueriesResponse } from './gh-api/remote-queries';
2829
import { getRepositorySelection, isValidSelection, RepositorySelection } from './repository-selection';
2930
import { parseVariantAnalysisQueryLanguage, VariantAnalysisSubmission } from './shared/variant-analysis';
3031
import { Repository } from './shared/repository';
@@ -39,18 +40,6 @@ export interface QlPack {
3940
defaultSuiteFile?: string;
4041
}
4142

42-
interface QueriesResponse {
43-
workflow_run_id: number,
44-
errors?: {
45-
invalid_repositories?: string[],
46-
repositories_without_database?: string[],
47-
private_repositories?: string[],
48-
cutoff_repositories?: string[],
49-
cutoff_repositories_count?: number,
50-
},
51-
repositories_queried: string[],
52-
}
53-
5443
/**
5544
* Well-known names for the query pack used by the server.
5645
*/
@@ -172,7 +161,7 @@ function isFileSystemRoot(dir: string): boolean {
172161
return pathObj.root === dir && pathObj.base === '';
173162
}
174163

175-
async function createRemoteQueriesTempDirectory() {
164+
export async function createRemoteQueriesTempDirectory() {
176165
const remoteQueryDir = await tmp.dir({ dir: tmpDir.name, unsafeCleanup: true });
177166
const queryPackDir = path.join(remoteQueryDir.path, 'query-pack');
178167
await fs.mkdirp(queryPackDir);
@@ -389,7 +378,7 @@ async function runRemoteQueriesApiRequest(
389378
controllerRepo: Repository,
390379
queryPackBase64: string,
391380
dryRun = false
392-
): Promise<void | QueriesResponse> {
381+
): Promise<void | RemoteQueriesResponse> {
393382
const data = {
394383
ref,
395384
language,
@@ -412,17 +401,18 @@ async function runRemoteQueriesApiRequest(
412401
}
413402

414403
try {
415-
const octokit = await credentials.getOctokit();
416-
const response: OctokitResponse<QueriesResponse, number> = await octokit.request(
417-
'POST /repositories/:controllerRepoId/code-scanning/codeql/queries',
418-
{
419-
controllerRepoId: controllerRepo.id,
420-
data
421-
}
422-
);
423-
const { popupMessage, logMessage } = parseResponse(controllerRepo, response.data);
404+
const response = await ghApiClient.submitRemoteQueries(credentials, {
405+
ref,
406+
language,
407+
repositories: repoSelection.repositories,
408+
repositoryLists: repoSelection.repositoryLists,
409+
repositoryOwners: repoSelection.owners,
410+
queryPack: queryPackBase64,
411+
controllerRepoId: controllerRepo.id,
412+
});
413+
const { popupMessage, logMessage } = parseResponse(controllerRepo, response);
424414
void showAndLogInformationMessage(popupMessage, { fullMessage: logMessage });
425-
return response.data;
415+
return response;
426416
} catch (error: any) {
427417
if (error.status === 404) {
428418
void showAndLogErrorMessage(`Controller repository was not found. Please make sure it's a valid repo name.${eol}`);
@@ -436,7 +426,7 @@ const eol = os.EOL;
436426
const eol2 = os.EOL + os.EOL;
437427

438428
// exported for testing only
439-
export function parseResponse(controllerRepo: Repository, response: QueriesResponse) {
429+
export function parseResponse(controllerRepo: Repository, response: RemoteQueriesResponse) {
440430
const repositoriesQueried = response.repositories_queried;
441431
const repositoryCount = repositoriesQueried.length;
442432

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface RemoteQueriesSubmission {
2+
ref: string;
3+
language: string;
4+
repositories?: string[];
5+
repositoryLists?: string[];
6+
repositoryOwners?: string[];
7+
queryPack: string;
8+
9+
controllerRepoId: number;
10+
}

0 commit comments

Comments
 (0)