Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create team, update team missing units #5188

Merged
merged 8 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -198,5 +198,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 @@ -90,6 +90,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 @@ -128,6 +142,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 @@ -6322,6 +6322,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 @@ -6697,6 +6713,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 @@ -7744,6 +7776,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.