Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29c8cf2

Browse files
authored
fix: handle CLI default organization when none exists in <v2.9.0 coderd (#12594)
* chore: 'coder org set' help message was incorrect* fix: handler coder cli against older versions of Coder
1 parentf78b5c1 commit29c8cf2

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

‎cli/organization_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package cli_test
22

33
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"net/url"
48
"testing"
9+
"time"
510

11+
"github.com/google/uuid"
612
"github.com/stretchr/testify/require"
713

814
"github.com/coder/coder/v2/cli/clitest"
@@ -16,6 +22,38 @@ import (
1622
funcTestCurrentOrganization(t*testing.T) {
1723
t.Parallel()
1824

25+
// This test emulates 2 cases:
26+
// 1. The user is not a part of the default organization, but only belongs to one.
27+
// 2. The user is connecting to an older Coder instance.
28+
t.Run("no-default",func(t*testing.T) {
29+
t.Parallel()
30+
31+
orgID:=uuid.New()
32+
srv:=httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
33+
json.NewEncoder(w).Encode([]codersdk.Organization{
34+
{
35+
ID:orgID,
36+
Name:"not-default",
37+
CreatedAt:time.Now(),
38+
UpdatedAt:time.Now(),
39+
IsDefault:false,
40+
},
41+
})
42+
}))
43+
defersrv.Close()
44+
45+
client:=codersdk.New(must(url.Parse(srv.URL)))
46+
inv,root:=clitest.New(t,"organizations","show","current")
47+
clitest.SetupConfig(t,client,root)
48+
pty:=ptytest.New(t).Attach(inv)
49+
errC:=make(chanerror)
50+
gofunc() {
51+
errC<-inv.Run()
52+
}()
53+
require.NoError(t,<-errC)
54+
pty.ExpectMatch(orgID.String())
55+
})
56+
1957
t.Run("OnlyID",func(t*testing.T) {
2058
t.Parallel()
2159
ownerClient:=coderdtest.New(t,nil)
@@ -108,3 +146,10 @@ func TestOrganizationSwitch(t *testing.T) {
108146
pty.ExpectMatch(exp.ID.String())
109147
})
110148
}
149+
150+
funcmust[Vany](vV,errerror)V {
151+
iferr!=nil {
152+
panic(err)
153+
}
154+
returnv
155+
}

‎cli/root.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,14 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C
749749
returnorg.IsDefault
750750
})
751751
ifindex<0 {
752-
return codersdk.Organization{},xerrors.Errorf("unable to determine current organization. Use 'coder set <org>' to select an organization to use")
752+
iflen(orgs)==1 {
753+
// If there is no "isDefault", but only 1 org is present. We can just
754+
// assume the single organization is correct. This is mainly a helper
755+
// for cli hitting an old instance, or a user that belongs to a single
756+
// org that is not the default.
757+
returnorgs[0],nil
758+
}
759+
return codersdk.Organization{},xerrors.Errorf("unable to determine current organization. Use 'coder org set <org>' to select an organization to use")
753760
}
754761

755762
returnorgs[index],nil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp