Skip to content

Commit 2dad33f

Browse files
committed
Change remote queries to test against submitted data
The remote queries tests were testing the data on the filesystem, rather than the data submitted to the server. This required using a `dryRun` parameter to prevent deleting the temporary directory, while we can actually just test against the submitted data. This will create an in-memory filesystem of the submitted query pack by un-tar-gz'ing the query pack into memory and using that to test the existence of certain files.
1 parent 5905cf8 commit 2dad33f

File tree

8 files changed

+241
-120
lines changed

8 files changed

+241
-120
lines changed

extensions/ql-vscode/package-lock.json

Lines changed: 19 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: 1 addition & 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",

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);
@@ -378,7 +367,7 @@ async function runRemoteQueriesApiRequest(
378367
controllerRepo: Repository,
379368
queryPackBase64: string,
380369
dryRun = false
381-
): Promise<void | QueriesResponse> {
370+
): Promise<void | RemoteQueriesResponse> {
382371
const data = {
383372
ref,
384373
language,
@@ -401,17 +390,18 @@ async function runRemoteQueriesApiRequest(
401390
}
402391

403392
try {
404-
const octokit = await credentials.getOctokit();
405-
const response: OctokitResponse<QueriesResponse, number> = await octokit.request(
406-
'POST /repositories/:controllerRepoId/code-scanning/codeql/queries',
407-
{
408-
controllerRepoId: controllerRepo.id,
409-
data
410-
}
411-
);
412-
const { popupMessage, logMessage } = parseResponse(controllerRepo, response.data);
393+
const response = await ghApiClient.submitRemoteQueries(credentials, {
394+
ref,
395+
language,
396+
repositories: repoSelection.repositories,
397+
repositoryLists: repoSelection.repositoryLists,
398+
repositoryOwners: repoSelection.owners,
399+
queryPack: queryPackBase64,
400+
controllerRepoId: controllerRepo.id,
401+
});
402+
const { popupMessage, logMessage } = parseResponse(controllerRepo, response);
413403
void showAndLogInformationMessage(popupMessage, { fullMessage: logMessage });
414-
return response.data;
404+
return response;
415405
} catch (error: any) {
416406
if (error.status === 404) {
417407
void showAndLogErrorMessage(`Controller repository was not found. Please make sure it's a valid repo name.${eol}`);
@@ -425,7 +415,7 @@ const eol = os.EOL;
425415
const eol2 = os.EOL + os.EOL;
426416

427417
// exported for testing only
428-
export function parseResponse(controllerRepo: Repository, response: QueriesResponse) {
418+
export function parseResponse(controllerRepo: Repository, response: RemoteQueriesResponse) {
429419
const repositoriesQueried = response.repositories_queried;
430420
const repositoryCount = repositoriesQueried.length;
431421

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)