Skip to content

Commit 42bac09

Browse files
authored
fix: Sort workspace agents by name (#3407)
Fixes #2778.
1 parent d275e52 commit 42bac09

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

coderd/workspaceresources.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"errors"
66
"net/http"
7+
"sort"
78

89
"github.com/google/uuid"
910

@@ -79,6 +80,9 @@ func (api *API) workspaceResource(rw http.ResponseWriter, r *http.Request) {
7980
}
8081
apiAgents = append(apiAgents, convertedAgent)
8182
}
83+
sort.Slice(apiAgents, func(i, j int) bool {
84+
return apiAgents[i].Name < apiAgents[j].Name
85+
})
8286

8387
metadata, err := api.Database.GetWorkspaceResourceMetadataByResourceID(r.Context(), workspaceResource.ID)
8488
if err != nil {

coderd/workspaceresources_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func TestWorkspaceResource(t *testing.T) {
2828
Type: "example",
2929
Agents: []*proto.Agent{{
3030
Id: "something",
31+
Name: "b",
32+
Auth: &proto.Agent_Token{},
33+
}, {
34+
Id: "another",
35+
Name: "a",
3136
Auth: &proto.Agent_Token{},
3237
}},
3338
}},
@@ -41,8 +46,12 @@ func TestWorkspaceResource(t *testing.T) {
4146
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
4247
resources, err := client.WorkspaceResourcesByBuild(context.Background(), workspace.LatestBuild.ID)
4348
require.NoError(t, err)
44-
_, err = client.WorkspaceResource(context.Background(), resources[0].ID)
49+
resource, err := client.WorkspaceResource(context.Background(), resources[0].ID)
4550
require.NoError(t, err)
51+
require.Len(t, resource.Agents, 2)
52+
// Ensure it's sorted alphabetically!
53+
require.Equal(t, "a", resource.Agents[0].Name)
54+
require.Equal(t, "b", resource.Agents[1].Name)
4655
})
4756

4857
t.Run("Apps", func(t *testing.T) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy