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