diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-11-24 11:49:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-24 03:49:41 +0000 |
commit | df1e7d0067bb39913eb681ccc920649884fb1938 (patch) | |
tree | 2419feab5c28658adb7f71878df646bdc9bdc50e /models/actions | |
parent | d24a8223ce1e47a0c9b103aae07f67c3112ca048 (diff) | |
download | gitea-df1e7d0067bb39913eb681ccc920649884fb1938.tar.gz gitea-df1e7d0067bb39913eb681ccc920649884fb1938.zip |
Use db.Find instead of writing methods for every object (#28084)
For those simple objects, it's unnecessary to write the find and count
methods again and again.
Diffstat (limited to 'models/actions')
-rw-r--r-- | models/actions/artifact.go | 50 | ||||
-rw-r--r-- | models/actions/run.go | 4 | ||||
-rw-r--r-- | models/actions/run_job_list.go | 16 | ||||
-rw-r--r-- | models/actions/run_list.go | 16 | ||||
-rw-r--r-- | models/actions/runner.go | 23 | ||||
-rw-r--r-- | models/actions/schedule_list.go | 16 | ||||
-rw-r--r-- | models/actions/schedule_spec_list.go | 19 | ||||
-rw-r--r-- | models/actions/task_list.go | 17 | ||||
-rw-r--r-- | models/actions/variable.go | 11 |
9 files changed, 48 insertions, 124 deletions
diff --git a/models/actions/artifact.go b/models/actions/artifact.go index 849a90fd10..42bd9c23cb 100644 --- a/models/actions/artifact.go +++ b/models/actions/artifact.go @@ -14,6 +14,8 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" + + "xorm.io/builder" ) // ArtifactStatus is the status of an artifact, uploading, expired or need-delete @@ -108,29 +110,37 @@ func UpdateArtifactByID(ctx context.Context, id int64, art *ActionArtifact) erro return err } -// ListArtifactsByRunID returns all artifacts of a run -func ListArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) { - arts := make([]*ActionArtifact, 0, 10) - return arts, db.GetEngine(ctx).Where("run_id=?", runID).Find(&arts) +type FindArtifactsOptions struct { + db.ListOptions + RepoID int64 + RunID int64 + ArtifactName string + Status int } -// ListArtifactsByRunIDAndArtifactName returns an artifacts of a run by artifact name -func ListArtifactsByRunIDAndArtifactName(ctx context.Context, runID int64, artifactName string) ([]*ActionArtifact, error) { - arts := make([]*ActionArtifact, 0, 10) - return arts, db.GetEngine(ctx).Where("run_id=? AND artifact_name=?", runID, artifactName).Find(&arts) -} +func (opts FindArtifactsOptions) ToConds() builder.Cond { + cond := builder.NewCond() + if opts.RepoID > 0 { + cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) + } + if opts.RunID > 0 { + cond = cond.And(builder.Eq{"run_id": opts.RunID}) + } + if opts.ArtifactName != "" { + cond = cond.And(builder.Eq{"artifact_name": opts.ArtifactName}) + } + if opts.Status > 0 { + cond = cond.And(builder.Eq{"status": opts.Status}) + } -// ListUploadedArtifactsByRunID returns all uploaded artifacts of a run -func ListUploadedArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) { - arts := make([]*ActionArtifact, 0, 10) - return arts, db.GetEngine(ctx).Where("run_id=? AND status=?", runID, ArtifactStatusUploadConfirmed).Find(&arts) + return cond } // ActionArtifactMeta is the meta data of an artifact type ActionArtifactMeta struct { ArtifactName string FileSize int64 - Status int64 + Status ArtifactStatus } // ListUploadedArtifactsMeta returns all uploaded artifacts meta of a run @@ -143,18 +153,6 @@ func ListUploadedArtifactsMeta(ctx context.Context, runID int64) ([]*ActionArtif Find(&arts) } -// ListArtifactsByRepoID returns all artifacts of a repo -func ListArtifactsByRepoID(ctx context.Context, repoID int64) ([]*ActionArtifact, error) { - arts := make([]*ActionArtifact, 0, 10) - return arts, db.GetEngine(ctx).Where("repo_id=?", repoID).Find(&arts) -} - -// ListArtifactsByRunIDAndName returns artifacts by name of a run -func ListArtifactsByRunIDAndName(ctx context.Context, runID int64, name string) ([]*ActionArtifact, error) { - arts := make([]*ActionArtifact, 0, 10) - return arts, db.GetEngine(ctx).Where("run_id=? AND artifact_name=?", runID, name).Find(&arts) -} - // ListNeedExpiredArtifacts returns all need expired artifacts but not deleted func ListNeedExpiredArtifacts(ctx context.Context) ([]*ActionArtifact, error) { arts := make([]*ActionArtifact, 0, 10) diff --git a/models/actions/run.go b/models/actions/run.go index 8078613fb8..4656aa22a2 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -170,7 +170,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err // CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow. func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string) error { // Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'. - runs, total, err := FindRuns(ctx, FindRunOptions{ + runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{ RepoID: repoID, Ref: ref, WorkflowID: workflowID, @@ -188,7 +188,7 @@ func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string // Iterate over each found run and cancel its associated jobs. for _, run := range runs { // Find all jobs associated with the current run. - jobs, _, err := FindRunJobs(ctx, FindRunJobOptions{ + jobs, err := db.Find[ActionRunJob](ctx, FindRunJobOptions{ RunID: run.ID, }) if err != nil { diff --git a/models/actions/run_job_list.go b/models/actions/run_job_list.go index a166396694..6ea6cb9d3b 100644 --- a/models/actions/run_job_list.go +++ b/models/actions/run_job_list.go @@ -61,7 +61,7 @@ type FindRunJobOptions struct { UpdatedBefore timeutil.TimeStamp } -func (opts FindRunJobOptions) toConds() builder.Cond { +func (opts FindRunJobOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RunID > 0 { cond = cond.And(builder.Eq{"run_id": opts.RunID}) @@ -83,17 +83,3 @@ func (opts FindRunJobOptions) toConds() builder.Cond { } return cond } - -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 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(ActionRunJob)) -} diff --git a/models/actions/run_list.go b/models/actions/run_list.go index cd053ea7b5..375c46221b 100644 --- a/models/actions/run_list.go +++ b/models/actions/run_list.go @@ -75,7 +75,7 @@ type FindRunOptions struct { Status []Status } -func (opts FindRunOptions) toConds() builder.Cond { +func (opts FindRunOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RepoID > 0 { cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) @@ -101,18 +101,8 @@ func (opts FindRunOptions) toConds() builder.Cond { return cond } -func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, 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 runs RunList - total, err := e.Desc("id").FindAndCount(&runs) - return runs, total, err -} - -func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) { - return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRun)) +func (opts FindRunOptions) ToOrders() string { + return "`id` DESC" } type StatusInfo struct { diff --git a/models/actions/runner.go b/models/actions/runner.go index 2c092c2b4a..717b770800 100644 --- a/models/actions/runner.go +++ b/models/actions/runner.go @@ -156,7 +156,7 @@ type FindRunnerOptions struct { WithAvailable bool // not only runners belong to, but also runners can be used } -func (opts FindRunnerOptions) toCond() builder.Cond { +func (opts FindRunnerOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RepoID > 0 { @@ -181,7 +181,7 @@ func (opts FindRunnerOptions) toCond() builder.Cond { return cond } -func (opts FindRunnerOptions) toOrder() string { +func (opts FindRunnerOptions) ToOrders() string { switch opts.Sort { case "online": return "last_online DESC" @@ -199,22 +199,6 @@ func (opts FindRunnerOptions) toOrder() string { return "last_online DESC" } -func CountRunners(ctx context.Context, opts FindRunnerOptions) (int64, error) { - return db.GetEngine(ctx). - Where(opts.toCond()). - Count(ActionRunner{}) -} - -func FindRunners(ctx context.Context, opts FindRunnerOptions) (runners RunnerList, err error) { - sess := db.GetEngine(ctx). - Where(opts.toCond()). - OrderBy(opts.toOrder()) - if opts.Page > 0 { - sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) - } - return runners, sess.Find(&runners) -} - // GetRunnerByUUID returns a runner via uuid func GetRunnerByUUID(ctx context.Context, uuid string) (*ActionRunner, error) { var runner ActionRunner @@ -263,8 +247,7 @@ func DeleteRunner(ctx context.Context, id int64) error { // CreateRunner creates new runner. func CreateRunner(ctx context.Context, t *ActionRunner) error { - _, err := db.GetEngine(ctx).Insert(t) - return err + return db.Insert(ctx, t) } func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) { diff --git a/models/actions/schedule_list.go b/models/actions/schedule_list.go index ddd9a1321e..b806550b87 100644 --- a/models/actions/schedule_list.go +++ b/models/actions/schedule_list.go @@ -67,7 +67,7 @@ type FindScheduleOptions struct { OwnerID int64 } -func (opts FindScheduleOptions) toConds() builder.Cond { +func (opts FindScheduleOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RepoID > 0 { cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) @@ -79,16 +79,6 @@ func (opts FindScheduleOptions) toConds() builder.Cond { return cond } -func FindSchedules(ctx context.Context, opts FindScheduleOptions) (ScheduleList, int64, error) { - e := db.GetEngine(ctx).Where(opts.toConds()) - if !opts.ListAll && 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)) +func (opts FindScheduleOptions) ToOrders() string { + return "`id` DESC" } diff --git a/models/actions/schedule_spec_list.go b/models/actions/schedule_spec_list.go index 6bf91cf819..e9ae268a6e 100644 --- a/models/actions/schedule_spec_list.go +++ b/models/actions/schedule_spec_list.go @@ -71,7 +71,7 @@ type FindSpecOptions struct { Next int64 } -func (opts FindSpecOptions) toConds() builder.Cond { +func (opts FindSpecOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RepoID > 0 { cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) @@ -84,23 +84,18 @@ func (opts FindSpecOptions) toConds() builder.Cond { return cond } +func (opts FindSpecOptions) ToOrders() string { + return "`id` DESC" +} + func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, 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 specs SpecList - total, err := e.Desc("id").FindAndCount(&specs) + specs, total, err := db.FindAndCount[ActionScheduleSpec](ctx, opts) if err != nil { return nil, 0, err } - if err := specs.LoadSchedules(ctx); err != nil { + if err := SpecList(specs).LoadSchedules(ctx); err != nil { return nil, 0, err } return specs, total, nil } - -func CountSpecs(ctx context.Context, opts FindSpecOptions) (int64, error) { - return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionScheduleSpec)) -} diff --git a/models/actions/task_list.go b/models/actions/task_list.go index 1f6b16772b..b07d00b8db 100644 --- a/models/actions/task_list.go +++ b/models/actions/task_list.go @@ -62,7 +62,7 @@ type FindTaskOptions struct { IDOrderDesc bool } -func (opts FindTaskOptions) toConds() builder.Cond { +func (opts FindTaskOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RepoID > 0 { cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) @@ -88,18 +88,9 @@ func (opts FindTaskOptions) toConds() builder.Cond { return cond } -func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, error) { - e := db.GetEngine(ctx).Where(opts.toConds()) - if opts.PageSize > 0 && opts.Page >= 1 { - e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) - } +func (opts FindTaskOptions) ToOrders() string { if opts.IDOrderDesc { - e.OrderBy("id DESC") + return "`id` DESC" } - var tasks TaskList - return tasks, e.Find(&tasks) -} - -func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) { - return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionTask)) + return "" } diff --git a/models/actions/variable.go b/models/actions/variable.go index e0bb59ccbe..030b7bae92 100644 --- a/models/actions/variable.go +++ b/models/actions/variable.go @@ -56,7 +56,7 @@ type FindVariablesOpts struct { RepoID int64 } -func (opts *FindVariablesOpts) toConds() builder.Cond { +func (opts FindVariablesOpts) ToConds() builder.Cond { cond := builder.NewCond() if opts.OwnerID > 0 { cond = cond.And(builder.Eq{"owner_id": opts.OwnerID}) @@ -67,15 +67,6 @@ func (opts *FindVariablesOpts) toConds() builder.Cond { return cond } -func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) { - var variables []*ActionVariable - sess := db.GetEngine(ctx) - if opts.PageSize != 0 { - sess = db.SetSessionPagination(sess, &opts.ListOptions) - } - return variables, sess.Where(opts.toConds()).Find(&variables) -} - func GetVariableByID(ctx context.Context, variableID int64) (*ActionVariable, error) { var variable ActionVariable has, err := db.GetEngine(ctx).Where("id=?", variableID).Get(&variable) |