[API] orgEditTeam make Fields optional (#9556)

* API: orgEditTeam make Fields optional

* add TestCase

* Update integrations/api_team_test.go

* suggestions from lafriks

use len() to check if string is empty

Co-Authored-By: Lauris BH <lauris@nix.lv>

* change ...

* use Where not ID to get mssql

* add return and code format

* fix test

* fix test ... null pointer exept

* update specific colums

* only specific colums too

Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
release/v1.15
6543 2020-01-09 14:15:14 +01:00 committed by zeripath
parent 71fe018977
commit 1080c768d3
4 changed files with 55 additions and 26 deletions

View File

@ -71,19 +71,33 @@ func TestAPITeam(t *testing.T) {
teamID := apiTeam.ID teamID := apiTeam.ID
// Edit team. // Edit team.
editDescription := "team 1"
editFalse := false
teamToEdit := &api.EditTeamOption{ teamToEdit := &api.EditTeamOption{
Name: "teamone", Name: "teamone",
Description: "team 1", Description: &editDescription,
IncludesAllRepositories: false,
Permission: "admin", Permission: "admin",
IncludesAllRepositories: &editFalse,
Units: []string{"repo.code", "repo.pulls", "repo.releases"}, Units: []string{"repo.code", "repo.pulls", "repo.releases"},
} }
req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEdit) req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEdit)
resp = session.MakeRequest(t, req, http.StatusOK) resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiTeam) DecodeJSON(t, resp, &apiTeam)
checkTeamResponse(t, &apiTeam, teamToEdit.Name, teamToEdit.Description, teamToEdit.IncludesAllRepositories, checkTeamResponse(t, &apiTeam, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
teamToEdit.Permission, teamToEdit.Units) teamToEdit.Permission, teamToEdit.Units)
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, teamToEdit.Description, teamToEdit.IncludesAllRepositories, checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
teamToEdit.Permission, teamToEdit.Units)
// Edit team Description only
editDescription = "first team"
teamToEditDesc := api.EditTeamOption{Description: &editDescription}
req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEditDesc)
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiTeam)
checkTeamResponse(t, &apiTeam, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
teamToEdit.Permission, teamToEdit.Units)
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
teamToEdit.Permission, teamToEdit.Units) teamToEdit.Permission, teamToEdit.Units)
// Read team. // Read team.
@ -91,7 +105,7 @@ func TestAPITeam(t *testing.T) {
req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID) req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID)
resp = session.MakeRequest(t, req, http.StatusOK) resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiTeam) DecodeJSON(t, resp, &apiTeam)
checkTeamResponse(t, &apiTeam, teamRead.Name, teamRead.Description, teamRead.IncludesAllRepositories, checkTeamResponse(t, &apiTeam, teamRead.Name, *teamToEditDesc.Description, teamRead.IncludesAllRepositories,
teamRead.Authorize.String(), teamRead.GetUnitNames()) teamRead.Authorize.String(), teamRead.GetUnitNames())
// Delete team. // Delete team.

View File

@ -590,7 +590,8 @@ func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
return ErrTeamAlreadyExist{t.OrgID, t.LowerName} return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
} }
if _, err = sess.ID(t.ID).AllCols().Update(t); err != nil { if _, err = sess.ID(t.ID).Cols("name", "lower_name", "description",
"can_create_org_repo", "authorize", "includes_all_repositories").Update(t); err != nil {
return fmt.Errorf("update: %v", err) return fmt.Errorf("update: %v", err)
} }
@ -605,8 +606,7 @@ func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
Delete(new(TeamUnit)); err != nil { Delete(new(TeamUnit)); err != nil {
return err return err
} }
if _, err = sess.Cols("org_id", "team_id", "type").Insert(&t.Units); err != nil {
if _, err = sess.Insert(&t.Units); err != nil {
errRollback := sess.Rollback() errRollback := sess.Rollback()
if errRollback != nil { if errRollback != nil {
log.Error("UpdateTeam sess.Rollback: %v", errRollback) log.Error("UpdateTeam sess.Rollback: %v", errRollback)

View File

@ -35,12 +35,12 @@ type CreateTeamOption struct {
// EditTeamOption options for editing a team // EditTeamOption options for editing a team
type EditTeamOption struct { type EditTeamOption struct {
// required: true // required: true
Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(30)"` Name string `json:"name" binding:"AlphaDashDot;MaxSize(30)"`
Description string `json:"description" binding:"MaxSize(255)"` Description *string `json:"description" binding:"MaxSize(255)"`
IncludesAllRepositories bool `json:"includes_all_repositories"` IncludesAllRepositories *bool `json:"includes_all_repositories"`
// enum: read,write,admin // enum: read,write,admin
Permission string `json:"permission"` Permission string `json:"permission"`
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.ext_wiki"] // example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.ext_wiki"]
Units []string `json:"units"` Units []string `json:"units"`
CanCreateOrgRepo bool `json:"can_create_org_repo"` CanCreateOrgRepo *bool `json:"can_create_org_repo"`
} }

View File

@ -192,30 +192,44 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
// "$ref": "#/responses/Team" // "$ref": "#/responses/Team"
team := ctx.Org.Team team := ctx.Org.Team
team.Description = form.Description if err := team.GetUnits(); err != nil {
unitTypes := models.FindUnitTypes(form.Units...) ctx.InternalServerError(err)
team.CanCreateOrgRepo = form.CanCreateOrgRepo return
}
if form.CanCreateOrgRepo != nil {
team.CanCreateOrgRepo = *form.CanCreateOrgRepo
}
if len(form.Name) > 0 {
team.Name = form.Name
}
if form.Description != nil {
team.Description = *form.Description
}
isAuthChanged := false isAuthChanged := false
isIncludeAllChanged := false isIncludeAllChanged := false
if !team.IsOwnerTeam() { if !team.IsOwnerTeam() && len(form.Permission) != 0 {
// Validate permission level. // Validate permission level.
auth := models.ParseAccessMode(form.Permission) auth := models.ParseAccessMode(form.Permission)
team.Name = form.Name
if team.Authorize != auth { if team.Authorize != auth {
isAuthChanged = true isAuthChanged = true
team.Authorize = auth team.Authorize = auth
} }
if team.IncludesAllRepositories != form.IncludesAllRepositories { if form.IncludesAllRepositories != nil {
isIncludeAllChanged = true isIncludeAllChanged = true
team.IncludesAllRepositories = form.IncludesAllRepositories team.IncludesAllRepositories = *form.IncludesAllRepositories
} }
} }
if team.Authorize < models.AccessModeOwner { if team.Authorize < models.AccessModeOwner {
if len(form.Units) > 0 {
var units = make([]*models.TeamUnit, 0, len(form.Units)) var units = make([]*models.TeamUnit, 0, len(form.Units))
unitTypes := models.FindUnitTypes(form.Units...)
for _, tp := range unitTypes { for _, tp := range unitTypes {
units = append(units, &models.TeamUnit{ units = append(units, &models.TeamUnit{
OrgID: ctx.Org.Team.OrgID, OrgID: ctx.Org.Team.OrgID,
@ -224,6 +238,7 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
} }
team.Units = units team.Units = units
} }
}
if err := models.UpdateTeam(team, isAuthChanged, isIncludeAllChanged); err != nil { if err := models.UpdateTeam(team, isAuthChanged, isIncludeAllChanged); err != nil {
ctx.Error(http.StatusInternalServerError, "EditTeam", err) ctx.Error(http.StatusInternalServerError, "EditTeam", err)