-
Notifications
You must be signed in to change notification settings - Fork 180
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
Implement workspace filtering by project #1455
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { apiClient } from '../../terraformCloud'; | ||
import { Project } from '../../terraformCloud/project'; | ||
|
||
export class ResetProjectItem implements vscode.QuickPickItem { | ||
get label() { | ||
return '$(clear-all) Clear project filter. Show all workspaces'; | ||
} | ||
get description() { | ||
return ''; | ||
} | ||
get alwaysShow() { | ||
return true; | ||
} | ||
} | ||
|
||
class ProjectItem implements vscode.QuickPickItem { | ||
constructor(protected project: Project) {} | ||
get label() { | ||
return this.project.attributes.name; | ||
} | ||
get description() { | ||
return this.project.id; | ||
} | ||
} | ||
|
||
async function createProjectItems(organization: string, search?: string): Promise<ProjectItem[]> { | ||
const projects = await apiClient.listProjects({ | ||
params: { | ||
organization_name: organization, | ||
}, | ||
// Include query parameter only if search argument is passed | ||
...(search && { | ||
queries: { | ||
q: search, | ||
}, | ||
}), | ||
}); | ||
|
||
return projects.data.map((project) => new ProjectItem(project)); | ||
} | ||
|
||
export class ProjectQuickPick { | ||
private quickPick: vscode.QuickPick<vscode.QuickPickItem>; | ||
private fetchTimerKey: NodeJS.Timeout | undefined; | ||
|
||
constructor(private organizationName: string) { | ||
this.quickPick = vscode.window.createQuickPick(); | ||
this.quickPick.title = 'Filter Workspaces'; | ||
this.quickPick.placeholder = 'Select a project (type to search)'; | ||
this.quickPick.onDidChangeValue(this.onDidChangeValue, this); | ||
} | ||
|
||
private onDidChangeValue() { | ||
clearTimeout(this.fetchTimerKey); | ||
// Only starts fetching projects after a user stopped typing for 300ms | ||
this.fetchTimerKey = setTimeout(() => this.fetchProjects.apply(this), 300); | ||
} | ||
|
||
private async fetchProjects() { | ||
// TODO?: To further improve performance, we could consider throttling this function | ||
const resetProjectItem = new ResetProjectItem(); | ||
const picks: vscode.QuickPickItem[] = [resetProjectItem, { label: '', kind: vscode.QuickPickItemKind.Separator }]; | ||
try { | ||
this.quickPick.busy = true; | ||
this.quickPick.show(); | ||
|
||
picks.push(...(await createProjectItems(this.organizationName, this.quickPick.value))); | ||
} catch (error) { | ||
let message = 'Failed to fetch projects'; | ||
if (error instanceof Error) { | ||
message = error.message; | ||
} else if (typeof error === 'string') { | ||
message = error; | ||
} | ||
|
||
picks.push({ label: `$(error) Error: ${message}`, alwaysShow: true }); | ||
console.error(error); | ||
} finally { | ||
this.quickPick.items = picks; | ||
this.quickPick.busy = false; | ||
} | ||
} | ||
|
||
async pick() { | ||
await this.fetchProjects(); | ||
|
||
const project = await new Promise<vscode.QuickPickItem | undefined>((c) => { | ||
this.quickPick.onDidAccept(() => c(this.quickPick.selectedItems[0])); | ||
this.quickPick.onDidHide(() => c(undefined)); | ||
this.quickPick.show(); | ||
}); | ||
this.quickPick.hide(); | ||
|
||
return project; | ||
} | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this to get the project name to display in the view. If the user selected a project, then we know the name both to filter and to add to the workspace, so wouldn't need to call out twice to get projects.
So, if user specifies project to filter, then use that project name to build the WorkspaceTreeItems. If not, call out and get a list of projects.
We can make this a followup ticket, so this is not stopping merging now.