2021-05-13 00:56:31 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2021-05-13 15:49:32 +02:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"time"
|
2021-05-13 00:56:31 +02:00
|
|
|
)
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
func (d *BoltDb) CreateTask(task db.Task) (newTask db.Task, err error) {
|
|
|
|
task.Created = time.Now()
|
|
|
|
err = d.getObject(0, db.TaskObject, intObjectID(task.ID), &newTask)
|
|
|
|
return
|
2021-05-13 00:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) UpdateTask(task db.Task) error {
|
2021-05-13 15:49:32 +02:00
|
|
|
return d.updateObject(0, db.TaskObject, task)
|
2021-05-13 00:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
|
2021-05-13 15:49:32 +02:00
|
|
|
newOutput, err := d.createObject(output.TaskID, db.TaskOutputObject, output)
|
|
|
|
if err != nil {
|
|
|
|
return db.TaskOutput{}, err
|
|
|
|
}
|
|
|
|
return newOutput.(db.TaskOutput), nil
|
2021-05-13 00:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
|
2021-05-13 15:49:32 +02:00
|
|
|
err = d.getObjects(0, db.TaskObject, params, func (tsk interface{}) bool {
|
|
|
|
task := tsk.(db.TaskWithTpl)
|
|
|
|
|
|
|
|
if task.ProjectID != projectID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if templateID != nil && task.TemplateID != *templateID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, &tasks)
|
|
|
|
|
|
|
|
var templates = make(map[int]db.Template)
|
|
|
|
var users = make(map[int]db.User)
|
|
|
|
|
|
|
|
for _, task := range tasks {
|
|
|
|
tpl, ok := templates[task.TemplateID]
|
|
|
|
if !ok {
|
|
|
|
tpl, err = d.GetTemplate(task.ProjectID, task.TemplateID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
templates[task.TemplateID] = tpl
|
|
|
|
}
|
|
|
|
task.TemplatePlaybook = tpl.Playbook
|
|
|
|
task.TemplateAlias = tpl.Alias
|
|
|
|
if task.UserID != nil {
|
|
|
|
usr, ok := users[*task.UserID]
|
|
|
|
if !ok {
|
|
|
|
usr, err = d.GetUser(*task.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
users[*task.UserID] = usr
|
|
|
|
}
|
|
|
|
task.UserName = &usr.Name
|
|
|
|
}
|
2021-05-13 00:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetTask(projectID int, taskID int) (task db.Task, err error) {
|
2021-05-13 15:49:32 +02:00
|
|
|
err = d.getObject(0, db.TaskObject, intObjectID(taskID), &task)
|
2021-05-13 00:56:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-13 15:49:32 +02:00
|
|
|
if task.ProjectID != projectID {
|
|
|
|
task = db.Task{}
|
2021-05-13 00:56:31 +02:00
|
|
|
err = db.ErrNotFound
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetTemplateTasks(projectID int, templateID int, params db.RetrieveQueryParams) ([]db.TaskWithTpl, error) {
|
|
|
|
return d.getTasks(projectID, &templateID, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetProjectTasks(projectID int, params db.RetrieveQueryParams) ([]db.TaskWithTpl, error) {
|
|
|
|
return d.getTasks(projectID, nil, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) {
|
|
|
|
// check if task exists in the project
|
|
|
|
_, err = d.GetTask(projectID, taskID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
err = d.deleteObject(0, db.TaskObject, intObjectID(taskID))
|
2021-05-13 00:56:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
_ = d.db.Update(func (tx *bbolt.Tx) error {
|
|
|
|
return tx.DeleteBucket(makeBucketId(db.TaskOutputObject, taskID))
|
|
|
|
})
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (outputs []db.TaskOutput, err error) {
|
2021-05-13 00:56:31 +02:00
|
|
|
// check if task exists in the project
|
|
|
|
_, err = d.GetTask(projectID, taskID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
err = d.getObjects(taskID, db.TaskOutputObject, db.RetrieveQueryParams{}, nil, &outputs)
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
return
|
|
|
|
}
|