mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: rename notification banners to announcement banners (#13419)
This commit is contained in:
@@ -58,7 +58,7 @@ func (f *appearanceFetcher) Fetch(ctx context.Context) (codersdk.AppearanceConfi
|
||||
var (
|
||||
applicationName string
|
||||
logoURL string
|
||||
notificationBannersJSON string
|
||||
announcementBannersJSON string
|
||||
)
|
||||
eg.Go(func() (err error) {
|
||||
applicationName, err = f.database.GetApplicationName(ctx)
|
||||
@@ -75,7 +75,7 @@ func (f *appearanceFetcher) Fetch(ctx context.Context) (codersdk.AppearanceConfi
|
||||
return nil
|
||||
})
|
||||
eg.Go(func() (err error) {
|
||||
notificationBannersJSON, err = f.database.GetNotificationBanners(ctx)
|
||||
announcementBannersJSON, err = f.database.GetAnnouncementBanners(ctx)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return xerrors.Errorf("get notification banners: %w", err)
|
||||
}
|
||||
@@ -89,22 +89,22 @@ func (f *appearanceFetcher) Fetch(ctx context.Context) (codersdk.AppearanceConfi
|
||||
cfg := codersdk.AppearanceConfig{
|
||||
ApplicationName: applicationName,
|
||||
LogoURL: logoURL,
|
||||
NotificationBanners: []codersdk.BannerConfig{},
|
||||
AnnouncementBanners: []codersdk.BannerConfig{},
|
||||
SupportLinks: agpl.DefaultSupportLinks,
|
||||
}
|
||||
|
||||
if notificationBannersJSON != "" {
|
||||
err = json.Unmarshal([]byte(notificationBannersJSON), &cfg.NotificationBanners)
|
||||
if announcementBannersJSON != "" {
|
||||
err = json.Unmarshal([]byte(announcementBannersJSON), &cfg.AnnouncementBanners)
|
||||
if err != nil {
|
||||
return codersdk.AppearanceConfig{}, xerrors.Errorf(
|
||||
"unmarshal notification banners json: %w, raw: %s", err, notificationBannersJSON,
|
||||
"unmarshal announcement banners json: %w, raw: %s", err, announcementBannersJSON,
|
||||
)
|
||||
}
|
||||
|
||||
// Redundant, but improves compatibility with slightly mismatched agent versions.
|
||||
// Maybe we can remove this after a grace period? -Kayla, May 6th 2024
|
||||
if len(cfg.NotificationBanners) > 0 {
|
||||
cfg.ServiceBanner = cfg.NotificationBanners[0]
|
||||
if len(cfg.AnnouncementBanners) > 0 {
|
||||
cfg.ServiceBanner = cfg.AnnouncementBanners[0]
|
||||
}
|
||||
}
|
||||
if len(f.supportLinks) > 0 {
|
||||
@@ -149,7 +149,7 @@ func (api *API) putAppearance(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, banner := range appearance.NotificationBanners {
|
||||
for _, banner := range appearance.AnnouncementBanners {
|
||||
if err := validateHexColor(banner.BackgroundColor); err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: fmt.Sprintf("Invalid color format: %q", banner.BackgroundColor),
|
||||
@@ -159,22 +159,22 @@ func (api *API) putAppearance(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if appearance.NotificationBanners == nil {
|
||||
appearance.NotificationBanners = []codersdk.BannerConfig{}
|
||||
if appearance.AnnouncementBanners == nil {
|
||||
appearance.AnnouncementBanners = []codersdk.BannerConfig{}
|
||||
}
|
||||
notificationBannersJSON, err := json.Marshal(appearance.NotificationBanners)
|
||||
announcementBannersJSON, err := json.Marshal(appearance.AnnouncementBanners)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: "Unable to marshal notification banners",
|
||||
Message: "Unable to marshal announcement banners",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = api.Database.UpsertNotificationBanners(ctx, string(notificationBannersJSON))
|
||||
err = api.Database.UpsertAnnouncementBanners(ctx, string(announcementBannersJSON))
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Unable to set notification banners",
|
||||
Message: "Unable to set announcement banners",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestCustomLogoAndCompanyName(t *testing.T) {
|
||||
require.Equal(t, uac.LogoURL, got.LogoURL)
|
||||
}
|
||||
|
||||
func TestNotificationBanners(t *testing.T) {
|
||||
func TestAnnouncementBanners(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("User", func(t *testing.T) {
|
||||
@@ -70,7 +70,7 @@ func TestNotificationBanners(t *testing.T) {
|
||||
// Without a license, there should be no banners.
|
||||
sb, err := basicUserClient.Appearance(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, sb.NotificationBanners)
|
||||
require.Empty(t, sb.AnnouncementBanners)
|
||||
|
||||
coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
@@ -81,11 +81,11 @@ func TestNotificationBanners(t *testing.T) {
|
||||
// Default state
|
||||
sb, err = basicUserClient.Appearance(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, sb.NotificationBanners)
|
||||
require.Empty(t, sb.AnnouncementBanners)
|
||||
|
||||
// Regular user should be unable to set the banner
|
||||
uac := codersdk.UpdateAppearanceConfig{
|
||||
NotificationBanners: []codersdk.BannerConfig{{Enabled: true}},
|
||||
AnnouncementBanners: []codersdk.BannerConfig{{Enabled: true}},
|
||||
}
|
||||
err = basicUserClient.UpdateAppearance(ctx, uac)
|
||||
require.Error(t, err)
|
||||
@@ -96,7 +96,7 @@ func TestNotificationBanners(t *testing.T) {
|
||||
|
||||
// But an admin can
|
||||
wantBanner := codersdk.UpdateAppearanceConfig{
|
||||
NotificationBanners: []codersdk.BannerConfig{{
|
||||
AnnouncementBanners: []codersdk.BannerConfig{{
|
||||
Enabled: true,
|
||||
Message: "The beep-bop will be boop-beeped on Saturday at 12AM PST.",
|
||||
BackgroundColor: "#00FF00",
|
||||
@@ -106,10 +106,10 @@ func TestNotificationBanners(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
gotBanner, err := adminClient.Appearance(ctx) //nolint:gocritic // we should assert at least once that the owner can get the banner
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, wantBanner.NotificationBanners, gotBanner.NotificationBanners)
|
||||
require.Equal(t, wantBanner.AnnouncementBanners, gotBanner.AnnouncementBanners)
|
||||
|
||||
// But even an admin can't give a bad color
|
||||
wantBanner.NotificationBanners[0].BackgroundColor = "#bad color"
|
||||
wantBanner.AnnouncementBanners[0].BackgroundColor = "#bad color"
|
||||
err = adminClient.UpdateAppearance(ctx, wantBanner)
|
||||
require.Error(t, err)
|
||||
var sdkErr *codersdk.Error
|
||||
@@ -139,7 +139,7 @@ func TestNotificationBanners(t *testing.T) {
|
||||
},
|
||||
})
|
||||
cfg := codersdk.UpdateAppearanceConfig{
|
||||
NotificationBanners: []codersdk.BannerConfig{{
|
||||
AnnouncementBanners: []codersdk.BannerConfig{{
|
||||
Enabled: true,
|
||||
Message: "The beep-bop will be boop-beeped on Saturday at 12AM PST.",
|
||||
BackgroundColor: "#00FF00",
|
||||
@@ -155,35 +155,35 @@ func TestNotificationBanners(t *testing.T) {
|
||||
|
||||
agentClient := agentsdk.New(client.URL)
|
||||
agentClient.SetSessionToken(r.AgentToken)
|
||||
banners := requireGetNotificationBanners(ctx, t, agentClient)
|
||||
require.Equal(t, cfg.NotificationBanners, banners)
|
||||
banners := requireGetAnnouncementBanners(ctx, t, agentClient)
|
||||
require.Equal(t, cfg.AnnouncementBanners, banners)
|
||||
|
||||
// Create an AGPL Coderd against the same database
|
||||
agplClient := coderdtest.New(t, &coderdtest.Options{Database: store, Pubsub: ps})
|
||||
agplAgentClient := agentsdk.New(agplClient.URL)
|
||||
agplAgentClient.SetSessionToken(r.AgentToken)
|
||||
banners = requireGetNotificationBanners(ctx, t, agplAgentClient)
|
||||
banners = requireGetAnnouncementBanners(ctx, t, agplAgentClient)
|
||||
require.Equal(t, []codersdk.BannerConfig{}, banners)
|
||||
|
||||
// No license means no banner.
|
||||
err = client.DeleteLicense(ctx, lic.ID)
|
||||
require.NoError(t, err)
|
||||
banners = requireGetNotificationBanners(ctx, t, agentClient)
|
||||
banners = requireGetAnnouncementBanners(ctx, t, agentClient)
|
||||
require.Equal(t, []codersdk.BannerConfig{}, banners)
|
||||
})
|
||||
}
|
||||
|
||||
func requireGetNotificationBanners(ctx context.Context, t *testing.T, client *agentsdk.Client) []codersdk.BannerConfig {
|
||||
func requireGetAnnouncementBanners(ctx context.Context, t *testing.T, client *agentsdk.Client) []codersdk.BannerConfig {
|
||||
cc, err := client.ConnectRPC(ctx)
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
_ = cc.Close()
|
||||
}()
|
||||
aAPI := proto.NewDRPCAgentClient(cc)
|
||||
bannersProto, err := aAPI.GetNotificationBanners(ctx, &proto.GetNotificationBannersRequest{})
|
||||
bannersProto, err := aAPI.GetAnnouncementBanners(ctx, &proto.GetAnnouncementBannersRequest{})
|
||||
require.NoError(t, err)
|
||||
banners := make([]codersdk.BannerConfig, 0, len(bannersProto.NotificationBanners))
|
||||
for _, bannerProto := range bannersProto.NotificationBanners {
|
||||
banners := make([]codersdk.BannerConfig, 0, len(bannersProto.AnnouncementBanners))
|
||||
for _, bannerProto := range bannersProto.AnnouncementBanners {
|
||||
banners = append(banners, agentsdk.BannerConfigFromProto(bannerProto))
|
||||
}
|
||||
return banners
|
||||
|
||||
Reference in New Issue
Block a user