Skip to content

Commit 89b00ea

Browse files
committed
Show input box if there's no controller repo defined in settings
1 parent 4bc5086 commit 89b00ea

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

extensions/ql-vscode/src/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,7 @@ const REMOTE_CONTROLLER_REPO = new Setting('controllerRepo', REMOTE_QUERIES_SETT
325325
export function getRemoteControllerRepo(): string | undefined {
326326
return REMOTE_CONTROLLER_REPO.getValue<string>() || undefined;
327327
}
328+
329+
export async function setRemoteControllerRepo(repo: string | undefined) {
330+
await REMOTE_CONTROLLER_REPO.updateValue(repo, ConfigurationTarget.Global);
331+
}

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

+33-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { findLanguage, showAndLogErrorMessage, showAndLogInformationMessage, sho
55
import { Credentials } from './authentication';
66
import * as cli from './cli';
77
import { logger } from './logging';
8-
import { getRemoteControllerRepo, getRemoteRepositoryLists } from './config';
8+
import { getRemoteControllerRepo, getRemoteRepositoryLists, setRemoteControllerRepo } from './config';
99
interface Config {
1010
repositories: string[];
1111
ref?: string;
@@ -16,6 +16,13 @@ interface RepoListQuickPickItem extends QuickPickItem {
1616
repoList: string[];
1717
}
1818

19+
/**
20+
* This regex matches strings of the form `owner/repo` where:
21+
* - `owner` is made up of alphanumeric characters or single hyphens, starting and ending in an alphanumeric character
22+
* - `repo` is made up of alphanumeric characters, hyphens, or underscores
23+
*/
24+
const REPO_REGEX = /^(?:[a-zA-Z0-9]+-)*[a-zA-Z0-9]+\/[a-zA-Z0-9-_]+$/;
25+
1926
/**
2027
* Gets the repositories to run the query against.
2128
*/
@@ -43,12 +50,6 @@ export async function getRepositories(): Promise<string[] | undefined> {
4350
}
4451
} else {
4552
void logger.log('No repository lists defined. Displaying text input box.');
46-
/**
47-
* This regex matches strings of the form `owner/repo` where:
48-
* - `owner` is made up of alphanumeric characters or single hyphens, starting and ending in an alphanumeric character
49-
* - `repo` is made up of alphanumeric characters, hyphens, or underscores
50-
*/
51-
const repoRegex = /^(?:[a-zA-Z0-9]+-)*[a-zA-Z0-9]+\/[a-zA-Z0-9-_]+$/;
5253
const remoteRepo = await window.showInputBox({
5354
title: 'Enter a GitHub repository in the format <owner>/<repo> (e.g. github/codeql)',
5455
placeHolder: '<owner>/<repo>',
@@ -58,7 +59,7 @@ export async function getRepositories(): Promise<string[] | undefined> {
5859
if (!remoteRepo) {
5960
void showAndLogErrorMessage('No repositories entered.');
6061
return;
61-
} else if (!repoRegex.test(remoteRepo)) { // Check if user entered invalid input
62+
} else if (!REPO_REGEX.test(remoteRepo)) { // Check if user entered invalid input
6263
void showAndLogErrorMessage('Invalid repository format. Must be in the format <owner>/<repo> (e.g. github/codeql)');
6364
return;
6465
}
@@ -103,18 +104,32 @@ export async function runRemoteQuery(cliServer: cli.CodeQLCliServer, credentials
103104
return; // No error message needed, since `getRepositories` already displays one.
104105
}
105106

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}.`);
107+
// Get the controller repo from the config, if it exists.
108+
// If it doesn't exist, prompt the user to enter it, and save that value to the config.
109+
let controllerRepo: string | undefined;
110+
controllerRepo = getRemoteControllerRepo();
111+
if (!controllerRepo) {
112+
void logger.log('No controller repository defined.');
113+
controllerRepo = await window.showInputBox({
114+
title: 'Controller repository in which to display progress and results of remote queries',
115+
placeHolder: '<owner>/<repo>',
116+
prompt: 'Enter the name of a GitHub repository in the format <owner>/<repo>',
117+
ignoreFocusOut: true,
118+
});
119+
if (!controllerRepo) {
120+
void showAndLogErrorMessage('No controller repository entered.');
121+
return;
122+
} else if (!REPO_REGEX.test(controllerRepo)) { // Check if user entered invalid input
123+
void showAndLogErrorMessage('Invalid repository format. Must be a valid GitHub repository in the format <owner>/<repo>.');
124+
return;
125+
}
126+
void logger.log(`Setting the controller repository as: ${controllerRepo}`);
127+
await setRemoteControllerRepo(controllerRepo);
116128
}
117129

130+
void logger.log(`Using controller repository: ${controllerRepo}`);
131+
const [owner, repo] = controllerRepo.split('/');
132+
118133
await runRemoteQueriesApiRequest(credentials, ref, language, repositories, query, owner, repo);
119134
}
120135

0 commit comments

Comments
 (0)