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/org_team.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/org_team.go')
-rw-r--r-- | models/org_team.go | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/models/org_team.go b/models/org_team.go index dc16fa5030..c42312323c 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -14,6 +14,7 @@ import ( "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,8 +33,8 @@ type Team struct { Name string Description string Authorize perm.AccessMode - Repos []*Repository `xorm:"-"` - Members []*user_model.User `xorm:"-"` + Repos []*repo_model.Repository `xorm:"-"` + Members []*user_model.User `xorm:"-"` NumRepos int NumMembers int Units []*TeamUnit `xorm:"-"` @@ -215,7 +216,8 @@ func (t *Team) HasRepository(repoID int64) bool { return t.hasRepository(db.GetEngine(db.DefaultContext), repoID) } -func (t *Team) addRepository(e db.Engine, repo *Repository) (err error) { +func (t *Team) addRepository(ctx context.Context, repo *repo_model.Repository) (err error) { + e := db.GetEngine(ctx) if err = addTeamRepo(e, t.OrgID, t.ID, repo.ID); err != nil { return err } @@ -226,7 +228,7 @@ func (t *Team) addRepository(e db.Engine, repo *Repository) (err error) { t.NumRepos++ - if err = repo.recalculateTeamAccesses(e, 0); err != nil { + if err = recalculateTeamAccesses(ctx, repo, 0); err != nil { return fmt.Errorf("recalculateAccesses: %v", err) } @@ -247,15 +249,16 @@ func (t *Team) addRepository(e db.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 db.Engine) error { - var orgRepos []Repository +func (t *Team) addAllRepositories(ctx context.Context) error { + var orgRepos []repo_model.Repository + e := db.GetEngine(ctx) if err := e.Where("owner_id = ?", t.OrgID).Find(&orgRepos); err != nil { return fmt.Errorf("get org repos: %v", err) } for _, repo := range orgRepos { if !t.hasRepository(e, repo.ID) { - if err := t.addRepository(e, &repo); err != nil { + if err := t.addRepository(ctx, &repo); err != nil { return fmt.Errorf("addRepository: %v", err) } } @@ -272,7 +275,7 @@ func (t *Team) AddAllRepositories() (err error) { } defer committer.Close() - if err = t.addAllRepositories(db.GetEngine(ctx)); err != nil { + if err = t.addAllRepositories(ctx); err != nil { return err } @@ -280,7 +283,7 @@ func (t *Team) AddAllRepositories() (err error) { } // AddRepository adds new repository to team of organization. -func (t *Team) AddRepository(repo *Repository) (err error) { +func (t *Team) AddRepository(repo *repo_model.Repository) (err error) { if repo.OwnerID != t.OrgID { return errors.New("Repository does not belong to organization") } else if t.HasRepository(repo.ID) { @@ -293,7 +296,7 @@ func (t *Team) AddRepository(repo *Repository) (err error) { } defer committer.Close() - if err = t.addRepository(db.GetEngine(ctx), repo); err != nil { + if err = t.addRepository(ctx, repo); err != nil { return err } @@ -312,7 +315,7 @@ func (t *Team) RemoveAllRepositories() (err error) { } defer committer.Close() - if err = t.removeAllRepositories(db.GetEngine(ctx)); err != nil { + if err = t.removeAllRepositories(ctx); err != nil { return err } @@ -321,16 +324,17 @@ 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 db.Engine) (err error) { +func (t *Team) removeAllRepositories(ctx context.Context) (err error) { + e := db.GetEngine(ctx) // Delete all accesses. for _, repo := range t.Repos { - if err := repo.recalculateTeamAccesses(e, t.ID); err != nil { + if err := recalculateTeamAccesses(ctx, repo, t.ID); err != nil { return err } // Remove watches from all users and now unaccessible repos for _, user := range t.Members { - has, err := hasAccess(e, user.ID, repo) + has, err := hasAccess(ctx, user.ID, repo) if err != nil { return err } else if has { @@ -365,7 +369,8 @@ func (t *Team) removeAllRepositories(e db.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 db.Engine, repo *Repository, recalculate bool) (err error) { +func (t *Team) removeRepository(ctx context.Context, repo *repo_model.Repository, recalculate bool) (err error) { + e := db.GetEngine(ctx) if err = removeTeamRepo(e, t.ID, repo.ID); err != nil { return err } @@ -377,7 +382,7 @@ func (t *Team) removeRepository(e db.Engine, repo *Repository, recalculate bool) // Don't need to recalculate when delete a repository from organization. if recalculate { - if err = repo.recalculateTeamAccesses(e, t.ID); err != nil { + if err = recalculateTeamAccesses(ctx, repo, t.ID); err != nil { return err } } @@ -387,7 +392,7 @@ func (t *Team) removeRepository(e db.Engine, repo *Repository, recalculate bool) return fmt.Errorf("getTeamUsersByTeamID: %v", err) } for _, teamUser := range teamUsers { - has, err := hasAccess(e, teamUser.UID, repo) + has, err := hasAccess(ctx, teamUser.UID, repo) if err != nil { return err } else if has { @@ -418,7 +423,7 @@ func (t *Team) RemoveRepository(repoID int64) error { return nil } - repo, err := GetRepositoryByID(repoID) + repo, err := repo_model.GetRepositoryByID(repoID) if err != nil { return err } @@ -429,7 +434,7 @@ func (t *Team) RemoveRepository(repoID int64) error { } defer committer.Close() - if err = t.removeRepository(db.GetEngine(ctx), repo, true); err != nil { + if err = t.removeRepository(ctx, repo, true); err != nil { return err } @@ -517,7 +522,7 @@ func NewTeam(t *Team) (err error) { // Add all repositories to the team if it has access to all of them. if t.IncludesAllRepositories { - err = t.addAllRepositories(db.GetEngine(ctx)) + err = t.addAllRepositories(ctx) if err != nil { return fmt.Errorf("addAllRepositories: %v", err) } @@ -660,7 +665,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) { } for _, repo := range t.Repos { - if err = repo.recalculateTeamAccesses(sess, 0); err != nil { + if err = recalculateTeamAccesses(ctx, repo, 0); err != nil { return fmt.Errorf("recalculateTeamAccesses: %v", err) } } @@ -668,7 +673,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) { // Add all repositories to the team if it has access to all of them. if includeAllChanged && t.IncludesAllRepositories { - err = t.addAllRepositories(sess) + err = t.addAllRepositories(ctx) if err != nil { return fmt.Errorf("addAllRepositories: %v", err) } @@ -695,7 +700,7 @@ func DeleteTeam(t *Team) error { return err } - if err := t.removeAllRepositories(sess); err != nil { + if err := t.removeAllRepositories(ctx); err != nil { return err } @@ -848,7 +853,7 @@ func AddTeamMember(team *Team, userID int64) error { // Give access to team repositories. for _, repo := range team.Repos { - if err := repo.recalculateUserAccess(sess, userID); err != nil { + if err := recalculateUserAccess(ctx, repo, userID); err != nil { return err } if setting.Service.AutoWatchNewRepos { @@ -894,17 +899,17 @@ func removeTeamMember(ctx context.Context, team *Team, userID int64) error { // Delete access to team repositories. for _, repo := range team.Repos { - if err := repo.recalculateUserAccess(e, userID); err != nil { + if err := recalculateUserAccess(ctx, repo, userID); err != nil { return err } // Remove watches from now unaccessible - if err := repo.reconsiderWatches(e, userID); err != nil { + if err := reconsiderWatches(ctx, repo, userID); err != nil { return err } // Remove issue assignments from now unaccessible - if err := repo.reconsiderIssueAssignees(e, userID); err != nil { + if err := reconsiderRepoIssuesAssignee(ctx, repo, userID); err != nil { return err } } |