Skip to content

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

Properly implement POST /issue

天舟14ae3ee6faca
diff --git a/backend/api/issue.go b/backend/api/issue.go
index 10e1a1e1d1..01d2ff23b9 100644
--- a/backend/api/issue.go
+++ b/backend/api/issue.go
@@ -93,6 +93,7 @@ type IssueCreate struct {
// Related fields
ProjectId int `jsonapi:"attr,projectId"`
PipelineId int
+ Pipeline PipelineCreate `jsonapi:"attr,pipeline"`
// Domain specific fields
Name string `jsonapi:"attr,name"`
diff --git a/backend/api/pipeline.go b/backend/api/pipeline.go
index 3e2e4af019..b13a237948 100644
--- a/backend/api/pipeline.go
+++ b/backend/api/pipeline.go
@@ -49,6 +49,9 @@ type PipelineCreate struct {
CreatorId int
WorkspaceId int
+ // Related fields
+ StageList []StageCreate `jsonapi:"attr,stageList"`
+
// Domain specific fields
Name string `jsonapi:"attr,name"`
}
diff --git a/backend/api/stage.go b/backend/api/stage.go
index 2fc63f99f8..570a961851 100644
--- a/backend/api/stage.go
+++ b/backend/api/stage.go
@@ -56,8 +56,9 @@ type StageCreate struct {
WorkspaceId int
// Related fields
- EnvironmentId int `jsonapi:"relation,environmentId"`
- PipelineId int `jsonapi:"relation,pipelineId"`
+ EnvironmentId int `jsonapi:"attr,environmentId"`
+ PipelineId int
+ TaskList []TaskCreate `jsonapi:"attr,taskList"`
// Domain specific fields
Name string `jsonapi:"attr,name"`
diff --git a/backend/api/task.go b/backend/api/task.go
index dc0f2c0b7f..7cab5dcdb3 100644
--- a/backend/api/task.go
+++ b/backend/api/task.go
@@ -50,6 +50,9 @@ const (
TaskManual TaskWhen = "MANUAL"
)
+type TaskPayload struct {
+}
+
type Task struct {
ID int `jsonapi:"primary,task"`
@@ -70,11 +73,11 @@ type Task struct {
Database *Database `jsonapi:"relation,database"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Status TaskStatus `jsonapi:"attr,status"`
- Type TaskType `jsonapi:"attr,type"`
- When TaskWhen `jsonapi:"attr,when"`
- Payload string `jsonapi:"attr,payload"`
+ Name string `jsonapi:"attr,name"`
+ Status TaskStatus `jsonapi:"attr,status"`
+ Type TaskType `jsonapi:"attr,type"`
+ When TaskWhen `jsonapi:"attr,when"`
+ Payload TaskPayload `jsonapi:"attr,payload"`
}
type TaskCreate struct {
@@ -84,15 +87,15 @@ type TaskCreate struct {
WorkspaceId int
// Related fields
- PipelineId int `jsonapi:"relation,pipelineId"`
- StageId int `jsonapi:"relation,stageId"`
- DatabaseId int `jsonapi:"relation,databaseId"`
+ PipelineId int
+ StageId int
+ DatabaseId int `jsonapi:"attr,databaseId"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Type TaskType `jsonapi:"attr,type"`
- When TaskWhen `jsonapi:"attr,when"`
- Payload string `jsonapi:"attr,payload"`
+ Name string `jsonapi:"attr,name"`
+ Type TaskType `jsonapi:"attr,type"`
+ When TaskWhen `jsonapi:"attr,when"`
+ Payload TaskPayload `jsonapi:"attr,payload"`
}
type TaskFind struct {
diff --git a/backend/server/issue.go b/backend/server/issue.go
index 0d52dbf57e..a11897f6e1 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -19,8 +19,36 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create issue request").SetInternal(err)
}
- issueCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ issueCreate.Pipeline.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ issueCreate.Pipeline.WorkspaceId = api.DEFAULT_WORKPSACE_ID
+ createdPipeline, err := s.PipelineService.CreatePipeline(context.Background(), &issueCreate.Pipeline)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create pipeline for issue").SetInternal(err)
+ }
+
+ for _, stageCreate := range issueCreate.Pipeline.StageList {
+ stageCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ stageCreate.WorkspaceId = api.DEFAULT_WORKPSACE_ID
+ stageCreate.PipelineId = createdPipeline.ID
+ createdStage, err := s.StageService.CreateStage(context.Background(), &stageCreate)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create stage for issue").SetInternal(err)
+ }
+ for _, taskCreate := range stageCreate.TaskList {
+ taskCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ taskCreate.WorkspaceId = api.DEFAULT_WORKPSACE_ID
+ taskCreate.PipelineId = createdPipeline.ID
+ taskCreate.StageId = createdStage.ID
+ _, err := s.TaskService.CreateTask(context.Background(), &taskCreate)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create task for issue").SetInternal(err)
+ }
+ }
+ }
+
+ issueCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ issueCreate.PipelineId = createdPipeline.ID
issue, err := s.IssueService.CreateIssue(context.Background(), issueCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create issue").SetInternal(err)
diff --git a/backend/store/issue.go b/backend/store/issue.go
index ac92a3d856..04f1a1e960 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -181,12 +181,14 @@ func (s *IssueService) createIssue(ctx context.Context, tx *Tx, create *api.Issu
}
issue.SubscriberIdList = []int{}
- for _, item := range strings.Split(idList, ",") {
- oneId, err := strconv.Atoi(item)
- if err != nil {
- s.l.Errorf("Issue Id %d contains invalid subscriber id: %s", issue.ID, item)
+ if idList != "" {
+ for _, item := range strings.Split(idList, ",") {
+ oneId, err := strconv.Atoi(item)
+ if err != nil {
+ s.l.Errorf("Issue Id %d contains invalid subscriber id: %s", issue.ID, item)
+ }
+ issue.SubscriberIdList = append(issue.SubscriberIdList, oneId)
}
- issue.SubscriberIdList = append(issue.SubscriberIdList, oneId)
}
if payload == "" {
@@ -267,12 +269,14 @@ func (s *IssueService) findIssueList(ctx context.Context, tx *Tx, find *api.Issu
}
issue.SubscriberIdList = []int{}
- for _, item := range strings.Split(idList, ",") {
- oneId, err := strconv.Atoi(item)
- if err != nil {
- s.l.Errorf("Issue Id %d contains invalid subscriber id: %s", issue.ID, item)
+ if idList != "" {
+ for _, item := range strings.Split(idList, ",") {
+ oneId, err := strconv.Atoi(item)
+ if err != nil {
+ s.l.Errorf("Issue Id %d contains invalid subscriber id: %s", issue.ID, item)
+ }
+ issue.SubscriberIdList = append(issue.SubscriberIdList, oneId)
}
- issue.SubscriberIdList = append(issue.SubscriberIdList, oneId)
}
if payload == "" {
@@ -359,12 +363,14 @@ func (s *IssueService) patchIssue(ctx context.Context, tx *Tx, patch *api.IssueP
}
issue.SubscriberIdList = []int{}
- for _, item := range strings.Split(idList, ",") {
- oneId, err := strconv.Atoi(item)
- if err != nil {
- s.l.Errorf("Issue Id %d contains invalid subscriber id: %s", issue.ID, item)
+ if idList != "" {
+ for _, item := range strings.Split(idList, ",") {
+ oneId, err := strconv.Atoi(item)
+ if err != nil {
+ s.l.Errorf("Issue Id %d contains invalid subscriber id: %s", issue.ID, item)
+ }
+ issue.SubscriberIdList = append(issue.SubscriberIdList, oneId)
}
- issue.SubscriberIdList = append(issue.SubscriberIdList, oneId)
}
if payload == "" {
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index 77925d1ae3..49a2f19d64 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -96,7 +96,7 @@ func (s *PipelineService) createPipeline(ctx context.Context, tx *Tx, create *ap
name,
`+"`status`"+`
)
- VALUES (?, ?, ?, 'OPEN')
+ VALUES (?, ?, ?, ?, 'OPEN')
RETURNING id, creator_id, created_ts, updater_id, updated_ts, workspace_id, name, `+"`status`"+`
`,
create.CreatorId,
diff --git a/backend/store/task.go b/backend/store/task.go
index 34a3544518..109787b9a0 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -2,6 +2,7 @@ package store
import (
"context"
+ "encoding/json"
"fmt"
"strings"
@@ -104,6 +105,10 @@ func (s *TaskService) PatchTask(ctx context.Context, patch *api.TaskPatch) (*api
// createTask creates a new task.
func (s *TaskService) createTask(ctx context.Context, tx *Tx, create *api.TaskCreate) (*api.Task, error) {
+ newPayload, err := json.Marshal(create.Payload)
+ if err != nil {
+ return nil, FormatError(err)
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO task (
creator_id,
@@ -130,7 +135,7 @@ func (s *TaskService) createTask(ctx context.Context, tx *Tx, create *api.TaskCr
create.Name,
create.Type,
create.When,
- create.Payload,
+ newPayload,
)
if err != nil {
@@ -140,6 +145,7 @@ func (s *TaskService) createTask(ctx context.Context, tx *Tx, create *api.TaskCr
row.Next()
var task api.Task
+ var payload string
if err := row.Scan(
&task.ID,
&task.CreatorId,
@@ -154,11 +160,19 @@ func (s *TaskService) createTask(ctx context.Context, tx *Tx, create *api.TaskCr
&task.Status,
&task.Type,
&task.When,
- &task.Payload,
+ &payload,
); err != nil {
return nil, FormatError(err)
}
+ if payload == "" {
+ task.Payload = api.TaskPayload{}
+ } else {
+ if err := json.Unmarshal([]byte(payload), &task.Payload); err != nil {
+ return nil, FormatError(err)
+ }
+ }
+
return &task, nil
}
@@ -207,6 +221,7 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *Tx, find *api.TaskFi
list := make([]*api.Task, 0)
for rows.Next() {
var task api.Task
+ var payload string
if err := rows.Scan(
&task.ID,
&task.CreatorId,
@@ -221,17 +236,24 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *Tx, find *api.TaskFi
&task.Status,
&task.Type,
&task.When,
- &task.Payload,
+ &payload,
); err != nil {
return nil, FormatError(err)
}
+ if payload == "" {
+ task.Payload = api.TaskPayload{}
+ } else {
+ if err := json.Unmarshal([]byte(payload), &task.Payload); err != nil {
+ return nil, FormatError(err)
+ }
+ }
+
list = append(list, &task)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
-
return list, nil
}
@@ -262,6 +284,7 @@ func (s *TaskService) patchTask(ctx context.Context, tx *Tx, patch *api.TaskPatc
if row.Next() {
var task api.Task
+ var payload string
if err := row.Scan(
&task.ID,
&task.CreatorId,
@@ -276,10 +299,19 @@ func (s *TaskService) patchTask(ctx context.Context, tx *Tx, patch *api.TaskPatc
&task.Status,
&task.Type,
&task.When,
- &task.Payload,
+ &payload,
); err != nil {
return nil, FormatError(err)
}
+
+ if payload == "" {
+ task.Payload = api.TaskPayload{}
+ } else {
+ if err := json.Unmarshal([]byte(payload), &task.Payload); err != nil {
+ return nil, FormatError(err)
+ }
+ }
+
return &task, nil
}