Semaphore/db/bolt/task.go

146 lines
3.2 KiB
Go
Raw Normal View History

package bolt
import (
"github.com/ansible-semaphore/semaphore/db"
"go.etcd.io/bbolt"
"time"
)
func (d *BoltDb) CreateTask(task db.Task) (newTask db.Task, err error) {
task.Created = time.Now()
2021-06-24 19:45:28 +02:00
res, err := d.createObject(0, db.TaskProps, task)
if err != nil {
return
}
newTask = res.(db.Task)
return
}
func (d *BoltDb) UpdateTask(task db.Task) error {
return d.updateObject(0, db.TaskProps, task)
}
func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
newOutput, err := d.createObject(output.TaskID, db.TaskOutputProps, output)
if err != nil {
return db.TaskOutput{}, err
}
return newOutput.(db.TaskOutput), nil
}
func (d *BoltDb) getTasks(projectID int, template *db.Template, params db.RetrieveQueryParams) (tasksWithTpl []db.TaskWithTpl, err error) {
2021-06-24 19:45:28 +02:00
var tasks []db.Task
err = d.getObjects(0, db.TaskProps, params, func(tsk interface{}) bool {
task := tsk.(db.Task)
if task.ProjectID != projectID {
return false
}
if template != nil && task.TemplateID != template.ID {
return false
}
return true
}, &tasks)
if err != nil {
return
}
var templates = make(map[int]db.Template)
var users = make(map[int]db.User)
2021-06-24 19:45:28 +02:00
tasksWithTpl = make([]db.TaskWithTpl, len(tasks))
for i, task := range tasks {
tpl, ok := templates[task.TemplateID]
if !ok {
if template == nil {
tpl, _ = d.GetTemplate(task.ProjectID, task.TemplateID)
} else {
tpl = *template
}
templates[task.TemplateID] = tpl
}
2021-06-24 19:45:28 +02:00
tasksWithTpl[i] = db.TaskWithTpl{Task: task}
tasksWithTpl[i].TemplatePlaybook = tpl.Playbook
tasksWithTpl[i].TemplateAlias = tpl.Alias
tasksWithTpl[i].TemplateType = tpl.Type
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
}
2021-06-24 19:45:28 +02:00
tasksWithTpl[i].UserName = &usr.Name
}
err = tasksWithTpl[i].Fill(d)
if err != nil {
return
}
}
return
}
func (d *BoltDb) GetTask(projectID int, taskID int) (task db.Task, err error) {
err = d.getObject(0, db.TaskProps, intObjectID(taskID), &task)
if err != nil {
return
}
if task.ProjectID != projectID {
task = db.Task{}
err = db.ErrNotFound
return
}
return
}
func (d *BoltDb) GetTemplateTasks(template db.Template, params db.RetrieveQueryParams) ([]db.TaskWithTpl, error) {
2021-10-13 20:51:35 +02:00
return d.getTasks(template.ProjectID, &template, 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
}
err = d.deleteObject(0, db.TaskProps, intObjectID(taskID))
if err != nil {
return
}
2021-06-24 19:45:28 +02:00
_ = d.db.Update(func(tx *bbolt.Tx) error {
return tx.DeleteBucket(makeBucketId(db.TaskOutputProps, taskID))
})
return
}
func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (outputs []db.TaskOutput, err error) {
// check if task exists in the project
_, err = d.GetTask(projectID, taskID)
if err != nil {
return
}
err = d.getObjects(taskID, db.TaskOutputProps, db.RetrieveQueryParams{}, nil, &outputs)
return
}