forked from Shiloh/githaven
0d55f64e6c
Replace #22751 1. only support the default branch in the repository setting. 2. autoload schedule data from the schedule table after starting the service. 3. support specific syntax like `@yearly`, `@monthly`, `@weekly`, `@daily`, `@hourly` ## How to use See the [GitHub Actions document](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule) for getting more detailed information. ```yaml on: schedule: - cron: '30 5 * * 1,3' - cron: '30 5 * * 2,4' jobs: test_schedule: runs-on: ubuntu-latest steps: - name: Not on Monday or Wednesday if: github.event.schedule != '30 5 * * 1,3' run: echo "This step will be skipped on Monday and Wednesday" - name: Every time run: echo "This step will always run" ``` Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com> --------- Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
107 lines
2.2 KiB
Go
107 lines
2.2 KiB
Go
// 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 {
|
|
ids := make(container.Set[int64], len(specs))
|
|
for _, spec := range specs {
|
|
ids.Add(spec.ScheduleID)
|
|
}
|
|
return ids.Values()
|
|
}
|
|
|
|
func (specs SpecList) LoadSchedules() error {
|
|
scheduleIDs := specs.GetScheduleIDs()
|
|
schedules, err := GetSchedulesMapByIDs(scheduleIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, spec := range specs {
|
|
spec.Schedule = schedules[spec.ScheduleID]
|
|
}
|
|
|
|
repoIDs := specs.GetRepoIDs()
|
|
repos, err := GetReposMapByIDs(repoIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, spec := range specs {
|
|
spec.Repo = repos[spec.RepoID]
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (specs SpecList) GetRepoIDs() []int64 {
|
|
ids := make(container.Set[int64], len(specs))
|
|
for _, spec := range specs {
|
|
ids.Add(spec.RepoID)
|
|
}
|
|
return ids.Values()
|
|
}
|
|
|
|
func (specs SpecList) LoadRepos() error {
|
|
repoIDs := specs.GetRepoIDs()
|
|
repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
|
|
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
|
|
}
|
|
|
|
func (opts FindSpecOptions) toConds() builder.Cond {
|
|
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
|
|
}
|
|
|
|
func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
|
|
e := db.GetEngine(ctx).Where(opts.toConds())
|
|
if opts.PageSize > 0 && opts.Page >= 1 {
|
|
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
}
|
|
var specs SpecList
|
|
total, err := e.Desc("id").FindAndCount(&specs)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := specs.LoadSchedules(); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return specs, total, nil
|
|
}
|
|
|
|
func CountSpecs(ctx context.Context, opts FindSpecOptions) (int64, error) {
|
|
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionScheduleSpec))
|
|
}
|