Skip to content

feat: add avatar urls to groups #4525

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

Merged
merged 16 commits into from
Oct 17, 2022
2 changes: 2 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2784,6 +2784,7 @@ func (q *fakeQuerier) UpdateGroupByID(_ context.Context, arg database.UpdateGrou
for i, group := range q.groups {
if group.ID == arg.ID {
group.Name = arg.Name
group.AvatarURL = arg.AvatarURL
q.groups[i] = group
return group, nil
}
Expand Down Expand Up @@ -3135,6 +3136,7 @@ func (q *fakeQuerier) InsertGroup(_ context.Context, arg database.InsertGroupPar
ID: arg.ID,
Name: arg.Name,
OrganizationID: arg.OrganizationID,
AvatarURL: arg.AvatarURL,
}

q.groups = append(q.groups, group)
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions coderd/database/migrations/000062_group_avatars.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TABLE groups DROP COLUMN avatar_url;

COMMIT;
5 changes: 5 additions & 0 deletions coderd/database/migrations/000062_group_avatars.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TABLE groups ADD COLUMN avatar_url text NOT NULL DEFAULT '';

COMMIT;
1 change: 1 addition & 0 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 65 additions & 21 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions coderd/database/queries/groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ AND
INSERT INTO groups (
id,
name,
organization_id
organization_id,
avatar_url
)
VALUES
( $1, $2, $3) RETURNING *;
( $1, $2, $3, $4) RETURNING *;

-- We use the organization_id as the id
-- for simplicity since all users is
Expand All @@ -95,9 +96,10 @@ VALUES
UPDATE
groups
SET
name = $1
name = $1,
avatar_url = $2
WHERE
id = $2
id = $3
RETURNING *;

-- name: InsertGroupMember :exec
Expand Down
5 changes: 4 additions & 1 deletion codersdk/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
)

type CreateGroupRequest struct {
Name string `json:"name"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
}

type Group struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
OrganizationID uuid.UUID `json:"organization_id"`
Members []User `json:"members"`
AvatarURL string `json:"avatar_url"`
}

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

func (c *Client) PatchGroup(ctx context.Context, group uuid.UUID, req PatchGroupRequest) (Group, error) {
Expand Down
39 changes: 30 additions & 9 deletions enterprise/coderd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (api *API) postGroupByOrganization(rw http.ResponseWriter, r *http.Request)
ID: uuid.New(),
Name: req.Name,
OrganizationID: org.ID,
AvatarURL: req.AvatarURL,
})
if database.IsUniqueViolation(err) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Expand Down Expand Up @@ -81,6 +82,12 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
return
}

// If the name matches the existing group name pretend we aren't
// updating the name at all.
if req.Name == group.Name {
req.Name = ""
}

users := make([]string, 0, len(req.AddUsers)+len(req.RemoveUsers))
users = append(users, req.AddUsers...)
users = append(users, req.RemoveUsers...)
Expand Down Expand Up @@ -109,7 +116,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
return
}
}
if req.Name != "" {
if req.Name != "" && req.Name != group.Name {
_, err := api.Database.GetGroupByOrgAndName(ctx, database.GetGroupByOrgAndNameParams{
OrganizationID: group.OrganizationID,
Name: req.Name,
Expand All @@ -123,16 +130,29 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
}

err := api.Database.InTx(func(tx database.Store) error {
var err error
group, err = tx.GetGroupByID(ctx, group.ID)
if err != nil {
return xerrors.Errorf("get group by ID: %w", err)
}

// TODO: Do we care about validating this?
if req.AvatarURL != nil {
group.AvatarURL = *req.AvatarURL
}
if req.Name != "" {
var err error
group, err = tx.UpdateGroupByID(ctx, database.UpdateGroupByIDParams{
ID: group.ID,
Name: req.Name,
})
if err != nil {
return xerrors.Errorf("update group by ID: %w", err)
}
group.Name = req.Name
}

group, err = tx.UpdateGroupByID(ctx, database.UpdateGroupByIDParams{
ID: group.ID,
Name: group.Name,
AvatarURL: group.AvatarURL,
})
if err != nil {
return xerrors.Errorf("update group by ID: %w", err)
}

for _, id := range req.AddUsers {
err := tx.InsertGroupMember(ctx, database.InsertGroupMemberParams{
GroupID: group.ID,
Expand Down Expand Up @@ -276,6 +296,7 @@ func convertGroup(g database.Group, users []database.User) codersdk.Group {
ID: g.ID,
Name: g.Name,
OrganizationID: g.OrganizationID,
AvatarURL: g.AvatarURL,
Members: convertUsers(users, orgs),
}
}
Expand Down
Loading