diff --git a/internal/provider/branch_resource.go b/internal/provider/branch_resource.go index ac50736..022b9d3 100644 --- a/internal/provider/branch_resource.go +++ b/internal/provider/branch_resource.go @@ -283,15 +283,21 @@ func readBranchDatabase(ctx context.Context, state *BranchResourceModel, client } func createBranch(ctx context.Context, plan *BranchResourceModel, client *api.ClientWithResponses) diag.Diagnostics { - resp, err := client.V1ListAllBranches(ctx, plan.ParentProjectRef.ValueString()) + resp, err := client.V1ListAllBranchesWithResponse(ctx, plan.ParentProjectRef.ValueString()) if err != nil { msg := fmt.Sprintf("Unable to enable branching, got error: %s", err) return diag.Diagnostics{diag.NewErrorDiagnostic("Client Error", msg)} } // 1. Enable branching - if resp.StatusCode == http.StatusUnprocessableEntity { + if resp.StatusCode() == http.StatusOK && resp.JSON200 == nil { + msg := fmt.Sprintf("Unable to list branches, got status %d: %s", resp.StatusCode(), resp.Body) + return diag.Diagnostics{diag.NewErrorDiagnostic("Client Error", msg)} + } + if resp.StatusCode() == http.StatusUnprocessableEntity || + (resp.StatusCode() == http.StatusOK && len(*resp.JSON200) == 0) { httpResp, err := client.V1CreateABranchWithResponse(ctx, plan.ParentProjectRef.ValueString(), api.CreateBranchBody{ BranchName: "Production", + IsDefault: Ptr(true), }) if err != nil { msg := fmt.Sprintf("Unable to enable branching, got error: %s", err) diff --git a/internal/provider/branch_resource_test.go b/internal/provider/branch_resource_test.go index c9cc568..c4e57ed 100644 --- a/internal/provider/branch_resource_test.go +++ b/internal/provider/branch_resource_test.go @@ -29,7 +29,8 @@ func TestAccBranchResource(t *testing.T) { // Step 1: create gock.New(defaultApiEndpoint). Get(branchesApiPath). - Reply(http.StatusUnprocessableEntity) + Reply(http.StatusOK). + JSON([]api.BranchResponse{}) gock.New(defaultApiEndpoint). Post(branchesApiPath). Reply(http.StatusCreated).