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