From 69e962c3c2957b9b8a725df7d98f1abd77fcfe02 Mon Sep 17 00:00:00 2001 From: Gerard van Engelen Date: Thu, 1 May 2025 06:50:40 +0200 Subject: [PATCH 1/2] fix(newrelic): set default revision template for notifications Signed-off-by: Gerard van Engelen --- pkg/services/newrelic.go | 5 ++ pkg/services/newrelic_test.go | 116 ++++++++++++++++++++++++---------- 2 files changed, 86 insertions(+), 35 deletions(-) diff --git a/pkg/services/newrelic.go b/pkg/services/newrelic.go index f0717be1..e912b68c 100644 --- a/pkg/services/newrelic.go +++ b/pkg/services/newrelic.go @@ -32,6 +32,11 @@ var ( ) func (n *NewrelicNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) { + revisionTemplate := n.Revision + if revisionTemplate == "" { + revisionTemplate = "{{.app.status.operationState.syncResult.revision}}" + } + revision, err := texttemplate.New(name).Funcs(f).Parse(revisionTemplate) if err != nil { return nil, err diff --git a/pkg/services/newrelic_test.go b/pkg/services/newrelic_test.go index d8978865..31fdcf26 100644 --- a/pkg/services/newrelic_test.go +++ b/pkg/services/newrelic_test.go @@ -11,48 +11,94 @@ import ( ) func TestGetTemplater_Newrelic(t *testing.T) { - n := Notification{ - Newrelic: &NewrelicNotification{ - Changelog: "Added: /v2/deployments.rb", - Description: "Deployment finished for {{.app.metadata.name}}. Visit: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}", - User: "{{.context.user}}", - }, - } - - templater, err := n.GetTemplater("newrelic", template.FuncMap{}) - if !assert.NoError(t, err) { - return - } - - var notification Notification - - err = templater(¬ification, map[string]interface{}{ - "context": map[string]interface{}{ - "argocdUrl": "https://example.com", - "user": "somebot", - }, - "app": map[string]interface{}{ - "metadata": map[string]interface{}{ - "name": "argocd-notifications", + t.Run("default revision template", func(t *testing.T) { + n := Notification{ + Newrelic: &NewrelicNotification{ + Changelog: "Added: /v2/deployments.rb", + Description: "Deployment finished for {{.app.metadata.name}}. Visit: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}", + User: "{{.context.user}}", + }, + } + + templater, err := n.GetTemplater("newrelic", template.FuncMap{}) + if !assert.NoError(t, err) { + return + } + + var notification Notification + + err = templater(¬ification, map[string]interface{}{ + "context": map[string]interface{}{ + "argocdUrl": "https://example.com", + "user": "somebot", }, - "status": map[string]interface{}{ - "operationState": map[string]interface{}{ - "syncResult": map[string]interface{}{ - "revision": "0123456789", + "app": map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": "argocd-notifications", + }, + "status": map[string]interface{}{ + "operationState": map[string]interface{}{ + "syncResult": map[string]interface{}{ + "revision": "0123456789", + }, }, }, }, - }, + }) + + if !assert.NoError(t, err) { + return + } + + assert.Equal(t, "0123456789", notification.Newrelic.Revision) + assert.Equal(t, "Added: /v2/deployments.rb", notification.Newrelic.Changelog) + assert.Equal(t, "Deployment finished for argocd-notifications. Visit: https://example.com/applications/argocd-notifications", notification.Newrelic.Description) + assert.Equal(t, "somebot", notification.Newrelic.User) }) - if !assert.NoError(t, err) { - return - } + t.Run("custom revision template", func(t *testing.T) { + n := Notification{ + Newrelic: &NewrelicNotification{ + Revision: "{{.app.status.custom.revision}}", + Changelog: "Added: /v2/deployments.rb", + Description: "Deployment finished for {{.app.metadata.name}}. Visit: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}", + User: "{{.context.user}}", + }, + } + + templater, err := n.GetTemplater("newrelic", template.FuncMap{}) + if !assert.NoError(t, err) { + return + } + + var notification Notification + + err = templater(¬ification, map[string]interface{}{ + "context": map[string]interface{}{ + "argocdUrl": "https://example.com", + "user": "somebot", + }, + "app": map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": "argocd-notifications", + }, + "status": map[string]interface{}{ + "custom": map[string]interface{}{ + "revision": "custom-revision-123", + }, + }, + }, + }) + + if !assert.NoError(t, err) { + return + } - assert.Equal(t, "0123456789", notification.Newrelic.Revision) - assert.Equal(t, "Added: /v2/deployments.rb", notification.Newrelic.Changelog) - assert.Equal(t, "Deployment finished for argocd-notifications. Visit: https://example.com/applications/argocd-notifications", notification.Newrelic.Description) - assert.Equal(t, "somebot", notification.Newrelic.User) + assert.Equal(t, "custom-revision-123", notification.Newrelic.Revision) + assert.Equal(t, "Added: /v2/deployments.rb", notification.Newrelic.Changelog) + assert.Equal(t, "Deployment finished for argocd-notifications. Visit: https://example.com/applications/argocd-notifications", notification.Newrelic.Description) + assert.Equal(t, "somebot", notification.Newrelic.User) + }) } func TestSend_Newrelic(t *testing.T) { From 978c5596ae6f2c5a204853e4aa64df5a2706aae0 Mon Sep 17 00:00:00 2001 From: Gerard van Engelen Date: Thu, 1 May 2025 07:06:20 +0200 Subject: [PATCH 2/2] fix(newrelic): update parameter descriptions and defaults in documentation Signed-off-by: Gerard van Engelen --- docs/services/newrelic.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/services/newrelic.md b/docs/services/newrelic.md index b0c7e340..232e27a5 100644 --- a/docs/services/newrelic.md +++ b/docs/services/newrelic.md @@ -2,8 +2,8 @@ ## Parameters -* `apiURL` - the api server url, e.g. https://api.newrelic.com -* `apiKey` - a [NewRelic ApiKey](https://docs.newrelic.com/docs/apis/rest-api-v2/get-started/introduction-new-relic-rest-api-v2/#api_key) +- `apiURL` - the api server url, e.g. https://api.newrelic.com +- `apiKey` - a [NewRelic ApiKey](https://docs.newrelic.com/docs/apis/rest-api-v2/get-started/introduction-new-relic-rest-api-v2/#api_key) ## Configuration @@ -43,12 +43,14 @@ metadata: ## Templates -* `description` - __optional__, high-level description of this deployment, visible in the [Summary](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/apm-overview-page) page and on the [Deployments](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page) page when you select an individual deployment. - * Defaults to `message` -* `changelog` - __optional__, A summary of what changed in this deployment, visible in the [Deployments](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page) page when you select (selected deployment) > Change log. - * Defaults to `{{(call .repo.GetCommitMetadata .app.status.sync.revision).Message}}` -* `user` - __optional__, A username to associate with the deployment, visible in the [Summary](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page) and on the [Deployments](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page). - * Defaults to `{{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}}` +- `revision` - **optional**, The revision being deployed. Can contain a custom template to extract the revision from your specific application status structure. + - Defaults to `{{.app.status.operationState.syncResult.revision}}` +- `description` - **optional**, high-level description of this deployment, visible in the [Summary](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/apm-overview-page) page and on the [Deployments](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page) page when you select an individual deployment. + - Defaults to `message` +- `changelog` - **optional**, A summary of what changed in this deployment, visible in the [Deployments](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page) page when you select (selected deployment) > Change log. + - Defaults to `{{(call .repo.GetCommitMetadata .app.status.sync.revision).Message}}` +- `user` - **optional**, A username to associate with the deployment, visible in the [Summary](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page) and on the [Deployments](https://docs.newrelic.com/docs/apm/applications-menu/events/deployments-page). + - Defaults to `{{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}}` ```yaml context: |