Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df1e7d0

Browse files
authoredNov 24, 2023
Use db.Find instead of writing methods for every object (go-gitea#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

+2-1
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

+24-26
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)

0 commit comments

Comments
 (0)
Please sign in to comment.