Skip to content

Commit 1a21f94

Browse files
committed
Merge branch 'main' into apps
2 parents 856f17d + 43f622a commit 1a21f94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+649
-665
lines changed

.github/codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ coverage:
1919
ignore:
2020
# This is generated code.
2121
- coderd/database/models.go
22-
- coderd/database/query.sql.go
22+
- coderd/database/queries.sql.go
2323
- coderd/database/databasefake
2424
# These are generated or don't require tests.
2525
- cmd

cli/autostart.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ func autostartShow() *cobra.Command {
4141
if err != nil {
4242
return err
4343
}
44-
organization, err := currentOrganization(cmd, client)
45-
if err != nil {
46-
return err
47-
}
4844

49-
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
45+
workspace, err := namedWorkspace(cmd, client, args[0])
5046
if err != nil {
5147
return err
5248
}
@@ -93,18 +89,14 @@ func autostartEnable() *cobra.Command {
9389
if err != nil {
9490
return err
9591
}
96-
organization, err := currentOrganization(cmd, client)
97-
if err != nil {
98-
return err
99-
}
10092

10193
spec := fmt.Sprintf("CRON_TZ=%s %s %s * * %s", autostartTimezone, autostartMinute, autostartHour, autostartDayOfWeek)
10294
validSchedule, err := schedule.Weekly(spec)
10395
if err != nil {
10496
return err
10597
}
10698

107-
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
99+
workspace, err := namedWorkspace(cmd, client, args[0])
108100
if err != nil {
109101
return err
110102
}
@@ -142,12 +134,8 @@ func autostartDisable() *cobra.Command {
142134
if err != nil {
143135
return err
144136
}
145-
organization, err := currentOrganization(cmd, client)
146-
if err != nil {
147-
return err
148-
}
149137

150-
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
138+
workspace, err := namedWorkspace(cmd, client, args[0])
151139
if err != nil {
152140
return err
153141
}

cli/bump.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ func bump() *cobra.Command {
4343
if err != nil {
4444
return xerrors.Errorf("create client: %w", err)
4545
}
46-
organization, err := currentOrganization(cmd, client)
47-
if err != nil {
48-
return xerrors.Errorf("get current org: %w", err)
49-
}
50-
51-
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
46+
workspace, err := namedWorkspace(cmd, client, args[0])
5247
if err != nil {
5348
return xerrors.Errorf("get workspace: %w", err)
5449
}

cli/configssh.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ func configSSH() *cobra.Command {
4848
if err != nil {
4949
return err
5050
}
51-
organization, err := currentOrganization(cmd, client)
52-
if err != nil {
53-
return err
54-
}
5551
if strings.HasPrefix(sshConfigFile, "~/") {
5652
dirname, _ := os.UserHomeDir()
5753
sshConfigFile = filepath.Join(dirname, sshConfigFile[2:])
@@ -65,7 +61,9 @@ func configSSH() *cobra.Command {
6561
sshConfigContent = sshConfigContent[:startIndex-1] + sshConfigContent[endIndex+len(sshEndToken):]
6662
}
6763

68-
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
64+
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
65+
Owner: codersdk.Me,
66+
})
6967
if err != nil {
7068
return err
7169
}

cli/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func create() *cobra.Command {
4949
workspaceName, err = cliui.Prompt(cmd, cliui.PromptOptions{
5050
Text: "Specify a name for your workspace:",
5151
Validate: func(workspaceName string) error {
52-
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
52+
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
5353
if err == nil {
5454
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
5555
}
@@ -75,7 +75,7 @@ func create() *cobra.Command {
7575
return xerrors.Errorf("TTL must be at least 1 minute")
7676
}
7777

78-
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
78+
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
7979
if err == nil {
8080
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
8181
}

cli/delete.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ func delete() *cobra.Command {
3030
if err != nil {
3131
return err
3232
}
33-
organization, err := currentOrganization(cmd, client)
34-
if err != nil {
35-
return err
36-
}
37-
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
33+
workspace, err := namedWorkspace(cmd, client, args[0])
3834
if err != nil {
3935
return err
4036
}

cli/delete_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package cli_test
22

33
import (
4+
"context"
45
"io"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
810

911
"github.com/coder/coder/cli/clitest"
1012
"github.com/coder/coder/coderd/coderdtest"
13+
"github.com/coder/coder/codersdk"
1114
"github.com/coder/coder/pty/ptytest"
1215
)
1316

@@ -38,4 +41,55 @@ func TestDelete(t *testing.T) {
3841
pty.ExpectMatch("Cleaning Up")
3942
<-doneChan
4043
})
44+
45+
t.Run("DifferentUser", func(t *testing.T) {
46+
t.Parallel()
47+
adminClient := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
48+
adminUser := coderdtest.CreateFirstUser(t, adminClient)
49+
orgID := adminUser.OrganizationID
50+
client := coderdtest.CreateAnotherUser(t, adminClient, orgID)
51+
user, err := client.User(context.Background(), codersdk.Me)
52+
require.NoError(t, err)
53+
54+
version := coderdtest.CreateTemplateVersion(t, adminClient, orgID, nil)
55+
coderdtest.AwaitTemplateVersionJob(t, adminClient, version.ID)
56+
template := coderdtest.CreateTemplate(t, adminClient, orgID, version.ID)
57+
workspace := coderdtest.CreateWorkspace(t, client, orgID, template.ID)
58+
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
59+
60+
cmd, root := clitest.New(t, "delete", user.Username+"/"+workspace.Name, "-y")
61+
clitest.SetupConfig(t, adminClient, root)
62+
doneChan := make(chan struct{})
63+
pty := ptytest.New(t)
64+
cmd.SetIn(pty.Input())
65+
cmd.SetOut(pty.Output())
66+
go func() {
67+
defer close(doneChan)
68+
err := cmd.Execute()
69+
// When running with the race detector on, we sometimes get an EOF.
70+
if err != nil {
71+
assert.ErrorIs(t, err, io.EOF)
72+
}
73+
}()
74+
75+
pty.ExpectMatch("Cleaning Up")
76+
<-doneChan
77+
78+
workspace, err = client.Workspace(context.Background(), workspace.ID)
79+
require.ErrorContains(t, err, "was deleted")
80+
})
81+
82+
t.Run("InvalidWorkspaceIdentifier", func(t *testing.T) {
83+
t.Parallel()
84+
client := coderdtest.New(t, nil)
85+
cmd, root := clitest.New(t, "delete", "a/b/c", "-y")
86+
clitest.SetupConfig(t, client, root)
87+
doneChan := make(chan struct{})
88+
go func() {
89+
defer close(doneChan)
90+
err := cmd.Execute()
91+
assert.ErrorContains(t, err, "invalid workspace name: \"a/b/c\"")
92+
}()
93+
<-doneChan
94+
})
4195
}

cli/portforward.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ func portForward() *cobra.Command {
7070
if err != nil {
7171
return err
7272
}
73-
organization, err := currentOrganization(cmd, client)
74-
if err != nil {
75-
return err
76-
}
7773

78-
workspace, agent, err := getWorkspaceAndAgent(cmd, client, organization.ID, codersdk.Me, args[0], false)
74+
workspace, agent, err := getWorkspaceAndAgent(cmd, client, codersdk.Me, args[0], false)
7975
if err != nil {
8076
return err
8177
}

cli/root.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ func currentOrganization(cmd *cobra.Command, client *codersdk.Client) (codersdk.
176176
return orgs[0], nil
177177
}
178178

179+
// namedWorkspace fetches and returns a workspace by an identifier, which may be either
180+
// a bare name (for a workspace owned by the current user) or a "user/workspace" combination,
181+
// where user is either a username or UUID.
182+
func namedWorkspace(cmd *cobra.Command, client *codersdk.Client, identifier string) (codersdk.Workspace, error) {
183+
parts := strings.Split(identifier, "/")
184+
185+
var owner, name string
186+
switch len(parts) {
187+
case 1:
188+
owner = codersdk.Me
189+
name = parts[0]
190+
case 2:
191+
owner = parts[0]
192+
name = parts[1]
193+
default:
194+
return codersdk.Workspace{}, xerrors.Errorf("invalid workspace name: %q", identifier)
195+
}
196+
197+
return client.WorkspaceByOwnerAndName(cmd.Context(), owner, name)
198+
}
199+
179200
// createConfig consumes the global configuration flag to produce a config root.
180201
func createConfig(cmd *cobra.Command) config.Root {
181202
globalRoot, err := cmd.Flags().GetString(varGlobalConfig)

cli/server.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,9 @@ func server() *cobra.Command {
412412
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit"))
413413

414414
if dev {
415-
organizations, err := client.OrganizationsByUser(cmd.Context(), codersdk.Me)
416-
if err != nil {
417-
return xerrors.Errorf("get organizations: %w", err)
418-
}
419-
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organizations[0].ID, codersdk.Me)
415+
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
416+
Owner: codersdk.Me,
417+
})
420418
if err != nil {
421419
return xerrors.Errorf("get workspaces: %w", err)
422420
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy