|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package actions |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/db" |
| 11 | + repo_model "code.gitea.io/gitea/models/repo" |
| 12 | + user_model "code.gitea.io/gitea/models/user" |
| 13 | + "code.gitea.io/gitea/modules/timeutil" |
| 14 | + webhook_module "code.gitea.io/gitea/modules/webhook" |
| 15 | + |
| 16 | + "github.com/robfig/cron/v3" |
| 17 | +) |
| 18 | + |
| 19 | +// ActionSchedule represents a schedule of a workflow file |
| 20 | +type ActionSchedule struct { |
| 21 | + ID int64 |
| 22 | + Title string |
| 23 | + Specs []string |
| 24 | + RepoID int64 `xorm:"index"` |
| 25 | + Repo *repo_model.Repository `xorm:"-"` |
| 26 | + OwnerID int64 `xorm:"index"` |
| 27 | + WorkflowID string |
| 28 | + TriggerUserID int64 |
| 29 | + TriggerUser *user_model.User `xorm:"-"` |
| 30 | + Ref string |
| 31 | + CommitSHA string |
| 32 | + Event webhook_module.HookEventType |
| 33 | + EventPayload string `xorm:"LONGTEXT"` |
| 34 | + Content []byte |
| 35 | + Created timeutil.TimeStamp `xorm:"created"` |
| 36 | + Updated timeutil.TimeStamp `xorm:"updated"` |
| 37 | +} |
| 38 | + |
| 39 | +func init() { |
| 40 | + db.RegisterModel(new(ActionSchedule)) |
| 41 | +} |
| 42 | + |
| 43 | +// GetSchedulesMapByIDs returns the schedules by given id slice. |
| 44 | +func GetSchedulesMapByIDs(ids []int64) (map[int64]*ActionSchedule, error) { |
| 45 | + schedules := make(map[int64]*ActionSchedule, len(ids)) |
| 46 | + return schedules, db.GetEngine(db.DefaultContext).In("id", ids).Find(&schedules) |
| 47 | +} |
| 48 | + |
| 49 | +// GetReposMapByIDs returns the repos by given id slice. |
| 50 | +func GetReposMapByIDs(ids []int64) (map[int64]*repo_model.Repository, error) { |
| 51 | + repos := make(map[int64]*repo_model.Repository, len(ids)) |
| 52 | + return repos, db.GetEngine(db.DefaultContext).In("id", ids).Find(&repos) |
| 53 | +} |
| 54 | + |
| 55 | +var cronParser = cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor) |
| 56 | + |
| 57 | +// CreateScheduleTask creates new schedule task. |
| 58 | +func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error { |
| 59 | + // Return early if there are no rows to insert |
| 60 | + if len(rows) == 0 { |
| 61 | + return nil |
| 62 | + } |
| 63 | + |
| 64 | + // Begin transaction |
| 65 | + ctx, committer, err := db.TxContext(ctx) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + defer committer.Close() |
| 70 | + |
| 71 | + // Loop through each schedule row |
| 72 | + for _, row := range rows { |
| 73 | + // Create new schedule row |
| 74 | + if err = db.Insert(ctx, row); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // Loop through each schedule spec and create a new spec row |
| 79 | + now := time.Now() |
| 80 | + |
| 81 | + for _, spec := range row.Specs { |
| 82 | + // Parse the spec and check for errors |
| 83 | + schedule, err := cronParser.Parse(spec) |
| 84 | + if err != nil { |
| 85 | + continue // skip to the next spec if there's an error |
| 86 | + } |
| 87 | + |
| 88 | + // Insert the new schedule spec row |
| 89 | + if err = db.Insert(ctx, &ActionScheduleSpec{ |
| 90 | + RepoID: row.RepoID, |
| 91 | + ScheduleID: row.ID, |
| 92 | + Spec: spec, |
| 93 | + Next: timeutil.TimeStamp(schedule.Next(now).Unix()), |
| 94 | + }); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Commit transaction |
| 101 | + return committer.Commit() |
| 102 | +} |
| 103 | + |
| 104 | +func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error { |
| 105 | + ctx, committer, err := db.TxContext(ctx) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + defer committer.Close() |
| 110 | + |
| 111 | + if _, err := db.GetEngine(ctx).Delete(&ActionSchedule{RepoID: id}); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + if _, err := db.GetEngine(ctx).Delete(&ActionScheduleSpec{RepoID: id}); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + return committer.Commit() |
| 120 | +} |
0 commit comments