diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2024-01-15 10:19:25 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-15 02:19:25 +0000 |
commit | 70c4aad8e1cbc46b049b015dcd6f2e5be5a69e72 (patch) | |
tree | 5c9f049437c1a84a402aeef3be0cb4b95dea667e /models/repo | |
parent | e5313248a8ed967a915f072d3991b6d046badf02 (diff) | |
download | gitea-70c4aad8e1cbc46b049b015dcd6f2e5be5a69e72.tar.gz gitea-70c4aad8e1cbc46b049b015dcd6f2e5be5a69e72.zip |
Move more functions to db.Find (#28419)
Following #28220
This PR move more functions to use `db.Find`.
---------
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'models/repo')
-rw-r--r-- | models/repo/archiver.go | 13 | ||||
-rw-r--r-- | models/repo/collaboration.go | 55 | ||||
-rw-r--r-- | models/repo/collaboration_test.go | 12 | ||||
-rw-r--r-- | models/repo/fork.go | 11 | ||||
-rw-r--r-- | models/repo/release.go | 42 |
5 files changed, 53 insertions, 80 deletions
diff --git a/models/repo/archiver.go b/models/repo/archiver.go index 1fccb29499..d9520c670c 100644 --- a/models/repo/archiver.go +++ b/models/repo/archiver.go @@ -111,7 +111,7 @@ type FindRepoArchiversOption struct { OlderThan time.Duration } -func (opts FindRepoArchiversOption) toConds() builder.Cond { +func (opts FindRepoArchiversOption) ToConds() builder.Cond { cond := builder.NewCond() if opts.OlderThan > 0 { cond = cond.And(builder.Lt{"created_unix": time.Now().Add(-opts.OlderThan).Unix()}) @@ -119,15 +119,8 @@ func (opts FindRepoArchiversOption) toConds() builder.Cond { return cond } -// FindRepoArchives find repo archivers -func FindRepoArchives(ctx context.Context, opts FindRepoArchiversOption) ([]*RepoArchiver, error) { - archivers := make([]*RepoArchiver, 0, opts.PageSize) - start, limit := opts.GetSkipTake() - err := db.GetEngine(ctx).Where(opts.toConds()). - Asc("created_unix"). - Limit(limit, start). - Find(&archivers) - return archivers, err +func (opts FindRepoArchiversOption) ToOrders() string { + return "created_unix ASC" } // SetArchiveRepoState sets if a repo is archived diff --git a/models/repo/collaboration.go b/models/repo/collaboration.go index 2018ae2a7d..7288082614 100644 --- a/models/repo/collaboration.go +++ b/models/repo/collaboration.go @@ -11,8 +11,9 @@ import ( "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/builder" ) // Collaboration represent the relation between an individual and a repository. @@ -37,35 +38,38 @@ type Collaborator struct { // GetCollaborators returns the collaborators for a repository func GetCollaborators(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*Collaborator, error) { - collaborations, err := getCollaborations(ctx, repoID, listOptions) + collaborations, err := db.Find[Collaboration](ctx, FindCollaborationOptions{ + ListOptions: listOptions, + RepoID: repoID, + }) if err != nil { - return nil, fmt.Errorf("getCollaborations: %w", err) + return nil, fmt.Errorf("db.Find[Collaboration]: %w", err) } collaborators := make([]*Collaborator, 0, len(collaborations)) + userIDs := make([]int64, 0, len(collaborations)) for _, c := range collaborations { - user, err := user_model.GetUserByID(ctx, c.UserID) - if err != nil { - if user_model.IsErrUserNotExist(err) { - log.Warn("Inconsistent DB: User: %d is listed as collaborator of %-v but does not exist", c.UserID, repoID) - user = user_model.NewGhostUser() - } else { - return nil, err - } + userIDs = append(userIDs, c.UserID) + } + + usersMap := make(map[int64]*user_model.User) + if err := db.GetEngine(ctx).In("id", userIDs).Find(&usersMap); err != nil { + return nil, fmt.Errorf("Find users map by user ids: %w", err) + } + + for _, c := range collaborations { + u := usersMap[c.UserID] + if u == nil { + u = user_model.NewGhostUser() } collaborators = append(collaborators, &Collaborator{ - User: user, + User: u, Collaboration: c, }) } return collaborators, nil } -// CountCollaborators returns total number of collaborators for a repository -func CountCollaborators(ctx context.Context, repoID int64) (int64, error) { - return db.GetEngine(ctx).Where("repo_id = ? ", repoID).Count(&Collaboration{}) -} - // GetCollaboration get collaboration for a repository id with a user id func GetCollaboration(ctx context.Context, repoID, uid int64) (*Collaboration, error) { collaboration := &Collaboration{ @@ -84,18 +88,13 @@ func IsCollaborator(ctx context.Context, repoID, userID int64) (bool, error) { return db.GetEngine(ctx).Get(&Collaboration{RepoID: repoID, UserID: userID}) } -func getCollaborations(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*Collaboration, error) { - if listOptions.Page == 0 { - collaborations := make([]*Collaboration, 0, 8) - return collaborations, db.GetEngine(ctx).Find(&collaborations, &Collaboration{RepoID: repoID}) - } - - e := db.GetEngine(ctx) - - e = db.SetEnginePagination(e, &listOptions) +type FindCollaborationOptions struct { + db.ListOptions + RepoID int64 +} - collaborations := make([]*Collaboration, 0, listOptions.PageSize) - return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repoID}) +func (opts FindCollaborationOptions) ToConds() builder.Cond { + return builder.And(builder.Eq{"repo_id": opts.RepoID}) } // ChangeCollaborationAccessMode sets new access mode for the collaboration. diff --git a/models/repo/collaboration_test.go b/models/repo/collaboration_test.go index 38114c307f..21a99dd557 100644 --- a/models/repo/collaboration_test.go +++ b/models/repo/collaboration_test.go @@ -89,17 +89,23 @@ func TestRepository_CountCollaborators(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}) - count, err := repo_model.CountCollaborators(db.DefaultContext, repo1.ID) + count, err := db.Count[repo_model.Collaboration](db.DefaultContext, repo_model.FindCollaborationOptions{ + RepoID: repo1.ID, + }) assert.NoError(t, err) assert.EqualValues(t, 2, count) repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22}) - count, err = repo_model.CountCollaborators(db.DefaultContext, repo2.ID) + count, err = db.Count[repo_model.Collaboration](db.DefaultContext, repo_model.FindCollaborationOptions{ + RepoID: repo2.ID, + }) assert.NoError(t, err) assert.EqualValues(t, 2, count) // Non-existent repository. - count, err = repo_model.CountCollaborators(db.DefaultContext, unittest.NonexistentID) + count, err = db.Count[repo_model.Collaboration](db.DefaultContext, repo_model.FindCollaborationOptions{ + RepoID: unittest.NonexistentID, + }) assert.NoError(t, err) assert.EqualValues(t, 0, count) } diff --git a/models/repo/fork.go b/models/repo/fork.go index 6be6ebc3f5..07cd31c269 100644 --- a/models/repo/fork.go +++ b/models/repo/fork.go @@ -56,13 +56,16 @@ func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) // GetForks returns all the forks of the repository func GetForks(ctx context.Context, repo *Repository, listOptions db.ListOptions) ([]*Repository, error) { + sess := db.GetEngine(ctx) + + var forks []*Repository if listOptions.Page == 0 { - forks := make([]*Repository, 0, repo.NumForks) - return forks, db.GetEngine(ctx).Find(&forks, &Repository{ForkID: repo.ID}) + forks = make([]*Repository, 0, repo.NumForks) + } else { + forks = make([]*Repository, 0, listOptions.PageSize) + sess = db.SetSessionPagination(sess, &listOptions) } - sess := db.GetPaginatedSession(&listOptions) - forks := make([]*Repository, 0, listOptions.PageSize) return forks, sess.Find(&forks, &Repository{ForkID: repo.ID}) } diff --git a/models/repo/release.go b/models/repo/release.go index 4514a034ed..72a73f8e80 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -225,6 +225,7 @@ func GetReleaseForRepoByID(ctx context.Context, repoID, id int64) (*Release, err // FindReleasesOptions describes the conditions to Find releases type FindReleasesOptions struct { db.ListOptions + RepoID int64 IncludeDrafts bool IncludeTags bool IsPreRelease util.OptionalBool @@ -233,9 +234,8 @@ type FindReleasesOptions struct { HasSha1 util.OptionalBool // useful to find draft releases which are created with existing tags } -func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { - cond := builder.NewCond() - cond = cond.And(builder.Eq{"repo_id": repoID}) +func (opts FindReleasesOptions) ToConds() builder.Cond { + var cond builder.Cond = builder.Eq{"repo_id": opts.RepoID} if !opts.IncludeDrafts { cond = cond.And(builder.Eq{"is_draft": false}) @@ -262,18 +262,8 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { return cond } -// GetReleasesByRepoID returns a list of releases of repository. -func GetReleasesByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) ([]*Release, error) { - sess := db.GetEngine(ctx). - Desc("created_unix", "id"). - Where(opts.toConds(repoID)) - - if opts.PageSize != 0 { - sess = db.SetSessionPagination(sess, &opts.ListOptions) - } - - rels := make([]*Release, 0, opts.PageSize) - return rels, sess.Find(&rels) +func (opts FindReleasesOptions) ToOrders() string { + return "created_unix DESC, id DESC" } // GetTagNamesByRepoID returns a list of release tag names of repository. @@ -286,23 +276,19 @@ func GetTagNamesByRepoID(ctx context.Context, repoID int64) ([]string, error) { IncludeDrafts: true, IncludeTags: true, HasSha1: util.OptionalBoolTrue, + RepoID: repoID, } tags := make([]string, 0) sess := db.GetEngine(ctx). Table("release"). Desc("created_unix", "id"). - Where(opts.toConds(repoID)). + Where(opts.ToConds()). Cols("tag_name") return tags, sess.Find(&tags) } -// CountReleasesByRepoID returns a number of releases matching FindReleaseOptions and RepoID. -func CountReleasesByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) (int64, error) { - return db.GetEngine(ctx).Where(opts.toConds(repoID)).Count(new(Release)) -} - // GetLatestReleaseByRepoID returns the latest release for a repository func GetLatestReleaseByRepoID(ctx context.Context, repoID int64) (*Release, error) { cond := builder.NewCond(). @@ -325,20 +311,6 @@ func GetLatestReleaseByRepoID(ctx context.Context, repoID int64) (*Release, erro return rel, nil } -// GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames. -func GetReleasesByRepoIDAndNames(ctx context.Context, repoID int64, tagNames []string) (rels []*Release, err error) { - err = db.GetEngine(ctx). - In("tag_name", tagNames). - Desc("created_unix"). - Find(&rels, Release{RepoID: repoID}) - return rels, err -} - -// GetReleaseCountByRepoID returns the count of releases of repository -func GetReleaseCountByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) (int64, error) { - return db.GetEngine(ctx).Where(opts.toConds(repoID)).Count(&Release{}) -} - type releaseMetaSearch struct { ID []int64 Rel []*Release |