Skip to content
Draft
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
43 changes: 42 additions & 1 deletion apps/cli-go/internal/orgs/create/create.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package create

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/go-errors/errors"
"github.com/supabase/cli/internal/orgs/list"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
)

type createOrganizationRequest struct {
Name string `json:"name"`
HeardFrom string `json:"heard_from,omitempty"`
Building string `json:"building,omitempty"`
}

var newConsole = utils.NewConsole

func Run(ctx context.Context, name string) error {
resp, err := utils.GetSupabase().V1CreateAnOrganizationWithResponse(ctx, api.V1CreateAnOrganizationJSONRequestBody{Name: name})
body, err := buildCreateOrganizationRequest(ctx, name)
if err != nil {
return err
}
payload, err := json.Marshal(body)
if err != nil {
return errors.Errorf("failed to encode organization request: %w", err)
}
resp, err := utils.GetSupabase().V1CreateAnOrganizationWithBodyWithResponse(ctx, "application/json", bytes.NewReader(payload))
if err != nil {
return errors.Errorf("failed to create organization: %w", err)
} else if resp.JSON201 == nil {
Expand All @@ -26,3 +45,25 @@ func Run(ctx context.Context, name string) error {
}
return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, *resp.JSON201)
}

func buildCreateOrganizationRequest(ctx context.Context, name string) (createOrganizationRequest, error) {
body := createOrganizationRequest{Name: name}
console := newConsole()
if utils.OutputFormat.Value != utils.OutputPretty || !console.IsTTY {
return body, nil
}

heardFrom, err := console.PromptText(ctx, "Where did you hear about us? ")
if err != nil {
return body, err
}
body.HeardFrom = strings.TrimSpace(heardFrom)

building, err := console.PromptText(ctx, "What are you building? ")
if err != nil {
return body, err
}
body.Building = strings.TrimSpace(building)

return body, nil
}
110 changes: 110 additions & 0 deletions apps/cli-go/internal/orgs/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/testing/fstest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
)

func TestOrganizationCreateCommand(t *testing.T) {
orgName := "Test Organization"

t.Cleanup(func() {
newConsole = utils.NewConsole
utils.OutputFormat.Value = utils.OutputPretty
})

t.Run("create an organization", func(t *testing.T) {
// Setup valid access token
token := apitest.RandomAccessToken(t)
Expand All @@ -24,6 +30,106 @@ func TestOrganizationCreateCommand(t *testing.T) {
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/organizations").
MatchType("json").
JSON(createOrganizationRequest{Name: orgName}).
Reply(http.StatusCreated).
JSON(api.OrganizationResponseV1{
Id: "combined-fuchsia-lion",
Name: orgName,
})
// Run test
assert.NoError(t, Run(context.Background(), orgName))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("sends optional survey fields from interactive prompts", func(t *testing.T) {
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
t.Cleanup(fstest.MockStdin(t, "GitHub\nAI coding assistant\n"))
newConsole = func() *utils.Console {
console := utils.NewConsole()
console.IsTTY = true
return console
}
t.Cleanup(func() {
newConsole = utils.NewConsole
})
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/organizations").
MatchType("json").
JSON(createOrganizationRequest{
Name: orgName,
HeardFrom: "GitHub",
Building: "AI coding assistant",
}).
Reply(http.StatusCreated).
JSON(api.OrganizationResponseV1{
Id: "combined-fuchsia-lion",
Name: orgName,
})
// Run test
assert.NoError(t, Run(context.Background(), orgName))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("omits blank survey prompt answers", func(t *testing.T) {
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
t.Cleanup(fstest.MockStdin(t, "\n\n"))
newConsole = func() *utils.Console {
console := utils.NewConsole()
console.IsTTY = true
return console
}
t.Cleanup(func() {
newConsole = utils.NewConsole
})
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/organizations").
MatchType("json").
JSON(createOrganizationRequest{Name: orgName}).
Reply(http.StatusCreated).
JSON(api.OrganizationResponseV1{
Id: "combined-fuchsia-lion",
Name: orgName,
})
// Run test
assert.NoError(t, Run(context.Background(), orgName))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("skips survey prompts for structured output", func(t *testing.T) {
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
utils.OutputFormat.Value = utils.OutputJson
t.Cleanup(func() {
utils.OutputFormat.Value = utils.OutputPretty
})
t.Cleanup(fstest.MockStdin(t, "GitHub\nAI coding assistant\n"))
newConsole = func() *utils.Console {
console := utils.NewConsole()
console.IsTTY = true
return console
}
t.Cleanup(func() {
newConsole = utils.NewConsole
})
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/organizations").
MatchType("json").
JSON(createOrganizationRequest{Name: orgName}).
Reply(http.StatusCreated).
JSON(api.OrganizationResponseV1{
Id: "combined-fuchsia-lion",
Expand All @@ -43,6 +149,8 @@ func TestOrganizationCreateCommand(t *testing.T) {
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/organizations").
MatchType("json").
JSON(createOrganizationRequest{Name: orgName}).
ReplyError(errors.New("network error"))
// Run test
assert.Error(t, Run(context.Background(), orgName))
Expand All @@ -58,6 +166,8 @@ func TestOrganizationCreateCommand(t *testing.T) {
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/organizations").
MatchType("json").
JSON(createOrganizationRequest{Name: orgName}).
Reply(http.StatusServiceUnavailable).
JSON(map[string]string{"message": "unavailable"})
// Run test
Expand Down
Loading