Semaphore/db/sql/task.go

151 lines
3.4 KiB
Go
Raw Normal View History

2021-03-12 18:41:41 +01:00
package sql
import (
"database/sql"
"github.com/Masterminds/squirrel"
"github.com/ansible-semaphore/semaphore/db"
2021-03-12 18:41:41 +01:00
)
func (d *SqlDb) CreateTaskStage(stage db.TaskStage) (db.TaskStage, error) {
_, err := d.exec(
"insert into task__stage (task_id, type) VALUES (?, ?, ?, ?)",
stage.TaskID,
stage.Type,
stage.Start)
return stage, err
}
func (d *SqlDb) GetTaskStages(projectID int, taskID int) ([]db.TaskStage, error) {
return nil, nil
}
2021-03-12 18:41:41 +01:00
func (d *SqlDb) CreateTask(task db.Task) (db.Task, error) {
err := d.sql.Insert(&task)
return task, err
}
func (d *SqlDb) UpdateTask(task db.Task) error {
2021-08-24 17:20:34 +02:00
_, err := d.exec(
"update task set status=?, start=?, `end`=? where id=?",
2021-03-12 18:41:41 +01:00
task.Status,
task.Start,
task.End,
task.ID)
return err
}
func (d *SqlDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
2021-08-24 17:20:34 +02:00
_, err := d.exec(
2021-03-12 18:41:41 +01:00
"insert into task__output (task_id, task, output, time) VALUES (?, '', ?, ?)",
output.TaskID,
output.Output,
output.Time)
return output, err
}
func (d *SqlDb) getTasks(projectID int, templateID *int, params db.RetrieveQueryParams, tasks *[]db.TaskWithTpl) (err error) {
2021-10-12 15:59:16 +02:00
fields := "task.*"
fields += ", tpl.playbook as tpl_playbook" +
", `user`.name as user_name" +
2022-02-03 08:05:13 +01:00
", tpl.name as tpl_alias" +
", tpl.type as tpl_type"
2021-10-12 15:59:16 +02:00
q := squirrel.Select(fields).
2021-03-12 18:41:41 +01:00
From("task").
Join("project__template as tpl on task.template_id=tpl.id").
LeftJoin("`user` on task.user_id=`user`.id").
2021-03-12 18:41:41 +01:00
OrderBy("task.created desc, id desc")
if templateID == nil {
q = q.Where("tpl.project_id=?", projectID)
} else {
q = q.Where("tpl.project_id=? AND task.template_id=?", projectID, templateID)
}
if params.Count > 0 {
q = q.Limit(uint64(params.Count))
}
query, args, _ := q.ToSql()
2021-10-12 15:59:16 +02:00
_, err = d.selectAll(tasks, query, args...)
2021-03-12 18:41:41 +01:00
for i := range *tasks {
err = (*tasks)[i].Fill(d)
if err != nil {
return
}
}
2021-03-12 18:41:41 +01:00
return
}
func (d *SqlDb) GetTask(projectID int, taskID int) (task db.Task, err error) {
q := squirrel.Select("task.*").
From("task").
Join("project__template as tpl on task.template_id=tpl.id").
Where("tpl.project_id=? AND task.id=?", projectID, taskID)
query, args, err := q.ToSql()
if err != nil {
return
}
2021-08-24 17:20:34 +02:00
err = d.selectOne(&task, query, args...)
2021-03-12 18:41:41 +01:00
if err == sql.ErrNoRows {
err = db.ErrNotFound
return
}
if err != nil {
return
2021-03-12 18:41:41 +01:00
}
return
}
func (d *SqlDb) GetTemplateTasks(projectID int, templateID int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
err = d.getTasks(projectID, &templateID, params, &tasks)
2021-10-12 15:59:16 +02:00
return
2021-03-12 18:41:41 +01:00
}
2021-10-12 15:59:16 +02:00
func (d *SqlDb) GetProjectTasks(projectID int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
err = d.getTasks(projectID, nil, params, &tasks)
return
2021-03-12 18:41:41 +01:00
}
func (d *SqlDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) {
2021-03-12 21:20:18 +01:00
// check if task exists in the project
2021-03-12 18:41:41 +01:00
_, err = d.GetTask(projectID, taskID)
if err != nil {
return
}
2021-08-24 17:20:34 +02:00
_, err = d.exec("delete from task__output where task_id=?", taskID)
2021-03-12 18:41:41 +01:00
if err != nil {
return
}
2021-08-24 17:20:34 +02:00
_, err = d.exec("delete from task where id=?", taskID)
2021-03-12 18:41:41 +01:00
return
}
func (d *SqlDb) GetTaskOutputs(projectID int, taskID int) (output []db.TaskOutput, err error) {
2021-03-12 21:20:18 +01:00
// check if task exists in the project
_, err = d.GetTask(projectID, taskID)
if err != nil {
return
}
_, err = d.selectAll(&output,
2021-03-12 18:41:41 +01:00
"select task_id, task, time, output from task__output where task_id=? order by time asc",
taskID)
return
}