Skip to content

feat: open an agent-defined directory by default #57

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 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios"
import { getUser, getWorkspaces, updateWorkspaceVersion } from "coder/site/src/api/api"
import { Workspace } from "coder/site/src/api/typesGenerated"
import { Workspace, WorkspaceAgent } from "coder/site/src/api/typesGenerated"
import * as vscode from "vscode"
import { Remote } from "./remote"
import { Storage } from "./storage"
Expand Down Expand Up @@ -104,6 +104,7 @@ export class Commands {
public async open(...args: string[]): Promise<void> {
let workspaceOwner: string
let workspaceName: string
let folderPath: string | undefined

if (args.length === 0) {
const quickPick = vscode.window.createQuickPick()
Expand Down Expand Up @@ -156,9 +157,20 @@ export class Commands {
}
workspaceOwner = workspace.owner_name
workspaceName = workspace.name

// TODO: multiple agent support
const agents = workspace.latest_build.resources.reduce((acc, resource) => {
return acc.concat(resource.agents || [])
}, [] as WorkspaceAgent[])

if (agents.length === 1) {
folderPath = agents[0].expanded_directory
}
} else {
workspaceOwner = args[0]
workspaceName = args[1]
// workspaceAgent is reserved for args[2], but multiple agents aren't supported yet.
folderPath = args[3]
}

// A workspace can have multiple agents, but that's handled
Expand All @@ -171,39 +183,46 @@ export class Commands {
newWindow = false
}

const output: {
workspaces: { folderUri: vscode.Uri; remoteAuthority: string }[]
} = await vscode.commands.executeCommand("_workbench.getRecentlyOpened")
const opened = output.workspaces.filter(
// Filter out `/` since that's added below.
(opened) => opened.folderUri?.authority === remoteAuthority,
)
if (opened.length > 0) {
let selected: (typeof opened)[0]
// If a folder isn't specified, we can try to open a recently opened folder.
if (!folderPath) {
const output: {
workspaces: { folderUri: vscode.Uri; remoteAuthority: string }[]
} = await vscode.commands.executeCommand("_workbench.getRecentlyOpened")
const opened = output.workspaces.filter(
// Filter out `/` since that's added below.
(opened) => opened.folderUri?.authority === remoteAuthority,
)
if (opened.length > 0) {
let selected: (typeof opened)[0]

if (opened.length > 1) {
const items: vscode.QuickPickItem[] = opened.map((folder): vscode.QuickPickItem => {
return {
label: folder.folderUri.path,
if (opened.length > 1) {
const items: vscode.QuickPickItem[] = opened.map((folder): vscode.QuickPickItem => {
return {
label: folder.folderUri.path,
}
})
const item = await vscode.window.showQuickPick(items, {
title: "Select a recently opened folder",
})
if (!item) {
return
}
})
const item = await vscode.window.showQuickPick(items, {
title: "Select a recently opened folder",
})
if (!item) {
return
selected = opened[items.indexOf(item)]
} else {
selected = opened[0]
}
selected = opened[items.indexOf(item)]
} else {
selected = opened[0]

folderPath = selected.folderUri.path
}
}

if (folderPath) {
await vscode.commands.executeCommand(
"vscode.openFolder",
vscode.Uri.from({
scheme: "vscode-remote",
authority: remoteAuthority,
path: selected.folderUri.path,
path: folderPath,
}),
// Open this in a new window!
newWindow,
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
const owner = params.get("owner")
const workspace = params.get("workspace")
const agent = params.get("agent")
const folder = params.get("folder")
if (!owner) {
throw new Error("owner must be specified as a query parameter")
}
Expand All @@ -45,7 +46,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
if (token) {
await storage.setSessionToken(token)
}
vscode.commands.executeCommand("coder.open", owner, workspace, agent)
vscode.commands.executeCommand("coder.open", owner, workspace, agent, folder)
}
},
})
Expand Down
4 changes: 3 additions & 1 deletion src/remote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios"
import {
getBuildInfo,
getTemplate,
getWorkspace,
getWorkspaceBuildLogs,
getWorkspaceByOwnerAndName,
Expand Down Expand Up @@ -134,9 +135,10 @@ export class Remote {
buildComplete = r
}),
)
const template = await getTemplate(this.storage.workspace.template_id)
this.storage.workspace = {
...this.storage.workspace,
latest_build: await startWorkspace(this.storage.workspace.id),
latest_build: await startWorkspace(this.storage.workspace.id, template.active_version_id),
}
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ [email protected]:

"coder@https://github.com/coder/coder":
version "0.0.0"
resolved "https://github.com/coder/coder#8a5760a2fea074fda65d199620aaa9ed447e7c8d"
resolved "https://github.com/coder/coder#7a1731b6205d9c68f6308ee362ff2d62124b6950"

collapse-white-space@^1.0.2:
version "1.0.6"
Expand Down