From dfaa5ec1d448f17203ec47d68f3e4db7d776b022 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Wed, 22 Oct 2025 17:07:08 -0400 Subject: [PATCH] refactor: extract conversion to integration to ConvertReceiverIntegrationToIntegration --- .../notifications/receiver/conversions.go | 80 ++++++++++--------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/pkg/registry/apps/alerting/notifications/receiver/conversions.go b/pkg/registry/apps/alerting/notifications/receiver/conversions.go index 993c63a46fd..9b7d514b471 100644 --- a/pkg/registry/apps/alerting/notifications/receiver/conversions.go +++ b/pkg/registry/apps/alerting/notifications/receiver/conversions.go @@ -128,47 +128,55 @@ func convertToDomainModel(receiver *model.Receiver) (*ngmodels.Receiver, map[str } storedSecureFields := make(map[string][]string, len(receiver.Spec.Integrations)) for _, integration := range receiver.Spec.Integrations { - t, err := alertingNotify.IntegrationTypeFromString(integration.Type) + grafanaIntegration, secureFields, err := ConvertReceiverIntegrationToIntegration(receiver.Spec.Title, integration) if err != nil { return nil, nil, err } - var config schema.IntegrationSchemaVersion - typeSchema, _ := alertingNotify.GetSchemaForIntegration(t) - if integration.Version != "" { - var ok bool - config, ok = typeSchema.GetVersion(schema.Version(integration.Version)) - if !ok { - return nil, nil, fmt.Errorf("invalid version %s for integration type %s", integration.Version, integration.Type) - } - } else { - config = typeSchema.GetCurrentVersion() - } - grafanaIntegration := ngmodels.Integration{ - Name: receiver.Spec.Title, - Config: config, - Settings: maps.Clone(integration.Settings), - SecureSettings: make(map[string]string), - } - if integration.Uid != nil { - grafanaIntegration.UID = *integration.Uid - } - if integration.DisableResolveMessage != nil { - grafanaIntegration.DisableResolveMessage = *integration.DisableResolveMessage - } - domain.Integrations = append(domain.Integrations, &grafanaIntegration) - - if grafanaIntegration.UID != "" { - // This is an existing integration, so we track the secure fields being requested to copy over from existing values. - secureFields := make([]string, 0, len(integration.SecureFields)) - for k, isSecure := range integration.SecureFields { - if isSecure { - secureFields = append(secureFields, k) - } - } - storedSecureFields[grafanaIntegration.UID] = secureFields - } + storedSecureFields[grafanaIntegration.UID] = secureFields } return domain, storedSecureFields, nil } + +func ConvertReceiverIntegrationToIntegration(receiverTitle string, integration model.ReceiverIntegration) (ngmodels.Integration, []string, error) { + t, err := alertingNotify.IntegrationTypeFromString(integration.Type) + if err != nil { + return ngmodels.Integration{}, nil, err + } + var config schema.IntegrationSchemaVersion + typeSchema, _ := alertingNotify.GetSchemaForIntegration(t) + if integration.Version != "" { + var ok bool + config, ok = typeSchema.GetVersion(schema.Version(integration.Version)) + if !ok { + return ngmodels.Integration{}, nil, fmt.Errorf("invalid version %s for integration type %s", integration.Version, integration.Type) + } + } else { + config = typeSchema.GetCurrentVersion() + } + grafanaIntegration := ngmodels.Integration{ + Name: receiverTitle, + Config: config, + Settings: maps.Clone(integration.Settings), + SecureSettings: make(map[string]string), + } + if integration.Uid != nil { + grafanaIntegration.UID = *integration.Uid + } + if integration.DisableResolveMessage != nil { + grafanaIntegration.DisableResolveMessage = *integration.DisableResolveMessage + } + + var secureFields []string + if grafanaIntegration.UID != "" { + // This is an existing integration, so we track the secure fields being requested to copy over from existing values. + secureFields = make([]string, 0, len(integration.SecureFields)) + for k, isSecure := range integration.SecureFields { + if isSecure { + secureFields = append(secureFields, k) + } + } + } + return grafanaIntegration, secureFields, nil +}