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