Skip to content

Commit df1e7d0

Browse files
authored
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.
1 parent d24a822 commit df1e7d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+611
-685
lines changed

cmd/admin_auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"text/tabwriter"
1010

1111
auth_model "code.gitea.io/gitea/models/auth"
12+
"code.gitea.io/gitea/models/db"
1213
auth_service "code.gitea.io/gitea/services/auth"
1314

1415
"github.com/urfave/cli/v2"
@@ -62,7 +63,7 @@ func runListAuth(c *cli.Context) error {
6263
return err
6364
}
6465

65-
authSources, err := auth_model.FindSources(ctx, auth_model.FindSourcesOptions{})
66+
authSources, err := db.Find[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
6667
if err != nil {
6768
return err
6869
}

models/actions/artifact.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"code.gitea.io/gitea/models/db"
1515
"code.gitea.io/gitea/modules/timeutil"
1616
"code.gitea.io/gitea/modules/util"
17+
18+
"xorm.io/builder"
1719
)
1820

1921
// 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
108110
return err
109111
}
110112

111-
// ListArtifactsByRunID returns all artifacts of a run
112-
func ListArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) {
113-
arts := make([]*ActionArtifact, 0, 10)
114-
return arts, db.GetEngine(ctx).Where("run_id=?", runID).Find(&arts)
113+
type FindArtifactsOptions struct {
114+
db.ListOptions
115+
RepoID int64
116+
RunID int64
117+
ArtifactName string
118+
Status int
115119
}
116120

117-
// ListArtifactsByRunIDAndArtifactName returns an artifacts of a run by artifact name
118-
func ListArtifactsByRunIDAndArtifactName(ctx context.Context, runID int64, artifactName string) ([]*ActionArtifact, error) {
119-
arts := make([]*ActionArtifact, 0, 10)
120-
return arts, db.GetEngine(ctx).Where("run_id=? AND artifact_name=?", runID, artifactName).Find(&arts)
121-
}
121+
func (opts FindArtifactsOptions) ToConds() builder.Cond {
122+
cond := builder.NewCond()
123+
if opts.RepoID > 0 {
124+
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
125+
}
126+
if opts.RunID > 0 {
127+
cond = cond.And(builder.Eq{"run_id": opts.RunID})
128+
}
129+
if opts.ArtifactName != "" {
130+
cond = cond.And(builder.Eq{"artifact_name": opts.ArtifactName})
131+
}
132+
if opts.Status > 0 {
133+
cond = cond.And(builder.Eq{"status": opts.Status})
134+
}
122135

123-
// ListUploadedArtifactsByRunID returns all uploaded artifacts of a run
124-
func ListUploadedArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) {
125-
arts := make([]*ActionArtifact, 0, 10)
126-
return arts, db.GetEngine(ctx).Where("run_id=? AND status=?", runID, ArtifactStatusUploadConfirmed).Find(&arts)
136+
return cond
127137
}
128138

129139
// ActionArtifactMeta is the meta data of an artifact
130140
type ActionArtifactMeta struct {
131141
ArtifactName string
132142
FileSize int64
133-
Status int64
143+
Status ArtifactStatus
134144
}
135145

136146
// ListUploadedArtifactsMeta returns all uploaded artifacts meta of a run
@@ -143,18 +153,6 @@ func ListUploadedArtifactsMeta(ctx context.Context, runID int64) ([]*ActionArtif
143153
Find(&arts)
144154
}
145155

146-
// ListArtifactsByRepoID returns all artifacts of a repo
147-
func ListArtifactsByRepoID(ctx context.Context, repoID int64) ([]*ActionArtifact, error) {
148-
arts := make([]*ActionArtifact, 0, 10)
149-
return arts, db.GetEngine(ctx).Where("repo_id=?", repoID).Find(&arts)
150-
}
151-
152-
// ListArtifactsByRunIDAndName returns artifacts by name of a run
153-
func ListArtifactsByRunIDAndName(ctx context.Context, runID int64, name string) ([]*ActionArtifact, error) {
154-
arts := make([]*ActionArtifact, 0, 10)
155-
return arts, db.GetEngine(ctx).Where("run_id=? AND artifact_name=?", runID, name).Find(&arts)
156-
}
157-
158156
// ListNeedExpiredArtifacts returns all need expired artifacts but not deleted
159157
func ListNeedExpiredArtifacts(ctx context.Context) ([]*ActionArtifact, error) {
160158
arts := make([]*ActionArtifact, 0, 10)

models/actions/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
170170
// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow.
171171
func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string) error {
172172
// Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'.
173-
runs, total, err := FindRuns(ctx, FindRunOptions{
173+
runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{
174174
RepoID: repoID,
175175
Ref: ref,
176176
WorkflowID: workflowID,
@@ -188,7 +188,7 @@ func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string
188188
// Iterate over each found run and cancel its associated jobs.
189189
for _, run := range runs {
190190
// Find all jobs associated with the current run.
191-
jobs, _, err := FindRunJobs(ctx, FindRunJobOptions{
191+
jobs, err := db.Find[ActionRunJob](ctx, FindRunJobOptions{
192192
RunID: run.ID,
193193
})
194194
if err != nil {

models/actions/run_job_list.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type FindRunJobOptions struct {
6161
UpdatedBefore timeutil.TimeStamp
6262
}
6363

64-
func (opts FindRunJobOptions) toConds() builder.Cond {
64+
func (opts FindRunJobOptions) ToConds() builder.Cond {
6565
cond := builder.NewCond()
6666
if opts.RunID > 0 {
6767
cond = cond.And(builder.Eq{"run_id": opts.RunID})
@@ -83,17 +83,3 @@ func (opts FindRunJobOptions) toConds() builder.Cond {
8383
}
8484
return cond
8585
}
86-
87-
func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (ActionJobList, int64, error) {
88-
e := db.GetEngine(ctx).Where(opts.toConds())
89-
if opts.PageSize > 0 && opts.Page >= 1 {
90-
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
91-
}
92-
var tasks ActionJobList
93-
total, err := e.FindAndCount(&tasks)
94-
return tasks, total, err
95-
}
96-
97-
func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
98-
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRunJob))
99-
}

models/actions/run_list.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type FindRunOptions struct {
7575
Status []Status
7676
}
7777

78-
func (opts FindRunOptions) toConds() builder.Cond {
78+
func (opts FindRunOptions) ToConds() builder.Cond {
7979
cond := builder.NewCond()
8080
if opts.RepoID > 0 {
8181
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
@@ -101,18 +101,8 @@ func (opts FindRunOptions) toConds() builder.Cond {
101101
return cond
102102
}
103103

104-
func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) {
105-
e := db.GetEngine(ctx).Where(opts.toConds())
106-
if opts.PageSize > 0 && opts.Page >= 1 {
107-
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
108-
}
109-
var runs RunList
110-
total, err := e.Desc("id").FindAndCount(&runs)
111-
return runs, total, err
112-
}
113-
114-
func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
115-
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRun))
104+
func (opts FindRunOptions) ToOrders() string {
105+
return "`id` DESC"
116106
}
117107

118108
type StatusInfo struct {

models/actions/runner.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ type FindRunnerOptions struct {
156156
WithAvailable bool // not only runners belong to, but also runners can be used
157157
}
158158

159-
func (opts FindRunnerOptions) toCond() builder.Cond {
159+
func (opts FindRunnerOptions) ToConds() builder.Cond {
160160
cond := builder.NewCond()
161161

162162
if opts.RepoID > 0 {
@@ -181,7 +181,7 @@ func (opts FindRunnerOptions) toCond() builder.Cond {
181181
return cond
182182
}
183183

184-
func (opts FindRunnerOptions) toOrder() string {
184+
func (opts FindRunnerOptions) ToOrders() string {
185185
switch opts.Sort {
186186
case "online":
187187
return "last_online DESC"
@@ -199,22 +199,6 @@ func (opts FindRunnerOptions) toOrder() string {
199199
return "last_online DESC"
200200
}
201201

202-
func CountRunners(ctx context.Context, opts FindRunnerOptions) (int64, error) {
203-
return db.GetEngine(ctx).
204-
Where(opts.toCond()).
205-
Count(ActionRunner{})
206-
}
207-
208-
func FindRunners(ctx context.Context, opts FindRunnerOptions) (runners RunnerList, err error) {
209-
sess := db.GetEngine(ctx).
210-
Where(opts.toCond()).
211-
OrderBy(opts.toOrder())
212-
if opts.Page > 0 {
213-
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
214-
}
215-
return runners, sess.Find(&runners)
216-
}
217-
218202
// GetRunnerByUUID returns a runner via uuid
219203
func GetRunnerByUUID(ctx context.Context, uuid string) (*ActionRunner, error) {
220204
var runner ActionRunner
@@ -263,8 +247,7 @@ func DeleteRunner(ctx context.Context, id int64) error {
263247

264248
// CreateRunner creates new runner.
265249
func CreateRunner(ctx context.Context, t *ActionRunner) error {
266-
_, err := db.GetEngine(ctx).Insert(t)
267-
return err
250+
return db.Insert(ctx, t)
268251
}
269252

270253
func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {

models/actions/schedule_list.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type FindScheduleOptions struct {
6767
OwnerID int64
6868
}
6969

70-
func (opts FindScheduleOptions) toConds() builder.Cond {
70+
func (opts FindScheduleOptions) ToConds() builder.Cond {
7171
cond := builder.NewCond()
7272
if opts.RepoID > 0 {
7373
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
@@ -79,16 +79,6 @@ func (opts FindScheduleOptions) toConds() builder.Cond {
7979
return cond
8080
}
8181

82-
func FindSchedules(ctx context.Context, opts FindScheduleOptions) (ScheduleList, int64, error) {
83-
e := db.GetEngine(ctx).Where(opts.toConds())
84-
if !opts.ListAll && opts.PageSize > 0 && opts.Page >= 1 {
85-
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
86-
}
87-
var schedules ScheduleList
88-
total, err := e.Desc("id").FindAndCount(&schedules)
89-
return schedules, total, err
90-
}
91-
92-
func CountSchedules(ctx context.Context, opts FindScheduleOptions) (int64, error) {
93-
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionSchedule))
82+
func (opts FindScheduleOptions) ToOrders() string {
83+
return "`id` DESC"
9484
}

models/actions/schedule_spec_list.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type FindSpecOptions struct {
7171
Next int64
7272
}
7373

74-
func (opts FindSpecOptions) toConds() builder.Cond {
74+
func (opts FindSpecOptions) ToConds() builder.Cond {
7575
cond := builder.NewCond()
7676
if opts.RepoID > 0 {
7777
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
@@ -84,23 +84,18 @@ func (opts FindSpecOptions) toConds() builder.Cond {
8484
return cond
8585
}
8686

87+
func (opts FindSpecOptions) ToOrders() string {
88+
return "`id` DESC"
89+
}
90+
8791
func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
88-
e := db.GetEngine(ctx).Where(opts.toConds())
89-
if opts.PageSize > 0 && opts.Page >= 1 {
90-
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
91-
}
92-
var specs SpecList
93-
total, err := e.Desc("id").FindAndCount(&specs)
92+
specs, total, err := db.FindAndCount[ActionScheduleSpec](ctx, opts)
9493
if err != nil {
9594
return nil, 0, err
9695
}
9796

98-
if err := specs.LoadSchedules(ctx); err != nil {
97+
if err := SpecList(specs).LoadSchedules(ctx); err != nil {
9998
return nil, 0, err
10099
}
101100
return specs, total, nil
102101
}
103-
104-
func CountSpecs(ctx context.Context, opts FindSpecOptions) (int64, error) {
105-
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionScheduleSpec))
106-
}

models/actions/task_list.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type FindTaskOptions struct {
6262
IDOrderDesc bool
6363
}
6464

65-
func (opts FindTaskOptions) toConds() builder.Cond {
65+
func (opts FindTaskOptions) ToConds() builder.Cond {
6666
cond := builder.NewCond()
6767
if opts.RepoID > 0 {
6868
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
@@ -88,18 +88,9 @@ func (opts FindTaskOptions) toConds() builder.Cond {
8888
return cond
8989
}
9090

91-
func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, error) {
92-
e := db.GetEngine(ctx).Where(opts.toConds())
93-
if opts.PageSize > 0 && opts.Page >= 1 {
94-
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
95-
}
91+
func (opts FindTaskOptions) ToOrders() string {
9692
if opts.IDOrderDesc {
97-
e.OrderBy("id DESC")
93+
return "`id` DESC"
9894
}
99-
var tasks TaskList
100-
return tasks, e.Find(&tasks)
101-
}
102-
103-
func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
104-
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionTask))
95+
return ""
10596
}

models/actions/variable.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type FindVariablesOpts struct {
5656
RepoID int64
5757
}
5858

59-
func (opts *FindVariablesOpts) toConds() builder.Cond {
59+
func (opts FindVariablesOpts) ToConds() builder.Cond {
6060
cond := builder.NewCond()
6161
if opts.OwnerID > 0 {
6262
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
@@ -67,15 +67,6 @@ func (opts *FindVariablesOpts) toConds() builder.Cond {
6767
return cond
6868
}
6969

70-
func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) {
71-
var variables []*ActionVariable
72-
sess := db.GetEngine(ctx)
73-
if opts.PageSize != 0 {
74-
sess = db.SetSessionPagination(sess, &opts.ListOptions)
75-
}
76-
return variables, sess.Where(opts.toConds()).Find(&variables)
77-
}
78-
7970
func GetVariableByID(ctx context.Context, variableID int64) (*ActionVariable, error) {
8071
var variable ActionVariable
8172
has, err := db.GetEngine(ctx).Where("id=?", variableID).Get(&variable)

0 commit comments

Comments
 (0)