-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(actions): support cron schedule task.
Signed-off-by: Bo-Yi.Wu <[email protected]>
- Loading branch information
Showing
10 changed files
with
464 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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/timeutil" | ||
webhook_module "code.gitea.io/gitea/modules/webhook" | ||
) | ||
|
||
// ActionSchedule represents a schedule of a workflow file | ||
type ActionSchedule struct { | ||
ID int64 | ||
Title string | ||
Specs []string | ||
EntryIDs []int `xorm:"entry_ids"` | ||
RepoID int64 `xorm:"index"` | ||
Repo *repo_model.Repository `xorm:"-"` | ||
OwnerID int64 `xorm:"index"` | ||
WorkflowID string `xorm:"index"` // the name of workflow file | ||
TriggerUserID int64 | ||
TriggerUser *user_model.User `xorm:"-"` | ||
Ref string | ||
CommitSHA string | ||
Event webhook_module.HookEventType | ||
EventPayload string `xorm:"LONGTEXT"` | ||
Content []byte | ||
Created timeutil.TimeStamp `xorm:"created"` | ||
Updated timeutil.TimeStamp `xorm:"updated"` | ||
} | ||
|
||
func init() { | ||
db.RegisterModel(new(ActionSchedule)) | ||
} | ||
|
||
// CreateScheduleTask creates new schedule task. | ||
func CreateScheduleTask(ctx context.Context, id int64, rows []*ActionSchedule) error { | ||
for _, row := range rows { | ||
if _, err := db.GetEngine(ctx).Insert(row); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error { | ||
if _, err := db.GetEngine(ctx).Delete(&ActionSchedule{RepoID: id}); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func UpdateSchedule(ctx context.Context, schedule *ActionSchedule, cols ...string) error { | ||
sess := db.GetEngine(ctx).ID(schedule.ID) | ||
if len(cols) > 0 { | ||
sess.Cols(cols...) | ||
} | ||
_, err := sess.Update(schedule) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 { | ||
ids := make(container.Set[int64], len(schedules)) | ||
for _, schedule := range schedules { | ||
ids.Add(schedule.TriggerUserID) | ||
} | ||
return ids.Values() | ||
} | ||
|
||
func (schedules ScheduleList) GetRepoIDs() []int64 { | ||
ids := make(container.Set[int64], len(schedules)) | ||
for _, schedule := range schedules { | ||
ids.Add(schedule.RepoID) | ||
} | ||
return ids.Values() | ||
} | ||
|
||
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] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (schedules ScheduleList) LoadRepos() error { | ||
repoIDs := schedules.GetRepoIDs() | ||
repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs) | ||
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 | ||
GetAll bool | ||
} | ||
|
||
func (opts FindScheduleOptions) toConds() builder.Cond { | ||
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 | ||
} | ||
|
||
func FindSchedules(ctx context.Context, opts FindScheduleOptions) (ScheduleList, int64, error) { | ||
e := db.GetEngine(ctx).Where(opts.toConds()) | ||
if !opts.GetAll && opts.PageSize > 0 && opts.Page >= 1 { | ||
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) | ||
} | ||
var schedules ScheduleList | ||
total, err := e.Desc("id").FindAndCount(&schedules) | ||
return schedules, total, err | ||
} | ||
|
||
func CountSchedules(ctx context.Context, opts FindScheduleOptions) (int64, error) { | ||
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionSchedule)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_20 //nolint | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/timeutil" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func AddActionScheduleTable(x *xorm.Engine) error { | ||
type ActionSchedule struct { | ||
ID int64 | ||
Title string | ||
Specs []string | ||
EntryIDs []int `xorm:"entry_ids"` | ||
RepoID int64 `xorm:"index"` | ||
OwnerID int64 `xorm:"index"` | ||
WorkflowID string `xorm:"index"` // the name of workflow file | ||
TriggerUserID int64 | ||
Ref string | ||
CommitSHA string | ||
Event string | ||
EventPayload string `xorm:"LONGTEXT"` | ||
Content []byte | ||
Created timeutil.TimeStamp `xorm:"created"` | ||
Updated timeutil.TimeStamp `xorm:"updated"` | ||
} | ||
|
||
return x.Sync( | ||
new(ActionSchedule), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.