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 Task (#1187)

Yilong Li89e40fe4e4d0
diff --git a/api/issue.go b/api/issue.go
index 099bd5c198..593653a61a 100644
--- a/api/issue.go
+++ b/api/issue.go
@@ -1,8 +1,6 @@
package api
import (
- "context"
-
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/vcs"
)
@@ -60,56 +58,6 @@ const (
IssueFieldSQL IssueFieldID = "7"
)
-// IssueRaw is the store model for an Issue.
-// Fields have exactly the same meanings as Issue.
-type IssueRaw struct {
- ID int
-
- // Standard fields
- CreatorID int
- CreatedTs int64
- UpdaterID int
- UpdatedTs int64
-
- // Related fields
- ProjectID int
- PipelineID int
-
- // Domain specific fields
- Name string
- Status IssueStatus
- Type IssueType
- Description string
- AssigneeID int
- Payload string
-}
-
-// ToIssue creates an instance of Issue based on the IssueRaw.
-// This is intended to be called when we need to compose an Issue relationship.
-func (raw *IssueRaw) ToIssue() *Issue {
- return &Issue{
- ID: raw.ID,
-
- // Standard fields
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdaterID: raw.UpdaterID,
- UpdatedTs: raw.UpdatedTs,
-
- // Related fields
- ProjectID: raw.ProjectID,
- PipelineID: raw.PipelineID,
-
- // Domain specific fields
- Name: raw.Name,
- Status: raw.Status,
- Type: raw.Type,
- Description: raw.Description,
- AssigneeID: raw.AssigneeID,
- Payload: raw.Payload,
- }
-}
-
// Issue is the API message for an issue.
type Issue struct {
ID int `jsonapi:"primary,issue"`
@@ -275,11 +223,3 @@ type IssueStatusPatch struct {
Status IssueStatus `jsonapi:"attr,status"`
Comment string `jsonapi:"attr,comment"`
}
-
-// IssueService is the services for issues.
-type IssueService interface {
- CreateIssue(ctx context.Context, create *IssueCreate) (*IssueRaw, error)
- FindIssueList(ctx context.Context, find *IssueFind) ([]*IssueRaw, error)
- FindIssue(ctx context.Context, find *IssueFind) (*IssueRaw, error)
- PatchIssue(ctx context.Context, patch *IssuePatch) (*IssueRaw, error)
-}
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 0b87ab88b6..88828187f3 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -461,7 +461,6 @@ func (m *Main) Run(ctx context.Context) error {
s.ColumnService = store.NewColumnService(m.l, db)
s.ViewService = store.NewViewService(m.l, db)
s.IndexService = store.NewIndexService(m.l, db)
- s.IssueService = store.NewIssueService(m.l, db, cacheService)
s.RepositoryService = store.NewRepositoryService(m.l, db, storeInstance)
s.DeploymentConfigService = store.NewDeploymentConfigService(m.l, db)
s.SheetService = store.NewSheetService(m.l, db)
diff --git a/server/activity.go b/server/activity.go
index 8d1020545e..0b369a9ed4 100644
--- a/server/activity.go
+++ b/server/activity.go
@@ -27,17 +27,13 @@ func (s *Server) registerActivityRoutes(g *echo.Group) {
issueFind := &api.IssueFind{
ID: &activityCreate.ContainerID,
}
- issueRaw, err := s.IssueService.FindIssue(ctx, issueFind)
+ issue, err := s.store.GetIssue(ctx, issueFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID when creating the comment: %d", activityCreate.ContainerID)).SetInternal(err)
}
- if issueRaw == nil {
+ if issue == nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to find issue ID for creating the comment: %d", activityCreate.ContainerID))
}
- issue, err := s.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose issue relation with ID %d", issueRaw.ID)).SetInternal(err)
- }
bytes, err := json.Marshal(api.ActivityIssueCommentCreatePayload{
IssueName: issue.Name,
diff --git a/server/issue.go b/server/issue.go
index 6af40e0795..806283adaf 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -73,18 +73,10 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
issueFind.PrincipalID = &userID
}
- issueRawList, err := s.IssueService.FindIssueList(ctx, issueFind)
+ issueList, err := s.store.FindIssue(ctx, issueFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch issue list").SetInternal(err)
}
- var issueList []*api.Issue
- for _, issueRaw := range issueRawList {
- issue, err := s.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return err
- }
- issueList = append(issueList, issue)
- }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, issueList); err != nil {
@@ -100,7 +92,7 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("issueID"))).SetInternal(err)
}
- issue, err := s.composeIssueByID(ctx, id)
+ issue, err := s.store.GetIssue(ctx, &api.IssueFind{ID: &id})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID: %v", id)).SetInternal(err)
}
@@ -136,29 +128,18 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
}
}
- issueFind := &api.IssueFind{
- ID: &id,
- }
- issueRaw, err := s.IssueService.FindIssue(ctx, issueFind)
+ issue, err := s.store.GetIssue(ctx, &api.IssueFind{ID: &id})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID when updating issue: %v", id)).SetInternal(err)
}
- if issueRaw == nil {
+ if issue == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to find issue ID to update: %d", id))
}
- issue, err := s.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose issue relationship with ID %v", id)).SetInternal(err)
- }
- updatedIssueRaw, err := s.IssueService.PatchIssue(ctx, issuePatch)
+ updatedIssue, err := s.store.PatchIssue(ctx, issuePatch)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update issue with ID %d", id)).SetInternal(err)
}
- updatedIssue, err := s.composeIssueRelationship(ctx, updatedIssueRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose issue relationship with ID %v", id)).SetInternal(err)
- }
payloadList := [][]byte{}
if issuePatch.Name != nil && *issuePatch.Name != issue.Name {
@@ -236,7 +217,7 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted update issue status request").SetInternal(err)
}
- issue, err := s.composeIssueByID(ctx, id)
+ issue, err := s.store.GetIssue(ctx, &api.IssueFind{ID: &id})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID: %v", id)).SetInternal(err)
}
@@ -262,76 +243,6 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
})
}
-func (s *Server) composeIssueByID(ctx context.Context, id int) (*api.Issue, error) {
- issueFind := &api.IssueFind{
- ID: &id,
- }
- issueRaw, err := s.IssueService.FindIssue(ctx, issueFind)
- if err != nil {
- return nil, err
- }
- if id > 0 && issueRaw == nil {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("issue not found with ID %v", id)}
- }
-
- if issueRaw == nil {
- return nil, nil
- }
-
- issue, err := s.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return nil, err
- }
- return issue, nil
-}
-
-func (s *Server) composeIssueRelationship(ctx context.Context, raw *api.IssueRaw) (*api.Issue, error) {
- issue := raw.ToIssue()
-
- creator, err := s.store.GetPrincipalByID(ctx, issue.CreatorID)
- if err != nil {
- return nil, err
- }
- issue.Creator = creator
-
- updater, err := s.store.GetPrincipalByID(ctx, issue.UpdaterID)
- if err != nil {
- return nil, err
- }
- issue.Updater = updater
-
- assignee, err := s.store.GetPrincipalByID(ctx, issue.AssigneeID)
- if err != nil {
- return nil, err
- }
- issue.Assignee = assignee
-
- issueSubscriberFind := &api.IssueSubscriberFind{
- IssueID: &issue.ID,
- }
- issueSubscriberList, err := s.store.FindIssueSubscriber(ctx, issueSubscriberFind)
- if err != nil {
- return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch subscriber list for issue %d", issue.ID)).SetInternal(err)
- }
- for _, issueSub := range issueSubscriberList {
- issue.SubscriberList = append(issue.SubscriberList, issueSub.Subscriber)
- }
-
- project, err := s.store.GetProjectByID(ctx, issue.ProjectID)
- if err != nil {
- return nil, err
- }
- issue.Project = project
-
- pipeline, err := s.store.GetPipelineByID(ctx, issue.PipelineID)
- if err != nil {
- return nil, err
- }
- issue.Pipeline = pipeline
-
- return issue, nil
-}
-
// TODO(dragonly): refactor validate only code path
func (s *Server) composeIssueRelationshipValidateOnly(ctx context.Context, issue *api.Issue) error {
var err error
@@ -434,13 +345,9 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate,
} else {
issueCreate.CreatorID = creatorID
issueCreate.PipelineID = pipeline.ID
- issueCreatedRaw, err := s.IssueService.CreateIssue(ctx, issueCreate)
- if err != nil {
- return nil, fmt.Errorf("failed to create issue with IssueCreate %v, error %w", issueCreate, err)
- }
- issue, err = s.composeIssueRelationship(ctx, issueCreatedRaw)
+ issue, err = s.store.CreateIssue(ctx, issueCreate)
if err != nil {
- return nil, fmt.Errorf("failed to compose issue")
+ return nil, err
}
// Create issue subscribers.
for _, subscriberID := range issueCreate.SubscriberIDList {
@@ -1136,7 +1043,7 @@ func (s *Server) changeIssueStatus(ctx context.Context, issue *api.Issue, newSta
Status: &pipelineStatus,
}
if _, err := s.store.PatchPipeline(ctx, pipelinePatch); err != nil {
- return nil, fmt.Errorf("failed to update issue(%v) status, failed to update pipeline status with patch %+v, error: %w", issue.Name, pipelinePatch, err)
+ return nil, fmt.Errorf("failed to update issue[%s] status, failed to update pipeline status with patch %+v, error: %w", issue.Name, pipelinePatch, err)
}
issuePatch := &api.IssuePatch{
@@ -1144,13 +1051,9 @@ func (s *Server) changeIssueStatus(ctx context.Context, issue *api.Issue, newSta
UpdaterID: updaterID,
Status: &newStatus,
}
- updatedIssueRaw, err := s.IssueService.PatchIssue(ctx, issuePatch)
- if err != nil {
- return nil, fmt.Errorf("failed to update issue(%v) status with patch %v, error: %w", issue.Name, issuePatch, err)
- }
- updatedIssue, err := s.composeIssueRelationship(ctx, updatedIssueRaw)
+ updatedIssue, err := s.store.PatchIssue(ctx, issuePatch)
if err != nil {
- return nil, fmt.Errorf("failed to compose issue(%v) relationship with IssueRaw %v, error: %v", issue.Name, updatedIssueRaw, err)
+ return nil, fmt.Errorf("failed to update issue[%s] status with patch %v, error: %w", issue.Name, issuePatch, err)
}
payload, err := json.Marshal(api.ActivityIssueStatusUpdatePayload{
diff --git a/server/server.go b/server/server.go
index 92d456d58a..09715ed5c1 100644
--- a/server/server.go
+++ b/server/server.go
@@ -43,7 +43,6 @@ type Server struct {
ColumnService api.ColumnService
ViewService api.ViewService
IndexService api.IndexService
- IssueService api.IssueService
RepositoryService api.RepositoryService
DeploymentConfigService api.DeploymentConfigService
LicenseService enterprise.LicenseService
diff --git a/server/task.go b/server/task.go
index 2d4b8a6ab4..e1857f878e 100644
--- a/server/task.go
+++ b/server/task.go
@@ -66,14 +66,10 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
issueFind := &api.IssueFind{
PipelineID: &task.PipelineID,
}
- issueRaw, err := s.IssueService.FindIssue(ctx, issueFind)
+ issue, err := s.store.GetIssue(ctx, issueFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue with pipeline ID %v", task.PipelineID)).SetInternal(err)
}
- issue, err := s.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose issue relationship with pipeline ID %v", task.PipelineID)).SetInternal(err)
- }
oldStatement := ""
newStatement := ""
@@ -308,9 +304,10 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Task not found with ID %d", taskID))
}
- issue, err := s.IssueService.FindIssue(ctx, &api.IssueFind{
+ issueFind := &api.IssueFind{
PipelineID: &task.PipelineID,
- })
+ }
+ issue, err := s.store.GetIssue(ctx, issueFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find issue").SetInternal(err)
}
@@ -470,18 +467,14 @@ func (s *Server) changeTaskStatusWithPatch(ctx context.Context, task *api.Task,
issueFind := &api.IssueFind{
PipelineID: &task.PipelineID,
}
- issueRaw, err := s.IssueService.FindIssue(ctx, issueFind)
+ issue, err := s.store.GetIssue(ctx, issueFind)
if err != nil {
// Not all pipelines belong to an issue, so it's OK if ENOTFOUND
return nil, fmt.Errorf("failed to fetch containing issue after changing the task status: %v, err: %w", task.Name, err)
}
- if issueRaw == nil {
+ if issue == nil {
return nil, fmt.Errorf("failed to find issue with pipeline ID %d, task: %s", task.PipelineID, task.Name)
}
- issue, err := s.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return nil, fmt.Errorf("failed to compose issue relationship with ID %d, err: %w", issueRaw.ID, err)
- }
// Create an activity
issueName := ""
diff --git a/server/task_executor.go b/server/task_executor.go
index d91d20e9e2..cd00da4512 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -97,7 +97,7 @@ func runMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.
issueFind := &api.IssueFind{
PipelineID: &task.PipelineID,
}
- issueRaw, err := server.IssueService.FindIssue(ctx, issueFind)
+ issue, err := server.store.GetIssue(ctx, issueFind)
if err != nil {
// If somehow we cannot find the issue, emit the error since it's not fatal.
l.Error("Failed to fetch containing issue for composing the migration info",
@@ -105,18 +105,13 @@ func runMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.
zap.Error(err),
)
}
- var issue *api.Issue
- if issueRaw == nil {
+ if issue == nil {
err := fmt.Errorf("failed to fetch containing issue for composing the migration info, issue not found with pipeline ID %v", task.PipelineID)
l.Error(err.Error(),
zap.Int("task_id", task.ID),
zap.Error(err),
)
} else {
- issue, err = server.composeIssueRelationship(ctx, issueRaw)
- if err != nil {
- return true, nil, err
- }
mi.IssueID = strconv.Itoa(issue.ID)
}
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index 7b698b33fd..c3d4803676 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -89,7 +89,7 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
issueFind := &api.IssueFind{
PipelineID: &task.PipelineID,
}
- issue, err := server.IssueService.FindIssue(ctx, issueFind)
+ issue, err := server.store.GetIssue(ctx, issueFind)
if err != nil {
// If somehow we unable to find the issue, we just emit the error since it's not
// critical enough to fail the entire operation.
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index 75db530c41..7909ab7cb9 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -158,7 +158,7 @@ func createBranchMigrationHistory(ctx context.Context, server *Server, sourceDat
issueFind := &api.IssueFind{
PipelineID: &task.PipelineID,
}
- issue, err := server.IssueService.FindIssue(ctx, issueFind)
+ issue, err := server.store.GetIssue(ctx, issueFind)
if err != nil {
return -1, "", fmt.Errorf("failed to fetch containing issue when creating the migration history: %v, err: %w", task.Name, err)
}
diff --git a/store/issue.go b/store/issue.go
index 9716d2a843..fb42f7f259 100644
--- a/store/issue.go
+++ b/store/issue.go
@@ -11,24 +11,115 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "go.uber.org/zap"
)
-var (
- _ api.IssueService = (*IssueService)(nil)
-)
+// issueRaw is the store model for an Issue.
+// Fields have exactly the same meanings as Issue.
+type issueRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ ProjectID int
+ PipelineID int
+
+ // Domain specific fields
+ Name string
+ Status api.IssueStatus
+ Type api.IssueType
+ Description string
+ AssigneeID int
+ Payload string
+}
-// IssueService represents a service for managing issue.
-type IssueService struct {
- l *zap.Logger
- db *DB
+// toIssue creates an instance of Issue based on the issueRaw.
+// This is intended to be called when we need to compose an Issue relationship.
+func (raw *issueRaw) toIssue() *api.Issue {
+ return &api.Issue{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ ProjectID: raw.ProjectID,
+ PipelineID: raw.PipelineID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Status: raw.Status,
+ Type: raw.Type,
+ Description: raw.Description,
+ AssigneeID: raw.AssigneeID,
+ Payload: raw.Payload,
+ }
+}
- cache api.CacheService
+// CreateIssue creates an instance of Issue
+func (s *Store) CreateIssue(ctx context.Context, create *api.IssueCreate) (*api.Issue, error) {
+ issueRaw, err := s.createIssueRaw(ctx, create)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create Issue with IssueCreate[%+v], error[%w]", create, err)
+ }
+ issue, err := s.composeIssue(ctx, issueRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose Issue with issueRaw[%+v], error[%w]", issueRaw, err)
+ }
+ return issue, nil
}
-// NewIssueService returns a new instance of IssueService.
-func NewIssueService(logger *zap.Logger, db *DB, cache api.CacheService) *IssueService {
- return &IssueService{l: logger, db: db, cache: cache}
+// GetIssue gets an instance of Issue
+func (s *Store) GetIssue(ctx context.Context, find *api.IssueFind) (*api.Issue, error) {
+ issueRaw, err := s.getIssueRaw(ctx, find)
+ if err != nil {
+ return nil, fmt.Errorf("failed to get Issue with IssueFind[%+v], error[%w]", find, err)
+ }
+ if issueRaw == nil {
+ return nil, nil
+ }
+ issue, err := s.composeIssue(ctx, issueRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose Issue with issueRaw[%+v], error[%w]", issueRaw, err)
+ }
+ return issue, nil
+}
+
+// FindIssue finds a list of Issue instances
+func (s *Store) FindIssue(ctx context.Context, find *api.IssueFind) ([]*api.Issue, error) {
+ issueRawList, err := s.findIssueRaw(ctx, find)
+ if err != nil {
+ return nil, fmt.Errorf("failed to find Issue list, error[%w]", err)
+ }
+ var issueList []*api.Issue
+ for _, raw := range issueRawList {
+ issue, err := s.composeIssue(ctx, raw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose Issue with issueRaw[%+v], error[%w]", raw, err)
+ }
+ issueList = append(issueList, issue)
+ }
+ return issueList, nil
+}
+
+// PatchIssue patches an instance of Issue
+func (s *Store) PatchIssue(ctx context.Context, patch *api.IssuePatch) (*api.Issue, error) {
+ issueRaw, err := s.patchIssueRaw(ctx, patch)
+ if err != nil {
+ return nil, fmt.Errorf("failed to patch Issue with IssuePatch[%+v], error[%w]", patch, err)
+ }
+ issue, err := s.composeIssue(ctx, issueRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose Issue with issueRaw[%+v], error[%w]", issueRaw, err)
+ }
+ return issue, nil
}
// CreatePipelineValidateOnly creates a pipeline for validation purpose
@@ -102,15 +193,66 @@ func (s *Store) CreatePipelineValidateOnly(ctx context.Context, pc *api.Pipeline
return pipeline, nil
}
-// CreateIssue creates a new issue.
-func (s *IssueService) CreateIssue(ctx context.Context, create *api.IssueCreate) (*api.IssueRaw, error) {
+//
+// private functions
+//
+
+func (s *Store) composeIssue(ctx context.Context, raw *issueRaw) (*api.Issue, error) {
+ issue := raw.toIssue()
+
+ creator, err := s.GetPrincipalByID(ctx, issue.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ issue.Creator = creator
+
+ updater, err := s.GetPrincipalByID(ctx, issue.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ issue.Updater = updater
+
+ assignee, err := s.GetPrincipalByID(ctx, issue.AssigneeID)
+ if err != nil {
+ return nil, err
+ }
+ issue.Assignee = assignee
+
+ issueSubscriberFind := &api.IssueSubscriberFind{
+ IssueID: &issue.ID,
+ }
+ issueSubscriberList, err := s.FindIssueSubscriber(ctx, issueSubscriberFind)
+ if err != nil {
+ return nil, err
+ }
+ for _, issueSub := range issueSubscriberList {
+ issue.SubscriberList = append(issue.SubscriberList, issueSub.Subscriber)
+ }
+
+ project, err := s.GetProjectByID(ctx, issue.ProjectID)
+ if err != nil {
+ return nil, err
+ }
+ issue.Project = project
+
+ pipeline, err := s.GetPipelineByID(ctx, issue.PipelineID)
+ if err != nil {
+ return nil, err
+ }
+ issue.Pipeline = pipeline
+
+ return issue, nil
+}
+
+// createIssueRaw creates a new issue.
+func (s *Store) createIssueRaw(ctx context.Context, create *api.IssueCreate) (*issueRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- issue, err := s.createIssue(ctx, tx.PTx, create)
+ issue, err := s.createIssueImpl(ctx, tx.PTx, create)
if err != nil {
return nil, err
}
@@ -126,15 +268,15 @@ func (s *IssueService) CreateIssue(ctx context.Context, create *api.IssueCreate)
return issue, nil
}
-// FindIssueList retrieves a list of issues based on find.
-func (s *IssueService) FindIssueList(ctx context.Context, find *api.IssueFind) ([]*api.IssueRaw, error) {
+// findIssueRaw retrieves a list of issues based on find.
+func (s *Store) findIssueRaw(ctx context.Context, find *api.IssueFind) ([]*issueRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findIssueList(ctx, tx.PTx, find)
+ list, err := s.findIssueImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
@@ -150,16 +292,16 @@ func (s *IssueService) FindIssueList(ctx context.Context, find *api.IssueFind) (
return list, nil
}
-// FindIssue retrieves a single issue based on find.
+// getIssueRaw retrieves a single issue based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *IssueService) FindIssue(ctx context.Context, find *api.IssueFind) (*api.IssueRaw, error) {
+func (s *Store) getIssueRaw(ctx context.Context, find *api.IssueFind) (*issueRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findIssueList(ctx, tx.PTx, find)
+ list, err := s.findIssueImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
@@ -175,16 +317,16 @@ func (s *IssueService) FindIssue(ctx context.Context, find *api.IssueFind) (*api
return list[0], nil
}
-// PatchIssue updates an existing issue by ID.
+// patchIssueRaw updates an existing issue by ID.
// Returns ENOTFOUND if issue does not exist.
-func (s *IssueService) PatchIssue(ctx context.Context, patch *api.IssuePatch) (*api.IssueRaw, error) {
+func (s *Store) patchIssueRaw(ctx context.Context, patch *api.IssuePatch) (*issueRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- issue, err := s.patchIssue(ctx, tx.PTx, patch)
+ issue, err := s.patchIssueImpl(ctx, tx.PTx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -200,8 +342,8 @@ func (s *IssueService) PatchIssue(ctx context.Context, patch *api.IssuePatch) (*
return issue, nil
}
-// createIssue creates a new issue.
-func (s *IssueService) createIssue(ctx context.Context, tx *sql.Tx, create *api.IssueCreate) (*api.IssueRaw, error) {
+// createIssueImpl creates a new issue.
+func (s *Store) createIssueImpl(ctx context.Context, tx *sql.Tx, create *api.IssueCreate) (*issueRaw, error) {
if create.Payload == "" {
create.Payload = "{}"
}
@@ -238,7 +380,7 @@ func (s *IssueService) createIssue(ctx context.Context, tx *sql.Tx, create *api.
defer row.Close()
row.Next()
- var issueRaw api.IssueRaw
+ var issueRaw issueRaw
if err := row.Scan(
&issueRaw.ID,
&issueRaw.CreatorID,
@@ -260,7 +402,7 @@ func (s *IssueService) createIssue(ctx context.Context, tx *sql.Tx, create *api.
return &issueRaw, nil
}
-func (s *IssueService) findIssueList(ctx context.Context, tx *sql.Tx, find *api.IssueFind) ([]*api.IssueRaw, error) {
+func (s *Store) findIssueImpl(ctx context.Context, tx *sql.Tx, find *api.IssueFind) ([]*issueRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -315,9 +457,9 @@ func (s *IssueService) findIssueList(ctx context.Context, tx *sql.Tx, find *api.
defer rows.Close()
// Iterate over result set and deserialize rows into issuerRawList.
- var issuerRawList []*api.IssueRaw
+ var issuerRawList []*issueRaw
for rows.Next() {
- var issueRaw api.IssueRaw
+ var issueRaw issueRaw
if err := rows.Scan(
&issueRaw.ID,
&issueRaw.CreatorID,
@@ -345,8 +487,8 @@ func (s *IssueService) findIssueList(ctx context.Context, tx *sql.Tx, find *api.
return issuerRawList, nil
}
-// patchIssue updates a issue by ID. Returns the new state of the issue after update.
-func (s *IssueService) patchIssue(ctx context.Context, tx *sql.Tx, patch *api.IssuePatch) (*api.IssueRaw, error) {
+// patchIssueImpl updates a issue by ID. Returns the new state of the issue after update.
+func (s *Store) patchIssueImpl(ctx context.Context, tx *sql.Tx, patch *api.IssuePatch) (*issueRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.Name; v != nil {
@@ -386,7 +528,7 @@ func (s *IssueService) patchIssue(ctx context.Context, tx *sql.Tx, patch *api.Is
defer row.Close()
if row.Next() {
- var issueRaw api.IssueRaw
+ var issueRaw issueRaw
if err := row.Scan(
&issueRaw.ID,
&issueRaw.CreatorID,