diff --git a/models/org_team.go b/models/org_team.go index 0cf6cdcccb..60f7a659e0 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -165,18 +165,19 @@ 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 { - err := e.Iterate(&Repository{OwnerID: t.OrgID}, func(i int, bean interface{}) error { - repo := bean.(*Repository) + var orgRepos []Repository + 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(e, &repo); err != nil { return fmt.Errorf("addRepository: %v", err) } } - return nil - }) - if err != nil { - return fmt.Errorf("Iterate organization repositories: %v", err) } + return nil } @@ -413,7 +414,7 @@ func GetTeamByID(teamID int64) (*Team, error) { } // UpdateTeam updates information of team. -func UpdateTeam(t *Team, authChanged bool) (err error) { +func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) { if len(t.Name) == 0 { return errors.New("empty team name") } @@ -479,7 +480,7 @@ func UpdateTeam(t *Team, authChanged bool) (err error) { } // Add all repositories to the team if it has access to all of them. - if t.IncludesAllRepositories { + if includeAllChanged && t.IncludesAllRepositories { err = t.addAllRepositories(sess) if err != nil { return fmt.Errorf("addAllRepositories: %v", err) diff --git a/models/org_team_test.go b/models/org_team_test.go index 0554430983..9194b0f5fc 100644 --- a/models/org_team_test.go +++ b/models/org_team_test.go @@ -209,7 +209,7 @@ func TestUpdateTeam(t *testing.T) { team.Name = "newName" team.Description = strings.Repeat("A long description!", 100) team.Authorize = AccessModeAdmin - assert.NoError(t, UpdateTeam(team, true)) + assert.NoError(t, UpdateTeam(team, true, false)) team = AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) assert.True(t, strings.HasPrefix(team.Description, "A long description!")) @@ -228,7 +228,7 @@ func TestUpdateTeam2(t *testing.T) { team.LowerName = "owners" team.Name = "Owners" team.Description = strings.Repeat("A long description!", 100) - err := UpdateTeam(team, true) + err := UpdateTeam(team, true, false) assert.True(t, IsErrTeamAlreadyExist(err)) CheckConsistencyFor(t, &Team{ID: team.ID}) @@ -471,7 +471,7 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) { teams[4].IncludesAllRepositories = true teamRepos[4] = repoIds for i, team := range teams { - assert.NoError(t, UpdateTeam(team, false), "%s: UpdateTeam", team.Name) + assert.NoError(t, UpdateTeam(team, false, true), "%s: UpdateTeam", team.Name) testTeamRepositories(team.ID, teamRepos[i]) } diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index e8d3c987aa..55fd02a830 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -198,7 +198,7 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) { team.Units = units } - if err := models.UpdateTeam(team, true); err != nil { + if err := models.UpdateTeam(team, true, true); err != nil { ctx.Error(500, "EditTeam", err) return } diff --git a/routers/org/teams.go b/routers/org/teams.go index ef63e9b835..02e811370d 100644 --- a/routers/org/teams.go +++ b/routers/org/teams.go @@ -270,6 +270,7 @@ func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) { ctx.Data["Units"] = models.Units isAuthChanged := false + isIncludeAllChanged := false if !t.IsOwnerTeam() { // Validate permission level. auth := models.ParseAccessMode(form.Permission) @@ -280,7 +281,10 @@ func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) { t.Authorize = auth } - t.IncludesAllRepositories = form.IncludesAllRepositories + if t.IncludesAllRepositories != form.IncludesAllRepositories { + isIncludeAllChanged = true + t.IncludesAllRepositories = form.IncludesAllRepositories + } } t.Description = form.Description if t.Authorize < models.AccessModeOwner { @@ -309,7 +313,7 @@ func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) { return } - if err := models.UpdateTeam(t, isAuthChanged); err != nil { + if err := models.UpdateTeam(t, isAuthChanged, isIncludeAllChanged); err != nil { ctx.Data["Err_TeamName"] = true switch { case models.IsErrTeamAlreadyExist(err):