2021-09-06 13:05:10 +02:00
|
|
|
package bolt
|
|
|
|
|
2022-01-31 23:16:00 +01:00
|
|
|
import (
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
2021-09-06 13:05:10 +02:00
|
|
|
|
|
|
|
func (d *BoltDb) GetSchedules() (schedules []db.Schedule, err error) {
|
|
|
|
var allProjects []db.Project
|
|
|
|
|
|
|
|
err = d.getObjects(0, db.ProjectProps, db.RetrieveQueryParams{}, nil, &allProjects)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proj := range allProjects {
|
|
|
|
var projSchedules []db.Schedule
|
|
|
|
projSchedules, err = d.GetProjectSchedules(proj.ID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
schedules = append(schedules, projSchedules...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetProjectSchedules(projectID int) (schedules []db.Schedule, err error) {
|
|
|
|
err = d.getObjects(projectID, db.ScheduleProps, db.RetrieveQueryParams{}, nil, &schedules)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:18:07 +02:00
|
|
|
func (d *BoltDb) GetTemplateSchedules(projectID int, templateID int) (schedules []db.Schedule, err error) {
|
2021-09-06 16:12:57 +02:00
|
|
|
schedules = make([]db.Schedule, 0)
|
|
|
|
|
2021-09-06 13:05:10 +02:00
|
|
|
projSchedules, err := d.GetProjectSchedules(projectID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range projSchedules {
|
|
|
|
if s.TemplateID == templateID {
|
2021-09-06 13:18:07 +02:00
|
|
|
schedules = append(schedules, s)
|
2021-09-06 13:05:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:18:07 +02:00
|
|
|
func (d *BoltDb) CreateSchedule(schedule db.Schedule) (newSchedule db.Schedule, err error) {
|
|
|
|
newTpl, err := d.createObject(schedule.ProjectID, db.ScheduleProps, schedule)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newSchedule = newTpl.(db.Schedule)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) UpdateSchedule(schedule db.Schedule) error {
|
|
|
|
return d.updateObject(schedule.ProjectID, db.ScheduleProps, schedule)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetSchedule(projectID int, scheduleID int) (schedule db.Schedule, err error) {
|
|
|
|
err = d.getObject(projectID, db.ScheduleProps, intObjectID(scheduleID), &schedule)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-31 23:16:00 +01:00
|
|
|
func (d *BoltDb) deleteSchedule(projectID int, scheduleID int, tx *bbolt.Tx) error {
|
|
|
|
return d.deleteObject(projectID, db.ScheduleProps, intObjectID(scheduleID), tx)
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:18:07 +02:00
|
|
|
func (d *BoltDb) DeleteSchedule(projectID int, scheduleID int) error {
|
2022-01-31 23:16:00 +01:00
|
|
|
return d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return d.deleteSchedule(projectID, scheduleID, tx)
|
|
|
|
})
|
2021-09-06 13:18:07 +02:00
|
|
|
}
|
2022-01-30 18:43:15 +01:00
|
|
|
|
|
|
|
func (d *BoltDb) SetScheduleCommitHash(projectID int, scheduleID int, hash string) error {
|
|
|
|
schedule, err := d.GetSchedule(projectID, scheduleID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
schedule.LastCommitHash = &hash
|
|
|
|
return d.updateObject(projectID, db.ScheduleProps, schedule)
|
|
|
|
}
|