Skip to content

Commit e0a14f6

Browse files
authored
feat: add avatar urls to groups (#4525)
1 parent 9b4ab82 commit e0a14f6

File tree

20 files changed

+227
-45
lines changed

20 files changed

+227
-45
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,6 +2784,7 @@ func (q *fakeQuerier) UpdateGroupByID(_ context.Context, arg database.UpdateGrou
27842784
for i, group := range q.groups {
27852785
if group.ID == arg.ID {
27862786
group.Name = arg.Name
2787+
group.AvatarURL = arg.AvatarURL
27872788
q.groups[i] = group
27882789
return group, nil
27892790
}
@@ -3135,6 +3136,7 @@ func (q *fakeQuerier) InsertGroup(_ context.Context, arg database.InsertGroupPar
31353136
ID: arg.ID,
31363137
Name: arg.Name,
31373138
OrganizationID: arg.OrganizationID,
3139+
AvatarURL: arg.AvatarURL,
31383140
}
31393141

31403142
q.groups = append(q.groups, group)

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BEGIN;
2+
3+
ALTER TABLE groups DROP COLUMN avatar_url;
4+
5+
COMMIT;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BEGIN;
2+
3+
ALTER TABLE groups ADD COLUMN avatar_url text NOT NULL DEFAULT '';
4+
5+
COMMIT;

coderd/database/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 65 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/groups.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ AND
7474
INSERT INTO groups (
7575
id,
7676
name,
77-
organization_id
77+
organization_id,
78+
avatar_url
7879
)
7980
VALUES
80-
( $1, $2, $3) RETURNING *;
81+
( $1, $2, $3, $4) RETURNING *;
8182

8283
-- We use the organization_id as the id
8384
-- for simplicity since all users is
@@ -95,9 +96,10 @@ VALUES
9596
UPDATE
9697
groups
9798
SET
98-
name = $1
99+
name = $1,
100+
avatar_url = $2
99101
WHERE
100-
id = $2
102+
id = $3
101103
RETURNING *;
102104

103105
-- name: InsertGroupMember :exec

codersdk/groups.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import (
1111
)
1212

1313
type CreateGroupRequest struct {
14-
Name string `json:"name"`
14+
Name string `json:"name"`
15+
AvatarURL string `json:"avatar_url"`
1516
}
1617

1718
type Group struct {
1819
ID uuid.UUID `json:"id"`
1920
Name string `json:"name"`
2021
OrganizationID uuid.UUID `json:"organization_id"`
2122
Members []User `json:"members"`
23+
AvatarURL string `json:"avatar_url"`
2224
}
2325

2426
func (c *Client) CreateGroup(ctx context.Context, orgID uuid.UUID, req CreateGroupRequest) (Group, error) {
@@ -77,6 +79,7 @@ type PatchGroupRequest struct {
7779
AddUsers []string `json:"add_users"`
7880
RemoveUsers []string `json:"remove_users"`
7981
Name string `json:"name"`
82+
AvatarURL *string `json:"avatar_url"`
8083
}
8184

8285
func (c *Client) PatchGroup(ctx context.Context, group uuid.UUID, req PatchGroupRequest) (Group, error) {

enterprise/coderd/groups.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (api *API) postGroupByOrganization(rw http.ResponseWriter, r *http.Request)
4343
ID: uuid.New(),
4444
Name: req.Name,
4545
OrganizationID: org.ID,
46+
AvatarURL: req.AvatarURL,
4647
})
4748
if database.IsUniqueViolation(err) {
4849
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
@@ -81,6 +82,12 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
8182
return
8283
}
8384

85+
// If the name matches the existing group name pretend we aren't
86+
// updating the name at all.
87+
if req.Name == group.Name {
88+
req.Name = ""
89+
}
90+
8491
users := make([]string, 0, len(req.AddUsers)+len(req.RemoveUsers))
8592
users = append(users, req.AddUsers...)
8693
users = append(users, req.RemoveUsers...)
@@ -109,7 +116,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
109116
return
110117
}
111118
}
112-
if req.Name != "" {
119+
if req.Name != "" && req.Name != group.Name {
113120
_, err := api.Database.GetGroupByOrgAndName(ctx, database.GetGroupByOrgAndNameParams{
114121
OrganizationID: group.OrganizationID,
115122
Name: req.Name,
@@ -123,16 +130,29 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
123130
}
124131

125132
err := api.Database.InTx(func(tx database.Store) error {
133+
var err error
134+
group, err = tx.GetGroupByID(ctx, group.ID)
135+
if err != nil {
136+
return xerrors.Errorf("get group by ID: %w", err)
137+
}
138+
139+
// TODO: Do we care about validating this?
140+
if req.AvatarURL != nil {
141+
group.AvatarURL = *req.AvatarURL
142+
}
126143
if req.Name != "" {
127-
var err error
128-
group, err = tx.UpdateGroupByID(ctx, database.UpdateGroupByIDParams{
129-
ID: group.ID,
130-
Name: req.Name,
131-
})
132-
if err != nil {
133-
return xerrors.Errorf("update group by ID: %w", err)
134-
}
144+
group.Name = req.Name
135145
}
146+
147+
group, err = tx.UpdateGroupByID(ctx, database.UpdateGroupByIDParams{
148+
ID: group.ID,
149+
Name: group.Name,
150+
AvatarURL: group.AvatarURL,
151+
})
152+
if err != nil {
153+
return xerrors.Errorf("update group by ID: %w", err)
154+
}
155+
136156
for _, id := range req.AddUsers {
137157
err := tx.InsertGroupMember(ctx, database.InsertGroupMemberParams{
138158
GroupID: group.ID,
@@ -276,6 +296,7 @@ func convertGroup(g database.Group, users []database.User) codersdk.Group {
276296
ID: g.ID,
277297
Name: g.Name,
278298
OrganizationID: g.OrganizationID,
299+
AvatarURL: g.AvatarURL,
279300
Members: convertUsers(users, orgs),
280301
}
281302
}

0 commit comments

Comments
 (0)