refactor: rename model

This commit is contained in:
Jason Song
2022-12-06 13:48:09 +08:00
parent f2c7bbb0bc
commit c07d0c1768
21 changed files with 180 additions and 180 deletions

View File

@@ -21,8 +21,8 @@ import (
"xorm.io/builder"
)
// BotRun represents a run of a workflow file
type BotRun struct {
// ActionRun represents a run of a workflow file
type ActionRun struct {
ID int64
Title string
RepoID int64 `xorm:"index unique(repo_index)"`
@@ -45,16 +45,16 @@ type BotRun struct {
}
func init() {
db.RegisterModel(new(BotRun))
db.RegisterModel(new(BotRunIndex))
db.RegisterModel(new(ActionRun))
db.RegisterModel(new(ActionRunIndex))
}
func (run *BotRun) HTMLURL() string {
func (run *ActionRun) HTMLURL() string {
return fmt.Sprintf("%s/bots/runs/%d", run.Repo.HTMLURL(), run.Index)
}
// LoadAttributes load Repo TriggerUser if not loaded
func (run *BotRun) LoadAttributes(ctx context.Context) error {
func (run *ActionRun) LoadAttributes(ctx context.Context) error {
if run == nil {
return nil
}
@@ -81,7 +81,7 @@ func (run *BotRun) LoadAttributes(ctx context.Context) error {
return nil
}
func (run *BotRun) TakeTime() time.Duration {
func (run *ActionRun) TakeTime() time.Duration {
if run.Started == 0 {
return 0
}
@@ -93,7 +93,7 @@ func (run *BotRun) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (run *BotRun) GetPushEventPayload() (*api.PushPayload, error) {
func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) {
if run.Event == webhook.HookEventPush {
var payload api.PushPayload
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
@@ -129,7 +129,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
}
// InsertRun inserts a bot run
func InsertRun(run *BotRun, jobs []*jobparser.SingleWorkflow) error {
func InsertRun(run *ActionRun, jobs []*jobparser.SingleWorkflow) error {
ctx, commiter, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
@@ -162,7 +162,7 @@ func InsertRun(run *BotRun, jobs []*jobparser.SingleWorkflow) error {
return err
}
runJobs := make([]*BotRunJob, 0, len(jobs))
runJobs := make([]*ActionRunJob, 0, len(jobs))
for _, v := range jobs {
id, job := v.Job()
needs := job.Needs()
@@ -172,7 +172,7 @@ func InsertRun(run *BotRun, jobs []*jobparser.SingleWorkflow) error {
if len(needs) > 0 {
status = StatusBlocked
}
runJobs = append(runJobs, &BotRunJob{
runJobs = append(runJobs, &ActionRunJob{
RunID: run.ID,
RepoID: run.RepoID,
OwnerID: run.OwnerID,
@@ -207,8 +207,8 @@ func (err ErrRunNotExist) Error() string {
return fmt.Sprintf("run [%d] is not exist", err.ID)
}
func GetRunByID(ctx context.Context, id int64) (*BotRun, error) {
var run BotRun
func GetRunByID(ctx context.Context, id int64) (*ActionRun, error) {
var run ActionRun
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run)
if err != nil {
return nil, err
@@ -221,8 +221,8 @@ func GetRunByID(ctx context.Context, id int64) (*BotRun, error) {
return &run, nil
}
func GetRunByIndex(ctx context.Context, repoID, index int64) (*BotRun, error) {
run := &BotRun{
func GetRunByIndex(ctx context.Context, repoID, index int64) (*ActionRun, error) {
run := &ActionRun{
RepoID: repoID,
Index: index,
}
@@ -239,7 +239,7 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*BotRun, error) {
return run, nil
}
func UpdateRun(ctx context.Context, run *BotRun, cols ...string) error {
func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
sess := db.GetEngine(ctx).ID(run.ID)
if len(cols) > 0 {
sess.Cols(cols...)
@@ -268,4 +268,4 @@ func UpdateRun(ctx context.Context, run *BotRun, cols ...string) error {
return err
}
type BotRunIndex db.ResourceIndex
type ActionRunIndex db.ResourceIndex

View File

@@ -15,14 +15,14 @@ import (
"xorm.io/builder"
)
// BotRunJob represents a job of a run
type BotRunJob struct {
// ActionRunJob represents a job of a run
type ActionRunJob struct {
ID int64
RunID int64 `xorm:"index"`
Run *BotRun `xorm:"-"`
RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"`
RunID int64 `xorm:"index"`
Run *ActionRun `xorm:"-"`
RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"`
IsForkPullRequest bool
Name string
Attempt int64
@@ -39,10 +39,10 @@ type BotRunJob struct {
}
func init() {
db.RegisterModel(new(BotRunJob))
db.RegisterModel(new(ActionRunJob))
}
func (job *BotRunJob) TakeTime() time.Duration {
func (job *ActionRunJob) TakeTime() time.Duration {
if job.Started == 0 {
return 0
}
@@ -54,7 +54,7 @@ func (job *BotRunJob) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (job *BotRunJob) LoadRun(ctx context.Context) error {
func (job *ActionRunJob) LoadRun(ctx context.Context) error {
if job.Run == nil {
run, err := GetRunByID(ctx, job.RunID)
if err != nil {
@@ -66,7 +66,7 @@ func (job *BotRunJob) LoadRun(ctx context.Context) error {
}
// LoadAttributes load Run if not loaded
func (job *BotRunJob) LoadAttributes(ctx context.Context) error {
func (job *ActionRunJob) LoadAttributes(ctx context.Context) error {
if job == nil {
return nil
}
@@ -87,8 +87,8 @@ func (err ErrRunJobNotExist) Error() string {
return fmt.Sprintf("run job [%d] is not exist", err.ID)
}
func GetRunJobByID(ctx context.Context, id int64) (*BotRunJob, error) {
var job BotRunJob
func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {
var job ActionRunJob
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
if err != nil {
return nil, err
@@ -101,15 +101,15 @@ func GetRunJobByID(ctx context.Context, id int64) (*BotRunJob, error) {
return &job, nil
}
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*BotRunJob, error) {
var jobs []*BotRunJob
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*ActionRunJob, error) {
var jobs []*ActionRunJob
if err := db.GetEngine(ctx).Where("run_id=?", runID).OrderBy("id").Find(&jobs); err != nil {
return nil, err
}
return jobs, nil
}
func UpdateRunJob(ctx context.Context, job *BotRunJob, cond builder.Cond, cols ...string) (int64, error) {
func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, cols ...string) (int64, error) {
e := db.GetEngine(ctx)
sess := e.ID(job.ID)
@@ -144,7 +144,7 @@ func UpdateRunJob(ctx context.Context, job *BotRunJob, cond builder.Cond, cols .
runStatus := aggregateJobStatus(jobs)
run := &BotRun{
run := &ActionRun{
ID: job.RunID,
Status: runStatus,
}
@@ -154,7 +154,7 @@ func UpdateRunJob(ctx context.Context, job *BotRunJob, cond builder.Cond, cols .
return affected, UpdateRun(ctx, run)
}
func aggregateJobStatus(jobs []*BotRunJob) Status {
func aggregateJobStatus(jobs []*ActionRunJob) Status {
allDone := true
allWaiting := true
hasFailure := false

View File

@@ -12,9 +12,9 @@ import (
"xorm.io/builder"
)
type RunJobList []*BotRunJob
type ActionJobList []*ActionRunJob
func (jobs RunJobList) GetRunIDs() []int64 {
func (jobs ActionJobList) GetRunIDs() []int64 {
runIDsMap := make(map[int64]struct{})
for _, j := range jobs {
if j.RunID == 0 {
@@ -29,9 +29,9 @@ func (jobs RunJobList) GetRunIDs() []int64 {
return runIDs
}
func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
runIDs := jobs.GetRunIDs()
runs := make(map[int64]*BotRun, len(runIDs))
runs := make(map[int64]*ActionRun, len(runIDs))
if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
return err
}
@@ -41,7 +41,7 @@ func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
}
}
if withRepo {
var runsList RunList = make([]*BotRun, 0, len(runs))
var runsList RunList = make([]*ActionRun, 0, len(runs))
for _, r := range runs {
runsList = append(runsList, r)
}
@@ -50,7 +50,7 @@ func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
return nil
}
func (jobs RunJobList) LoadAttributes(ctx context.Context, withRepo bool) error {
func (jobs ActionJobList) LoadAttributes(ctx context.Context, withRepo bool) error {
return jobs.LoadRuns(ctx, withRepo)
}
@@ -87,16 +87,16 @@ func (opts FindRunJobOptions) toConds() builder.Cond {
return cond
}
func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (RunJobList, int64, error) {
func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (ActionJobList, 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 tasks RunJobList
var tasks ActionJobList
total, err := e.FindAndCount(&tasks)
return tasks, total, err
}
func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotRunJob))
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRunJob))
}

View File

@@ -14,7 +14,7 @@ import (
"xorm.io/builder"
)
type RunList []*BotRun
type RunList []*ActionRun
// GetUserIDs returns a slice of user's id
func (runs RunList) GetUserIDs() []int64 {
@@ -110,5 +110,5 @@ func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error)
}
func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotRun))
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRun))
}

View File

@@ -33,8 +33,8 @@ func (err ErrRunnerNotExist) Error() string {
return fmt.Sprintf("Bot runner token [%s] is not exist", err.Token)
}
// BotRunner represents runner machines
type BotRunner struct {
// ActionRunner represents runner machines
type ActionRunner struct {
ID int64
UUID string `xorm:"CHAR(36) UNIQUE"`
Name string `xorm:"VARCHAR(32)"`
@@ -64,7 +64,7 @@ type BotRunner struct {
Deleted timeutil.TimeStamp `xorm:"deleted"`
}
func (r *BotRunner) OwnType() string {
func (r *ActionRunner) OwnType() string {
if r.OwnerID == 0 {
return "Global"
}
@@ -75,7 +75,7 @@ func (r *BotRunner) OwnType() string {
return r.Repo.FullName()
}
func (r *BotRunner) Status() runnerv1.RunnerStatus {
func (r *ActionRunner) Status() runnerv1.RunnerStatus {
if time.Since(r.LastOnline.AsTime()) > time.Minute {
return runnerv1.RunnerStatus_RUNNER_STATUS_OFFLINE
}
@@ -85,11 +85,11 @@ func (r *BotRunner) Status() runnerv1.RunnerStatus {
return runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE
}
func (r *BotRunner) StatusName() string {
func (r *ActionRunner) StatusName() string {
return strings.ToLower(strings.TrimPrefix(r.Status().String(), "RUNNER_STATUS_"))
}
func (r *BotRunner) IsOnline() bool {
func (r *ActionRunner) IsOnline() bool {
status := r.Status()
if status == runnerv1.RunnerStatus_RUNNER_STATUS_IDLE || status == runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE {
return true
@@ -98,12 +98,12 @@ func (r *BotRunner) IsOnline() bool {
}
// AllLabels returns agent and custom labels
func (r *BotRunner) AllLabels() []string {
func (r *ActionRunner) AllLabels() []string {
return append(r.AgentLabels, r.CustomLabels...)
}
// Editable checks if the runner is editable by the user
func (r *BotRunner) Editable(ownerID, repoID int64) bool {
func (r *ActionRunner) Editable(ownerID, repoID int64) bool {
if ownerID == 0 && repoID == 0 {
return true
}
@@ -114,7 +114,7 @@ func (r *BotRunner) Editable(ownerID, repoID int64) bool {
}
// LoadAttributes loads the attributes of the runner
func (r *BotRunner) LoadAttributes(ctx context.Context) error {
func (r *ActionRunner) LoadAttributes(ctx context.Context) error {
if r.OwnerID > 0 {
var user user_model.User
has, err := db.GetEngine(ctx).ID(r.OwnerID).Get(&user)
@@ -138,13 +138,13 @@ func (r *BotRunner) LoadAttributes(ctx context.Context) error {
return nil
}
func (r *BotRunner) GenerateToken() (err error) {
func (r *ActionRunner) GenerateToken() (err error) {
r.Token, r.TokenSalt, r.TokenHash, _, err = generateSaltedToken()
return err
}
func init() {
db.RegisterModel(&BotRunner{})
db.RegisterModel(&ActionRunner{})
}
type FindRunnerOptions struct {
@@ -195,7 +195,7 @@ func (opts FindRunnerOptions) toOrder() string {
func CountRunners(opts FindRunnerOptions) (int64, error) {
return db.GetEngine(db.DefaultContext).
Table(BotRunner{}).
Table(ActionRunner{}).
Where(opts.toCond()).
OrderBy(opts.toOrder()).
Count()
@@ -212,8 +212,8 @@ func FindRunners(opts FindRunnerOptions) (runners RunnerList, err error) {
}
// GetUsableRunner returns the usable runner
func GetUsableRunner(opts FindRunnerOptions) (*BotRunner, error) {
var runner BotRunner
func GetUsableRunner(opts FindRunnerOptions) (*ActionRunner, error) {
var runner ActionRunner
has, err := db.GetEngine(db.DefaultContext).
Where(opts.toCond()).
Asc("last_online").
@@ -229,8 +229,8 @@ func GetUsableRunner(opts FindRunnerOptions) (*BotRunner, error) {
}
// GetRunnerByUUID returns a bot runner via uuid
func GetRunnerByUUID(uuid string) (*BotRunner, error) {
var runner BotRunner
func GetRunnerByUUID(uuid string) (*ActionRunner, error) {
var runner ActionRunner
has, err := db.GetEngine(db.DefaultContext).Where("uuid=?", uuid).Get(&runner)
if err != nil {
return nil, err
@@ -243,8 +243,8 @@ func GetRunnerByUUID(uuid string) (*BotRunner, error) {
}
// GetRunnerByID returns a bot runner via id
func GetRunnerByID(id int64) (*BotRunner, error) {
var runner BotRunner
func GetRunnerByID(id int64) (*ActionRunner, error) {
var runner ActionRunner
has, err := db.GetEngine(db.DefaultContext).Where("id=?", id).Get(&runner)
if err != nil {
return nil, err
@@ -257,7 +257,7 @@ func GetRunnerByID(id int64) (*BotRunner, error) {
}
// UpdateRunner updates runner's information.
func UpdateRunner(ctx context.Context, r *BotRunner, cols ...string) error {
func UpdateRunner(ctx context.Context, r *ActionRunner, cols ...string) error {
e := db.GetEngine(ctx)
var err error
if len(cols) == 0 {
@@ -269,15 +269,15 @@ func UpdateRunner(ctx context.Context, r *BotRunner, cols ...string) error {
}
// DeleteRunner deletes a runner by given ID.
func DeleteRunner(ctx context.Context, r *BotRunner) error {
func DeleteRunner(ctx context.Context, r *ActionRunner) error {
e := db.GetEngine(ctx)
_, err := e.Delete(r)
return err
}
// FindRunnersByRepoID returns all workers for the repository
func FindRunnersByRepoID(repoID int64) ([]*BotRunner, error) {
var runners []*BotRunner
func FindRunnersByRepoID(repoID int64) ([]*ActionRunner, error) {
var runners []*ActionRunner
err := db.GetEngine(db.DefaultContext).Where("repo_id=? OR repo_id=0", repoID).
Find(&runners)
if err != nil {
@@ -288,7 +288,7 @@ func FindRunnersByRepoID(repoID int64) ([]*BotRunner, error) {
}
// NewRunner creates new runner.
func NewRunner(ctx context.Context, t *BotRunner) error {
func NewRunner(ctx context.Context, t *ActionRunner) error {
_, err := db.GetEngine(ctx).Insert(t)
return err
}

View File

@@ -12,7 +12,7 @@ import (
"code.gitea.io/gitea/modules/container"
)
type RunnerList []*BotRunner
type RunnerList []*ActionRunner
// GetUserIDs returns a slice of user's id
func (runners RunnerList) GetUserIDs() []int64 {

View File

@@ -23,8 +23,8 @@ func (err ErrRunnerTokenNotExist) Error() string {
return fmt.Sprintf("runner token [%s] is not exist", err.Token)
}
// BotRunnerToken represents runner tokens
type BotRunnerToken struct {
// ActionRunnerToken represents runner tokens
type ActionRunnerToken struct {
ID int64
Token string `xorm:"UNIQUE"`
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
@@ -39,12 +39,12 @@ type BotRunnerToken struct {
}
func init() {
db.RegisterModel(new(BotRunnerToken))
db.RegisterModel(new(ActionRunnerToken))
}
// GetRunnerToken returns a bot runner via token
func GetRunnerToken(token string) (*BotRunnerToken, error) {
var runnerToken BotRunnerToken
func GetRunnerToken(token string) (*ActionRunnerToken, error) {
var runnerToken ActionRunnerToken
has, err := db.GetEngine(db.DefaultContext).Where("token=?", token).Get(&runnerToken)
if err != nil {
return nil, err
@@ -57,7 +57,7 @@ func GetRunnerToken(token string) (*BotRunnerToken, error) {
}
// UpdateRunnerToken updates runner token information.
func UpdateRunnerToken(ctx context.Context, r *BotRunnerToken, cols ...string) (err error) {
func UpdateRunnerToken(ctx context.Context, r *ActionRunnerToken, cols ...string) (err error) {
e := db.GetEngine(ctx)
if len(cols) == 0 {
@@ -69,12 +69,12 @@ func UpdateRunnerToken(ctx context.Context, r *BotRunnerToken, cols ...string) (
}
// NewRunnerToken creates a new runner token
func NewRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
func NewRunnerToken(ownerID, repoID int64) (*ActionRunnerToken, error) {
token, err := util.CryptoRandomString(40)
if err != nil {
return nil, err
}
runnerToken := &BotRunnerToken{
runnerToken := &ActionRunnerToken{
OwnerID: ownerID,
RepoID: repoID,
IsActive: false,
@@ -85,8 +85,8 @@ func NewRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
}
// GetUnactivatedRunnerToken returns a unactivated runner token
func GetUnactivatedRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
var runnerToken BotRunnerToken
func GetUnactivatedRunnerToken(ownerID, repoID int64) (*ActionRunnerToken, error) {
var runnerToken ActionRunnerToken
has, err := db.GetEngine(db.DefaultContext).Where("owner_id=? AND repo_id=? AND is_active=?", ownerID, repoID, false).OrderBy("id DESC").Get(&runnerToken)
if err != nil {
return nil, err

View File

@@ -5,7 +5,7 @@ package actions
import runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
// Status represents the status of BotRun, BotRunJob, BotTask, or BotTaskStep
// Status represents the status of ActionRun, ActionRunJob, ActionTask, or ActionTaskStep
type Status int
const (

View File

@@ -28,12 +28,12 @@ import (
"xorm.io/builder"
)
// BotTask represents a distribution of job
type BotTask struct {
// ActionTask represents a distribution of job
type ActionTask struct {
ID int64
JobID int64
Job *BotRunJob `xorm:"-"`
Steps []*BotTaskStep `xorm:"-"`
Job *ActionRunJob `xorm:"-"`
Steps []*ActionTaskStep `xorm:"-"`
Attempt int64
RunnerID int64 `xorm:"index"`
Status Status `xorm:"index"`
@@ -64,7 +64,7 @@ type BotTask struct {
var successfulTokenTaskCache *lru.Cache
func init() {
db.RegisterModel(new(BotTask), func() error {
db.RegisterModel(new(ActionTask), func() error {
if setting.SuccessfulTokensCacheSize > 0 {
var err error
successfulTokenTaskCache, err = lru.New(setting.SuccessfulTokensCacheSize)
@@ -78,7 +78,7 @@ func init() {
})
}
func (task *BotTask) TakeTime() time.Duration {
func (task *ActionTask) TakeTime() time.Duration {
if task.Started == 0 {
return 0
}
@@ -90,15 +90,15 @@ func (task *BotTask) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (task *BotTask) IsStopped() bool {
func (task *ActionTask) IsStopped() bool {
return task.Stopped > 0
}
func (task *BotTask) GetRepo() string {
func (task *ActionTask) GetRepo() string {
return "xxxx"
}
func (task *BotTask) GetCommitSHA() string {
func (task *ActionTask) GetCommitSHA() string {
if task.Job == nil {
return ""
}
@@ -109,7 +109,7 @@ func (task *BotTask) GetCommitSHA() string {
return task.Job.Run.CommitSHA
}
func (task *BotTask) GetCommitSHAShort() string {
func (task *ActionTask) GetCommitSHAShort() string {
commitSHA := task.GetCommitSHA()
if len(commitSHA) > 8 {
return commitSHA[:8]
@@ -117,14 +117,14 @@ func (task *BotTask) GetCommitSHAShort() string {
return commitSHA
}
func (task *BotTask) GetBuildViewLink() string {
func (task *ActionTask) GetBuildViewLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.Link() + "/bots/runs/" + strconv.FormatInt(task.ID, 10)
}
func (task *BotTask) GetCommitLink() string {
func (task *ActionTask) GetCommitLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
@@ -134,21 +134,21 @@ func (task *BotTask) GetCommitLink() string {
return ""
}
func (task *BotTask) GetRepoName() string {
func (task *ActionTask) GetRepoName() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.FullName()
}
func (task *BotTask) GetRepoLink() string {
func (task *ActionTask) GetRepoLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.Link()
}
func (task *BotTask) LoadJob(ctx context.Context) error {
func (task *ActionTask) LoadJob(ctx context.Context) error {
if task.Job == nil {
job, err := GetRunJobByID(ctx, task.JobID)
if err != nil {
@@ -160,7 +160,7 @@ func (task *BotTask) LoadJob(ctx context.Context) error {
}
// LoadAttributes load Job Steps if not loaded
func (task *BotTask) LoadAttributes(ctx context.Context) error {
func (task *ActionTask) LoadAttributes(ctx context.Context) error {
if task == nil {
return nil
}
@@ -183,7 +183,7 @@ func (task *BotTask) LoadAttributes(ctx context.Context) error {
return nil
}
func (task *BotTask) GenerateToken() (err error) {
func (task *ActionTask) GenerateToken() (err error) {
task.Token, task.TokenSalt, task.TokenHash, task.TokenLastEight, err = generateSaltedToken()
return err
}
@@ -213,8 +213,8 @@ func (indexes *LogIndexes) ToDB() ([]byte, error) {
return buf[:i], nil
}
func GetTaskByID(ctx context.Context, id int64) (*BotTask, error) {
var task BotTask
func GetTaskByID(ctx context.Context, id int64) (*ActionTask, error) {
var task ActionTask
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&task)
if err != nil {
return nil, err
@@ -225,7 +225,7 @@ func GetTaskByID(ctx context.Context, id int64) (*BotTask, error) {
return &task, nil
}
func GetRunningTaskByToken(ctx context.Context, token string) (*BotTask, error) {
func GetRunningTaskByToken(ctx context.Context, token string) (*ActionTask, error) {
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
if token == "" {
return nil, errNotExist
@@ -243,7 +243,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*BotTask, error)
lastEight := token[len(token)-8:]
if id := getTaskIDFromCache(token); id > 0 {
task := &BotTask{
task := &ActionTask{
TokenLastEight: lastEight,
}
// Re-get the task from the db in case it has been deleted in the intervening period
@@ -257,7 +257,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*BotTask, error)
successfulTokenTaskCache.Remove(token)
}
var tasks []*BotTask
var tasks []*ActionTask
err := db.GetEngine(ctx).Where("token_last_eight = ? AND status = ?", lastEight, StatusRunning).Find(&tasks)
if err != nil {
return nil, err
@@ -277,7 +277,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*BotTask, error)
return nil, errNotExist
}
func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool, error) {
func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask, bool, error) {
dbCtx, commiter, err := db.TxContext(ctx)
if err != nil {
return nil, false, err
@@ -297,13 +297,13 @@ func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool
jobCond = builder.In("run_id", builder.Select("id").From("bot_run").Where(jobCond))
}
var jobs []*BotRunJob
var jobs []*ActionRunJob
if err := e.Where("task_id=? AND status=?", 0, StatusWaiting).And(jobCond).Asc("id").Find(&jobs); err != nil {
return nil, false, err
}
// TODO: a more efficient way to filter labels
var job *BotRunJob
var job *ActionRunJob
labels := runner.AgentLabels
labels = append(labels, runner.CustomLabels...)
log.Trace("runner labels: %v", labels)
@@ -325,7 +325,7 @@ func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool
job.Started = now
job.Status = StatusRunning
task := &BotTask{
task := &ActionTask{
JobID: job.ID,
Attempt: job.Attempt,
RunnerID: runner.ID,
@@ -358,9 +358,9 @@ func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool
return nil, false, err
}
steps := make([]*BotTaskStep, len(workflowJob.Steps))
steps := make([]*ActionTaskStep, len(workflowJob.Steps))
for i, v := range workflowJob.Steps {
steps[i] = &BotTaskStep{
steps[i] = &ActionTaskStep{
Name: v.String(),
TaskID: task.ID,
Number: int64(i),
@@ -397,7 +397,7 @@ func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool
return task, true, nil
}
func UpdateTask(ctx context.Context, task *BotTask, cols ...string) error {
func UpdateTask(ctx context.Context, task *ActionTask, cols ...string) error {
sess := db.GetEngine(ctx).ID(task.ID)
if len(cols) > 0 {
sess.Cols(cols...)
@@ -406,7 +406,7 @@ func UpdateTask(ctx context.Context, task *BotTask, cols ...string) error {
return err
}
func UpdateTaskByState(state *runnerv1.TaskState) (*BotTask, error) {
func UpdateTaskByState(state *runnerv1.TaskState) (*ActionTask, error) {
stepStates := map[int64]*runnerv1.StepState{}
for _, v := range state.Steps {
stepStates[v.Id] = v
@@ -420,7 +420,7 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*BotTask, error) {
e := db.GetEngine(ctx)
task := &BotTask{}
task := &ActionTask{}
if has, err := e.ID(state.Id).Get(task); err != nil {
return nil, err
} else if !has {
@@ -430,7 +430,7 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*BotTask, error) {
if state.Result != runnerv1.Result_RESULT_UNSPECIFIED {
task.Status = Status(state.Result)
task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix())
if _, err := UpdateRunJob(ctx, &BotRunJob{
if _, err := UpdateRunJob(ctx, &ActionRunJob{
ID: task.JobID,
Status: task.Status,
Stopped: task.Stopped,
@@ -482,7 +482,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
}
e := db.GetEngine(ctx)
task := &BotTask{}
task := &ActionTask{}
if has, err := e.ID(taskID).Get(task); err != nil {
return err
} else if !has {
@@ -495,7 +495,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
now := timeutil.TimeStampNow()
task.Status = status
task.Stopped = now
if _, err := UpdateRunJob(ctx, &BotRunJob{
if _, err := UpdateRunJob(ctx, &ActionRunJob{
ID: task.JobID,
Status: task.Status,
Stopped: task.Stopped,

View File

@@ -12,7 +12,7 @@ import (
"xorm.io/builder"
)
type TaskList []*BotTask
type TaskList []*ActionTask
func (tasks TaskList) GetJobIDs() []int64 {
jobIDsMap := make(map[int64]struct{})
@@ -31,7 +31,7 @@ func (tasks TaskList) GetJobIDs() []int64 {
func (tasks TaskList) LoadJobs(ctx context.Context) error {
jobIDs := tasks.GetJobIDs()
jobs := make(map[int64]*BotRunJob, len(jobIDs))
jobs := make(map[int64]*ActionRunJob, len(jobIDs))
if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
return err
}
@@ -41,7 +41,7 @@ func (tasks TaskList) LoadJobs(ctx context.Context) error {
}
}
var jobsList RunJobList = make([]*BotRunJob, 0, len(jobs))
var jobsList ActionJobList = make([]*ActionRunJob, 0, len(jobs))
for _, j := range jobs {
jobsList = append(jobsList, j)
}
@@ -104,5 +104,5 @@ func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, int64, erro
}
func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotTask))
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionTask))
}

View File

@@ -11,8 +11,8 @@ import (
"code.gitea.io/gitea/modules/timeutil"
)
// BotTaskStep represents a step of BotTask
type BotTaskStep struct {
// ActionTaskStep represents a step of ActionTask
type ActionTaskStep struct {
ID int64
Name string
TaskID int64 `xorm:"index unique(task_number)"`
@@ -27,7 +27,7 @@ type BotTaskStep struct {
Updated timeutil.TimeStamp `xorm:"updated"`
}
func (step *BotTaskStep) TakeTime() time.Duration {
func (step *ActionTaskStep) TakeTime() time.Duration {
if step.Started == 0 {
return 0
}
@@ -40,10 +40,10 @@ func (step *BotTaskStep) TakeTime() time.Duration {
}
func init() {
db.RegisterModel(new(BotTaskStep))
db.RegisterModel(new(ActionTaskStep))
}
func GetTaskStepsByTaskID(ctx context.Context, taskID int64) ([]*BotTaskStep, error) {
var steps []*BotTaskStep
func GetTaskStepsByTaskID(ctx context.Context, taskID int64) ([]*ActionTaskStep, error) {
var steps []*ActionTaskStep
return steps, db.GetEngine(ctx).Where("task_id=?", taskID).OrderBy("number").Find(&steps)
}