2021-03-12 18:41:41 +01:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2024-02-27 12:06:05 +01:00
|
|
|
"github.com/Masterminds/squirrel"
|
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(
|
2021-09-17 00:19:34 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2022-01-28 11:45:31 +01:00
|
|
|
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.*"
|
2021-10-20 13:56:29 +02:00
|
|
|
fields += ", tpl.playbook as tpl_playbook" +
|
|
|
|
", `user`.name as user_name" +
|
2022-02-03 08:05:13 +01:00
|
|
|
", tpl.name as tpl_alias" +
|
2021-10-20 13:56:29 +02:00
|
|
|
", 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").
|
2021-08-24 19:52:35 +02:00
|
|
|
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
|
|
|
|
2021-10-20 13:56:29 +02: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
|
2021-10-20 13:56:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-03-12 18:41:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-28 11:45:31 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
_, 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
|
|
|
|
}
|