Skip to content

Commit 3e08d1d

Browse files
authored
feat: use branch ref in all commands (#4140)
fix: use branch ref in all commands
1 parent 19ed473 commit 3e08d1d

File tree

11 files changed

+124
-123
lines changed

11 files changed

+124
-123
lines changed

api/overlay.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ actions:
3535
- target: $.components.schemas.*.properties.private_jwk.discriminator
3636
description: Replaces discriminated union with concrete type
3737
remove: true
38+
- target: $.paths.*.*.parameters[?(@.name=='branch_id_or_ref')]
39+
update:
40+
schema:
41+
type: string

cmd/branches.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func promptBranchId(ctx context.Context, fsys afero.Fs) error {
249249
for i, branch := range branches {
250250
items[i] = utils.PromptItem{
251251
Summary: branch.Name,
252-
Details: branch.Id.String(),
252+
Details: branch.ProjectRef,
253253
}
254254
}
255255
title := "Select a branch:"

internal/branches/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ func Run(ctx context.Context, body api.CreateBranchBody, fsys afero.Fs) error {
3434
return errors.New("Unexpected error creating preview branch: " + string(resp.Body))
3535
}
3636

37-
fmt.Println("Created preview branch:", resp.JSON201.Id)
37+
fmt.Println("Created preview branch:", resp.JSON201.ProjectRef)
3838
return nil
3939
}

internal/branches/delete/delete.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import (
66
"net/http"
77

88
"github.com/go-errors/errors"
9-
"github.com/supabase/cli/internal/branches/get"
9+
"github.com/supabase/cli/internal/branches/pause"
1010
"github.com/supabase/cli/internal/utils"
1111
)
1212

1313
func Run(ctx context.Context, branchId string) error {
14-
parsed, err := get.GetBranchID(ctx, branchId)
14+
projectRef, err := pause.GetBranchProjectRef(ctx, branchId)
1515
if err != nil {
1616
return err
1717
}
18-
resp, err := utils.GetSupabase().V1DeleteABranchWithResponse(ctx, parsed)
18+
resp, err := utils.GetSupabase().V1DeleteABranchWithResponse(ctx, projectRef)
1919
if err != nil {
2020
return errors.Errorf("failed to delete preview branch: %w", err)
2121
}
2222
if resp.StatusCode() != http.StatusOK {
2323
return errors.New("Unexpected error deleting preview branch: " + string(resp.Body))
2424
}
25-
fmt.Println("Deleted preview branch:", branchId)
25+
fmt.Println("Deleted preview branch:", projectRef)
2626
return nil
2727
}

internal/branches/get/get.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ func Run(ctx context.Context, branchId string, fsys afero.Fs) error {
5454

5555
func getBranchDetail(ctx context.Context, branchId string) (api.BranchDetailResponse, error) {
5656
var result api.BranchDetailResponse
57-
parsed, err := GetBranchID(ctx, branchId)
58-
if err != nil {
59-
return result, err
57+
if err := uuid.Validate(branchId); err != nil && !utils.ProjectRefPattern.Match([]byte(branchId)) {
58+
resp, err := utils.GetSupabase().V1GetABranchWithResponse(ctx, flags.ProjectRef, branchId)
59+
if err != nil {
60+
return result, errors.Errorf("failed to find branch: %w", err)
61+
} else if resp.JSON200 == nil {
62+
return result, errors.Errorf("unexpected find branch status %d: %s", resp.StatusCode(), string(resp.Body))
63+
}
64+
branchId = resp.JSON200.ProjectRef
6065
}
61-
resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, parsed)
66+
resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId)
6267
if err != nil {
6368
return result, errors.Errorf("failed to get branch: %w", err)
6469
} else if resp.JSON200 == nil {
@@ -77,20 +82,6 @@ func getBranchDetail(ctx context.Context, branchId string) (api.BranchDetailResp
7782
return *resp.JSON200, nil
7883
}
7984

80-
func GetBranchID(ctx context.Context, branchId string) (uuid.UUID, error) {
81-
parsed, err := uuid.Parse(branchId)
82-
if err == nil {
83-
return parsed, nil
84-
}
85-
resp, err := utils.GetSupabase().V1GetABranchWithResponse(ctx, flags.ProjectRef, branchId)
86-
if err != nil {
87-
return parsed, errors.Errorf("failed to get branch: %w", err)
88-
} else if resp.JSON200 == nil {
89-
return parsed, errors.Errorf("unexpected get branch status %d: %s", resp.StatusCode(), string(resp.Body))
90-
}
91-
return resp.JSON200.Id, nil
92-
}
93-
9485
func getPoolerConfig(ctx context.Context, ref string) (api.SupavisorConfigResponse, error) {
9586
var result api.SupavisorConfigResponse
9687
resp, err := utils.GetSupabase().V1GetPoolerConfigWithResponse(ctx, ref)

internal/branches/list/list.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Run(ctx context.Context, fsys afero.Fs) error {
2222

2323
switch utils.OutputFormat.Value {
2424
case utils.OutputPretty:
25-
table := `|ID|BRANCH PROJECT ID|NAME|DEFAULT|GIT BRANCH|STATUS|CREATED AT (UTC)|UPDATED AT (UTC)|
25+
table := `|ID|NAME|DEFAULT|GIT BRANCH|STATUS|CREATED AT (UTC)|UPDATED AT (UTC)|
2626
|-|-|-|-|-|-|-|-|
2727
`
2828
for _, branch := range branches {
@@ -31,8 +31,7 @@ func Run(ctx context.Context, fsys afero.Fs) error {
3131
gitBranch = *branch.GitBranch
3232
}
3333
table += fmt.Sprintf(
34-
"|`%s`|`%s`|`%s`|`%t`|`%s`|`%s`|`%s`|`%s`|\n",
35-
branch.Id,
34+
"|`%s`|`%s`|`%t`|`%s`|`%s`|`%s`|`%s`|\n",
3635
branch.ProjectRef,
3736
strings.ReplaceAll(branch.Name, "|", "\\|"),
3837
branch.IsDefault,

internal/branches/pause/pause.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ func Run(ctx context.Context, branchId string) error {
2424
}
2525

2626
func GetBranchProjectRef(ctx context.Context, branchId string) (string, error) {
27-
if parsed, err := uuid.Parse(branchId); err == nil {
28-
resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, parsed)
27+
if utils.ProjectRefPattern.Match([]byte(branchId)) {
28+
return branchId, nil
29+
}
30+
if err := uuid.Validate(branchId); err == nil {
31+
resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId)
2932
if err != nil {
3033
return "", errors.Errorf("failed to get branch: %w", err)
3134
} else if resp.JSON200 == nil {
@@ -35,9 +38,9 @@ func GetBranchProjectRef(ctx context.Context, branchId string) (string, error) {
3538
}
3639
resp, err := utils.GetSupabase().V1GetABranchWithResponse(ctx, flags.ProjectRef, branchId)
3740
if err != nil {
38-
return "", errors.Errorf("failed to get branch: %w", err)
41+
return "", errors.Errorf("failed to find branch: %w", err)
3942
} else if resp.JSON200 == nil {
40-
return "", errors.Errorf("unexpected get branch status %d: %s", resp.StatusCode(), string(resp.Body))
43+
return "", errors.Errorf("unexpected find branch status %d: %s", resp.StatusCode(), string(resp.Body))
4144
}
4245
return resp.JSON200.ProjectRef, nil
4346
}

internal/branches/update/update.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ import (
66

77
"github.com/go-errors/errors"
88
"github.com/spf13/afero"
9-
"github.com/supabase/cli/internal/branches/get"
9+
"github.com/supabase/cli/internal/branches/pause"
1010
"github.com/supabase/cli/internal/utils"
1111
"github.com/supabase/cli/pkg/api"
1212
)
1313

1414
func Run(ctx context.Context, branchId string, body api.UpdateBranchBody, fsys afero.Fs) error {
15-
parsed, err := get.GetBranchID(ctx, branchId)
15+
projectRef, err := pause.GetBranchProjectRef(ctx, branchId)
1616
if err != nil {
1717
return err
1818
}
19-
resp, err := utils.GetSupabase().V1UpdateABranchConfigWithResponse(ctx, parsed, body)
19+
resp, err := utils.GetSupabase().V1UpdateABranchConfigWithResponse(ctx, projectRef, body)
2020
if err != nil {
2121
return errors.Errorf("failed to update preview branch: %w", err)
2222
}
2323
if resp.JSON200 == nil {
2424
return errors.New("Unexpected error updating preview branch: " + string(resp.Body))
2525
}
26-
fmt.Println("Updated preview branch:", resp.JSON200.Id)
26+
fmt.Println("Updated preview branch:", projectRef)
2727
return nil
2828
}

pkg/api/client.cfg.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package: api
22
generate:
33
client: true
4+
output-options:
5+
overlay:
6+
path: api/overlay.yaml
47
output: pkg/api/client.gen.go

0 commit comments

Comments
 (0)