Skip to content

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

Implement change issue status

天舟287677dd939a
diff --git a/backend/api/issue.go b/backend/api/issue.go
index 76d344edbd..0bccb7f4d5 100644
--- a/backend/api/issue.go
+++ b/backend/api/issue.go
@@ -121,13 +121,28 @@ type IssuePatch struct {
ProjectId *int `jsonapi:"attr,projectId"`
// Domain specific fields
- Name *string `jsonapi:"attr,name"`
+ Name *string `jsonapi:"attr,name"`
+ // Status is only set manualy via IssueStatusPatch
+ Status *IssueStatus
Description *string `jsonapi:"attr,description"`
AssigneeId *int `jsonapi:"attr,assigneeId"`
SubscriberIdList *[]int `jsonapi:"attr,subscriberIdList"`
Payload *string `jsonapi:"attr,payload"`
}
+type IssueStatusPatch struct {
+ ID int `jsonapi:"primary,issueStatusPatch"`
+
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ UpdaterId int
+ WorkspaceId int
+
+ // Domain specific fields
+ Status *string `jsonapi:"attr,status"`
+ Comment *string `jsonapi:"attr,comment"`
+}
+
type IssueService interface {
CreateIssue(ctx context.Context, create *IssueCreate) (*Issue, error)
FindIssueList(ctx context.Context, find *IssueFind) ([]*Issue, error)
diff --git a/backend/api/task.go b/backend/api/task.go
index be3adb2aaf..ab64a8c280 100644
--- a/backend/api/task.go
+++ b/backend/api/task.go
@@ -5,11 +5,12 @@ import "context"
type TaskStatus string
const (
- TaskPending TaskStatus = "PENDING"
- TaskRunning TaskStatus = "RUNNING"
- TaskDone TaskStatus = "DONE"
- TaskFailed TaskStatus = "FAILED"
- TaskSkipped TaskStatus = "SKIPPED"
+ TaskPending TaskStatus = "PENDING"
+ TaskRunning TaskStatus = "RUNNING"
+ TaskDone TaskStatus = "DONE"
+ TaskFailed TaskStatus = "FAILED"
+ TaskCanceled TaskStatus = "CANCELED"
+ TaskSkipped TaskStatus = "SKIPPED"
)
func (e TaskStatus) String() string {
@@ -22,12 +23,18 @@ func (e TaskStatus) String() string {
return "DONE"
case TaskFailed:
return "FAILED"
+ case TaskCanceled:
+ return "CANCELED"
case TaskSkipped:
return "SKIPPED"
}
return "UNKNOWN"
}
+func (e TaskStatus) IsEndStatus() bool {
+ return e == TaskDone || e == TaskSkipped
+}
+
type TaskType string
const (
diff --git a/backend/server/issue.go b/backend/server/issue.go
index c886e5f7e8..0d52dbf57e 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
- "sort"
"strconv"
"github.com/bytebase/bytebase"
@@ -112,6 +111,101 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
}
return nil
})
+
+ g.PATCH("/issue/:issueId/status", func(c echo.Context) error {
+ id, err := strconv.Atoi(c.Param("issueId"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
+ }
+
+ issueStatusPatch := &api.IssueStatusPatch{
+ ID: id,
+ WorkspaceId: api.DEFAULT_WORKPSACE_ID,
+ UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
+ }
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, issueStatusPatch); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted update issue status request").SetInternal(err)
+ }
+
+ if issueStatusPatch.Status != nil {
+ issue, err := s.ComposeIssueById(context.Background(), id, c.Get(getIncludeKey()).([]string))
+ if err != nil {
+ return err
+ }
+
+ var pipelineStatus api.PipelineStatus
+ pipelinePatch := &api.PipelinePatch{
+ ID: issue.PipelineId,
+ WorkspaceId: api.DEFAULT_WORKPSACE_ID,
+ UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
+ }
+ switch api.IssueStatus(*issueStatusPatch.Status) {
+ case api.Issue_Open:
+ pipelineStatus = api.Pipeline_Open
+ case api.Issue_Done:
+ // Returns error if any of the tasks is not in the end status.
+ for _, stage := range issue.Pipeline.StageList {
+ for _, task := range stage.TaskList {
+ if task.Status.IsEndStatus() {
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Failed to resolve issue: %v. Task %v is in %v status.", issue.Name, task.Name, task.Status))
+ }
+ }
+ }
+ pipelineStatus = api.Pipeline_Done
+ case api.Issue_Canceled:
+ // If we want to cancel the issue, we find the current running tasks, mark each of them CANCELED.
+ // We keep PENDING and FAILED tasks as is since the issue maybe reopened later, and it's better to
+ // keep those tasks in the same state before the issue was canceled.
+ for _, stage := range issue.Pipeline.StageList {
+ for _, task := range stage.TaskList {
+ if task.Status == api.TaskRunning {
+ taskStatus := api.TaskCanceled
+ taskPatch := &api.TaskPatch{
+ ID: id,
+ WorkspaceId: api.DEFAULT_WORKPSACE_ID,
+ UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
+ Status: &taskStatus,
+ }
+ if _, err := s.TaskService.PatchTask(context.Background(), taskPatch); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to cancel issue: %v. Failed to cancel task: %v.", issue.Name, task.Name)).SetInternal(err)
+ }
+ }
+ }
+ }
+ pipelineStatus = api.Pipeline_Canceled
+ }
+
+ pipelinePatch.Status = &pipelineStatus
+ if _, err := s.PipelineService.PatchPipeline(context.Background(), pipelinePatch); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update issue: %v", id)).SetInternal(err)
+ }
+ }
+
+ issueStatus := api.IssueStatus(*issueStatusPatch.Status)
+ issuePatch := &api.IssuePatch{
+ ID: id,
+ WorkspaceId: api.DEFAULT_WORKPSACE_ID,
+ UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
+ Status: &issueStatus,
+ }
+ updatedIssue, err := s.IssueService.PatchIssue(context.Background(), issuePatch)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Issue ID not found: %d", id))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update issue ID: %v", id)).SetInternal(err)
+ }
+
+ if err := s.ComposeIssueRelationship(context.Background(), updatedIssue, c.Get(getIncludeKey()).([]string)); err != nil {
+ return err
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, updatedIssue); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal issue ID response: %v", id)).SetInternal(err)
+ }
+ return nil
+ })
}
func (s *Server) ComposeIssueById(ctx context.Context, id int, includeList []string) (*api.Issue, error) {
@@ -135,6 +229,7 @@ func (s *Server) ComposeIssueById(ctx context.Context, id int, includeList []str
func (s *Server) ComposeIssueRelationship(ctx context.Context, issue *api.Issue, includeList []string) error {
var err error
+
issue.Creator, err = s.ComposePrincipalById(context.Background(), issue.CreatorId, includeList)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch creator for issue: %v", issue.Name)).SetInternal(err)
@@ -150,18 +245,14 @@ func (s *Server) ComposeIssueRelationship(ctx context.Context, issue *api.Issue,
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch assignee for issue: %v", issue.Name)).SetInternal(err)
}
- if sort.SearchStrings(includeList, "project") >= 0 {
- issue.Project, err = s.ComposeProjectlById(context.Background(), issue.ProjectId, includeList)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project for issue: %v", issue.Name)).SetInternal(err)
- }
+ issue.Project, err = s.ComposeProjectlById(context.Background(), issue.ProjectId, includeList)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project for issue: %v", issue.Name)).SetInternal(err)
}
- if sort.SearchStrings(includeList, "pipeline") >= 0 {
- issue.Pipeline, err = s.ComposePipelineById(context.Background(), issue.PipelineId, includeList)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch pipeline for issue: %v", issue.Name)).SetInternal(err)
- }
+ issue.Pipeline, err = s.ComposePipelineById(context.Background(), issue.PipelineId, includeList)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch pipeline for issue: %v", issue.Name)).SetInternal(err)
}
return nil
diff --git a/backend/store/issue.go b/backend/store/issue.go
index e1b1d40d1b..86167849eb 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -274,6 +274,9 @@ func (s *IssueService) patchIssue(ctx context.Context, tx *Tx, patch *api.IssueP
if v := patch.Name; v != nil {
set, args = append(set, "name = ?"), append(args, *v)
}
+ if v := patch.Status; v != nil {
+ set, args = append(set, "`status` = ?"), append(args, api.IssueStatus(*v))
+ }
if v := patch.Description; v != nil {
set, args = append(set, "description = ?"), append(args, *v)
}
diff --git a/frontend/src/miragejs/routes/issue.js b/frontend/src/miragejs/routes/issue.js
index ef5ac0bf29..4cd9e1d87b 100644
--- a/frontend/src/miragejs/routes/issue.js
+++ b/frontend/src/miragejs/routes/issue.js
@@ -278,11 +278,6 @@ export default function configureIssue(route) {
const ts = Date.now();
const pipeline = schema.pipelines.find(issue.pipelineId);
- // Pipeline and issue status is 1-to-1 mapping, so we just change the pipeline status accordingly.
- pipeline.update({
- status: attrs.status,
- });
-
const stageList = schema.stages.where({ pipelineId: pipeline.id }).models;
if (attrs.status == "DONE") {
// Returns error if any of the tasks is not in the end status.
@@ -293,13 +288,9 @@ export default function configureIssue(route) {
}).models;
for (let j = 0; j < taskList.length; j++) {
- if (
- taskList[j].status != "DONE" &&
- taskList[j].status != "CANCELED" &&
- taskList[j].status != "SKIPPED"
- ) {
+ if (taskList[j].status != "DONE" && taskList[j].status != "SKIPPED") {
return new Response(
- 404,
+ 409,
{},
{
errors: `Can't resolve issue ${issue.name}. Task ${taskList[j].name} in stage ${stageList[i].name} is in ${taskList[j].status} status`,
@@ -312,32 +303,25 @@ export default function configureIssue(route) {
pipeline.update({ status: "DONE" });
}
- // If issue is canceled, we find the current running stages and tasks, mark each of them CANCELED.
- // We keep PENDING stages and tasks as is since the issue maybe reopened later, and it's better to
+ // If issue is canceled, we find the current running tasks, mark each of them CANCELED.
+ // We keep PENDING tasks as is since the issue maybe reopened later, and it's better to
// keep them in the state before it was canceled.
if (attrs.status == "CANCELED") {
- pipeline.update({ status: "CAMCELED" });
-
for (let i = 0; i < stageList.length; i++) {
- if (stageList[i].status == "RUNNING") {
- schema.stages.find(stageList[i].id).update({
- status: "CANCELED",
- });
+ const taskList = schema.tasks.where({
+ issueId: issue.id,
+ stageId: stageList[i].id,
+ }).models;
- const taskList = schema.tasks.where({
- issueId: issue.id,
- stageId: stageList[i].id,
- }).models;
-
- for (let j = 0; j < taskList.length; j++) {
- if (taskList[j].status == "RUNNING") {
- schema.tasks.find(taskList[j].id).update({
- status: "CANCELED",
- });
- }
+ for (let j = 0; j < taskList.length; j++) {
+ if (taskList[j].status == "RUNNING") {
+ schema.tasks.find(taskList[j].id).update({
+ status: "CANCELED",
+ });
}
}
}
+ pipeline.update({ status: "CAMCELED" });
}
// If issue is opened, we just move the pipeline to the PENDING status.