|
1 | 1 | import { QuickPickItem, window } from 'vscode'; |
2 | 2 | import { showAndLogErrorMessage } from '../helpers'; |
3 | | -import { getRemoteRepositoryLists } from '../config'; |
4 | 3 | import { logger } from '../logging'; |
| 4 | +import { getRemoteRepositoryLists } from '../config'; |
5 | 5 | import { REPO_REGEX } from '../pure/helpers-pure'; |
6 | 6 |
|
| 7 | +export interface RepositorySelection { |
| 8 | + repositories?: string[]; |
| 9 | + repositoryLists?: string[] |
| 10 | +} |
| 11 | + |
7 | 12 | interface RepoListQuickPickItem extends QuickPickItem { |
8 | | - repoList: string[]; |
| 13 | + repositories?: string[]; |
| 14 | + repositoryList?: string; |
| 15 | + useCustomRepository?: boolean; |
9 | 16 | } |
10 | 17 |
|
11 | 18 | /** |
12 | | - * Gets the repositories to run the query against. |
| 19 | + * Gets the repositories or repository lists to run the query against. |
| 20 | + * @returns The user selection. |
13 | 21 | */ |
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; |
| 22 | +export async function getRepositorySelection(): Promise<RepositorySelection> { |
| 23 | + const quickPickItems = [ |
| 24 | + createCustomRepoQuickPickItem(), |
| 25 | + ...createSystemDefinedRepoListsQuickPickItems(), |
| 26 | + ...createUserDefinedRepoListsQuickPickItems(), |
| 27 | + ]; |
| 28 | + |
| 29 | + const options = { |
| 30 | + placeHolder: 'Select a repository list. You can define repository lists in the `codeQL.variantAnalysis.repositoryLists` setting.', |
| 31 | + ignoreFocusOut: true, |
| 32 | + }; |
| 33 | + |
| 34 | + const quickpick = await window.showQuickPick<RepoListQuickPickItem>( |
| 35 | + quickPickItems, |
| 36 | + options); |
| 37 | + |
| 38 | + if (quickpick?.repositories?.length) { |
| 39 | + void logger.log(`Selected repositories: ${quickpick.repositories.join(', ')}`); |
| 40 | + return { repositories: quickpick.repositories }; |
| 41 | + } else if (quickpick?.repositoryList) { |
| 42 | + void logger.log(`Selected repository list: ${quickpick.repositoryList}`); |
| 43 | + return { repositoryLists: [quickpick.repositoryList] }; |
| 44 | + } else if (quickpick?.useCustomRepository) { |
| 45 | + const customRepo = await getCustomRepo(); |
| 46 | + if (!customRepo || !REPO_REGEX.test(customRepo)) { |
| 47 | + void showAndLogErrorMessage('Invalid repository format. Please enter a valid repository in the format <owner>/<repo> (e.g. github/codeql)'); |
| 48 | + return {}; |
35 | 49 | } |
| 50 | + void logger.log(`Entered repository: ${customRepo}`); |
| 51 | + return { repositories: [customRepo] }; |
36 | 52 | } 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 | + void showAndLogErrorMessage('No repositories selected.'); |
| 54 | + return {}; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Checks if the selection is valid or not. |
| 60 | + * @param repoSelection The selection to check. |
| 61 | + * @returns A boolean flag indicating if the selection is valid or not. |
| 62 | + */ |
| 63 | +export function isValidSelection(repoSelection: RepositorySelection): boolean { |
| 64 | + if (repoSelection.repositories === undefined && repoSelection.repositoryLists === undefined) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + if (repoSelection.repositories !== undefined && repoSelection.repositories.length === 0) { |
| 68 | + return false; |
53 | 69 | } |
| 70 | + if (repoSelection.repositoryLists?.length === 0) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +function createSystemDefinedRepoListsQuickPickItems(): RepoListQuickPickItem[] { |
| 78 | + const topNs = [10, 100, 1000]; |
| 79 | + |
| 80 | + return topNs.map(n => ({ |
| 81 | + label: '$(star) Top ' + n, |
| 82 | + repositoryList: `top_${n}`, |
| 83 | + alwaysShow: true |
| 84 | + } as RepoListQuickPickItem)); |
| 85 | +} |
| 86 | + |
| 87 | +function createUserDefinedRepoListsQuickPickItems(): RepoListQuickPickItem[] { |
| 88 | + const repoLists = getRemoteRepositoryLists(); |
| 89 | + if (!repoLists) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + return Object.entries(repoLists).map<RepoListQuickPickItem>(([label, repositories]) => ( |
| 94 | + { |
| 95 | + label, // the name of the repository list |
| 96 | + repositories // the actual array of repositories |
| 97 | + } |
| 98 | + )); |
| 99 | +} |
| 100 | + |
| 101 | +function createCustomRepoQuickPickItem(): RepoListQuickPickItem { |
| 102 | + return { |
| 103 | + label: '$(edit) Enter a GitHub repository', |
| 104 | + useCustomRepository: true, |
| 105 | + alwaysShow: true, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +async function getCustomRepo(): Promise<string | undefined> { |
| 110 | + return await window.showInputBox({ |
| 111 | + title: 'Enter a GitHub repository in the format <owner>/<repo> (e.g. github/codeql)', |
| 112 | + placeHolder: '<owner>/<repo>', |
| 113 | + prompt: 'Tip: you can save frequently used repositories in the `codeQL.variantAnalysis.repositoryLists` setting', |
| 114 | + ignoreFocusOut: true, |
| 115 | + }); |
54 | 116 | } |
0 commit comments