Skip to content

Commit dc1a6c1

Browse files
committed
feat: add support for schedule specs and tasks
- Add a new function `GetSchedulesMapByIDs` to retrieve schedules by ID - Create new schedule and schedule spec when creating a schedule task - Delete schedule specs when deleting a schedule - Add a new model `ActionScheduleSpec` for schedule specs Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 5424793 commit dc1a6c1

File tree

4 files changed

+154
-10
lines changed

4 files changed

+154
-10
lines changed

models/actions/schedule.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func init() {
3838
db.RegisterModel(new(ActionSchedule))
3939
}
4040

41+
// GetSchedulesMapByIDs returns the schedules by given id slice.
42+
func GetSchedulesMapByIDs(ids []int64) (map[int64]*ActionSchedule, error) {
43+
schedules := make(map[int64]*ActionSchedule, len(ids))
44+
return schedules, db.GetEngine(db.DefaultContext).In("id", ids).Find(&schedules)
45+
}
46+
4147
// CreateScheduleTask creates new schedule task.
4248
func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
4349
ctx, committer, err := db.TxContext(db.DefaultContext)
@@ -47,27 +53,42 @@ func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
4753
defer committer.Close()
4854

4955
if len(rows) > 0 {
50-
if err = db.Insert(ctx, rows); err != nil {
51-
return err
56+
for _, row := range rows {
57+
// create new schedule
58+
if err = db.Insert(ctx, row); err != nil {
59+
return err
60+
}
61+
62+
// create new schedule spec
63+
for _, spec := range row.Specs {
64+
if err = db.Insert(ctx, &ActionScheduleSpec{
65+
RepoID: row.RepoID,
66+
ScheduleID: row.ID,
67+
Spec: spec,
68+
}); err != nil {
69+
return err
70+
}
71+
}
5272
}
5373
}
5474

5575
return committer.Commit()
5676
}
5777

5878
func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error {
79+
ctx, committer, err := db.TxContext(db.DefaultContext)
80+
if err != nil {
81+
return err
82+
}
83+
defer committer.Close()
84+
5985
if _, err := db.GetEngine(ctx).Delete(&ActionSchedule{RepoID: id}); err != nil {
6086
return err
6187
}
62-
return nil
63-
}
6488

65-
func UpdateSchedule(ctx context.Context, schedule *ActionSchedule, cols ...string) error {
66-
sess := db.GetEngine(ctx).ID(schedule.ID)
67-
if len(cols) > 0 {
68-
sess.Cols(cols...)
89+
if _, err := db.GetEngine(ctx).Delete(&ActionScheduleSpec{RepoID: id}); err != nil {
90+
return err
6991
}
70-
_, err := sess.Update(schedule)
7192

72-
return err
93+
return committer.Commit()
7394
}

models/actions/schedule_spec.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"code.gitea.io/gitea/models/db"
8+
repo_model "code.gitea.io/gitea/models/repo"
9+
)
10+
11+
// ActionScheduleSpec represents a schedule spec of a workflow file
12+
type ActionScheduleSpec struct {
13+
ID int64
14+
RepoID int64
15+
Repo *repo_model.Repository `xorm:"-"`
16+
ScheduleID int64
17+
Schedule *ActionSchedule `xorm:"-"`
18+
19+
Spec string
20+
}
21+
22+
func init() {
23+
db.RegisterModel(new(ActionScheduleSpec))
24+
}

models/actions/schedule_spec_list.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/models/db"
10+
repo_model "code.gitea.io/gitea/models/repo"
11+
"code.gitea.io/gitea/modules/container"
12+
"xorm.io/builder"
13+
)
14+
15+
type SpecList []*ActionScheduleSpec
16+
17+
func (specs SpecList) GetScheduleIDs() []int64 {
18+
ids := make(container.Set[int64], len(specs))
19+
for _, spec := range specs {
20+
ids.Add(spec.ScheduleID)
21+
}
22+
return ids.Values()
23+
}
24+
25+
func (specs SpecList) LoadSchedules() error {
26+
scheduleIDs := specs.GetScheduleIDs()
27+
schedules, err := GetSchedulesMapByIDs(scheduleIDs)
28+
if err != nil {
29+
return err
30+
}
31+
for _, spec := range specs {
32+
spec.Schedule = schedules[spec.ScheduleID]
33+
}
34+
return nil
35+
}
36+
37+
func (specs SpecList) GetRepoIDs() []int64 {
38+
ids := make(container.Set[int64], len(specs))
39+
for _, spec := range specs {
40+
ids.Add(spec.RepoID)
41+
}
42+
return ids.Values()
43+
}
44+
45+
func (specs SpecList) LoadRepos() error {
46+
repoIDs := specs.GetRepoIDs()
47+
repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
48+
if err != nil {
49+
return err
50+
}
51+
for _, spec := range specs {
52+
spec.Repo = repos[spec.RepoID]
53+
}
54+
return nil
55+
}
56+
57+
type FindSpecOptions struct {
58+
db.ListOptions
59+
RepoID int64
60+
GetAll bool
61+
}
62+
63+
func (opts FindSpecOptions) toConds() builder.Cond {
64+
cond := builder.NewCond()
65+
if opts.RepoID > 0 {
66+
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
67+
}
68+
69+
return cond
70+
}
71+
72+
func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
73+
e := db.GetEngine(ctx).Where(opts.toConds())
74+
if !opts.GetAll && opts.PageSize > 0 && opts.Page >= 1 {
75+
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
76+
}
77+
var specs SpecList
78+
total, err := e.Desc("id").FindAndCount(&specs)
79+
if err != nil {
80+
return nil, 0, err
81+
}
82+
83+
if err := specs.LoadSchedules(); err != nil {
84+
return nil, 0, err
85+
}
86+
return specs, total, nil
87+
}
88+
89+
func CountSpecs(ctx context.Context, opts FindSpecOptions) (int64, error) {
90+
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionScheduleSpec))
91+
}

models/migrations/v1_20/v245.go

+8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ func AddActionScheduleTable(x *xorm.Engine) error {
2828
Updated timeutil.TimeStamp `xorm:"updated"`
2929
}
3030

31+
type ActionScheduleSpec struct {
32+
ID int64
33+
RepoID int64
34+
ScheduleID int64
35+
Spec string
36+
}
37+
3138
return x.Sync(
3239
new(ActionSchedule),
40+
new(ActionScheduleSpec),
3341
)
3442
}

0 commit comments

Comments
 (0)