Skip to content

Commit 0377d47

Browse files
authored
fix: ignore project not found error on linking (#2790)
* fix: ignore project not found error on linking * chore: update unit tests
1 parent 7adbe9b commit 0377d47

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

internal/link/link.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package link
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"os"
78
"strconv"
89
"strings"
@@ -238,24 +239,31 @@ func updatePoolerConfig(config api.SupavisorConfigResponse) {
238239
}
239240
}
240241

242+
var errProjectPaused = errors.New("project is paused")
243+
241244
func checkRemoteProjectStatus(ctx context.Context, projectRef string) error {
242245
resp, err := utils.GetSupabase().V1GetProjectWithResponse(ctx, projectRef)
243246
if err != nil {
244247
return errors.Errorf("failed to retrieve remote project status: %w", err)
245248
}
246-
if resp.JSON200 == nil {
249+
switch resp.StatusCode() {
250+
case http.StatusNotFound:
251+
// Ignore not found error to support linking branch projects
252+
return nil
253+
case http.StatusOK:
254+
// resp.JSON200 is not nil, proceed
255+
default:
247256
return errors.New("Unexpected error retrieving remote project status: " + string(resp.Body))
248257
}
249258

250259
switch resp.JSON200.Status {
251260
case api.V1ProjectResponseStatusINACTIVE:
252261
utils.CmdSuggestion = fmt.Sprintf("An admin must unpause it from the Supabase dashboard at %s", utils.Aqua(fmt.Sprintf("%s/project/%s", utils.GetSupabaseDashboardURL(), projectRef)))
253-
return errors.New("project is paused")
262+
return errors.New(errProjectPaused)
254263
case api.V1ProjectResponseStatusACTIVEHEALTHY:
255264
// Project is in the desired state, do nothing
256-
return nil
257265
default:
258-
fmt.Fprintf(os.Stderr, "%s: Project status is %s instead of Active Healthy. Some operations might fail.\n", utils.Yellow("Warning"), resp.JSON200.Status)
266+
fmt.Fprintf(os.Stderr, "%s: Project status is %s instead of Active Healthy. Some operations might fail.\n", utils.Yellow("WARNING"), resp.JSON200.Status)
259267
}
260268

261269
return nil

internal/link/link_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package link
33
import (
44
"context"
55
"errors"
6+
"net/http"
67
"testing"
78

89
"github.com/h2non/gock"
@@ -209,20 +210,37 @@ func TestLinkCommand(t *testing.T) {
209210
assert.NoError(t, err)
210211
assert.False(t, exists)
211212
})
213+
}
214+
215+
func TestStatusCheck(t *testing.T) {
216+
project := "test-project"
217+
218+
t.Run("ignores project not found", func(t *testing.T) {
219+
// Flush pending mocks after test execution
220+
defer gock.OffAll()
221+
// Mock project status
222+
gock.New(utils.DefaultApiHost).
223+
Get("/v1/projects/" + project).
224+
Reply(http.StatusNotFound)
225+
// Run test
226+
err := checkRemoteProjectStatus(context.Background(), project)
227+
// Check error
228+
assert.NoError(t, err)
229+
assert.Empty(t, apitest.ListUnmatchedRequests())
230+
})
231+
212232
t.Run("throws error on project inactive", func(t *testing.T) {
213-
// Setup in-memory fs
214-
fsys := afero.NewReadOnlyFs(afero.NewMemMapFs())
215233
// Flush pending mocks after test execution
216234
defer gock.OffAll()
217235
// Mock project status
218236
gock.New(utils.DefaultApiHost).
219237
Get("/v1/projects/" + project).
220-
Reply(200).
238+
Reply(http.StatusOK).
221239
JSON(api.V1ProjectResponse{Status: api.V1ProjectResponseStatusINACTIVE})
222240
// Run test
223-
err := Run(context.Background(), project, fsys)
241+
err := checkRemoteProjectStatus(context.Background(), project)
224242
// Check error
225-
assert.ErrorContains(t, err, "project is paused")
243+
assert.ErrorIs(t, err, errProjectPaused)
226244
assert.Empty(t, apitest.ListUnmatchedRequests())
227245
})
228246
}

0 commit comments

Comments
 (0)