From a4bfef265d9e512830350635a0489c2cdcd6508f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 19 Sep 2021 19:49:59 +0800 Subject: Move db related basic functions to models/db (#17075) * Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath --- models/repo_collaboration.go | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'models/repo_collaboration.go') diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go index 84e49b2c65..1199a56404 100644 --- a/models/repo_collaboration.go +++ b/models/repo_collaboration.go @@ -8,6 +8,7 @@ package models import ( "fmt" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" "xorm.io/builder" @@ -23,7 +24,11 @@ type Collaboration struct { UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } -func (repo *Repository) addCollaborator(e Engine, u *User) error { +func init() { + db.RegisterModel(new(Collaboration)) +} + +func (repo *Repository) addCollaborator(e db.Engine, u *User) error { collaboration := &Collaboration{ RepoID: repo.ID, UserID: u.ID, @@ -46,7 +51,7 @@ func (repo *Repository) addCollaborator(e Engine, u *User) error { // AddCollaborator adds new collaboration to a repository with default access mode. func (repo *Repository) AddCollaborator(u *User) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -59,7 +64,7 @@ func (repo *Repository) AddCollaborator(u *User) error { return sess.Commit() } -func (repo *Repository) getCollaborations(e Engine, listOptions ListOptions) ([]*Collaboration, error) { +func (repo *Repository) getCollaborations(e db.Engine, listOptions ListOptions) ([]*Collaboration, error) { if listOptions.Page == 0 { collaborations := make([]*Collaboration, 0, 8) return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID}) @@ -77,7 +82,7 @@ type Collaborator struct { Collaboration *Collaboration } -func (repo *Repository) getCollaborators(e Engine, listOptions ListOptions) ([]*Collaborator, error) { +func (repo *Repository) getCollaborators(e db.Engine, listOptions ListOptions) ([]*Collaborator, error) { collaborations, err := repo.getCollaborations(e, listOptions) if err != nil { return nil, fmt.Errorf("getCollaborations: %v", err) @@ -99,15 +104,15 @@ func (repo *Repository) getCollaborators(e Engine, listOptions ListOptions) ([]* // GetCollaborators returns the collaborators for a repository func (repo *Repository) GetCollaborators(listOptions ListOptions) ([]*Collaborator, error) { - return repo.getCollaborators(x, listOptions) + return repo.getCollaborators(db.DefaultContext().Engine(), listOptions) } // CountCollaborators returns total number of collaborators for a repository func (repo *Repository) CountCollaborators() (int64, error) { - return x.Where("repo_id = ? ", repo.ID).Count(&Collaboration{}) + return db.DefaultContext().Engine().Where("repo_id = ? ", repo.ID).Count(&Collaboration{}) } -func (repo *Repository) getCollaboration(e Engine, uid int64) (*Collaboration, error) { +func (repo *Repository) getCollaboration(e db.Engine, uid int64) (*Collaboration, error) { collaboration := &Collaboration{ RepoID: repo.ID, UserID: uid, @@ -119,16 +124,16 @@ func (repo *Repository) getCollaboration(e Engine, uid int64) (*Collaboration, e return collaboration, err } -func (repo *Repository) isCollaborator(e Engine, userID int64) (bool, error) { +func (repo *Repository) isCollaborator(e db.Engine, userID int64) (bool, error) { return e.Get(&Collaboration{RepoID: repo.ID, UserID: userID}) } // IsCollaborator check if a user is a collaborator of a repository func (repo *Repository) IsCollaborator(userID int64) (bool, error) { - return repo.isCollaborator(x, userID) + return repo.isCollaborator(db.DefaultContext().Engine(), userID) } -func (repo *Repository) changeCollaborationAccessMode(e Engine, uid int64, mode AccessMode) error { +func (repo *Repository) changeCollaborationAccessMode(e db.Engine, uid int64, mode AccessMode) error { // Discard invalid input if mode <= AccessModeNone || mode > AccessModeOwner { return nil @@ -164,7 +169,7 @@ func (repo *Repository) changeCollaborationAccessMode(e Engine, uid int64, mode // ChangeCollaborationAccessMode sets new access mode for the collaboration. func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -184,7 +189,7 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) { UserID: uid, } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -212,7 +217,7 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) { return sess.Commit() } -func (repo *Repository) reconsiderIssueAssignees(e Engine, uid int64) error { +func (repo *Repository) reconsiderIssueAssignees(e db.Engine, uid int64) error { user, err := getUserByID(e, uid) if err != nil { return err @@ -230,7 +235,7 @@ func (repo *Repository) reconsiderIssueAssignees(e Engine, uid int64) error { return nil } -func (repo *Repository) reconsiderWatches(e Engine, uid int64) error { +func (repo *Repository) reconsiderWatches(e db.Engine, uid int64) error { if has, err := hasAccess(e, uid, repo); err != nil || has { return err } @@ -243,7 +248,7 @@ func (repo *Repository) reconsiderWatches(e Engine, uid int64) error { return removeIssueWatchersByRepoID(e, uid, repo.ID) } -func (repo *Repository) getRepoTeams(e Engine) (teams []*Team, err error) { +func (repo *Repository) getRepoTeams(e db.Engine) (teams []*Team, err error) { return teams, e. Join("INNER", "team_repo", "team_repo.team_id = team.id"). Where("team.org_id = ?", repo.OwnerID). @@ -254,7 +259,7 @@ func (repo *Repository) getRepoTeams(e 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(x) + return repo.getRepoTeams(db.DefaultContext().Engine()) } // IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository @@ -262,7 +267,7 @@ func (repo *Repository) IsOwnerMemberCollaborator(userID int64) (bool, error) { if repo.OwnerID == userID { return true, nil } - teamMember, err := x.Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id"). + teamMember, err := db.DefaultContext().Engine().Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id"). Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id"). Where("team_repo.repo_id = ?", repo.ID). And("team_unit.`type` = ?", UnitTypeCode). @@ -274,5 +279,5 @@ func (repo *Repository) IsOwnerMemberCollaborator(userID int64) (bool, error) { return true, nil } - return x.Get(&Collaboration{RepoID: repo.ID, UserID: userID}) + return db.DefaultContext().Engine().Get(&Collaboration{RepoID: repo.ID, UserID: userID}) } -- cgit v1.2.3