2023-08-23 21:06:51 -06: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 06:27:30 -06:00
|
|
|
return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
|
|
|
|
return spec.ScheduleID, true
|
|
|
|
})
|
2023-08-23 21:06:51 -06:00
|
|
|
}
|
|
|
|
|
2023-09-16 08:39:12 -06:00
|
|
|
func (specs SpecList) LoadSchedules(ctx context.Context) error {
|
2023-08-23 21:06:51 -06:00
|
|
|
scheduleIDs := specs.GetScheduleIDs()
|
2023-09-16 08:39:12 -06:00
|
|
|
schedules, err := GetSchedulesMapByIDs(ctx, scheduleIDs)
|
2023-08-23 21:06:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, spec := range specs {
|
|
|
|
spec.Schedule = schedules[spec.ScheduleID]
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs := specs.GetRepoIDs()
|
2023-09-16 08:39:12 -06:00
|
|
|
repos, err := GetReposMapByIDs(ctx, repoIDs)
|
2023-08-23 21:06:51 -06: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 06:27:30 -06:00
|
|
|
return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
|
|
|
|
return spec.RepoID, true
|
|
|
|
})
|
2023-08-23 21:06:51 -06:00
|
|
|
}
|
|
|
|
|
2023-10-10 22:24:07 -06:00
|
|
|
func (specs SpecList) LoadRepos(ctx context.Context) error {
|
2023-08-23 21:06:51 -06:00
|
|
|
repoIDs := specs.GetRepoIDs()
|
2023-10-10 22:24:07 -06:00
|
|
|
repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
|
2023-08-23 21:06:51 -06: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 20:49:41 -07:00
|
|
|
func (opts FindSpecOptions) ToConds() builder.Cond {
|
2023-08-23 21:06:51 -06: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 20:49:41 -07:00
|
|
|
func (opts FindSpecOptions) ToOrders() string {
|
|
|
|
return "`id` DESC"
|
|
|
|
}
|
|
|
|
|
2023-08-23 21:06:51 -06:00
|
|
|
func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
|
2023-11-23 20:49:41 -07:00
|
|
|
specs, total, err := db.FindAndCount[ActionScheduleSpec](ctx, opts)
|
2023-08-23 21:06:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2023-11-23 20:49:41 -07:00
|
|
|
if err := SpecList(specs).LoadSchedules(ctx); err != nil {
|
2023-08-23 21:06:51 -06:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return specs, total, nil
|
|
|
|
}
|