diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/org_team.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
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 <art27@cantab.net>
Diffstat (limited to 'models/org_team.go')
-rw-r--r-- | models/org_team.go | 108 |
1 files changed, 58 insertions, 50 deletions
diff --git a/models/org_team.go b/models/org_team.go index 7516016a1d..280d814eef 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -11,6 +11,7 @@ import ( "sort" "strings" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -37,6 +38,13 @@ type Team struct { CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"` } +func init() { + db.RegisterModel(new(Team)) + db.RegisterModel(new(TeamUser)) + db.RegisterModel(new(TeamRepo)) + db.RegisterModel(new(TeamUnit)) +} + // SearchTeamOptions holds the search options type SearchTeamOptions struct { ListOptions @@ -74,7 +82,7 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) { cond = cond.And(builder.Eq{"org_id": opts.OrgID}) - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() count, err := sess. @@ -112,10 +120,10 @@ func (t *Team) ColorFormat(s fmt.State) { // GetUnits return a list of available units for a team func (t *Team) GetUnits() error { - return t.getUnits(x) + return t.getUnits(db.DefaultContext().Engine()) } -func (t *Team) getUnits(e Engine) (err error) { +func (t *Team) getUnits(e db.Engine) (err error) { if t.Units != nil { return nil } @@ -152,7 +160,7 @@ func (t *Team) IsMember(userID int64) bool { return isMember } -func (t *Team) getRepositories(e Engine) error { +func (t *Team) getRepositories(e db.Engine) error { if t.Repos != nil { return nil } @@ -165,13 +173,13 @@ func (t *Team) getRepositories(e Engine) error { // GetRepositories returns paginated repositories in team of organization. func (t *Team) GetRepositories(opts *SearchTeamOptions) error { if opts.Page == 0 { - return t.getRepositories(x) + return t.getRepositories(db.DefaultContext().Engine()) } return t.getRepositories(getPaginatedSession(opts)) } -func (t *Team) getMembers(e Engine) (err error) { +func (t *Team) getMembers(e db.Engine) (err error) { t.Members, err = getTeamMembers(e, t.ID) return err } @@ -179,7 +187,7 @@ func (t *Team) getMembers(e Engine) (err error) { // GetMembers returns paginated members in team of organization. func (t *Team) GetMembers(opts *SearchMembersOptions) (err error) { if opts.Page == 0 { - return t.getMembers(x) + return t.getMembers(db.DefaultContext().Engine()) } return t.getMembers(getPaginatedSession(opts)) @@ -196,16 +204,16 @@ func (t *Team) RemoveMember(userID int64) error { return RemoveTeamMember(t, userID) } -func (t *Team) hasRepository(e Engine, repoID int64) bool { +func (t *Team) hasRepository(e db.Engine, repoID int64) bool { return hasTeamRepo(e, t.OrgID, t.ID, repoID) } // HasRepository returns true if given repository belong to team. func (t *Team) HasRepository(repoID int64) bool { - return t.hasRepository(x, repoID) + return t.hasRepository(db.DefaultContext().Engine(), repoID) } -func (t *Team) addRepository(e Engine, repo *Repository) (err error) { +func (t *Team) addRepository(e db.Engine, repo *Repository) (err error) { if err = addTeamRepo(e, t.OrgID, t.ID, repo.ID); err != nil { return err } @@ -237,7 +245,7 @@ func (t *Team) addRepository(e Engine, repo *Repository) (err error) { // addAllRepositories adds all repositories to the team. // If the team already has some repositories they will be left unchanged. -func (t *Team) addAllRepositories(e Engine) error { +func (t *Team) addAllRepositories(e db.Engine) error { var orgRepos []Repository if err := e.Where("owner_id = ?", t.OrgID).Find(&orgRepos); err != nil { return fmt.Errorf("get org repos: %v", err) @@ -256,7 +264,7 @@ func (t *Team) addAllRepositories(e Engine) error { // AddAllRepositories adds all repositories to the team func (t *Team) AddAllRepositories() (err error) { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -277,7 +285,7 @@ func (t *Team) AddRepository(repo *Repository) (err error) { return nil } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -296,7 +304,7 @@ func (t *Team) RemoveAllRepositories() (err error) { return nil } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -311,7 +319,7 @@ func (t *Team) RemoveAllRepositories() (err error) { // removeAllRepositories removes all repositories from team and recalculates access // Note: Shall not be called if team includes all repositories -func (t *Team) removeAllRepositories(e Engine) (err error) { +func (t *Team) removeAllRepositories(e db.Engine) (err error) { // Delete all accesses. for _, repo := range t.Repos { if err := repo.recalculateTeamAccesses(e, t.ID); err != nil { @@ -355,7 +363,7 @@ func (t *Team) removeAllRepositories(e Engine) (err error) { // removeRepository removes a repository from a team and recalculates access // Note: Repository shall not be removed from team if it includes all repositories (unless the repository is deleted) -func (t *Team) removeRepository(e Engine, repo *Repository, recalculate bool) (err error) { +func (t *Team) removeRepository(e db.Engine, repo *Repository, recalculate bool) (err error) { if err = removeTeamRepo(e, t.ID, repo.ID); err != nil { return err } @@ -413,7 +421,7 @@ func (t *Team) RemoveRepository(repoID int64) error { return err } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -428,10 +436,10 @@ func (t *Team) RemoveRepository(repoID int64) error { // UnitEnabled returns if the team has the given unit type enabled func (t *Team) UnitEnabled(tp UnitType) bool { - return t.unitEnabled(x, tp) + return t.unitEnabled(db.DefaultContext().Engine(), tp) } -func (t *Team) unitEnabled(e Engine, tp UnitType) bool { +func (t *Team) unitEnabled(e db.Engine, tp UnitType) bool { if err := t.getUnits(e); err != nil { log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error()) } @@ -465,7 +473,7 @@ func NewTeam(t *Team) (err error) { return err } - has, err := x.ID(t.OrgID).Get(new(User)) + has, err := db.DefaultContext().Engine().ID(t.OrgID).Get(new(User)) if err != nil { return err } @@ -474,7 +482,7 @@ func NewTeam(t *Team) (err error) { } t.LowerName = strings.ToLower(t.Name) - has, err = x. + has, err = db.DefaultContext().Engine(). Where("org_id=?", t.OrgID). And("lower_name=?", t.LowerName). Get(new(Team)) @@ -485,7 +493,7 @@ func NewTeam(t *Team) (err error) { return ErrTeamAlreadyExist{t.OrgID, t.LowerName} } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -532,7 +540,7 @@ func NewTeam(t *Team) (err error) { return sess.Commit() } -func getTeam(e Engine, orgID int64, name string) (*Team, error) { +func getTeam(e db.Engine, orgID int64, name string) (*Team, error) { t := &Team{ OrgID: orgID, LowerName: strings.ToLower(name), @@ -548,7 +556,7 @@ func getTeam(e Engine, orgID int64, name string) (*Team, error) { // GetTeam returns team by given team name and organization. func GetTeam(orgID int64, name string) (*Team, error) { - return getTeam(x, orgID, name) + return getTeam(db.DefaultContext().Engine(), orgID, name) } // GetTeamIDsByNames returns a slice of team ids corresponds to names. @@ -569,11 +577,11 @@ func GetTeamIDsByNames(orgID int64, names []string, ignoreNonExistent bool) ([]i } // getOwnerTeam returns team by given team name and organization. -func getOwnerTeam(e Engine, orgID int64) (*Team, error) { +func getOwnerTeam(e db.Engine, orgID int64) (*Team, error) { return getTeam(e, orgID, ownerTeamName) } -func getTeamByID(e Engine, teamID int64) (*Team, error) { +func getTeamByID(e db.Engine, teamID int64) (*Team, error) { t := new(Team) has, err := e.ID(teamID).Get(t) if err != nil { @@ -586,7 +594,7 @@ func getTeamByID(e Engine, teamID int64) (*Team, error) { // GetTeamByID returns team by given ID. func GetTeamByID(teamID int64) (*Team, error) { - return getTeamByID(x, teamID) + return getTeamByID(db.DefaultContext().Engine(), teamID) } // GetTeamNamesByID returns team's lower name from a list of team ids. @@ -596,7 +604,7 @@ func GetTeamNamesByID(teamIDs []int64) ([]string, error) { } var teamNames []string - err := x.Table("team"). + err := db.DefaultContext().Engine().Table("team"). Select("lower_name"). In("id", teamIDs). Asc("name"). @@ -615,7 +623,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) { t.Description = t.Description[:255] } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -689,7 +697,7 @@ func DeleteTeam(t *Team) error { return err } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -745,7 +753,7 @@ type TeamUser struct { UID int64 `xorm:"UNIQUE(s)"` } -func isTeamMember(e Engine, orgID, teamID, userID int64) (bool, error) { +func isTeamMember(e db.Engine, orgID, teamID, userID int64) (bool, error) { return e. Where("org_id=?", orgID). And("team_id=?", teamID). @@ -756,17 +764,17 @@ func isTeamMember(e Engine, orgID, teamID, userID int64) (bool, error) { // IsTeamMember returns true if given user is a member of team. func IsTeamMember(orgID, teamID, userID int64) (bool, error) { - return isTeamMember(x, orgID, teamID, userID) + return isTeamMember(db.DefaultContext().Engine(), orgID, teamID, userID) } -func getTeamUsersByTeamID(e Engine, teamID int64) ([]*TeamUser, error) { +func getTeamUsersByTeamID(e db.Engine, teamID int64) ([]*TeamUser, error) { teamUsers := make([]*TeamUser, 0, 10) return teamUsers, e. Where("team_id=?", teamID). Find(&teamUsers) } -func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) { +func getTeamMembers(e db.Engine, teamID int64) (_ []*User, err error) { teamUsers, err := getTeamUsersByTeamID(e, teamID) if err != nil { return nil, fmt.Errorf("get team-users: %v", err) @@ -787,10 +795,10 @@ func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) { // GetTeamMembers returns all members in given team of organization. func GetTeamMembers(teamID int64) ([]*User, error) { - return getTeamMembers(x, teamID) + return getTeamMembers(db.DefaultContext().Engine(), teamID) } -func getUserOrgTeams(e Engine, orgID, userID int64) (teams []*Team, err error) { +func getUserOrgTeams(e db.Engine, orgID, userID int64) (teams []*Team, err error) { return teams, e. Join("INNER", "team_user", "team_user.team_id = team.id"). Where("team.org_id = ?", orgID). @@ -798,7 +806,7 @@ func getUserOrgTeams(e Engine, orgID, userID int64) (teams []*Team, err error) { Find(&teams) } -func getUserRepoTeams(e Engine, orgID, userID, repoID int64) (teams []*Team, err error) { +func getUserRepoTeams(e db.Engine, orgID, userID, repoID int64) (teams []*Team, err error) { return teams, e. Join("INNER", "team_user", "team_user.team_id = team.id"). Join("INNER", "team_repo", "team_repo.team_id = team.id"). @@ -810,7 +818,7 @@ func getUserRepoTeams(e Engine, orgID, userID, repoID int64) (teams []*Team, err // GetUserOrgTeams returns all teams that user belongs to in given organization. func GetUserOrgTeams(orgID, userID int64) ([]*Team, error) { - return getUserOrgTeams(x, orgID, userID) + return getUserOrgTeams(db.DefaultContext().Engine(), orgID, userID) } // AddTeamMember adds new membership of given team to given organization, @@ -830,7 +838,7 @@ func AddTeamMember(team *Team, userID int64) error { return err } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -925,7 +933,7 @@ func removeTeamMember(e *xorm.Session, team *Team, userID int64) error { // RemoveTeamMember removes member from given team of given organization. func RemoveTeamMember(team *Team, userID int64) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -938,17 +946,17 @@ func RemoveTeamMember(team *Team, userID int64) error { // IsUserInTeams returns if a user in some teams func IsUserInTeams(userID int64, teamIDs []int64) (bool, error) { - return isUserInTeams(x, userID, teamIDs) + return isUserInTeams(db.DefaultContext().Engine(), userID, teamIDs) } -func isUserInTeams(e Engine, userID int64, teamIDs []int64) (bool, error) { +func isUserInTeams(e db.Engine, userID int64, teamIDs []int64) (bool, error) { return e.Where("uid=?", userID).In("team_id", teamIDs).Exist(new(TeamUser)) } // UsersInTeamsCount counts the number of users which are in userIDs and teamIDs func UsersInTeamsCount(userIDs, teamIDs []int64) (int64, error) { var ids []int64 - if err := x.In("uid", userIDs).In("team_id", teamIDs). + if err := db.DefaultContext().Engine().In("uid", userIDs).In("team_id", teamIDs). Table("team_user"). Cols("uid").GroupBy("uid").Find(&ids); err != nil { return 0, err @@ -971,7 +979,7 @@ type TeamRepo struct { RepoID int64 `xorm:"UNIQUE(s)"` } -func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool { +func hasTeamRepo(e db.Engine, orgID, teamID, repoID int64) bool { has, _ := e. Where("org_id=?", orgID). And("team_id=?", teamID). @@ -982,10 +990,10 @@ func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool { // HasTeamRepo returns true if given repository belongs to team. func HasTeamRepo(orgID, teamID, repoID int64) bool { - return hasTeamRepo(x, orgID, teamID, repoID) + return hasTeamRepo(db.DefaultContext().Engine(), orgID, teamID, repoID) } -func addTeamRepo(e Engine, orgID, teamID, repoID int64) error { +func addTeamRepo(e db.Engine, orgID, teamID, repoID int64) error { _, err := e.InsertOne(&TeamRepo{ OrgID: orgID, TeamID: teamID, @@ -994,7 +1002,7 @@ func addTeamRepo(e Engine, orgID, teamID, repoID int64) error { return err } -func removeTeamRepo(e Engine, teamID, repoID int64) error { +func removeTeamRepo(e db.Engine, teamID, repoID int64) error { _, err := e.Delete(&TeamRepo{ TeamID: teamID, RepoID: repoID, @@ -1005,7 +1013,7 @@ func removeTeamRepo(e Engine, teamID, repoID int64) error { // GetTeamsWithAccessToRepo returns all teams in an organization that have given access level to the repository. func GetTeamsWithAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, error) { teams := make([]*Team, 0, 5) - return teams, x.Where("team.authorize >= ?", mode). + return teams, db.DefaultContext().Engine().Where("team.authorize >= ?", mode). Join("INNER", "team_repo", "team_repo.team_id = team.id"). And("team_repo.org_id = ?", orgID). And("team_repo.repo_id = ?", repoID). @@ -1032,13 +1040,13 @@ func (t *TeamUnit) Unit() Unit { return Units[t.Type] } -func getUnitsByTeamID(e Engine, teamID int64) (units []*TeamUnit, err error) { +func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error) { return units, e.Where("team_id = ?", teamID).Find(&units) } // UpdateTeamUnits updates a teams's units func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err |