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 1 commit
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
Next Next commit
fix create team, update team missing units
  • Loading branch information
lunny committed Nov 9, 2018
commit 0f899ebaddcd052119f0fc9f4192696bd790558e
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
14 changes: 14 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,13 @@ var (
UnitTypeExternalWiki: UnitExternalWiki,
}
)

// FindUnitTypes give the unit key name and return unit
func FindUnitTypes(nameKeys ...string) (res []UnitType) {
for t, u := range Units {
if strings.EqualFold(u.NameKey, u.NameKey) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always true. You are comparing u.NameKey with u.NameKey...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

res = append(res, t)
}
}
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
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.