10000 chore: app deduplication · coder/coder@d1da3a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit d1da3a1

Browse files
chore: app deduplication
1 parent e17b228 commit d1da3a1

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

agent/agentcontainers/api.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11391139
codersdk.DisplayAppPortForward: true,
11401140
}
11411141

1142-
var apps []SubAgentApp
1142+
var appsWithPossibleDuplicates []SubAgentApp
11431143

11441144
if config, err := api.dccli.ReadConfig(ctx, dc.WorkspaceFolder, dc.ConfigPath,
11451145
[]string{
@@ -1165,7 +1165,7 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11651165
displayAppsMap[app] = enabled
11661166
}
11671167

1168-
apps = append(apps, customization.Apps...)
1168+
appsWithPossibleDuplicates = append(appsWithPossibleDuplicates, customization.Apps...)
11691169
}
11701170
}
11711171

@@ -1177,6 +1177,25 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11771177
}
11781178
slices.Sort(displayApps)
11791179

1180+
appSlugs := make(map[string]struct{})
1181+
apps := make([]SubAgentApp, 0, len(appsWithPossibleDuplicates))
1182+
1183+
// We want to deduplicate the apps based on their slugs here.
1184+
// As we want to prioritize later apps, we will walk through this
1185+
// backwards.
1186+
for _, app := range slices.Backward(appsWithPossibleDuplicates) {
1187+
if _, slugAlreadyExists := appSlugs[app.Slug]; slugAlreadyExists {
1188+
continue
1189< 10000 span class="diff-text-marker">+
}
1190+
1191+
appSlugs[app.Slug] = struct{}{}
1192+
apps = append(apps, app)
1193+
}
1194+
1195+
// Apps is currently in reverse order here, so by reversing it we restore
1196+
// it to the original order.
1197+
slices.Reverse(apps)
1198+
11801199
subAgentConfig.DisplayApps = displayApps
11811200
subAgentConfig.Apps = apps
11821201
}

agent/agentcontainers/api_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,48 @@ func TestAPI(t *testing.T) {
16881688
assert.Equal(t, int32(3), subAgent.Apps[2].Order)
16891689
},
16901690
},
1691+
{
1692+
name: "AppDeduplication",
1693+
customization: []agentcontainers.CoderCustomization{
1694+
{
1695+
Apps: []agentcontainers.SubAgentApp{
1696+
{
1697+
Slug: "foo-app",
1698+
Hidden: true,
1699+
Order: 1,
1700+
},
1701+
{
1702+
Slug: "bar-app",
1703+
},
1704+
},
1705+
},
1706+
{
1707+
Apps: []agentcontainers.SubAgentApp{
1708+
{
1709+
Slug: "foo-app",
1710+
Order: 2,
1711+
},
1712+
{
1713+
Slug: "baz-app",
1714+
},
1715+
},
1716+
},
1717+
},
1718+
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
1719+
require.Len(t, subAgent.Apps, 3)
1720+
1721+
// As the original "foo-app" gets overriden by the later "foo-app",
1722+
// we expect "bar-app" to be first in the order.
1723+
assert.Equal(t, "bar-app", subAgent.Apps[0].Slug)
1724+
assert.Equal(t, "foo-app", subAgent.Apps[1].Slug)
1725+
assert.Equal(t, "baz-app", subAgent.Apps[2].Slug)
1726+
1727+
// We do not expect the properties from the original "foo-app" to be
1728+
// carried over.
1729+
assert.Equal(t, false, subAgent.Apps[1].Hidden)
1730+
assert.Equal(t, int32(2), subAgent.Apps[1].Order)
1731+
},
1732+
},
16911733
}
16921734

16931735
for _, tt := range tests {

0 commit comments

Comments
 (0)
0