Skip to content

Commit 7a79d39

Browse files
committed
Add new setting to specify controller repo
1 parent 41ae5a4 commit 7a79d39

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

extensions/ql-vscode/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@
256256
},
257257
"default": null,
258258
"markdownDescription": "[For internal use only] Lists of GitHub repositories that you want to query remotely. This should be a JSON object where each key is a user-specified name for this repository list, and the value is an array of GitHub repositories (of the form `<owner>/<repo>`)."
259+
},
260+
"codeQL.remoteQueries.controllerRepo": {
261+
"type": "string",
262+
"default": "",
263+
"pattern": "^$|^(?:[a-zA-Z0-9]+-)*[a-zA-Z0-9]+/[a-zA-Z0-9-_]+$",
264+
"patternErrorMessage": "Please enter a valid GitHub repository",
265+
"markdownDescription": "[For internal use only] The name of the GitHub repository where you can view the progress and results of the \"Run Remote query\" command. The repository should be of the form `<owner>/<repo>`)."
259266
}
260267
}
261268
},

extensions/ql-vscode/src/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,15 @@ const REMOTE_REPO_LISTS = new Setting('repositoryLists', REMOTE_QUERIES_SETTING)
313313
export function getRemoteRepositoryLists(): Record<string, string[]> | undefined {
314314
return REMOTE_REPO_LISTS.getValue<Record<string, string[]>>() || undefined;
315315
}
316+
317+
/**
318+
* The name of the "controller" repository that you want to use with the "Run Remote query" command.
319+
* Note: This command is only available for internal users.
320+
*
321+
* This setting should be a GitHub repository of the form `<owner>/<repo>`.
322+
*/
323+
const REMOTE_CONTROLLER_REPO = new Setting('controllerRepo', REMOTE_QUERIES_SETTING);
324+
325+
export function getRemoteControllerRepo(): string | undefined {
326+
return REMOTE_CONTROLLER_REPO.getValue<string>() || undefined;
327+
}

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import { findLanguage, showAndLogErrorMessage, showAndLogInformationMessage, sho
55
import { Credentials } from './authentication';
66
import * as cli from './cli';
77
import { logger } from './logging';
8-
import { getRemoteRepositoryLists } from './config';
8+
import { getRemoteControllerRepo, getRemoteRepositoryLists } from './config';
99
interface Config {
1010
repositories: string[];
1111
ref?: string;
1212
language?: string;
1313
}
1414

15-
// Test "controller" repository and workflow.
16-
const OWNER = 'dsp-testing';
17-
const REPO = 'qc-controller';
18-
1915
interface RepoListQuickPickItem extends QuickPickItem {
2016
repoList: string[];
2117
}
@@ -107,18 +103,30 @@ export async function runRemoteQuery(cliServer: cli.CodeQLCliServer, credentials
107103
return; // No error message needed, since `getRepositories` already displays one.
108104
}
109105

110-
await runRemoteQueriesApiRequest(credentials, ref, language, repositories, query);
106+
// Get the controller repo
107+
let owner: string;
108+
let repo: string;
109+
const controllerRepo = getRemoteControllerRepo();
110+
if (controllerRepo) {
111+
void logger.log(`Using controller repository: ${controllerRepo}`);
112+
[owner, repo] = controllerRepo.split('/');
113+
} else {
114+
[owner, repo] = ['dsp-testing', 'qc-controller'];
115+
void logger.log(`No controller repository defined in the 'codeQL.remoteQueries.controllerRepo' setting. Using default repository: ${owner}/${repo}.`);
116+
}
117+
118+
await runRemoteQueriesApiRequest(credentials, ref, language, repositories, query, owner, repo);
111119
}
112120

113-
async function runRemoteQueriesApiRequest(credentials: Credentials, ref: string, language: string, repositories: string[], query: string) {
121+
async function runRemoteQueriesApiRequest(credentials: Credentials, ref: string, language: string, repositories: string[], query: string, owner: string, repo: string) {
114122
const octokit = await credentials.getOctokit();
115123

116124
try {
117125
await octokit.request(
118126
'POST /repos/:owner/:repo/code-scanning/codeql/queries',
119127
{
120-
owner: OWNER,
121-
repo: REPO,
128+
owner,
129+
repo,
122130
data: {
123131
ref: ref,
124132
language: language,
@@ -127,15 +135,15 @@ async function runRemoteQueriesApiRequest(credentials: Credentials, ref: string,
127135
}
128136
}
129137
);
130-
void showAndLogInformationMessage(`Successfully scheduled runs. [Click here to see the progress](https://github.com/${OWNER}/${REPO}/actions).`);
138+
void showAndLogInformationMessage(`Successfully scheduled runs. [Click here to see the progress](https://github.com/${owner}/${repo}/actions).`);
131139

132140
} catch (error) {
133-
await attemptRerun(error, credentials, ref, language, repositories, query);
141+
await attemptRerun(error, credentials, ref, language, repositories, query, owner, repo);
134142
}
135143
}
136144

137145
/** Attempts to rerun the query on only the valid repositories */
138-
export async function attemptRerun(error: any, credentials: Credentials, ref: string, language: string, repositories: string[], query: string) {
146+
export async function attemptRerun(error: any, credentials: Credentials, ref: string, language: string, repositories: string[], query: string, owner: string, repo: string) {
139147
if (typeof error.message === 'string' && error.message.includes('Some repositories were invalid')) {
140148
const invalidRepos = error?.response?.data?.invalid_repos || [];
141149
const reposWithoutDbUploads = error?.response?.data?.repos_without_db_uploads || [];
@@ -158,7 +166,7 @@ export async function attemptRerun(error: any, credentials: Credentials, ref: st
158166
if (rerunQuery) {
159167
const validRepositories = repositories.filter(r => !invalidRepos.includes(r) && !reposWithoutDbUploads.includes(r));
160168
void logger.log(`Rerunning query on set of valid repositories: ${JSON.stringify(validRepositories)}`);
161-
await runRemoteQueriesApiRequest(credentials, ref, language, validRepositories, query);
169+
await runRemoteQueriesApiRequest(credentials, ref, language, validRepositories, query, owner, repo);
162170
}
163171

164172
} else {

0 commit comments

Comments
 (0)