Skip to content

Commit 1080c76

Browse files
6543lafrikslunny
authored andcommitted
[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 <[email protected]> * 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 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 71fe018 commit 1080c76

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

integrations/api_team_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,41 @@ func TestAPITeam(t *testing.T) {
7171
teamID := apiTeam.ID
7272

7373
// Edit team.
74+
editDescription := "team 1"
75+
editFalse := false
7476
teamToEdit := &api.EditTeamOption{
7577
Name: "teamone",
76-
Description: "team 1",
77-
IncludesAllRepositories: false,
78+
Description: &editDescription,
7879
Permission: "admin",
80+
IncludesAllRepositories: &editFalse,
7981
Units: []string{"repo.code", "repo.pulls", "repo.releases"},
8082
}
83+
8184
req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEdit)
8285
resp = session.MakeRequest(t, req, http.StatusOK)
8386
DecodeJSON(t, resp, &apiTeam)
84-
checkTeamResponse(t, &apiTeam, teamToEdit.Name, teamToEdit.Description, teamToEdit.IncludesAllRepositories,
87+
checkTeamResponse(t, &apiTeam, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
88+
teamToEdit.Permission, teamToEdit.Units)
89+
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
90+
teamToEdit.Permission, teamToEdit.Units)
91+
92+
// Edit team Description only
93+
editDescription = "first team"
94+
teamToEditDesc := api.EditTeamOption{Description: &editDescription}
95+
req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEditDesc)
96+
resp = session.MakeRequest(t, req, http.StatusOK)
97+
DecodeJSON(t, resp, &apiTeam)
98+
checkTeamResponse(t, &apiTeam, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
8599
teamToEdit.Permission, teamToEdit.Units)
86-
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, teamToEdit.Description, teamToEdit.IncludesAllRepositories,
100+
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
87101
teamToEdit.Permission, teamToEdit.Units)
88102

89103
// Read team.
90104
teamRead := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
91105
req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID)
92106
resp = session.MakeRequest(t, req, http.StatusOK)
93107
DecodeJSON(t, resp, &apiTeam)
94-
checkTeamResponse(t, &apiTeam, teamRead.Name, teamRead.Description, teamRead.IncludesAllRepositories,
108+
checkTeamResponse(t, &apiTeam, teamRead.Name, *teamToEditDesc.Description, teamRead.IncludesAllRepositories,
95109
teamRead.Authorize.String(), teamRead.GetUnitNames())
96110

97111
// Delete team.

models/org_team.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
590590
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
591591
}
592592

593-
if _, err = sess.ID(t.ID).AllCols().Update(t); err != nil {
593+
if _, err = sess.ID(t.ID).Cols("name", "lower_name", "description",
594+
"can_create_org_repo", "authorize", "includes_all_repositories").Update(t); err != nil {
594595
return fmt.Errorf("update: %v", err)
595596
}
596597

@@ -605,8 +606,7 @@ func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
605606
Delete(new(TeamUnit)); err != nil {
606607
return err
607608
}
608-
609-
if _, err = sess.Insert(&t.Units); err != nil {
609+
if _, err = sess.Cols("org_id", "team_id", "type").Insert(&t.Units); err != nil {
610610
errRollback := sess.Rollback()
611611
if errRollback != nil {
612612
log.Error("UpdateTeam sess.Rollback: %v", errRollback)

modules/structs/org_team.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ type CreateTeamOption struct {
3535
// EditTeamOption options for editing a team
3636
type EditTeamOption struct {
3737
// required: true
38-
Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(30)"`
39-
Description string `json:"description" binding:"MaxSize(255)"`
40-
IncludesAllRepositories bool `json:"includes_all_repositories"`
38+
Name string `json:"name" binding:"AlphaDashDot;MaxSize(30)"`
39+
Description *string `json:"description" binding:"MaxSize(255)"`
40+
IncludesAllRepositories *bool `json:"includes_all_repositories"`
4141
// enum: read,write,admin
4242
Permission string `json:"permission"`
4343
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.ext_wiki"]
4444
Units []string `json:"units"`
45-
CanCreateOrgRepo bool `json:"can_create_org_repo"`
45+
CanCreateOrgRepo *bool `json:"can_create_org_repo"`
4646
}

routers/api/v1/org/team.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,37 +192,52 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
192192
// "$ref": "#/responses/Team"
193193

194194
team := ctx.Org.Team
195-
team.Description = form.Description
196-
unitTypes := models.FindUnitTypes(form.Units...)
197-
team.CanCreateOrgRepo = form.CanCreateOrgRepo
195+
if err := team.GetUnits(); err != nil {
196+
ctx.InternalServerError(err)
197+
return
198+
}
199+
200+
if form.CanCreateOrgRepo != nil {
201+
team.CanCreateOrgRepo = *form.CanCreateOrgRepo
202+
}
203+
204+
if len(form.Name) > 0 {
205+
team.Name = form.Name
206+
}
207+
208+
if form.Description != nil {
209+
team.Description = *form.Description
210+
}
198211

199212
isAuthChanged := false
200213
isIncludeAllChanged := false
201-
if !team.IsOwnerTeam() {
214+
if !team.IsOwnerTeam() && len(form.Permission) != 0 {
202215
// Validate permission level.
203216
auth := models.ParseAccessMode(form.Permission)
204217

205-
team.Name = form.Name
206218
if team.Authorize != auth {
207219
isAuthChanged = true
208220
team.Authorize = auth
209221
}
210222

211-
if team.IncludesAllRepositories != form.IncludesAllRepositories {
223+
if form.IncludesAllRepositories != nil {
212224
isIncludeAllChanged = true
213-
team.IncludesAllRepositories = form.IncludesAllRepositories
225+
team.IncludesAllRepositories = *form.IncludesAllRepositories
214226
}
215227
}
216228

217229
if team.Authorize < models.AccessModeOwner {
218-
var units = make([]*models.TeamUnit, 0, len(form.Units))
219-
for _, tp := range unitTypes {
220-
units = append(units, &models.TeamUnit{
221-
OrgID: ctx.Org.Team.OrgID,
222-
Type: tp,
223-
})
230+
if len(form.Units) > 0 {
231+
var units = make([]*models.TeamUnit, 0, len(form.Units))
232+
unitTypes := models.FindUnitTypes(form.Units...)
233+
for _, tp := range unitTypes {
234+
units = append(units, &models.TeamUnit{
235+
OrgID: ctx.Org.Team.OrgID,
236+
Type: tp,
237+
})
238+
}
239+
team.Units = units
224240
}
225-
team.Units = units
226241
}
227242

228243
if err := models.UpdateTeam(team, isAuthChanged, isIncludeAllChanged); err != nil {

0 commit comments

Comments
 (0)