Skip to content

Commit

Permalink
Fix create team, update team missing units (#5188) (#5313)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored and techknowlogick committed Nov 11, 2018
1 parent 297e619 commit 8670dec
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func (t *Team) getUnits(e Engine) (err error) {
return err
}

// GetUnitNames returns the team units names
func (t *Team) GetUnitNames() (res []string) {
for _, u := range t.Units {
res = append(res, Units[u.Type].NameKey)
}
return
}

// HasWriteAccess returns true if team has at least write level access mode.
func (t *Team) HasWriteAccess() bool {
return t.Authorize >= AccessModeWrite
Expand Down Expand Up @@ -367,6 +375,24 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
return fmt.Errorf("update: %v", err)
}

// update units for team
if len(t.Units) > 0 {
for _, unit := range t.Units {
unit.TeamID = t.ID
}
// Delete team-unit.
if _, err := sess.
Where("team_id=?", t.ID).
Delete(new(TeamUnit)); err != nil {
return err
}

if _, err = sess.Insert(&t.Units); err != nil {
sess.Rollback()
return err
}
}

// Update access for team members if needed.
if authChanged {
if err = t.getRepositories(sess); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions models/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

package models

import (
"strings"
)

// UnitType is Unit's Type
type UnitType int

Expand Down Expand Up @@ -137,3 +141,16 @@ var (
UnitTypeExternalWiki: UnitExternalWiki,
}
)

// FindUnitTypes give the unit key name and return unit
func FindUnitTypes(nameKeys ...string) (res []UnitType) {
for _, key := range nameKeys {
for t, u := range Units {
if strings.EqualFold(key, u.NameKey) {
res = append(res, t)
break
}
}
}
return
}
1 change: 1 addition & 0 deletions routers/api/v1/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,6 @@ func ToTeam(team *models.Team) *api.Team {
Name: team.Name,
Description: team.Description,
Permission: team.Authorize.String(),
Units: team.GetUnitNames(),
}
}
27 changes: 27 additions & 0 deletions routers/api/v1/org/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
Description: form.Description,
Authorize: models.ParseAccessMode(form.Permission),
}

unitTypes := models.FindUnitTypes(form.Units...)

if team.Authorize < models.AccessModeOwner {
var units = make([]*models.TeamUnit, 0, len(form.Units))
for _, tp := range unitTypes {
units = append(units, &models.TeamUnit{
OrgID: ctx.Org.Organization.ID,
Type: tp,
})
}
team.Units = units
}

if err := models.NewTeam(team); err != nil {
if models.IsErrTeamAlreadyExist(err) {
ctx.Error(422, "", err)
Expand Down Expand Up @@ -127,6 +141,19 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
team.Name = form.Name
team.Description = form.Description
team.Authorize = models.ParseAccessMode(form.Permission)
unitTypes := models.FindUnitTypes(form.Units...)

if team.Authorize < models.AccessModeOwner {
var units = make([]*models.TeamUnit, 0, len(form.Units))
for _, tp := range unitTypes {
units = append(units, &models.TeamUnit{
OrgID: ctx.Org.Organization.ID,
Type: tp,
})
}
team.Units = units
}

if err := models.UpdateTeam(team, true); err != nil {
ctx.Error(500, "EditTeam", err)
return
Expand Down
48 changes: 48 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6128,6 +6128,22 @@
"admin"
],
"x-go-name": "Permission"
},
"units": {
"type": "array",
"enum": [
"repo.code",
"repo.issues",
"repo.ext_issues",
"repo.wiki",
"repo.pulls",
"repo.releases",
"repo.ext_wiki"
],
"items": {
"type": "string"
},
"x-go-name": "Units"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
Expand Down Expand Up @@ -6503,6 +6519,22 @@
"admin"
],
"x-go-name": "Permission"
},
"units": {
"type": "array",
"enum": [
"repo.code",
"repo.issues",
"repo.ext_issues",
"repo.wiki",
"repo.pulls",
"repo.releases",
"repo.ext_wiki"
],
"items": {
"type": "string"
},
"x-go-name": "Units"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
Expand Down Expand Up @@ -7550,6 +7582,22 @@
"owner"
],
"x-go-name": "Permission"
},
"units": {
"type": "array",
"enum": [
"repo.code",
"repo.issues",
"repo.ext_issues",
"repo.wiki",
"repo.pulls",
"repo.releases",
"repo.ext_wiki"
],
"items": {
"type": "string"
},
"x-go-name": "Units"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
Expand Down
7 changes: 7 additions & 0 deletions vendor/code.gitea.io/sdk/gitea/org_team.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8670dec

Please sign in to comment.