Skip to content

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

feat(store): ListTaskRuns (#7078)

p0ny15bcdcd01fd6
diff --git a/backend/store/issue.go b/backend/store/issue.go
index 55cadeb03c..eeaf323662 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -366,7 +366,7 @@ func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage)
composedTask.Updater = updater
for _, taskRun := range taskRuns {
- if taskRun.TaskID == task.ID {
+ if taskRun.TaskUID == task.ID {
composedTaskRun := taskRun.toTaskRun()
creator, err := s.GetPrincipalByID(ctx, composedTaskRun.CreatorID)
if err != nil {
diff --git a/backend/store/task.go b/backend/store/task.go
index 41224f6649..51176e1579 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -547,9 +547,9 @@ func (s *Store) UpdateTaskStatusV2(ctx context.Context, patch *api.TaskStatusPat
if taskRun == nil {
if patch.Status == api.TaskRunning {
if err := s.createTaskRunImpl(ctx, tx, &TaskRunMessage{
- TaskID: task.ID,
- Name: fmt.Sprintf("%s %d", task.Name, time.Now().Unix()),
- Type: task.Type,
+ TaskUID: task.ID,
+ Name: fmt.Sprintf("%s %d", task.Name, time.Now().Unix()),
+ Type: task.Type,
}, patch.UpdaterID); err != nil {
return nil, err
}
@@ -559,7 +559,7 @@ func (s *Store) UpdateTaskStatusV2(ctx context.Context, patch *api.TaskStatusPat
return nil, errors.Errorf("task is already running: %v", task.Name)
}
taskRunStatusPatch := &TaskRunStatusPatch{
- ID: &taskRun.ID,
+ ID: taskRun.ID,
UpdaterID: patch.UpdaterID,
Code: patch.Code,
Result: patch.Result,
diff --git a/backend/store/task_run.go b/backend/store/task_run.go
index 8586855af8..a8515bd2e1 100644
--- a/backend/store/task_run.go
+++ b/backend/store/task_run.go
@@ -14,14 +14,16 @@ import (
// TaskRunMessage is message for task run.
type TaskRunMessage struct {
- TaskID int
- Name string
- Status api.TaskRunStatus
- Type api.TaskType
- Code common.Code
- Comment string
- Result string
- Payload string
+ TaskUID int
+ StageUID int
+ PipelineUID int
+ Name string
+ Status api.TaskRunStatus
+ Type api.TaskType
+ Code common.Code
+ Comment string
+ Result string
+ Payload string
// Output only.
ID int
@@ -31,6 +33,14 @@ type TaskRunMessage struct {
UpdatedTs int64
}
+// FindTaskRunMessage is the message for finding task runs.
+type FindTaskRunMessage struct {
+ UID *int
+ TaskUID *int
+ StageUID *int
+ PipelineUID *int
+}
+
// TaskRunCreate is the API message for creating a task run.
type TaskRunCreate struct {
CreatorID int
@@ -53,14 +63,11 @@ type TaskRunFind struct {
// TaskRunStatusPatch is the API message for patching a task run.
type TaskRunStatusPatch struct {
- ID *int
+ ID int
// Standard fields
UpdaterID int
- // Related fields
- TaskID *int
-
// Domain specific fields
Status api.TaskRunStatus
Code *common.Code
@@ -76,7 +83,7 @@ func (taskRun *TaskRunMessage) toTaskRun() *api.TaskRun {
CreatedTs: taskRun.CreatedTs,
UpdaterID: taskRun.UpdaterID,
UpdatedTs: taskRun.UpdatedTs,
- TaskID: taskRun.TaskID,
+ TaskID: taskRun.TaskUID,
Name: taskRun.Name,
Status: taskRun.Status,
Type: taskRun.Type,
@@ -87,6 +94,81 @@ func (taskRun *TaskRunMessage) toTaskRun() *api.TaskRun {
}
}
+// ListTaskRuns lists task runs.
+func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*TaskRunMessage, error) {
+ where, args := []string{"TRUE"}, []any{}
+ if v := find.UID; v != nil {
+ where, args = append(where, fmt.Sprintf("task_run.id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.TaskUID; v != nil {
+ where, args = append(where, fmt.Sprintf("task_run.task_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.StageUID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.stage_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.PipelineUID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)
+ }
+
+ rows, err := s.db.db.QueryContext(ctx, fmt.Sprintf(`
+ SELECT
+ task_run.id,
+ task_run.creator_id,
+ task_run.created_ts,
+ task_run.updater_id,
+ task_run.updated_ts,
+ task_run.task_id,
+ task_run.name,
+ task_run.status,
+ task_run.type,
+ task_run.code,
+ task_run.comment,
+ task_run.result,
+ task_run.payload,
+ task.pipeline_id,
+ task.stage_id
+ FROM task_run
+ JOIN task ON task.id = task_run.task_id
+ WHERE %s`, strings.Join(where, " AND ")),
+ args...,
+ )
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ var taskRuns []*TaskRunMessage
+ for rows.Next() {
+ var taskRun TaskRunMessage
+ if err := rows.Scan(
+ &taskRun.ID,
+ &taskRun.CreatorID,
+ &taskRun.CreatedTs,
+ &taskRun.UpdaterID,
+ &taskRun.UpdatedTs,
+ &taskRun.TaskUID,
+ &taskRun.Name,
+ &taskRun.Status,
+ &taskRun.Type,
+ &taskRun.Code,
+ &taskRun.Comment,
+ &taskRun.Result,
+ &taskRun.Payload,
+ &taskRun.PipelineUID,
+ &taskRun.StageUID,
+ ); err != nil {
+ return nil, err
+ }
+
+ taskRuns = append(taskRuns, &taskRun)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+
+ return taskRuns, nil
+}
+
// createTaskRunImpl creates a new taskRun.
func (*Store) createTaskRunImpl(ctx context.Context, tx *Tx, create *TaskRunMessage, creatorID int) error {
if create.Payload == "" {
@@ -106,7 +188,7 @@ func (*Store) createTaskRunImpl(ctx context.Context, tx *Tx, create *TaskRunMess
if _, err := tx.ExecContext(ctx, query,
creatorID,
creatorID,
- create.TaskID,
+ create.TaskUID,
create.Name,
api.TaskRunRunning,
create.Type,
@@ -150,12 +232,7 @@ func (*Store) patchTaskRunStatusImpl(ctx context.Context, tx *Tx, patch *TaskRun
// Build WHERE clause.
where := []string{"TRUE"}
- if v := patch.ID; v != nil {
- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
- }
- if v := patch.TaskID; v != nil {
- where, args = append(where, fmt.Sprintf("task_id = $%d", len(args)+1)), append(args, *v)
- }
+ where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, patch.ID)
var taskRun TaskRunMessage
if err := tx.QueryRowContext(ctx, `
@@ -171,7 +248,7 @@ func (*Store) patchTaskRunStatusImpl(ctx context.Context, tx *Tx, patch *TaskRun
&taskRun.CreatedTs,
&taskRun.UpdaterID,
&taskRun.UpdatedTs,
- &taskRun.TaskID,
+ &taskRun.TaskUID,
&taskRun.Name,
&taskRun.Status,
&taskRun.Type,
@@ -209,7 +286,6 @@ func (s *Store) ListTaskRun(ctx context.Context, find *TaskRunFind) ([]*TaskRunM
}
func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([]*TaskRunMessage, error) {
- joinClause := ""
where, args := []string{"TRUE"}, []any{}
if v := find.TaskID; v != nil {
where, args = append(where, fmt.Sprintf("task_run.task_id = $%d", len(args)+1)), append(args, *v)
@@ -220,9 +296,6 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([
if v := find.PipelineID; v != nil {
where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)
}
- if find.StageID != nil || find.PipelineID != nil {
- joinClause = "JOIN task ON task.id = task_run.task_id"
- }
if v := find.StatusList; v != nil {
list := []string{}
@@ -230,7 +303,7 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([
list = append(list, fmt.Sprintf("$%d", len(args)+1))
args = append(args, status)
}
- where = append(where, fmt.Sprintf("status in (%s)", strings.Join(list, ",")))
+ where = append(where, fmt.Sprintf("task_run.status in (%s)", strings.Join(list, ",")))
}
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
@@ -247,10 +320,12 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([
task_run.code,
task_run.comment,
task_run.result,
- task_run.payload
+ task_run.payload,
+ task.pipeline_id,
+ task.stage_id
FROM task_run
- %s
- WHERE %s`, joinClause, strings.Join(where, " AND ")),
+ JOIN task ON task.id = task_run.task_id
+ WHERE %s`, strings.Join(where, " AND ")),
args...,
)
if err != nil {
@@ -267,7 +342,7 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([
&taskRun.CreatedTs,
&taskRun.UpdaterID,
&taskRun.UpdatedTs,
- &taskRun.TaskID,
+ &taskRun.TaskUID,
&taskRun.Name,
&taskRun.Status,
&taskRun.Type,
@@ -275,6 +350,8 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([
&taskRun.Comment,
&taskRun.Result,
&taskRun.Payload,
+ &taskRun.PipelineUID,
+ &taskRun.StageUID,
); err != nil {
return nil, err
}