Skip to content

Commit e5dcec8

Browse files
authored
Move repository selection code to own module (#1269)
1 parent ad3565d commit e5dcec8

3 files changed

Lines changed: 60 additions & 52 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { QuickPickItem, window } from 'vscode';
2+
import { showAndLogErrorMessage } from '../helpers';
3+
import { getRemoteRepositoryLists } from '../config';
4+
import { logger } from '../logging';
5+
import { REPO_REGEX } from '../pure/helpers-pure';
6+
7+
interface RepoListQuickPickItem extends QuickPickItem {
8+
repoList: string[];
9+
}
10+
11+
/**
12+
* Gets the repositories to run the query against.
13+
*/
14+
export async function getRepositories(): Promise<string[] | undefined> {
15+
const repoLists = getRemoteRepositoryLists();
16+
if (repoLists && Object.keys(repoLists).length) {
17+
const quickPickItems = Object.entries(repoLists).map<RepoListQuickPickItem>(([key, value]) => (
18+
{
19+
label: key, // the name of the repository list
20+
repoList: value, // the actual array of repositories
21+
}
22+
));
23+
const quickpick = await window.showQuickPick<RepoListQuickPickItem>(
24+
quickPickItems,
25+
{
26+
placeHolder: 'Select a repository list. You can define repository lists in the `codeQL.variantAnalysis.repositoryLists` setting.',
27+
ignoreFocusOut: true,
28+
});
29+
if (quickpick?.repoList.length) {
30+
void logger.log(`Selected repositories: ${quickpick.repoList.join(', ')}`);
31+
return quickpick.repoList;
32+
} else {
33+
void showAndLogErrorMessage('No repositories selected.');
34+
return;
35+
}
36+
} else {
37+
void logger.log('No repository lists defined. Displaying text input box.');
38+
const remoteRepo = await window.showInputBox({
39+
title: 'Enter a GitHub repository in the format <owner>/<repo> (e.g. github/codeql)',
40+
placeHolder: '<owner>/<repo>',
41+
prompt: 'Tip: you can save frequently used repositories in the `codeQL.variantAnalysis.repositoryLists` setting',
42+
ignoreFocusOut: true,
43+
});
44+
if (!remoteRepo) {
45+
void showAndLogErrorMessage('No repositories entered.');
46+
return;
47+
} else if (!REPO_REGEX.test(remoteRepo)) { // Check if user entered invalid input
48+
void showAndLogErrorMessage('Invalid repository format. Must be in the format <owner>/<repo> (e.g. github/codeql)');
49+
return;
50+
}
51+
void logger.log(`Entered repository: ${remoteRepo}`);
52+
return [remoteRepo];
53+
}
54+
}

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

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CancellationToken, QuickPickItem, Uri, window } from 'vscode';
1+
import { CancellationToken, Uri, window } from 'vscode';
22
import * as path from 'path';
33
import * as yaml from 'js-yaml';
44
import * as fs from 'fs-extra';
@@ -15,13 +15,14 @@ import {
1515
import { Credentials } from '../authentication';
1616
import * as cli from '../cli';
1717
import { logger } from '../logging';
18-
import { getActionBranch, getRemoteControllerRepo, getRemoteRepositoryLists, setRemoteControllerRepo } from '../config';
18+
import { getActionBranch, getRemoteControllerRepo, setRemoteControllerRepo } from '../config';
1919
import { ProgressCallback, UserCancellationException } from '../commandRunner';
2020
import { OctokitResponse } from '@octokit/types/dist-types';
2121
import { RemoteQuery } from './remote-query';
2222
import { RemoteQuerySubmissionResult } from './remote-query-submission-result';
2323
import { QueryMetadata } from '../pure/interface-types';
2424
import { getErrorMessage, REPO_REGEX } from '../pure/helpers-pure';
25+
import { getRepositories } from './repository-selection';
2526

2627
export interface QlPack {
2728
name: string;
@@ -30,9 +31,6 @@ export interface QlPack {
3031
defaultSuite?: Record<string, unknown>[];
3132
defaultSuiteFile?: string;
3233
}
33-
interface RepoListQuickPickItem extends QuickPickItem {
34-
repoList: string[];
35-
}
3634

3735
interface QueriesResponse {
3836
workflow_run_id: number
@@ -43,51 +41,6 @@ interface QueriesResponse {
4341
*/
4442
const QUERY_PACK_NAME = 'codeql-remote/query';
4543

46-
/**
47-
* Gets the repositories to run the query against.
48-
*/
49-
export async function getRepositories(): Promise<string[] | undefined> {
50-
const repoLists = getRemoteRepositoryLists();
51-
if (repoLists && Object.keys(repoLists).length) {
52-
const quickPickItems = Object.entries(repoLists).map<RepoListQuickPickItem>(([key, value]) => (
53-
{
54-
label: key, // the name of the repository list
55-
repoList: value, // the actual array of repositories
56-
}
57-
));
58-
const quickpick = await window.showQuickPick<RepoListQuickPickItem>(
59-
quickPickItems,
60-
{
61-
placeHolder: 'Select a repository list. You can define repository lists in the `codeQL.variantAnalysis.repositoryLists` setting.',
62-
ignoreFocusOut: true,
63-
});
64-
if (quickpick?.repoList.length) {
65-
void logger.log(`Selected repositories: ${quickpick.repoList.join(', ')}`);
66-
return quickpick.repoList;
67-
} else {
68-
void showAndLogErrorMessage('No repositories selected.');
69-
return;
70-
}
71-
} else {
72-
void logger.log('No repository lists defined. Displaying text input box.');
73-
const remoteRepo = await window.showInputBox({
74-
title: 'Enter a GitHub repository in the format <owner>/<repo> (e.g. github/codeql)',
75-
placeHolder: '<owner>/<repo>',
76-
prompt: 'Tip: you can save frequently used repositories in the `codeQL.variantAnalysis.repositoryLists` setting',
77-
ignoreFocusOut: true,
78-
});
79-
if (!remoteRepo) {
80-
void showAndLogErrorMessage('No repositories entered.');
81-
return;
82-
} else if (!REPO_REGEX.test(remoteRepo)) { // Check if user entered invalid input
83-
void showAndLogErrorMessage('Invalid repository format. Must be in the format <owner>/<repo> (e.g. github/codeql)');
84-
return;
85-
}
86-
void logger.log(`Entered repository: ${remoteRepo}`);
87-
return [remoteRepo];
88-
}
89-
}
90-
9144
/**
9245
* Two possibilities:
9346
* 1. There is no qlpack.yml in this directory. Assume this is a lone query and generate a synthetic qlpack for it.

extensions/ql-vscode/src/vscode-tests/no-workspace/run-remote-query.test.ts renamed to extensions/ql-vscode/src/vscode-tests/no-workspace/remote-queries/repository-selection.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as pq from 'proxyquire';
66

77
const proxyquire = pq.noPreserveCache();
88

9-
describe('run-remote-query', function() {
9+
describe('repository-selection', function() {
1010

1111
describe('getRepositories', () => {
1212
let sandbox: sinon.SinonSandbox;
@@ -21,7 +21,8 @@ describe('run-remote-query', function() {
2121
showInputBoxSpy = sandbox.stub(window, 'showInputBox');
2222
getRemoteRepositoryListsSpy = sandbox.stub();
2323
showAndLogErrorMessageSpy = sandbox.stub();
24-
mod = proxyquire('../../remote-queries/run-remote-query', {
24+
// extensions/ql-vscode/src/remote-queries/repository-selection.ts
25+
mod = proxyquire('../../../remote-queries/repository-selection', {
2526
'../config': {
2627
getRemoteRepositoryLists: getRemoteRepositoryListsSpy
2728
},

0 commit comments

Comments
 (0)