Skip to content

Commit e682e13

Browse files
committed
fix issue where update doesn't work when submitting the existing name
1 parent 1c1d579 commit e682e13

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

enterprise/coderd/groups.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,21 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
111111
}
112112
}
113113
if req.Name != "" {
114-
_, err := api.Database.GetGroupByOrgAndName(ctx, database.GetGroupByOrgAndNameParams{
114+
existingGroup, err := api.Database.GetGroupByOrgAndName(ctx, database.GetGroupByOrgAndNameParams{
115115
OrganizationID: group.OrganizationID,
116116
Name: req.Name,
117117
})
118118
if err == nil {
119-
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
120-
Message: fmt.Sprintf("A group with name %q already exists.", req.Name),
121-
})
122-
return
119+
// We may have just queried ourself. This should really
120+
// go in the tx.
121+
if existingGroup.ID != group.ID {
122+
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
123+
Message: fmt.Sprintf("A group with name %q already exists.", req.Name),
124+
})
125+
return
126+
}
127+
// If we queried ourself then we don't want to update the name.
128+
req.Name = ""
123129
}
124130
}
125131

@@ -139,7 +145,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
139145

140146
group, err = tx.UpdateGroupByID(ctx, database.UpdateGroupByIDParams{
141147
ID: group.ID,
142-
Name: req.Name,
148+
Name: group.Name,
143149
AvatarURL: group.AvatarURL,
144150
})
145151
if err != nil {

0 commit comments

Comments
 (0)