Skip to content

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

Automatically mark issue as DONE if the last task is DONE and assignee is SYSTEM_BOT

天舟e0654c2a0c7c
diff --git a/backend/server/issue.go b/backend/server/issue.go
index e07cd43156..7c3a17f930 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -228,76 +228,14 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID: %v", id)).SetInternal(err)
}
- var pipelineStatus api.PipelineStatus
- pipelinePatch := &api.PipelinePatch{
- ID: issue.PipelineId,
- UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
- }
- switch issueStatusPatch.Status {
- case api.Issue_Open:
- pipelineStatus = api.Pipeline_Open
- case api.Issue_Done:
- // Returns error if any of the tasks is not DONE.
- for _, stage := range issue.Pipeline.StageList {
- for _, task := range stage.TaskList {
- if task.Status != api.TaskDone {
- return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Failed to resolve issue: %v. Task %v has not finished", issue.Name, task.Name))
- }
- }
- }
- 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 {
- if _, err := s.ChangeTaskStatus(context.Background(), task, api.TaskCanceled, c.Get(GetPrincipalIdContextKey()).(int)); 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)
- }
-
- issuePatch := &api.IssuePatch{
- ID: id,
- UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
- Status: &issueStatusPatch.Status,
- }
- updatedIssue, err := s.IssueService.PatchIssue(context.Background(), issuePatch)
+ updatedIssue, err := s.ChangeIssueStatus(context.Background(), issue, issueStatusPatch.Status, issueStatusPatch.UpdaterId, issueStatusPatch.Comment)
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.StatusNotFound).SetInternal(err)
+ } else if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
+ return echo.NewHTTPError(http.StatusConflict).SetInternal(err)
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update issue ID: %v", id)).SetInternal(err)
- }
-
- payload, err := json.Marshal(api.ActivityIssueStatusUpdatePayload{
- OldStatus: issue.Status,
- NewStatus: issueStatusPatch.Status,
- })
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal activity after changing the issue status: %v", issue.Name)).SetInternal(err)
- }
-
- activityCreate := &api.ActivityCreate{
- CreatorId: c.Get(GetPrincipalIdContextKey()).(int),
- ContainerId: issue.ID,
- Type: api.ActivityIssueStatusUpdate,
- Comment: issueStatusPatch.Comment,
- Payload: string(payload),
- }
- _, err = s.ActivityService.CreateActivity(context.Background(), activityCreate)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after changing the issue status: %v", issue.Name)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError).SetInternal(err)
}
if err := s.ComposeIssueRelationship(context.Background(), updatedIssue, c.Get(getIncludeKey()).([]string)); err != nil {
@@ -429,3 +367,79 @@ func (s *Server) CreateIssue(ctx context.Context, issueCreate *api.IssueCreate,
return issue, nil
}
+
+func (s *Server) ChangeIssueStatus(ctx context.Context, issue *api.Issue, newStatus api.IssueStatus, updatorId int, comment string) (*api.Issue, error) {
+ var pipelineStatus api.PipelineStatus
+ switch newStatus {
+ case api.Issue_Open:
+ pipelineStatus = api.Pipeline_Open
+ case api.Issue_Done:
+ // Returns error if any of the tasks is not DONE.
+ for _, stage := range issue.Pipeline.StageList {
+ for _, task := range stage.TaskList {
+ if task.Status != api.TaskDone {
+ return nil, &bytebase.Error{Code: bytebase.ECONFLICT, Message: fmt.Sprintf("failed to resolve issue: %v, task %v has not finished", issue.Name, task.Name)}
+ }
+ }
+ }
+ 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 {
+ if _, err := s.ChangeTaskStatus(context.Background(), task, api.TaskCanceled, updatorId); err != nil {
+ return nil, fmt.Errorf("failed to cancel issue: %v, failed to cancel task: %v, error: %w", issue.Name, task.Name, err)
+ }
+ }
+ }
+ }
+ pipelineStatus = api.Pipeline_Canceled
+ }
+
+ pipelinePatch := &api.PipelinePatch{
+ ID: issue.PipelineId,
+ UpdaterId: updatorId,
+ Status: &pipelineStatus,
+ }
+ if _, err := s.PipelineService.PatchPipeline(context.Background(), pipelinePatch); err != nil {
+ return nil, fmt.Errorf("failed to update issue status: %v, failed to update pipeline status: %w", issue.Name, err)
+ }
+
+ issuePatch := &api.IssuePatch{
+ ID: issue.ID,
+ UpdaterId: updatorId,
+ Status: &newStatus,
+ }
+ updatedIssue, err := s.IssueService.PatchIssue(context.Background(), issuePatch)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return nil, fmt.Errorf("failed to update issue status: %v, error: %w", issue.Name, err)
+ }
+ return nil, fmt.Errorf("failed update issue status: %v, error: %w", issue.Name, err)
+ }
+
+ payload, err := json.Marshal(api.ActivityIssueStatusUpdatePayload{
+ OldStatus: issue.Status,
+ NewStatus: newStatus,
+ })
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal activity after changing the issue status: %v, error: %w", issue.Name, err)
+ }
+
+ activityCreate := &api.ActivityCreate{
+ CreatorId: updatorId,
+ ContainerId: issue.ID,
+ Type: api.ActivityIssueStatusUpdate,
+ Comment: comment,
+ Payload: string(payload),
+ }
+ _, err = s.ActivityService.CreateActivity(context.Background(), activityCreate)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create activity after changing the issue status: %v, error: %w", issue.Name, err)
+ }
+
+ return updatedIssue, nil
+}
diff --git a/backend/server/task.go b/backend/server/task.go
index b125091b41..7823e98e82 100644
--- a/backend/server/task.go
+++ b/backend/server/task.go
@@ -225,5 +225,22 @@ func (s *Server) ChangeTaskStatusWithPatch(ctx context.Context, task *api.Task,
}
}
+ // If this is the last task in the pipeline and just completed, and the assignee is system bot,
+ // then we mark the issue as DONE.
+ if updatedTask.Status == "DONE" && issue.AssigneeId == api.SYSTEM_BOT_ID {
+ issue.Pipeline, err = s.ComposePipelineById(context.Background(), issue.PipelineId, []string{})
+ if err != nil {
+ return nil, fmt.Errorf("failed to fetch pipeline to mark issue %v as DONE after completing task %v", issue.Name, updatedTask.Name)
+ }
+
+ lastStage := issue.Pipeline.StageList[len(issue.Pipeline.StageList)-1]
+ if lastStage.TaskList[len(lastStage.TaskList)-1].ID == task.ID {
+ _, err := s.ChangeIssueStatus(context.Background(), issue, api.Issue_Done, taskStatusPatch.UpdaterId, "")
+ if err != nil {
+ return nil, fmt.Errorf("failed to mark issue %v as DONE after completing task %v", issue.Name, updatedTask.Name)
+ }
+ }
+ }
+
return updatedTask, nil
}
diff --git a/backend/server/task_scheduler.go b/backend/server/task_scheduler.go
index ad78e804ff..ad27ae0515 100644
--- a/backend/server/task_scheduler.go
+++ b/backend/server/task_scheduler.go
@@ -49,11 +49,11 @@ func (s *TaskScheduler) Run() error {
}
pipelineList, err := s.server.PipelineService.FindPipelineList(context.Background(), pipelineFind)
if err != nil {
- s.l.Info(fmt.Sprintf("Failed to retrieve open pipelines: %v\n", err))
+ s.l.Error(fmt.Sprintf("Failed to retrieve open pipelines: %v\n", err))
}
for _, pipeline := range pipelineList {
if err := s.server.ComposePipelineRelationship(context.Background(), pipeline, []string{}); err != nil {
- s.l.Info(fmt.Sprintf("Failed to fetch stage info for pipeline: %v. Error: %v", pipeline.Name, err))
+ s.l.Error(fmt.Sprintf("Failed to fetch stage info for pipeline: %v. Error: %v", pipeline.Name, err))
continue
}
@@ -63,7 +63,7 @@ func (s *TaskScheduler) Run() error {
if task.Status == api.TaskPending {
_, err = s.Schedule(context.Background(), task)
if err != nil {
- s.l.Info(fmt.Sprintf("Failed to schedule next running task: %v. Error: %v", task.Name, err))
+ s.l.Error(fmt.Sprintf("Failed to schedule next running task: %v. Error: %v", task.Name, err))
}
}
goto PIPELINE_END
@@ -80,13 +80,13 @@ func (s *TaskScheduler) Run() error {
}
taskList, err := s.server.TaskService.FindTaskList(context.Background(), taskFind)
if err != nil {
- s.l.Info(fmt.Sprintf("Failed to retrieve running tasks: %v\n", err))
+ s.l.Error(fmt.Sprintf("Failed to retrieve running tasks: %v\n", err))
}
for _, task := range taskList {
executor, ok := s.executors[string(task.Type)]
if !ok {
- s.l.Info(fmt.Sprintf("Unknown task type: %v. Skip", task.Type))
+ s.l.Error(fmt.Sprintf("Unknown task type: %v. Skip", task.Type))
continue
}
@@ -105,7 +105,14 @@ func (s *TaskScheduler) Run() error {
}
s.l.Info(fmt.Sprintf("Task completed '%v(%v)'\n", task.Name, task.ID))
- s.server.ChangeTaskStatus(context.Background(), task, api.TaskDone, api.SYSTEM_BOT_ID)
+ _, err = s.server.ChangeTaskStatus(context.Background(), task, api.TaskDone, api.SYSTEM_BOT_ID)
+ if err != nil {
+ s.l.Error("Failed to mark task as DONE",
+ zap.Error(err),
+ zap.Int("task_id", task.ID),
+ zap.String("task_name", task.Name),
+ )
+ }
}
}
}()
diff --git a/frontend/src/components/IssueStatusTransitionButtonGroup.vue b/frontend/src/components/IssueStatusTransitionButtonGroup.vue
index 633a53cf59..76b5ddd5d7 100644
--- a/frontend/src/components/IssueStatusTransitionButtonGroup.vue
+++ b/frontend/src/components/IssueStatusTransitionButtonGroup.vue
@@ -317,7 +317,7 @@ export default {
}
const taskList = allTaskList(pipeline);
- // Don't display the Resolve action if the last task is NOT in end status.
+ // Don't display the Resolve action if the last task is NOT in DONE status.
if (
item == "RESOLVE" &&
taskList.length > 0 &&