2023-08-23 22:06:51 -05:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SpecList []*ActionScheduleSpec
|
|
|
|
|
|
|
|
func (specs SpecList) GetScheduleIDs() []int64 {
|
2024-04-09 07:27:30 -05:00
|
|
|
return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
|
|
|
|
return spec.ScheduleID, true
|
|
|
|
})
|
2023-08-23 22:06:51 -05:00
|
|
|
}
|
|
|
|
|
2023-09-16 09:39:12 -05:00
|
|
|
func (specs SpecList) LoadSchedules(ctx context.Context) error {
|
2023-08-23 22:06:51 -05:00
|
|
|
scheduleIDs := specs.GetScheduleIDs()
|
2023-09-16 09:39:12 -05:00
|
|
|
schedules, err := GetSchedulesMapByIDs(ctx, scheduleIDs)
|
2023-08-23 22:06:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, spec := range specs {
|
|
|
|
spec.Schedule = schedules[spec.ScheduleID]
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs := specs.GetRepoIDs()
|
2023-09-16 09:39:12 -05:00
|
|
|
repos, err := GetReposMapByIDs(ctx, repoIDs)
|
2023-08-23 22:06:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, spec := range specs {
|
|
|
|
spec.Repo = repos[spec.RepoID]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (specs SpecList) GetRepoIDs() []int64 {
|
2024-04-09 07:27:30 -05:00
|
|
|
return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
|
|
|
|
return spec.RepoID, true
|
|
|
|
})
|
2023-08-23 22:06:51 -05:00
|
|
|
}
|
|
|
|
|
2023-10-10 23:24:07 -05:00
|
|
|
func (specs SpecList) LoadRepos(ctx context.Context) error {
|
2023-08-23 22:06:51 -05:00
|
|
|
repoIDs := specs.GetRepoIDs()
|
2023-10-10 23:24:07 -05:00
|
|
|
repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
|
2023-08-23 22:06:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, spec := range specs {
|
|
|
|
spec.Repo = repos[spec.RepoID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type FindSpecOptions struct {
|
|
|
|
db.ListOptions
|
|
|
|
RepoID int64
|
|
|
|
Next int64
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:49:41 -05:00
|
|
|
func (opts FindSpecOptions) ToConds() builder.Cond {
|
2023-08-23 22:06:51 -05:00
|
|
|
cond := builder.NewCond()
|
|
|
|
if opts.RepoID > 0 {
|
|
|
|
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Next > 0 {
|
|
|
|
cond = cond.And(builder.Lte{"next": opts.Next})
|
|
|
|
}
|
|
|
|
|
|
|
|
return cond
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:49:41 -05:00
|
|
|
func (opts FindSpecOptions) ToOrders() string {
|
|
|
|
return "`id` DESC"
|
|
|
|
}
|
|
|
|
|
2023-08-23 22:06:51 -05:00
|
|
|
func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
|
2023-11-23 22:49:41 -05:00
|
|
|
specs, total, err := db.FindAndCount[ActionScheduleSpec](ctx, opts)
|
2023-08-23 22:06:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:49:41 -05:00
|
|
|
if err := SpecList(specs).LoadSchedules(ctx); err != nil {
|
2023-08-23 22:06:51 -05:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return specs, total, nil
|
|
|
|
}
|