-
Notifications
You must be signed in to change notification settings - Fork 226
Add support for system defined repository lists #1271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0af8146
Move remote queries test files to be under remote-queries dir
charisk 68f9a60
Remove leftover comment
charisk f4376d2
Add support for system defined repository lists
charisk 1392fa0
Fix test
charisk a15f0c6
Invert logic to make it more readable
charisk bea0fcd
Merge branch 'main' into charisk/system-defined-repo-lists
charisk f67df09
Apply suggestions from code review
charisk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 102 additions & 40 deletions
142
extensions/ql-vscode/src/remote-queries/repository-selection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,116 @@ | ||
| import { QuickPickItem, window } from 'vscode'; | ||
| import { showAndLogErrorMessage } from '../helpers'; | ||
| import { getRemoteRepositoryLists } from '../config'; | ||
| import { logger } from '../logging'; | ||
| import { getRemoteRepositoryLists } from '../config'; | ||
| import { REPO_REGEX } from '../pure/helpers-pure'; | ||
|
|
||
| export interface RepositorySelection { | ||
| repositories?: string[]; | ||
| repositoryLists?: string[] | ||
| } | ||
|
|
||
| interface RepoListQuickPickItem extends QuickPickItem { | ||
| repoList: string[]; | ||
| repositories?: string[]; | ||
| repositoryList?: string; | ||
| useCustomRepository?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the repositories to run the query against. | ||
| * Gets the repositories or repository lists to run the query against. | ||
| * @returns The user selection. | ||
| */ | ||
| export async function getRepositories(): Promise<string[] | undefined> { | ||
| const repoLists = getRemoteRepositoryLists(); | ||
| if (repoLists && Object.keys(repoLists).length) { | ||
| const quickPickItems = Object.entries(repoLists).map<RepoListQuickPickItem>(([key, value]) => ( | ||
| { | ||
| label: key, // the name of the repository list | ||
| repoList: value, // the actual array of repositories | ||
| } | ||
| )); | ||
| const quickpick = await window.showQuickPick<RepoListQuickPickItem>( | ||
| quickPickItems, | ||
| { | ||
| placeHolder: 'Select a repository list. You can define repository lists in the `codeQL.variantAnalysis.repositoryLists` setting.', | ||
| ignoreFocusOut: true, | ||
| }); | ||
| if (quickpick?.repoList.length) { | ||
| void logger.log(`Selected repositories: ${quickpick.repoList.join(', ')}`); | ||
| return quickpick.repoList; | ||
| } else { | ||
| void showAndLogErrorMessage('No repositories selected.'); | ||
| return; | ||
| export async function getRepositorySelection(): Promise<RepositorySelection> { | ||
| const quickPickItems = [ | ||
| createCustomRepoQuickPickItem(), | ||
| ...createSystemDefinedRepoListsQuickPickItems(), | ||
| ...createUserDefinedRepoListsQuickPickItems(), | ||
| ]; | ||
|
|
||
| const options = { | ||
| placeHolder: 'Select a repository list. You can define repository lists in the `codeQL.variantAnalysis.repositoryLists` setting.', | ||
| ignoreFocusOut: true, | ||
| }; | ||
|
|
||
| const quickpick = await window.showQuickPick<RepoListQuickPickItem>( | ||
| quickPickItems, | ||
| options); | ||
|
|
||
| if (quickpick?.repositories?.length) { | ||
| void logger.log(`Selected repositories: ${quickpick.repositories.join(', ')}`); | ||
| return { repositories: quickpick.repositories }; | ||
| } else if (quickpick?.repositoryList) { | ||
| void logger.log(`Selected repository list: ${quickpick.repositoryList}`); | ||
| return { repositoryLists: [quickpick.repositoryList] }; | ||
| } else if (quickpick?.useCustomRepository) { | ||
| const customRepo = await getCustomRepo(); | ||
| if (!customRepo || !REPO_REGEX.test(customRepo)) { | ||
| void showAndLogErrorMessage('Invalid repository format. Please enter a valid repository in the format <owner>/<repo> (e.g. github/codeql)'); | ||
| return {}; | ||
| } | ||
| void logger.log(`Entered repository: ${customRepo}`); | ||
| return { repositories: [customRepo] }; | ||
| } else { | ||
| void logger.log('No repository lists defined. Displaying text input box.'); | ||
| const remoteRepo = await window.showInputBox({ | ||
| title: 'Enter a GitHub repository in the format <owner>/<repo> (e.g. github/codeql)', | ||
| placeHolder: '<owner>/<repo>', | ||
| prompt: 'Tip: you can save frequently used repositories in the `codeQL.variantAnalysis.repositoryLists` setting', | ||
| ignoreFocusOut: true, | ||
| }); | ||
| if (!remoteRepo) { | ||
| void showAndLogErrorMessage('No repositories entered.'); | ||
| return; | ||
| } else if (!REPO_REGEX.test(remoteRepo)) { // Check if user entered invalid input | ||
| void showAndLogErrorMessage('Invalid repository format. Must be in the format <owner>/<repo> (e.g. github/codeql)'); | ||
| return; | ||
| } | ||
| void logger.log(`Entered repository: ${remoteRepo}`); | ||
| return [remoteRepo]; | ||
| void showAndLogErrorMessage('No repositories selected.'); | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the selection is valid or not. | ||
| * @param repoSelection The selection to check. | ||
| * @returns A boolean flag indicating if the selection is valid or not. | ||
| */ | ||
| export function isValidSelection(repoSelection: RepositorySelection): boolean { | ||
| if (repoSelection.repositories === undefined && repoSelection.repositoryLists === undefined) { | ||
| return false; | ||
| } | ||
| if (repoSelection.repositories !== undefined && repoSelection.repositories.length === 0) { | ||
| return false; | ||
| } | ||
| if (repoSelection.repositoryLists?.length === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function createSystemDefinedRepoListsQuickPickItems(): RepoListQuickPickItem[] { | ||
| const topNs = [10, 100, 1000]; | ||
|
|
||
| return topNs.map(n => ({ | ||
| label: '$(star) Top ' + n, | ||
|
aeisenberg marked this conversation as resolved.
|
||
| repositoryList: `top_${n}`, | ||
| alwaysShow: true | ||
| } as RepoListQuickPickItem)); | ||
| } | ||
|
|
||
| function createUserDefinedRepoListsQuickPickItems(): RepoListQuickPickItem[] { | ||
| const repoLists = getRemoteRepositoryLists(); | ||
| if (!repoLists) { | ||
| return []; | ||
| } | ||
|
|
||
| return Object.entries(repoLists).map<RepoListQuickPickItem>(([label, repositories]) => ( | ||
| { | ||
| label, // the name of the repository list | ||
| repositories // the actual array of repositories | ||
| } | ||
| )); | ||
| } | ||
|
|
||
| function createCustomRepoQuickPickItem(): RepoListQuickPickItem { | ||
| return { | ||
| label: '$(edit) Enter a GitHub repository', | ||
| useCustomRepository: true, | ||
| alwaysShow: true, | ||
| }; | ||
| } | ||
|
|
||
| async function getCustomRepo(): Promise<string | undefined> { | ||
| return await window.showInputBox({ | ||
| title: 'Enter a GitHub repository in the format <owner>/<repo> (e.g. github/codeql)', | ||
| placeHolder: '<owner>/<repo>', | ||
| prompt: 'Tip: you can save frequently used repositories in the `codeQL.variantAnalysis.repositoryLists` setting', | ||
| ignoreFocusOut: true, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.