refactor: rename import alias
This commit is contained in:
@@ -7,13 +7,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
bots_model "code.gitea.io/gitea/models/actions"
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
bots_module "code.gitea.io/gitea/modules/actions"
|
||||
actions_module "code.gitea.io/gitea/modules/actions"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/queue"
|
||||
@@ -26,26 +26,26 @@ func Init() {
|
||||
}
|
||||
|
||||
func DeleteResourceOfRepository(ctx context.Context, repo *repo_model.Repository) error {
|
||||
tasks, _, err := bots_model.FindTasks(ctx, bots_model.FindTaskOptions{RepoID: repo.ID})
|
||||
tasks, _, err := actions_model.FindTasks(ctx, actions_model.FindTaskOptions{RepoID: repo.ID})
|
||||
if err != nil {
|
||||
return fmt.Errorf("find task of repo %v: %w", repo.ID, err)
|
||||
}
|
||||
|
||||
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
||||
e := db.GetEngine(ctx)
|
||||
if _, err := e.Delete(&bots_model.BotTaskStep{RepoID: repo.ID}); err != nil {
|
||||
if _, err := e.Delete(&actions_model.BotTaskStep{RepoID: repo.ID}); err != nil {
|
||||
return fmt.Errorf("delete bots task steps of repo %d: %w", repo.ID, err)
|
||||
}
|
||||
if _, err := e.Delete(&bots_model.BotTask{RepoID: repo.ID}); err != nil {
|
||||
if _, err := e.Delete(&actions_model.BotTask{RepoID: repo.ID}); err != nil {
|
||||
return fmt.Errorf("delete bots tasks of repo %d: %w", repo.ID, err)
|
||||
}
|
||||
if _, err := e.Delete(&bots_model.BotRunJob{RepoID: repo.ID}); err != nil {
|
||||
if _, err := e.Delete(&actions_model.BotRunJob{RepoID: repo.ID}); err != nil {
|
||||
return fmt.Errorf("delete bots run jobs of repo %d: %w", repo.ID, err)
|
||||
}
|
||||
if _, err := e.Delete(&bots_model.BotRun{RepoID: repo.ID}); err != nil {
|
||||
if _, err := e.Delete(&actions_model.BotRun{RepoID: repo.ID}); err != nil {
|
||||
return fmt.Errorf("delete bots runs of repo %d: %w", repo.ID, err)
|
||||
}
|
||||
if _, err := e.Delete(&bots_model.BotRunner{RepoID: repo.ID}); err != nil {
|
||||
if _, err := e.Delete(&actions_model.BotRunner{RepoID: repo.ID}); err != nil {
|
||||
return fmt.Errorf("delete bots runner of repo %d: %w", repo.ID, err)
|
||||
}
|
||||
return nil
|
||||
@@ -55,7 +55,7 @@ func DeleteResourceOfRepository(ctx context.Context, repo *repo_model.Repository
|
||||
|
||||
// remove logs file after tasks have been deleted, to avoid new log files
|
||||
for _, task := range tasks {
|
||||
err := bots_module.RemoveLogs(ctx, task.LogInStorage, task.LogFilename)
|
||||
err := actions_module.RemoveLogs(ctx, task.LogInStorage, task.LogFilename)
|
||||
if err != nil {
|
||||
log.Error("remove log file %q: %v", task.LogFilename, err)
|
||||
// go on
|
||||
@@ -65,7 +65,7 @@ func DeleteResourceOfRepository(ctx context.Context, repo *repo_model.Repository
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateCommitStatus(ctx context.Context, job *bots_model.BotRunJob) error {
|
||||
func CreateCommitStatus(ctx context.Context, job *actions_model.BotRunJob) error {
|
||||
if err := job.LoadAttributes(ctx); err != nil {
|
||||
return fmt.Errorf("load run: %w", err)
|
||||
}
|
||||
@@ -122,15 +122,15 @@ func CreateCommitStatus(ctx context.Context, job *bots_model.BotRunJob) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func toCommitStatus(status bots_model.Status) api.CommitStatusState {
|
||||
func toCommitStatus(status actions_model.Status) api.CommitStatusState {
|
||||
switch status {
|
||||
case bots_model.StatusSuccess:
|
||||
case actions_model.StatusSuccess:
|
||||
return api.CommitStatusSuccess
|
||||
case bots_model.StatusFailure, bots_model.StatusCancelled, bots_model.StatusSkipped:
|
||||
case actions_model.StatusFailure, actions_model.StatusCancelled, actions_model.StatusSkipped:
|
||||
return api.CommitStatusFailure
|
||||
case bots_model.StatusWaiting, bots_model.StatusBlocked:
|
||||
case actions_model.StatusWaiting, actions_model.StatusBlocked:
|
||||
return api.CommitStatusPending
|
||||
case bots_model.StatusRunning:
|
||||
case actions_model.StatusRunning:
|
||||
return api.CommitStatusRunning
|
||||
default:
|
||||
return api.CommitStatusError
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
bots_model "code.gitea.io/gitea/models/actions"
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
@@ -21,8 +21,8 @@ const (
|
||||
|
||||
// StopZombieTasks stops the task which have running status, but haven't been updated for a long time
|
||||
func StopZombieTasks(ctx context.Context) error {
|
||||
tasks, _, err := bots_model.FindTasks(ctx, bots_model.FindTaskOptions{
|
||||
Status: bots_model.StatusRunning,
|
||||
tasks, _, err := actions_model.FindTasks(ctx, actions_model.FindTaskOptions{
|
||||
Status: actions_model.StatusRunning,
|
||||
UpdatedBefore: timeutil.TimeStamp(time.Now().Add(-zombieTaskTimeout).Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -32,7 +32,7 @@ func StopZombieTasks(ctx context.Context) error {
|
||||
|
||||
for _, task := range tasks {
|
||||
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
||||
if err := bots_model.StopTask(ctx, task.ID, bots_model.StatusFailure); err != nil {
|
||||
if err := actions_model.StopTask(ctx, task.ID, actions_model.StatusFailure); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := task.LoadJob(ctx); err != nil {
|
||||
@@ -49,8 +49,8 @@ func StopZombieTasks(ctx context.Context) error {
|
||||
|
||||
// StopEndlessTasks stops the tasks which have running status and continuous updates, but don't end for a long time
|
||||
func StopEndlessTasks(ctx context.Context) error {
|
||||
tasks, _, err := bots_model.FindTasks(ctx, bots_model.FindTaskOptions{
|
||||
Status: bots_model.StatusRunning,
|
||||
tasks, _, err := actions_model.FindTasks(ctx, actions_model.FindTaskOptions{
|
||||
Status: actions_model.StatusRunning,
|
||||
StartedBefore: timeutil.TimeStamp(time.Now().Add(-endlessTaskTimeout).Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -60,7 +60,7 @@ func StopEndlessTasks(ctx context.Context) error {
|
||||
|
||||
for _, task := range tasks {
|
||||
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
||||
if err := bots_model.StopTask(ctx, task.ID, bots_model.StatusFailure); err != nil {
|
||||
if err := actions_model.StopTask(ctx, task.ID, actions_model.StatusFailure); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := task.LoadJob(ctx); err != nil {
|
||||
@@ -77,8 +77,8 @@ func StopEndlessTasks(ctx context.Context) error {
|
||||
|
||||
// CancelAbandonedJobs cancels the jobs which have waiting status, but haven't been picked by a runner for a long time
|
||||
func CancelAbandonedJobs(ctx context.Context) error {
|
||||
jobs, _, err := bots_model.FindRunJobs(ctx, bots_model.FindRunJobOptions{
|
||||
Statuses: []bots_model.Status{bots_model.StatusWaiting, bots_model.StatusBlocked},
|
||||
jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{
|
||||
Statuses: []actions_model.Status{actions_model.StatusWaiting, actions_model.StatusBlocked},
|
||||
UpdatedBefore: timeutil.TimeStamp(time.Now().Add(-abandonedJobTimeout).Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -88,10 +88,10 @@ func CancelAbandonedJobs(ctx context.Context) error {
|
||||
|
||||
now := timeutil.TimeStampNow()
|
||||
for _, job := range jobs {
|
||||
job.Status = bots_model.StatusCancelled
|
||||
job.Status = actions_model.StatusCancelled
|
||||
job.Stopped = now
|
||||
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
||||
if _, err := bots_model.UpdateRunJob(ctx, job, nil, "status", "stopped"); err != nil {
|
||||
if _, err := actions_model.UpdateRunJob(ctx, job, nil, "status", "stopped"); err != nil {
|
||||
return err
|
||||
}
|
||||
return CreateCommitStatus(ctx, job)
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
bots_model "code.gitea.io/gitea/models/actions"
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/queue"
|
||||
@@ -46,11 +46,11 @@ func jobEmitterQueueHandle(data ...queue.Data) []queue.Data {
|
||||
|
||||
func checkJobsOfRun(ctx context.Context, runID int64) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
jobs, _, err := bots_model.FindRunJobs(ctx, bots_model.FindRunJobOptions{RunID: runID})
|
||||
jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: runID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
idToJobs := make(map[string][]*bots_model.BotRunJob, len(jobs))
|
||||
idToJobs := make(map[string][]*actions_model.BotRunJob, len(jobs))
|
||||
for _, job := range jobs {
|
||||
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func checkJobsOfRun(ctx context.Context, runID int64) error {
|
||||
for _, job := range jobs {
|
||||
if status, ok := updates[job.ID]; ok {
|
||||
job.Status = status
|
||||
if n, err := bots_model.UpdateRunJob(ctx, job, builder.Eq{"status": bots_model.StatusBlocked}, "status"); err != nil {
|
||||
if n, err := actions_model.UpdateRunJob(ctx, job, builder.Eq{"status": actions_model.StatusBlocked}, "status"); err != nil {
|
||||
return err
|
||||
} else if n != 1 {
|
||||
return fmt.Errorf("no affected for updating blocked job %v", job.ID)
|
||||
@@ -71,17 +71,17 @@ func checkJobsOfRun(ctx context.Context, runID int64) error {
|
||||
}
|
||||
|
||||
type jobStatusResolver struct {
|
||||
statuses map[int64]bots_model.Status
|
||||
statuses map[int64]actions_model.Status
|
||||
needs map[int64][]int64
|
||||
}
|
||||
|
||||
func newJobStatusResolver(jobs bots_model.RunJobList) *jobStatusResolver {
|
||||
idToJobs := make(map[string][]*bots_model.BotRunJob, len(jobs))
|
||||
func newJobStatusResolver(jobs actions_model.RunJobList) *jobStatusResolver {
|
||||
idToJobs := make(map[string][]*actions_model.BotRunJob, len(jobs))
|
||||
for _, job := range jobs {
|
||||
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
|
||||
}
|
||||
|
||||
statuses := make(map[int64]bots_model.Status, len(jobs))
|
||||
statuses := make(map[int64]actions_model.Status, len(jobs))
|
||||
needs := make(map[int64][]int64, len(jobs))
|
||||
for _, job := range jobs {
|
||||
statuses[job.ID] = job.Status
|
||||
@@ -97,8 +97,8 @@ func newJobStatusResolver(jobs bots_model.RunJobList) *jobStatusResolver {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *jobStatusResolver) Resolve() map[int64]bots_model.Status {
|
||||
ret := map[int64]bots_model.Status{}
|
||||
func (r *jobStatusResolver) Resolve() map[int64]actions_model.Status {
|
||||
ret := map[int64]actions_model.Status{}
|
||||
for i := 0; i < len(r.statuses); i++ {
|
||||
updated := r.resolve()
|
||||
if len(updated) == 0 {
|
||||
@@ -112,10 +112,10 @@ func (r *jobStatusResolver) Resolve() map[int64]bots_model.Status {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (r *jobStatusResolver) resolve() map[int64]bots_model.Status {
|
||||
ret := map[int64]bots_model.Status{}
|
||||
func (r *jobStatusResolver) resolve() map[int64]actions_model.Status {
|
||||
ret := map[int64]actions_model.Status{}
|
||||
for id, status := range r.statuses {
|
||||
if status != bots_model.StatusBlocked {
|
||||
if status != actions_model.StatusBlocked {
|
||||
continue
|
||||
}
|
||||
allDone, allSucceed := true, true
|
||||
@@ -124,15 +124,15 @@ func (r *jobStatusResolver) resolve() map[int64]bots_model.Status {
|
||||
if !needStatus.IsDone() {
|
||||
allDone = false
|
||||
}
|
||||
if needStatus.In(bots_model.StatusFailure, bots_model.StatusCancelled, bots_model.StatusSkipped) {
|
||||
if needStatus.In(actions_model.StatusFailure, actions_model.StatusCancelled, actions_model.StatusSkipped) {
|
||||
allSucceed = false
|
||||
}
|
||||
}
|
||||
if allDone {
|
||||
if allSucceed {
|
||||
ret[id] = bots_model.StatusWaiting
|
||||
ret[id] = actions_model.StatusWaiting
|
||||
} else {
|
||||
ret[id] = bots_model.StatusSkipped
|
||||
ret[id] = actions_model.StatusSkipped
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ package actions
|
||||
import (
|
||||
"testing"
|
||||
|
||||
bots_model "code.gitea.io/gitea/models/actions"
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -14,61 +14,61 @@ import (
|
||||
func Test_jobStatusResolver_Resolve(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jobs bots_model.RunJobList
|
||||
want map[int64]bots_model.Status
|
||||
jobs actions_model.RunJobList
|
||||
want map[int64]actions_model.Status
|
||||
}{
|
||||
{
|
||||
name: "no blocked",
|
||||
jobs: bots_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: bots_model.StatusWaiting, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: bots_model.StatusWaiting, Needs: []string{}},
|
||||
{ID: 3, JobID: "3", Status: bots_model.StatusWaiting, Needs: []string{}},
|
||||
jobs: actions_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: actions_model.StatusWaiting, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: actions_model.StatusWaiting, Needs: []string{}},
|
||||
{ID: 3, JobID: "3", Status: actions_model.StatusWaiting, Needs: []string{}},
|
||||
},
|
||||
want: map[int64]bots_model.Status{},
|
||||
want: map[int64]actions_model.Status{},
|
||||
},
|
||||
{
|
||||
name: "single blocked",
|
||||
jobs: bots_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: bots_model.StatusSuccess, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: bots_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: bots_model.StatusWaiting, Needs: []string{}},
|
||||
jobs: actions_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: actions_model.StatusSuccess, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: actions_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: actions_model.StatusWaiting, Needs: []string{}},
|
||||
},
|
||||
want: map[int64]bots_model.Status{
|
||||
2: bots_model.StatusWaiting,
|
||||
want: map[int64]actions_model.Status{
|
||||
2: actions_model.StatusWaiting,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple blocked",
|
||||
jobs: bots_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: bots_model.StatusSuccess, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: bots_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: bots_model.StatusBlocked, Needs: []string{"1"}},
|
||||
jobs: actions_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: actions_model.StatusSuccess, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: actions_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: actions_model.StatusBlocked, Needs: []string{"1"}},
|
||||
},
|
||||
want: map[int64]bots_model.Status{
|
||||
2: bots_model.StatusWaiting,
|
||||
3: bots_model.StatusWaiting,
|
||||
want: map[int64]actions_model.Status{
|
||||
2: actions_model.StatusWaiting,
|
||||
3: actions_model.StatusWaiting,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "chain blocked",
|
||||
jobs: bots_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: bots_model.StatusFailure, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: bots_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: bots_model.StatusBlocked, Needs: []string{"2"}},
|
||||
jobs: actions_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: actions_model.StatusFailure, Needs: []string{}},
|
||||
{ID: 2, JobID: "2", Status: actions_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: actions_model.StatusBlocked, Needs: []string{"2"}},
|
||||
},
|
||||
want: map[int64]bots_model.Status{
|
||||
2: bots_model.StatusSkipped,
|
||||
3: bots_model.StatusSkipped,
|
||||
want: map[int64]actions_model.Status{
|
||||
2: actions_model.StatusSkipped,
|
||||
3: actions_model.StatusSkipped,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "loop need",
|
||||
jobs: bots_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: bots_model.StatusBlocked, Needs: []string{"3"}},
|
||||
{ID: 2, JobID: "2", Status: bots_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: bots_model.StatusBlocked, Needs: []string{"2"}},
|
||||
jobs: actions_model.RunJobList{
|
||||
{ID: 1, JobID: "1", Status: actions_model.StatusBlocked, Needs: []string{"3"}},
|
||||
{ID: 2, JobID: "2", Status: actions_model.StatusBlocked, Needs: []string{"1"}},
|
||||
{ID: 3, JobID: "3", Status: actions_model.StatusBlocked, Needs: []string{"2"}},
|
||||
},
|
||||
want: map[int64]bots_model.Status{},
|
||||
want: map[int64]actions_model.Status{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user