Change code for adding all repositories

Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
This commit is contained in:
David Svantesson 2019-10-05 08:59:40 +02:00
parent 8f297caf26
commit 550cbccdff
4 changed files with 20 additions and 15 deletions

View file

@ -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)

View file

@ -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])
}

View file

@ -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
}

View file

@ -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):