Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: store return nil on VCS entity non-existence (#305)

spinningbotede5f95c4089
diff --git a/server/project.go b/server/project.go
index a33c607edc..ed377d6265 100644
--- a/server/project.go
+++ b/server/project.go
@@ -169,11 +169,11 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
vcs, err := s.VCSService.FindVCS(ctx, vcsFind)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", repositoryCreate.VCSID))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find VCS for creating repository: %d", repositoryCreate.VCSID)).SetInternal(err)
}
+ if vcs == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", repositoryCreate.VCSID))
+ }
repositoryCreate.WebhookURLHost = fmt.Sprintf("%s:%d", s.host, s.port)
repositoryCreate.WebhookEndpointID = uuid.New().String()
@@ -341,6 +341,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update repository for project ID: %d", projectID)).SetInternal(err)
}
+ if vcs == nil {
+ err := fmt.Errorf("Failed to find VCS configuration for ID: %d", repository.VCSID)
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
+ }
// Updates the webhook after we successfully update the repository.
// This is because in case the webhook update fails, we can still have a reconcile process to reconcile the webhook state.
// If we update it before we update the repository, then if the repository update fails, then the reconcile process will reconcile the webhook to the pre-update state which is likely not intended.
@@ -428,6 +432,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete repository for project ID: %d", projectID)).SetInternal(err)
}
+ if vcs == nil {
+ err := fmt.Errorf("VCS not found for ID: %d", repository.VCSID)
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
+ }
repositoryDelete := &api.RepositoryDelete{
ProjectID: projectID,
diff --git a/server/repository.go b/server/repository.go
index f9698bf84d..e834c21655 100644
--- a/server/repository.go
+++ b/server/repository.go
@@ -2,6 +2,7 @@ package server
import (
"context"
+ "fmt"
"github.com/bytebase/bytebase/api"
)
@@ -23,6 +24,10 @@ func (s *Server) composeRepositoryRelationship(ctx context.Context, repository *
if err != nil {
return err
}
+ // We should always expect VCS to exist when ID isn't the default zero.
+ if repository.VCSID > 0 && repository.VCS == nil {
+ return fmt.Errorf("VCS not found for ID: %v", repository.VCSID)
+ }
repository.Project, err = s.composeProjectByID(ctx, repository.ProjectID)
if err != nil {
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index 4829763420..cbc0ab7dd4 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -166,6 +166,9 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
if err != nil {
return true, nil, fmt.Errorf("failed to sync schema file %s after applying migration %s to %q", latestSchemaFile, mi.Version, databaseName)
}
+ if repository.VCS == nil {
+ return true, nil, fmt.Errorf("VCS ID not found: %d", repository.VCSID)
+ }
// Writes back the latest schema file to the same branch as the push event.
// Ref format refs/heads/<<branch>>
diff --git a/server/vcs.go b/server/vcs.go
index 17b0c2332f..797433c8fa 100644
--- a/server/vcs.go
+++ b/server/vcs.go
@@ -73,11 +73,11 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
vcs, err := s.composeVCSByID(ctx, id)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch vcs ID: %v", id)).SetInternal(err)
}
+ if vcs == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", id))
+ }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, vcs); err != nil {
@@ -182,10 +182,11 @@ func (s *Server) composeVCSByID(ctx context.Context, id int) (*api.VCS, error) {
return nil, err
}
- if err := s.composeVCSRelationship(ctx, vcs); err != nil {
- return nil, err
+ if vcs != nil {
+ if err := s.composeVCSRelationship(ctx, vcs); err != nil {
+ return nil, err
+ }
}
-
return vcs, nil
}
diff --git a/server/webhook.go b/server/webhook.go
index 5965b681b6..89054f7f55 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -59,6 +59,10 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
if err := s.composeRepositoryRelationship(ctx, repository); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch repository relationship: %v", repository.Name)).SetInternal(err)
}
+ if repository.VCS == nil {
+ err := fmt.Errorf("VCS not found for ID: %v", repository.VCSID)
+ return echo.NewHTTPError(http.StatusInternalServerError, err).SetInternal(err)
+ }
if c.Request().Header.Get("X-Gitlab-Token") != repository.WebhookSecretToken {
return echo.NewHTTPError(http.StatusBadRequest, "Secret token mismatch")
diff --git a/store/vcs.go b/store/vcs.go
index 3b8c0dce3a..0549708bc0 100644
--- a/store/vcs.go
+++ b/store/vcs.go
@@ -62,7 +62,6 @@ func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api
}
// FindVCS retrieves a single vcs based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *VCSService) FindVCS(ctx context.Context, find *api.VCSFind) (*api.VCS, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -74,8 +73,10 @@ func (s *VCSService) FindVCS(ctx context.Context, find *api.VCSFind) (*api.VCS,
list, err := findVCSList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("vcs not found: %+v", find)}
+ }
+
+ if len(list) == 0 {
+ return nil, nil
} else if len(list) > 1 {
return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d vcss with filter %+v, expect 1", len(list), find)}
}