Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: remove the batch ids from patch task status (#4004)
Danny Xue0d9af343299
diff --git a/api/task.go b/api/task.goindex 6eff26fe24..c1004e1116 100644--- a/api/task.go+++ b/api/task.go@@ -332,7 +332,7 @@ type TaskPatch struct {// TaskStatusPatch is the API message for patching a task status.type TaskStatusPatch struct {- IDList []int+ ID int// Standard fields// Value is assigned from the jwt subject field passed by the client.diff --git a/server/runner/taskrun/scheduler.go b/server/runner/taskrun/scheduler.goindex aff08c7bbd..a3798f589b 100644--- a/server/runner/taskrun/scheduler.go+++ b/server/runner/taskrun/scheduler.go@@ -251,7 +251,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {code := common.ErrorCode(err)result := string(bytes)taskStatusPatch := &api.TaskStatusPatch{- IDList: []int{task.ID},+ ID: task.ID,UpdaterID: api.SystemBotID,Status: api.TaskFailed,Code: &code,@@ -279,7 +279,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {code := common.Okresult := string(bytes)taskStatusPatch := &api.TaskStatusPatch{- IDList: []int{task.ID},+ ID: task.ID,UpdaterID: api.SystemBotID,Status: api.TaskDone,Code: &code,@@ -359,7 +359,7 @@ func (s *Scheduler) PatchTaskStatement(ctx context.Context, task *api.Task, task// TODO(d): revisit this as task pending is only a short-period of time.if taskPatched.Status == api.TaskPending {t, err := s.PatchTaskStatus(ctx, taskPatched, &api.TaskStatusPatch{- IDList: []int{taskPatch.ID},+ ID: taskPatch.ID,UpdaterID: taskPatch.UpdaterID,Status: api.TaskPendingApproval,})@@ -853,7 +853,7 @@ func (s *Scheduler) scheduleIfNeeded(ctx context.Context, task *api.Task) error}if _, err := s.PatchTaskStatus(ctx, task, &api.TaskStatusPatch{- IDList: []int{task.ID},+ ID: task.ID,UpdaterID: api.SystemBotID,Status: api.TaskRunning,}); err != nil {@@ -906,7 +906,7 @@ func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {}if ok {if _, err := s.PatchTaskStatus(ctx, task, &api.TaskStatusPatch{- IDList: []int{task.ID},+ ID: task.ID,UpdaterID: api.SystemBotID,Status: api.TaskPending,}); err != nil {@@ -972,10 +972,6 @@ func (s *Scheduler) PatchTaskStatus(ctx context.Context, task *api.Task, taskSta}}- if len(taskStatusPatch.IDList) != 1 {- return nil, errors.Errorf("expect to patch 1 task, get %d", len(taskStatusPatch.IDList))- }-if taskStatusPatch.Status == api.TaskCanceled {if !taskCancellationImplemented[task.Type] {return nil, common.Errorf(common.NotImplemented, "Canceling task type %s is not supported", task.Type)@@ -1261,7 +1257,7 @@ func (s *Scheduler) ChangeIssueStatus(ctx context.Context, issue *api.Issue, newfor _, task := range stage.TaskList {if task.Status == api.TaskRunning {if _, err := s.PatchTaskStatus(ctx, task, &api.TaskStatusPatch{- IDList: []int{task.ID},+ ID: task.ID,UpdaterID: updaterID,Status: api.TaskCanceled,}); err != nil {@@ -1443,6 +1439,7 @@ func (s *Scheduler) onTaskPatched(ctx context.Context, issue *api.Issue, taskPatreturn nil}+func (s *Scheduler) onTaskPatchedWithoutIssue(ctx context.Context, taskPatched *api.Task) error {pipeline, err := s.store.GetPipelineByID(ctx, taskPatched.PipelineID)if err != nil {diff --git a/server/task.go b/server/task.goindex 3d156a9a56..44c232b190 100644--- a/server/task.go+++ b/server/task.go@@ -125,7 +125,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {currentPrincipalID := c.Get(getPrincipalIDContextKey()).(int)taskStatusPatch := &api.TaskStatusPatch{- IDList: []int{taskID},+ ID: taskID,UpdaterID: currentPrincipalID,}if err := jsonapi.UnmarshalPayload(c.Request().Body, taskStatusPatch); err != nil {diff --git a/store/task.go b/store/task.goindex 6902fb91f0..e6dab8090e 100644--- a/store/task.go+++ b/store/task.go@@ -710,67 +710,65 @@ func (s *Store) patchTaskStatusImpl(ctx context.Context, tx *Tx, patch *api.Task// Updates the corresponding task run if applicable.// We update the task run first because updating task below returns row and it's a bit complicated to// arrange code to prevent that opening row interfering with the task run update.- for _, id := range patch.IDList {- taskFind := &api.TaskFind{- ID: &id,- }- taskRawObj, err := s.getTaskRawTx(ctx, tx, taskFind)- if err != nil {- return nil, err- }- if taskRawObj == nil {- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("task ID not found: %d", id)}- }+ taskFind := &api.TaskFind{+ ID: &patch.ID,+ }+ taskRawObj, err := s.getTaskRawTx(ctx, tx, taskFind)+ if err != nil {+ return nil, err+ }+ if taskRawObj == nil {+ return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("task ID not found: %d", patch.ID)}+ }- taskRunFind := &api.TaskRunFind{- TaskID: &taskRawObj.ID,- StatusList: &[]api.TaskRunStatus{- api.TaskRunRunning,- },- }- taskRunRaw, err := s.getTaskRunRawTx(ctx, tx, taskRunFind)- if err != nil {- return nil, err- }- if taskRunRaw == nil {- if patch.Status == api.TaskRunning {- taskRunCreate := &api.TaskRunCreate{- CreatorID: patch.UpdaterID,- TaskID: taskRawObj.ID,- Name: fmt.Sprintf("%s %d", taskRawObj.Name, time.Now().Unix()),- Type: taskRawObj.Type,- Payload: taskRawObj.Payload,- }- // insert a running taskRun- if _, err := s.createTaskRunImpl(ctx, tx, taskRunCreate); err != nil {- return nil, err- }- }- } else {- if patch.Status == api.TaskRunning {- return nil, errors.Errorf("task is already running: %v", taskRawObj.Name)- }- taskRunStatusPatch := &api.TaskRunStatusPatch{- ID: &taskRunRaw.ID,- UpdaterID: patch.UpdaterID,- Code: patch.Code,- Result: patch.Result,- Comment: patch.Comment,- }- switch patch.Status {- case api.TaskDone:- taskRunStatusPatch.Status = api.TaskRunDone- case api.TaskFailed:- taskRunStatusPatch.Status = api.TaskRunFailed- case api.TaskPending:- case api.TaskPendingApproval:- case api.TaskCanceled:- taskRunStatusPatch.Status = api.TaskRunCanceled+ taskRunFind := &api.TaskRunFind{+ TaskID: &taskRawObj.ID,+ StatusList: &[]api.TaskRunStatus{+ api.TaskRunRunning,+ },+ }+ taskRunRaw, err := s.getTaskRunRawTx(ctx, tx, taskRunFind)+ if err != nil {+ return nil, err+ }+ if taskRunRaw == nil {+ if patch.Status == api.TaskRunning {+ taskRunCreate := &api.TaskRunCreate{+ CreatorID: patch.UpdaterID,+ TaskID: taskRawObj.ID,+ Name: fmt.Sprintf("%s %d", taskRawObj.Name, time.Now().Unix()),+ Type: taskRawObj.Type,+ Payload: taskRawObj.Payload,}- if _, err := s.patchTaskRunStatusImpl(ctx, tx, taskRunStatusPatch); err != nil {+ // insert a running taskRun+ if _, err := s.createTaskRunImpl(ctx, tx, taskRunCreate); err != nil {return nil, err}}+ } else {+ if patch.Status == api.TaskRunning {+ return nil, errors.Errorf("task is already running: %v", taskRawObj.Name)+ }+ taskRunStatusPatch := &api.TaskRunStatusPatch{+ ID: &taskRunRaw.ID,+ UpdaterID: patch.UpdaterID,+ Code: patch.Code,+ Result: patch.Result,+ Comment: patch.Comment,+ }+ switch patch.Status {+ case api.TaskDone:+ taskRunStatusPatch.Status = api.TaskRunDone+ case api.TaskFailed:+ taskRunStatusPatch.Status = api.TaskRunFailed+ case api.TaskPending:+ case api.TaskPendingApproval:+ case api.TaskCanceled:+ taskRunStatusPatch.Status = api.TaskRunCanceled+ }+ if _, err := s.patchTaskRunStatusImpl(ctx, tx, taskRunStatusPatch); err != nil {+ return nil, err+ }}// Updates the task@@ -788,16 +786,11 @@ func (s *Store) patchTaskStatusImpl(ctx context.Context, tx *Tx, patch *api.Taskset = append(set, fmt.Sprintf(`payload = payload || %s`, strings.Join(payloadSet, "||")))}- var ids []string- for _, id := range patch.IDList {- ids = append(ids, strconv.Itoa(id))- }-// Execute update query with RETURNING.rows, err := tx.QueryContext(ctx, `UPDATE taskSET `+strings.Join(set, ", ")+`- WHERE id in (`+strings.Join(ids, ",")+`)+ WHERE id = `+fmt.Sprintf("%d", patch.ID)+`RETURNING id, creator_id, created_ts, updater_id, updated_ts, pipeline_id, stage_id, instance_id, database_id, name, status, type, payload, earliest_allowed_ts`,args...,diff --git a/tests/schema_update_test.go b/tests/schema_update_test.goindex fad3f3cdde..50ff44d1a3 100644--- a/tests/schema_update_test.go+++ b/tests/schema_update_test.go@@ -558,7 +558,7 @@ func TestVCS(t *testing.T) {task := stage.TaskList[0]// simulate retrying the failed task._, err = ctl.patchTaskStatus(api.TaskStatusPatch{- IDList: []int{task.ID},+ ID: task.ID,UpdaterID: api.SystemBotID,Status: api.TaskPendingApproval,}, issue.PipelineID, task.ID)