Skip to content

feat: Enable custom support links #6313

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 13 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Go tests
  • Loading branch information
mtojek committed Feb 23, 2023
commit 132324ccd1fd215957462c69b940849e13f147fc
16 changes: 13 additions & 3 deletions enterprise/coderd/appearance.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func (api *API) appearance(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()

if !isEntitled {
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AppearanceConfig{})
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AppearanceConfig{
SupportLinks: defaultSupportLinks,
})
return
}

Expand Down Expand Up @@ -85,8 +87,9 @@ func (api *API) appearance(rw http.ResponseWriter, r *http.Request) {
}
}

cfg.SupportLinks = defaultSupportLinks
if len(api.DeploymentConfig.Support.Links.Value) > 0 {
if len(api.DeploymentConfig.Support.Links.Value) == 0 {
cfg.SupportLinks = defaultSupportLinks
} else {
cfg.SupportLinks = api.DeploymentConfig.Support.Links.Value
}

Expand Down Expand Up @@ -128,6 +131,13 @@ func (api *API) putAppearance(rw http.ResponseWriter, r *http.Request) {
return
}

if len(appearance.SupportLinks) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Support links cannot be dynamically updated, use the config file instead.",
})
return
}

if appearance.ServiceBanner.Enabled {
if err := validateHexColor(appearance.ServiceBanner.BackgroundColor); err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Expand Down
69 changes: 69 additions & 0 deletions enterprise/coderd/appearance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func TestServiceBanners(t *testing.T) {

basicUserClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)

sb.SupportLinks = nil // clean "support links" as they can't be modified using API

// Regular user should be unable to set the banner
sb.ServiceBanner.Enabled = true
err = basicUserClient.UpdateAppearance(ctx, sb)
Expand All @@ -60,10 +62,77 @@ func TestServiceBanners(t *testing.T) {
require.NoError(t, err)
gotBanner, err := adminClient.Appearance(ctx)
require.NoError(t, err)
gotBanner.SupportLinks = nil // clean "support links" before comparison
require.Equal(t, wantBanner, gotBanner)

// But even an admin can't give a bad color
wantBanner.ServiceBanner.BackgroundColor = "#bad color"
err = adminClient.UpdateAppearance(ctx, wantBanner)
require.Error(t, err)
}

func TestCustomSupportLinks(t *testing.T) {
t.Parallel()

supportLinks := []codersdk.LinkConfig{
{
Name: "First link",
Target: "http://first-link-1",
Icon: "chat",
},
{
Name: "Second link",
Target: "http://second-link-2",
Icon: "bug",
},
}
cfg := coderdtest.DeploymentConfig(t)
cfg.Support = new(codersdk.SupportConfig)
cfg.Support.Links = &codersdk.DeploymentConfigField[[]codersdk.LinkConfig]{
Value: supportLinks,
}

client := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentConfig: cfg,
},
})
coderdtest.CreateFirstUser(t, client)
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()

appearance, err := client.Appearance(ctx)
require.NoError(t, err)

require.Len(t, appearance.SupportLinks, 2)
require.Equal(t, supportLinks, appearance.SupportLinks)
}

func TestDefaultSupportLinks(t *testing.T) {
t.Parallel()

client := coderdenttest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()

appearance, err := client.Appearance(ctx)
require.NoError(t, err)

require.Len(t, appearance.SupportLinks, 3) // Documentation, Report a bug, Join the Coder Discord
require.Equal(t, appearance.SupportLinks[0].Name, "Documentation")
require.Equal(t, appearance.SupportLinks[1].Name, "Report a bug")
require.Equal(t, appearance.SupportLinks[2].Name, "Join the Coder Discord")
}