Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29671,6 +29671,9 @@ const docTemplate = `{
"enabled": {
"type": "boolean"
},
"error": {
"type": "string"
},
"metadata": {
"type": "object",
"additionalProperties": {
Expand Down
3 changes: 3 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -29665,6 +29665,9 @@
"enabled": {
"type": "boolean"
},
"error": {
"type": "string"
},
"metadata": {
"type": "object",
"additionalProperties": {
Expand Down
2 changes: 2 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,8 @@ definitions:
type: string
enabled:
type: boolean
error:
type: string
metadata:
additionalProperties:
type: string
Expand Down
2 changes: 2 additions & 0 deletions internal/api/handler/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type availableNotificationProviderResponse struct {
DisplayName string `json:"displayName"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
Error string `json:"error,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}

Expand Down Expand Up @@ -359,6 +360,7 @@ func (h *NotificationsHandler) ListNotificationProviders(ctx echo.Context) error
DisplayName: provider.DisplayName,
Description: provider.Description,
Enabled: provider.Enabled,
Error: provider.Error,
Metadata: provider.Metadata,
})
}
Expand Down
54 changes: 54 additions & 0 deletions internal/api/handler/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ import (
"go.uber.org/zap"
)

type stubProviderCatalog struct {
providers []notification.ProviderMetadata
}

func (s stubProviderCatalog) Provider(providerID string) (notification.Provider, bool) {
return nil, false
}

func (s stubProviderCatalog) ProviderIDs() []string {
providerIDs := make([]string, 0, len(s.providers))
for _, provider := range s.providers {
providerIDs = append(providerIDs, provider.ProviderType)
}
return providerIDs
}

func (s stubProviderCatalog) Providers() []notification.ProviderMetadata {
return append([]notification.ProviderMetadata(nil), s.providers...)
}

type stubNotificationTestEnqueuer struct {
started bool
jobIDs []int64
Expand Down Expand Up @@ -71,3 +91,37 @@ func TestSendTestNotificationReturnsJobIDs(t *testing.T) {
require.Equal(t, []int64{42}, response.Data.JobIDs)
require.Equal(t, notification.DeliveryChannelEmail, response.Data.ProviderType)
}

func TestListNotificationProvidersReturnsProviderErrors(t *testing.T) {
e := echo.New()
handler := &NotificationsHandler{
sugar: zap.NewNop().Sugar(),
providers: stubProviderCatalog{providers: []notification.ProviderMetadata{
{
ProviderType: notification.DeliveryChannelSlack,
DisplayName: "Slack",
Description: "Configured Slack workspace",
Enabled: true,
Error: "invalid_auth",
},
}},
}

req := httptest.NewRequest(http.MethodGet, "/api/admin/notifications/providers", nil)
rec := httptest.NewRecorder()

err := handler.ListNotificationProviders(e.NewContext(req, rec))
require.NoError(t, err)
require.Equal(t, http.StatusOK, rec.Code)

var response GenericDataListResponse[availableNotificationProviderResponse]
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &response))
require.Len(t, response.Data, 1)
require.Equal(t, availableNotificationProviderResponse{
ProviderType: notification.DeliveryChannelSlack,
DisplayName: "Slack",
Description: "Configured Slack workspace",
Enabled: true,
Error: "invalid_auth",
}, response.Data[0])
}
18 changes: 12 additions & 6 deletions internal/service/notification/providers/slack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Provider struct {
workspaceConfigurationMu sync.Mutex
workspaceConfigurationLoaded bool
workspaceConfiguration slacksvc.WorkspaceConfiguration
workspaceConfigurationErr error
}

const (
Expand Down Expand Up @@ -163,7 +164,10 @@ func (p *Provider) ProviderMetadata() notification.ProviderMetadata {
Enabled: p.enabled(),
}

configuration := p.workspaceConfigurationDetails()
configuration, err := p.workspaceConfigurationDetails()
if err != nil {
metadata.Error = err.Error()
}
if configuration.WorkspaceName != "" {
metadata.Description = fmt.Sprintf("Configured Slack workspace %s", configuration.WorkspaceName)
}
Expand Down Expand Up @@ -213,29 +217,31 @@ func (p *Provider) enabled() bool {
return sender != nil && sender.IsEnabled()
}

func (p *Provider) workspaceConfigurationDetails() slacksvc.WorkspaceConfiguration {
func (p *Provider) workspaceConfigurationDetails() (slacksvc.WorkspaceConfiguration, error) {
if p == nil || p.workspaceConfigurationResolver == nil {
return slacksvc.WorkspaceConfiguration{}
return slacksvc.WorkspaceConfiguration{}, nil
}

p.workspaceConfigurationMu.Lock()
defer p.workspaceConfigurationMu.Unlock()

if p.workspaceConfigurationLoaded {
return p.workspaceConfiguration
return p.workspaceConfiguration, p.workspaceConfigurationErr
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

configuration, err := p.workspaceConfigurationResolver(ctx)
if err != nil {
return p.workspaceConfiguration
p.workspaceConfigurationErr = err
return p.workspaceConfiguration, err
}

p.workspaceConfiguration = configuration
p.workspaceConfigurationErr = nil
p.workspaceConfigurationLoaded = true
return p.workspaceConfiguration
return p.workspaceConfiguration, nil
}

func (p *Provider) ResolveUserTarget(user notification.User) (notification.Target, bool, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ func TestProviderMetadataRetriesWorkspaceDetailsAfterResolverError(t *testing.T)

firstMetadata := provider.ProviderMetadata()
assert.Equal(t, "Configured Slack workspace", firstMetadata.Description)
assert.Equal(t, "temporary slack failure", firstMetadata.Error)
assert.Empty(t, firstMetadata.Metadata)

secondMetadata := provider.ProviderMetadata()
assert.Equal(t, "Configured Slack workspace Recovered Workspace", secondMetadata.Description)
assert.Empty(t, secondMetadata.Error)
assert.Equal(t, "Recovered Workspace", secondMetadata.Metadata[MetadataKeyWorkspaceName])
assert.Equal(t, "T456", secondMetadata.Metadata[MetadataKeyTeamID])
assert.Equal(t, 2, attempts)
Expand Down
1 change: 1 addition & 0 deletions internal/service/notification/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ProviderMetadata struct {
DisplayName string
Description string
Enabled bool
Error string
Metadata map[string]string
}

Expand Down
Loading