diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /models/repo_collaboration.go | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'models/repo_collaboration.go')
-rw-r--r-- | models/repo_collaboration.go | 79 |
1 files changed, 41 insertions, 38 deletions
diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go index 08682eef61..ab6a3bafbe 100644 --- a/models/repo_collaboration.go +++ b/models/repo_collaboration.go @@ -6,10 +6,12 @@ package models import ( + "context" "fmt" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" @@ -32,11 +34,12 @@ func init() { db.RegisterModel(new(Collaboration)) } -func (repo *Repository) addCollaborator(e db.Engine, u *user_model.User) error { +func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_model.User) error { collaboration := &Collaboration{ RepoID: repo.ID, UserID: u.ID, } + e := db.GetEngine(ctx) has, err := e.Get(collaboration) if err != nil { @@ -50,34 +53,34 @@ func (repo *Repository) addCollaborator(e db.Engine, u *user_model.User) error { return err } - return repo.recalculateUserAccess(e, u.ID) + return recalculateUserAccess(ctx, repo, u.ID) } // AddCollaborator adds new collaboration to a repository with default access mode. -func (repo *Repository) AddCollaborator(u *user_model.User) error { +func AddCollaborator(repo *repo_model.Repository, u *user_model.User) error { ctx, committer, err := db.TxContext() if err != nil { return err } defer committer.Close() - if err := repo.addCollaborator(db.GetEngine(ctx), u); err != nil { + if err := addCollaborator(ctx, repo, u); err != nil { return err } return committer.Commit() } -func (repo *Repository) getCollaborations(e db.Engine, listOptions db.ListOptions) ([]*Collaboration, error) { +func getCollaborations(e db.Engine, repoID int64, listOptions db.ListOptions) ([]*Collaboration, error) { if listOptions.Page == 0 { collaborations := make([]*Collaboration, 0, 8) - return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID}) + return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repoID}) } e = db.SetEnginePagination(e, &listOptions) collaborations := make([]*Collaboration, 0, listOptions.PageSize) - return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID}) + return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repoID}) } // Collaborator represents a user with collaboration details. @@ -86,8 +89,8 @@ type Collaborator struct { Collaboration *Collaboration } -func (repo *Repository) getCollaborators(e db.Engine, listOptions db.ListOptions) ([]*Collaborator, error) { - collaborations, err := repo.getCollaborations(e, listOptions) +func getCollaborators(e db.Engine, repoID int64, listOptions db.ListOptions) ([]*Collaborator, error) { + collaborations, err := getCollaborations(e, repoID, listOptions) if err != nil { return nil, fmt.Errorf("getCollaborations: %v", err) } @@ -97,7 +100,7 @@ func (repo *Repository) getCollaborators(e db.Engine, listOptions db.ListOptions user, err := user_model.GetUserByIDEngine(e, 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, repo) + 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 @@ -112,18 +115,18 @@ func (repo *Repository) getCollaborators(e db.Engine, listOptions db.ListOptions } // GetCollaborators returns the collaborators for a repository -func (repo *Repository) GetCollaborators(listOptions db.ListOptions) ([]*Collaborator, error) { - return repo.getCollaborators(db.GetEngine(db.DefaultContext), listOptions) +func GetCollaborators(repoID int64, listOptions db.ListOptions) ([]*Collaborator, error) { + return getCollaborators(db.GetEngine(db.DefaultContext), repoID, listOptions) } // CountCollaborators returns total number of collaborators for a repository -func (repo *Repository) CountCollaborators() (int64, error) { - return db.GetEngine(db.DefaultContext).Where("repo_id = ? ", repo.ID).Count(&Collaboration{}) +func CountCollaborators(repoID int64) (int64, error) { + return db.GetEngine(db.DefaultContext).Where("repo_id = ? ", repoID).Count(&Collaboration{}) } -func (repo *Repository) getCollaboration(e db.Engine, uid int64) (*Collaboration, error) { +func getCollaboration(e db.Engine, repoID, uid int64) (*Collaboration, error) { collaboration := &Collaboration{ - RepoID: repo.ID, + RepoID: repoID, UserID: uid, } has, err := e.Get(collaboration) @@ -133,16 +136,16 @@ func (repo *Repository) getCollaboration(e db.Engine, uid int64) (*Collaboration return collaboration, err } -func (repo *Repository) isCollaborator(e db.Engine, userID int64) (bool, error) { - return e.Get(&Collaboration{RepoID: repo.ID, UserID: userID}) +func isCollaborator(e db.Engine, repoID, userID int64) (bool, error) { + return e.Get(&Collaboration{RepoID: repoID, UserID: userID}) } // IsCollaborator check if a user is a collaborator of a repository -func (repo *Repository) IsCollaborator(userID int64) (bool, error) { - return repo.isCollaborator(db.GetEngine(db.DefaultContext), userID) +func IsCollaborator(repoID, userID int64) (bool, error) { + return isCollaborator(db.GetEngine(db.DefaultContext), repoID, userID) } -func (repo *Repository) changeCollaborationAccessMode(e db.Engine, uid int64, mode perm.AccessMode) error { +func changeCollaborationAccessMode(e db.Engine, repo *repo_model.Repository, uid int64, mode perm.AccessMode) error { // Discard invalid input if mode <= perm.AccessModeNone || mode > perm.AccessModeOwner { return nil @@ -177,14 +180,14 @@ func (repo *Repository) changeCollaborationAccessMode(e db.Engine, uid int64, mo } // ChangeCollaborationAccessMode sets new access mode for the collaboration. -func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode perm.AccessMode) error { +func ChangeCollaborationAccessMode(repo *repo_model.Repository, uid int64, mode perm.AccessMode) error { ctx, committer, err := db.TxContext() if err != nil { return err } defer committer.Close() - if err := repo.changeCollaborationAccessMode(db.GetEngine(ctx), uid, mode); err != nil { + if err := changeCollaborationAccessMode(db.GetEngine(ctx), repo, uid, mode); err != nil { return err } @@ -192,7 +195,7 @@ func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode perm.Acces } // DeleteCollaboration removes collaboration relation between the user and repository. -func (repo *Repository) DeleteCollaboration(uid int64) (err error) { +func DeleteCollaboration(repo *repo_model.Repository, uid int64) (err error) { collaboration := &Collaboration{ RepoID: repo.ID, UserID: uid, @@ -208,7 +211,7 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) { if has, err := sess.Delete(collaboration); err != nil || has == 0 { return err - } else if err = repo.recalculateAccesses(sess); err != nil { + } else if err = recalculateAccesses(ctx, repo); err != nil { return err } @@ -216,29 +219,29 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) { return err } - if err = repo.reconsiderWatches(sess, uid); err != nil { + if err = reconsiderWatches(ctx, repo, uid); err != nil { return err } // Unassign a user from any issue (s)he has been assigned to in the repository - if err := repo.reconsiderIssueAssignees(sess, uid); err != nil { + if err := reconsiderRepoIssuesAssignee(ctx, repo, uid); err != nil { return err } return committer.Commit() } -func (repo *Repository) reconsiderIssueAssignees(e db.Engine, uid int64) error { - user, err := user_model.GetUserByIDEngine(e, uid) +func reconsiderRepoIssuesAssignee(ctx context.Context, repo *repo_model.Repository, uid int64) error { + user, err := user_model.GetUserByIDEngine(db.GetEngine(ctx), uid) if err != nil { return err } - if canAssigned, err := canBeAssigned(e, user, repo, true); err != nil || canAssigned { + if canAssigned, err := canBeAssigned(ctx, user, repo, true); err != nil || canAssigned { return err } - if _, err := e.Where(builder.Eq{"assignee_id": uid}). + if _, err := db.GetEngine(ctx).Where(builder.Eq{"assignee_id": uid}). In("issue_id", builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repo.ID})). Delete(&IssueAssignees{}); err != nil { return fmt.Errorf("Could not delete assignee[%d] %v", uid, err) @@ -246,11 +249,11 @@ func (repo *Repository) reconsiderIssueAssignees(e db.Engine, uid int64) error { return nil } -func (repo *Repository) reconsiderWatches(e db.Engine, uid int64) error { - if has, err := hasAccess(e, uid, repo); err != nil || has { +func reconsiderWatches(ctx context.Context, repo *repo_model.Repository, uid int64) error { + if has, err := hasAccess(ctx, uid, repo); err != nil || has { return err } - + e := db.GetEngine(ctx) if err := watchRepo(e, uid, repo.ID, false); err != nil { return err } @@ -259,7 +262,7 @@ func (repo *Repository) reconsiderWatches(e db.Engine, uid int64) error { return removeIssueWatchersByRepoID(e, uid, repo.ID) } -func (repo *Repository) getRepoTeams(e db.Engine) (teams []*Team, err error) { +func getRepoTeams(e db.Engine, repo *repo_model.Repository) (teams []*Team, err error) { return teams, e. Join("INNER", "team_repo", "team_repo.team_id = team.id"). Where("team.org_id = ?", repo.OwnerID). @@ -269,12 +272,12 @@ func (repo *Repository) getRepoTeams(e db.Engine) (teams []*Team, err error) { } // GetRepoTeams gets the list of teams that has access to the repository -func (repo *Repository) GetRepoTeams() ([]*Team, error) { - return repo.getRepoTeams(db.GetEngine(db.DefaultContext)) +func GetRepoTeams(repo *repo_model.Repository) ([]*Team, error) { + return getRepoTeams(db.GetEngine(db.DefaultContext), repo) } // IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository -func (repo *Repository) IsOwnerMemberCollaborator(userID int64) (bool, error) { +func IsOwnerMemberCollaborator(repo *repo_model.Repository, userID int64) (bool, error) { if repo.OwnerID == userID { return true, nil } |