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"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ScheduleList []*ActionSchedule
|
|
|
|
|
|
|
|
// GetUserIDs returns a slice of user's id
|
|
|
|
func (schedules ScheduleList) GetUserIDs() []int64 {
|
2024-04-09 17:57:30 +05:30
|
|
|
return container.FilterSlice(schedules, func(schedule *ActionSchedule) (int64, bool) {
|
|
|
|
return schedule.TriggerUserID, true
|
|
|
|
})
|
2023-08-24 08:36:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (schedules ScheduleList) GetRepoIDs() []int64 {
|
2024-04-09 17:57:30 +05:30
|
|
|
return container.FilterSlice(schedules, func(schedule *ActionSchedule) (int64, bool) {
|
|
|
|
return schedule.RepoID, true
|
|
|
|
})
|
2023-08-24 08:36:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (schedules ScheduleList) LoadTriggerUser(ctx context.Context) error {
|
|
|
|
userIDs := schedules.GetUserIDs()
|
|
|
|
users := make(map[int64]*user_model.User, len(userIDs))
|
|
|
|
if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, schedule := range schedules {
|
|
|
|
if schedule.TriggerUserID == user_model.ActionsUserID {
|
|
|
|
schedule.TriggerUser = user_model.NewActionsUser()
|
|
|
|
} else {
|
|
|
|
schedule.TriggerUser = users[schedule.TriggerUserID]
|
2024-04-11 12:41:32 +05:30
|
|
|
if schedule.TriggerUser == nil {
|
|
|
|
schedule.TriggerUser = user_model.NewGhostUser()
|
|
|
|
}
|
2023-08-24 08:36:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-11 09:54:07 +05:30
|
|
|
func (schedules ScheduleList) LoadRepos(ctx context.Context) error {
|
2023-08-24 08:36:51 +05:30
|
|
|
repoIDs := schedules.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 _, schedule := range schedules {
|
|
|
|
schedule.Repo = repos[schedule.RepoID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type FindScheduleOptions struct {
|
|
|
|
db.ListOptions
|
|
|
|
RepoID int64
|
|
|
|
OwnerID int64
|
|
|
|
}
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
func (opts FindScheduleOptions) 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.OwnerID > 0 {
|
|
|
|
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
|
|
|
|
}
|
|
|
|
|
|
|
|
return cond
|
|
|
|
}
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
func (opts FindScheduleOptions) ToOrders() string {
|
|
|
|
return "`id` DESC"
|
2023-08-24 08:36:51 +05:30
|
|
|
}
|