Skip to content

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

refactor: decouple api/service/store packages for VCS (#959)

Yilong Li6bbd752e469c
diff --git a/api/vcs.go b/api/vcs.go
index 01c29dd5d1..44f41109d8 100644
--- a/api/vcs.go
+++ b/api/vcs.go
@@ -1,52 +1,11 @@
package api
import (
- "context"
"encoding/json"
"github.com/bytebase/bytebase/plugin/vcs"
)
-// VCSRaw is the store model for a VCS (Version Control System).
-// Fields have exactly the same meanings as VCS.
-type VCSRaw struct {
- ID int
-
- // Standard fields
- CreatorID int
- CreatedTs int64
- UpdaterID int
- UpdatedTs int64
-
- // Domain specific fields
- Name string
- Type vcs.Type
- InstanceURL string
- APIURL string
- ApplicationID string
- Secret string
-}
-
-// ToVCS creates an instance of VCS based on the VCSRaw.
-// This is intended to be called when we need to compose a VCS relationship.
-func (raw *VCSRaw) ToVCS() *VCS {
- return &VCS{
- ID: raw.ID,
-
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdaterID: raw.UpdaterID,
- UpdatedTs: raw.UpdatedTs,
-
- Name: raw.Name,
- Type: raw.Type,
- InstanceURL: raw.InstanceURL,
- APIURL: raw.APIURL,
- ApplicationID: raw.ApplicationID,
- Secret: raw.Secret,
- }
-}
-
// VCS is the API message for a VCS (Version Control System).
type VCS struct {
ID int `jsonapi:"primary,vcs"`
@@ -121,12 +80,3 @@ type VCSDelete struct {
// Value is assigned from the jwt subject field passed by the client.
DeleterID int
}
-
-// VCSService is the service for VCSs.
-type VCSService interface {
- CreateVCS(ctx context.Context, create *VCSCreate) (*VCSRaw, error)
- FindVCSList(ctx context.Context, find *VCSFind) ([]*VCSRaw, error)
- FindVCS(ctx context.Context, find *VCSFind) (*VCSRaw, error)
- PatchVCS(ctx context.Context, patch *VCSPatch) (*VCSRaw, error)
- DeleteVCS(ctx context.Context, delete *VCSDelete) error
-}
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 98f870996c..f074ad49a4 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -402,7 +402,6 @@ func (m *Main) Run(ctx context.Context) error {
s.ActivityService = store.NewActivityService(m.l, db)
s.InboxService = store.NewInboxService(m.l, db, s.ActivityService)
s.BookmarkService = store.NewBookmarkService(m.l, db)
- s.VCSService = store.NewVCSService(m.l, db)
s.RepositoryService = store.NewRepositoryService(m.l, db, s.ProjectService)
s.AnomalyService = store.NewAnomalyService(m.l, db)
s.LabelService = store.NewLabelService(m.l, db)
diff --git a/server/auth.go b/server/auth.go
index ba9bc56714..c78d52c716 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -21,13 +21,13 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
g.GET("/auth/provider", func(c echo.Context) error {
ctx := context.Background()
vcsFind := &api.VCSFind{}
- list, err := s.VCSService.FindVCSList(ctx, vcsFind)
+ vcsList, err := s.store.FindVCS(ctx, vcsFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch vcs list").SetInternal(err)
}
var authProviderList []*api.AuthProvider
- for _, vcs := range list {
+ for _, vcs := range vcsList {
newProvider := &api.AuthProvider{
ID: vcs.ID,
Type: vcs.Type,
@@ -83,8 +83,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, gitlabLogin); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted gitlab login request").SetInternal(err)
}
- findVCS := &api.VCSFind{ID: &gitlabLogin.ID}
- vcsFound, err := s.VCSService.FindVCS(ctx, findVCS)
+ vcsFound, err := s.store.GetVCSByID(ctx, gitlabLogin.ID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch vcs, name: %v, ID: %v", gitlabLogin.Name, gitlabLogin.Name)).SetInternal(err)
}
diff --git a/server/project.go b/server/project.go
index 8193068109..fadae18351 100644
--- a/server/project.go
+++ b/server/project.go
@@ -210,16 +210,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Malformatted create linked repository request: %s", err.Error()))
}
- vcsFind := &api.VCSFind{
- ID: &repositoryCreate.VCSID,
- }
- vcs, err := s.VCSService.FindVCS(ctx, vcsFind)
+ vcs, err := s.store.GetVCSByID(ctx, repositoryCreate.VCSID)
if err != nil {
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()
@@ -382,17 +376,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
if repoPatch.BranchFilter != nil {
- vcsFind := &api.VCSFind{
- ID: &repo.VCSID,
- }
- vcs, err := s.VCSService.FindVCS(ctx, vcsFind)
+ vcs, err := s.store.GetVCSByID(ctx, repo.VCSID)
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", repo.VCSID)
- return echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
- }
// Update 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.
@@ -466,17 +453,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
repository := repoRawList[0]
- vcsFind := &api.VCSFind{
- ID: &repository.VCSID,
- }
- vcs, err := s.VCSService.FindVCS(ctx, vcsFind)
+ vcs, err := s.store.GetVCSByID(ctx, repository.VCSID)
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/project_member.go b/server/project_member.go
index 9b4de734a3..3b0a1a7eef 100644
--- a/server/project_member.go
+++ b/server/project_member.go
@@ -43,16 +43,10 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch relevant VCS repo, Project ID: %s", c.Param("projectID"))).SetInternal(err)
}
- vcsFind := &api.VCSFind{
- ID: &repo.VCSID,
- }
- vcs, err := s.VCSService.FindVCS(ctx, vcsFind)
+ vcs, err := s.store.GetVCSByID(ctx, repo.VCSID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find VCS for sync project member: %d", repo.VCSID)).SetInternal(err)
}
- if vcs == nil {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", repo.VCSID))
- }
vcsProjectMemberList, err := vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{Logger: s.l}).FetchRepositoryActiveMemberList(ctx,
common.OauthContext{
ClientID: vcs.ApplicationID,
diff --git a/server/repository.go b/server/repository.go
index b813d1ccf5..db22b17c61 100644
--- a/server/repository.go
+++ b/server/repository.go
@@ -22,7 +22,7 @@ func (s *Server) composeRepositoryRelationship(ctx context.Context, raw *api.Rep
}
repository.Updater = updater
- vcs, err := s.composeVCSByID(ctx, repository.VCSID)
+ vcs, err := s.store.GetVCSByID(ctx, repository.VCSID)
if err != nil {
return nil, err
}
diff --git a/server/server.go b/server/server.go
index 631e3faa8a..c1c3b864cb 100644
--- a/server/server.go
+++ b/server/server.go
@@ -60,7 +60,6 @@ type Server struct {
ActivityService api.ActivityService
InboxService api.InboxService
BookmarkService api.BookmarkService
- VCSService api.VCSService
RepositoryService api.RepositoryService
AnomalyService api.AnomalyService
LabelService api.LabelService
diff --git a/server/task_executor.go b/server/task_executor.go
index 72bed37634..2a64f6ca06 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -189,7 +189,7 @@ func runMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.
// TODO(dragonly): revisit the usage of a not-fully-composed Repository here
repo := repoRawOutter.ToRepository()
- vcs, err := server.composeVCSByID(ctx, repoRawOutter.VCSID)
+ vcs, err := server.store.GetVCSByID(ctx, repoRawOutter.VCSID)
if err != nil {
return true, nil, fmt.Errorf("failed to sync schema file %s after applying migration %s to %q", latestSchemaFile, mi.Version, databaseName)
}
diff --git a/server/vcs.go b/server/vcs.go
index 790152bf5d..d03bc03b64 100644
--- a/server/vcs.go
+++ b/server/vcs.go
@@ -27,16 +27,11 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
vcsCreate.InstanceURL = strings.TrimRight(vcsCreate.InstanceURL, "/")
vcsCreate.APIURL = vcs.Get(vcs.GitLabSelfHost, vcs.ProviderConfig{Logger: s.l}).APIURL(vcsCreate.InstanceURL)
- vcsRaw, err := s.VCSService.CreateVCS(ctx, vcsCreate)
+ vcs, err := s.store.CreateVCS(ctx, vcsCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create VCS").SetInternal(err)
}
- vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch created VCS relationship").SetInternal(err)
- }
-
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, vcs); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create VCS response").SetInternal(err)
@@ -47,20 +42,11 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
g.GET("/vcs", func(c echo.Context) error {
ctx := context.Background()
vcsFind := &api.VCSFind{}
- vcsRawList, err := s.VCSService.FindVCSList(ctx, vcsFind)
+ vcsList, err := s.store.FindVCS(ctx, vcsFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch vcs list").SetInternal(err)
}
- var vcsList []*api.VCS
- for _, vcsRaw := range vcsRawList {
- vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch vcs relationship: %v", vcs.ID)).SetInternal(err)
- }
- vcsList = append(vcsList, vcs)
- }
-
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, vcsList); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal vcs list response").SetInternal(err)
@@ -75,13 +61,10 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("vcsID"))).SetInternal(err)
}
- vcs, err := s.composeVCSByID(ctx, id)
+ vcs, err := s.store.GetVCSByID(ctx, id)
if err != nil {
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 {
@@ -105,7 +88,7 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted change VCS request").SetInternal(err)
}
- vcsRaw, err := s.VCSService.PatchVCS(ctx, vcsPatch)
+ vcs, err := s.store.PatchVCS(ctx, vcsPatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS ID not found: %d", id))
@@ -113,11 +96,6 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to change VCS ID: %v", id)).SetInternal(err)
}
- vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch updated VCS relationship").SetInternal(err)
- }
-
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, vcs); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal VCS change response: %v", id)).SetInternal(err)
@@ -136,8 +114,8 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
ID: id,
DeleterID: c.Get(getPrincipalIDContextKey()).(int),
}
- if err := s.VCSService.DeleteVCS(ctx, vcsDelete); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete VCS ID: %v", id)).SetInternal(err)
+ if err := s.store.DeleteVCS(ctx, vcsDelete); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete VCS with ID[%d]", id)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -176,40 +154,3 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return nil
})
}
-
-func (s *Server) composeVCSByID(ctx context.Context, id int) (*api.VCS, error) {
- vcsFind := &api.VCSFind{
- ID: &id,
- }
- vcsRaw, err := s.VCSService.FindVCS(ctx, vcsFind)
- if err != nil {
- return nil, err
- }
- if vcsRaw == nil {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("VCS not found with ID %v", id)}
- }
-
- vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
- if err != nil {
- return nil, err
- }
- return vcs, nil
-}
-
-func (s *Server) composeVCSRelationship(ctx context.Context, raw *api.VCSRaw) (*api.VCS, error) {
- vcs := raw.ToVCS()
-
- creator, err := s.store.GetPrincipalByID(ctx, vcs.CreatorID)
- if err != nil {
- return nil, err
- }
- vcs.Creator = creator
-
- updater, err := s.store.GetPrincipalByID(ctx, vcs.UpdaterID)
- if err != nil {
- return nil, err
- }
- vcs.Updater = updater
-
- return vcs, nil
-}
diff --git a/store/principal.go b/store/principal.go
index 402b492d21..e10f96dbd3 100644
--- a/store/principal.go
+++ b/store/principal.go
@@ -30,9 +30,9 @@ type principalRaw struct {
PasswordHash string
}
-// ToPrincipal creates an instance of Principal based on the principalRaw.
+// toPrincipal creates an instance of Principal based on the principalRaw.
// This is intended to be called when we need to compose a Principal relationship.
-func (raw *principalRaw) ToPrincipal() *api.Principal {
+func (raw *principalRaw) toPrincipal() *api.Principal {
return &api.Principal{
ID: raw.ID,
@@ -59,7 +59,7 @@ func (s *Store) CreatePrincipal(ctx context.Context, create *api.PrincipalCreate
}
// NOTE: Currently the corresponding Member object is not created yet.
// YES, we are returning a Principal with empty Role field. OMG.
- principal := principalRaw.ToPrincipal()
+ principal := principalRaw.toPrincipal()
return principal, nil
}
@@ -109,7 +109,7 @@ func (s *Store) PatchPrincipal(ctx context.Context, patch *api.PrincipalPatch) (
return principal, nil
}
-// GetPrincipalByID composes an instance of Principal by ID
+// GetPrincipalByID gets a composesd instance of Principal by ID
func (s *Store) GetPrincipalByID(ctx context.Context, id int) (*api.Principal, error) {
principalFind := &api.PrincipalFind{
ID: &id,
@@ -244,7 +244,7 @@ func (s *Store) patchPrincipalRaw(ctx context.Context, patch *api.PrincipalPatch
// composePrincipal composes an instance of Principal by principalRaw
func (s *Store) composePrincipal(ctx context.Context, raw *principalRaw) (*api.Principal, error) {
- principal := raw.ToPrincipal()
+ principal := raw.toPrincipal()
if principal.ID == api.SystemBotID {
principal.Role = api.Owner
diff --git a/store/vcs.go b/store/vcs.go
index f69eeb3cc1..8aca318bb4 100644
--- a/store/vcs.go
+++ b/store/vcs.go
@@ -8,33 +8,145 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "go.uber.org/zap"
+ "github.com/bytebase/bytebase/plugin/vcs"
)
-var (
- _ api.VCSService = (*VCSService)(nil)
-)
+// vcsRaw is the store model for a VCS (Version Control System).
+// Fields have exactly the same meanings as VCS.
+type vcsRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Domain specific fields
+ Name string
+ Type vcs.Type
+ InstanceURL string
+ APIURL string
+ ApplicationID string
+ Secret string
+}
+
+// toVCS creates an instance of VCS based on the VCSRaw.
+// This is intended to be called when we need to compose a VCS relationship.
+func (raw *vcsRaw) toVCS() *api.VCS {
+ return &api.VCS{
+ ID: raw.ID,
+
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ Name: raw.Name,
+ Type: raw.Type,
+ InstanceURL: raw.InstanceURL,
+ APIURL: raw.APIURL,
+ ApplicationID: raw.ApplicationID,
+ Secret: raw.Secret,
+ }
+}
+
+// CreateVCS creates an instance of VCS
+func (s *Store) CreateVCS(ctx context.Context, create *api.VCSCreate) (*api.VCS, error) {
+ vcsRaw, err := s.createVCSRaw(ctx, create)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to create VCS with VCSCreate[%+v], error[%w]", create, err)
+ }
+ vcs, err := s.composeVCS(ctx, vcsRaw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose VCS with vcsRaw[%+v], error[%w]", vcsRaw, err)
+ }
+ return vcs, nil
+}
+
+// FindVCS finds a list of VCS instances
+func (s *Store) FindVCS(ctx context.Context, find *api.VCSFind) ([]*api.VCS, error) {
+ vcsRawList, err := s.findVCSRaw(ctx, find)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to find VCS list, error[%w]", err)
+ }
+ var vcsList []*api.VCS
+ for _, raw := range vcsRawList {
+ vcs, err := s.composeVCS(ctx, raw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose VCS with vcsRaw[%+v], error[%w]", raw, err)
+ }
+ vcsList = append(vcsList, vcs)
+ }
+ return vcsList, nil
+}
+
+// GetVCSByID gets a composesd instance of VCS by ID
+func (s *Store) GetVCSByID(ctx context.Context, id int) (*api.VCS, error) {
+ vcsRaw, err := s.getVCSRaw(ctx, id)
+ if err != nil {
+ return nil, err
+ }
+
+ vcs, err := s.composeVCS(ctx, vcsRaw)
+ if err != nil {
+ return nil, err
+ }
+ return vcs, nil
+}
-// VCSService represents a service for managing vcs.
-type VCSService struct {
- l *zap.Logger
- db *DB
+// PatchVCS patches an instance of VCS
+func (s *Store) PatchVCS(ctx context.Context, patch *api.VCSPatch) (*api.VCS, error) {
+ vcsRaw, err := s.patchVCSRaw(ctx, patch)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to patch VCS with VCSPatch[%+v], error[%w]", patch, err)
+ }
+ vcs, err := s.composeVCS(ctx, vcsRaw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose VCS with vcsRaw[%+v], error[%w]", vcsRaw, err)
+ }
+ return vcs, nil
}
-// NewVCSService returns a new instance of VCSService.
-func NewVCSService(logger *zap.Logger, db *DB) *VCSService {
- return &VCSService{l: logger, db: db}
+// DeleteVCS deletes an instance of VCS
+func (s *Store) DeleteVCS(ctx context.Context, delete *api.VCSDelete) error {
+ if err := s.deleteVCSRaw(ctx, delete); err != nil {
+ return fmt.Errorf("Failed to delete VCS with VCSDelete[%+v], error[%w]", delete, err)
+ }
+ return nil
}
-// CreateVCS creates a new vcs.
-func (s *VCSService) CreateVCS(ctx context.Context, create *api.VCSCreate) (*api.VCSRaw, error) {
+//
+// private functions
+//
+
+func (s *Store) composeVCS(ctx context.Context, raw *vcsRaw) (*api.VCS, error) {
+ vcs := raw.toVCS()
+
+ creator, err := s.GetPrincipalByID(ctx, vcs.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ vcs.Creator = creator
+
+ updater, err := s.GetPrincipalByID(ctx, vcs.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ vcs.Updater = updater
+
+ return vcs, nil
+}
+
+// createVCSRaw creates a new vcs.
+func (s *Store) createVCSRaw(ctx context.Context, create *api.VCSCreate) (*vcsRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- vcs, err := createVCS(ctx, tx.PTx, create)
+ vcs, err := createVCSImpl(ctx, tx.PTx, create)
if err != nil {
return nil, err
}
@@ -46,15 +158,15 @@ func (s *VCSService) CreateVCS(ctx context.Context, create *api.VCSCreate) (*api
return vcs, nil
}
-// FindVCSList retrieves a list of VCSs based on find conditions.
-func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api.VCSRaw, error) {
+// findVCSRaw retrieves a list of VCSs based on find conditions.
+func (s *Store) findVCSRaw(ctx context.Context, find *api.VCSFind) ([]*vcsRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := findVCSList(ctx, tx.PTx, find)
+ list, err := findVCSImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
@@ -62,38 +174,41 @@ func (s *VCSService) FindVCSList(ctx context.Context, find *api.VCSFind) ([]*api
return list, nil
}
-// FindVCS retrieves a single vcs based on find.
+// getVCSRaw retrieves a single vcs based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *VCSService) FindVCS(ctx context.Context, find *api.VCSFind) (*api.VCSRaw, error) {
+func (s *Store) getVCSRaw(ctx context.Context, id int) (*vcsRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := findVCSList(ctx, tx.PTx, find)
+ find := &api.VCSFind{
+ ID: &id,
+ }
+ list, err := findVCSImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
- return nil, nil
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("VCS not found with VCSFind[%+v]", find)}
} 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)}
}
return list[0], nil
}
-// PatchVCS updates an existing vcs by ID.
+// patchVCSRaw updates an existing vcs by ID.
// Returns ENOTFOUND if vcs does not exist.
-func (s *VCSService) PatchVCS(ctx context.Context, patch *api.VCSPatch) (*api.VCSRaw, error) {
+func (s *Store) patchVCSRaw(ctx context.Context, patch *api.VCSPatch) (*vcsRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- vcs, err := patchVCS(ctx, tx.PTx, patch)
+ vcs, err := patchVCSImpl(ctx, tx.PTx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -105,15 +220,15 @@ func (s *VCSService) PatchVCS(ctx context.Context, patch *api.VCSPatch) (*api.VC
return vcs, nil
}
-// DeleteVCS deletes an existing vcs by ID.
-func (s *VCSService) DeleteVCS(ctx context.Context, delete *api.VCSDelete) error {
+// deleteVCSRaw deletes an existing vcs by ID.
+func (s *Store) deleteVCSRaw(ctx context.Context, delete *api.VCSDelete) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return FormatError(err)
}
defer tx.PTx.Rollback()
- if err := deleteVCS(ctx, tx.PTx, delete); err != nil {
+ if err := deleteVCSImpl(ctx, tx.PTx, delete); err != nil {
return FormatError(err)
}
@@ -124,8 +239,12 @@ func (s *VCSService) DeleteVCS(ctx context.Context, delete *api.VCSDelete) error
return nil
}
-// createVCS creates a new vcs.
-func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCSRaw, error) {
+//
+// database access implementations
+//
+
+// createVCSImpl creates a new vcs.
+func createVCSImpl(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*vcsRaw, error) {
// Insert row into database.
row, err := tx.QueryContext(ctx, `
INSERT INTO vcs (
@@ -157,7 +276,7 @@ func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCS
defer row.Close()
row.Next()
- var vcs api.VCSRaw
+ var vcs vcsRaw
if err := row.Scan(
&vcs.ID,
&vcs.CreatorID,
@@ -177,7 +296,7 @@ func createVCS(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*api.VCS
return &vcs, nil
}
-func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCSRaw, error) {
+func findVCSImpl(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*vcsRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -207,9 +326,9 @@ func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCS
defer rows.Close()
// Iterate over result set and deserialize rows into list.
- var list []*api.VCSRaw
+ var list []*vcsRaw
for rows.Next() {
- var vcs api.VCSRaw
+ var vcs vcsRaw
if err := rows.Scan(
&vcs.ID,
&vcs.CreatorID,
@@ -235,8 +354,8 @@ func findVCSList(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*api.VCS
return list, nil
}
-// patchVCS updates a vcs by ID. Returns the new state of the vcs after update.
-func patchVCS(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*api.VCSRaw, error) {
+// patchVCSImpl updates a vcs by ID. Returns the new state of the vcs after update.
+func patchVCSImpl(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*vcsRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.Name; v != nil {
@@ -265,7 +384,7 @@ func patchVCS(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*api.VCSRaw
defer row.Close()
if row.Next() {
- var vcs api.VCSRaw
+ var vcs vcsRaw
if err := row.Scan(
&vcs.ID,
&vcs.CreatorID,
@@ -288,8 +407,8 @@ func patchVCS(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*api.VCSRaw
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("vcs ID not found: %d", patch.ID)}
}
-// deleteVCS permanently deletes a vcs by ID.
-func deleteVCS(ctx context.Context, tx *sql.Tx, delete *api.VCSDelete) error {
+// deleteVCSImpl permanently deletes a vcs by ID.
+func deleteVCSImpl(ctx context.Context, tx *sql.Tx, delete *api.VCSDelete) error {
// Remove row from database.
if _, err := tx.ExecContext(ctx, `DELETE FROM vcs WHERE id = $1`, delete.ID); err != nil {
return FormatError(err)