From c0913e2c447fbba58d99f188a59eeb3f9e856f07 Mon Sep 17 00:00:00 2001 From: Arthur Outhenin-Chalandre Date: Wed, 7 Feb 2024 08:57:55 +0100 Subject: [PATCH] github: reuse existing deployment when sending new notification (#235) Try to list existing deployment with the same sha/environment/ref and reuse it if found. This allows ArgoCD notification to update the same deployment with multiple deployment status instead of creating a new one on each deployment notification. Signed-off-by: Arthur Outhenin-Chalandre Co-authored-by: pasha-codefresh --- pkg/services/github.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pkg/services/github.go b/pkg/services/github.go index 14f32fb1..6200c8f4 100644 --- a/pkg/services/github.go +++ b/pkg/services/github.go @@ -338,21 +338,39 @@ func (g gitHubService) Send(notification Notification, _ Destination) error { if notification.GitHub.Deployment != nil { // maximum is 140 characters description := trunc(notification.Message, 140) - deployment, _, err := g.client.Repositories.CreateDeployment( + deployments, _, err := g.client.Repositories.ListDeployments( context.Background(), u[0], u[1], - &github.DeploymentRequest{ - Ref: ¬ification.GitHub.revision, - Environment: ¬ification.GitHub.Deployment.Environment, - RequiredContexts: ¬ification.GitHub.Deployment.RequiredContexts, - AutoMerge: notification.GitHub.Deployment.AutoMerge, - TransientEnvironment: notification.GitHub.Deployment.TransientEnvironment, + &github.DeploymentsListOptions{ + Ref: notification.GitHub.revision, + Environment: notification.GitHub.Deployment.Environment, }, ) if err != nil { return err } + + var deployment *github.Deployment + if len(deployments) != 0 { + deployment = deployments[0] + } else { + deployment, _, err = g.client.Repositories.CreateDeployment( + context.Background(), + u[0], + u[1], + &github.DeploymentRequest{ + Ref: ¬ification.GitHub.revision, + Environment: ¬ification.GitHub.Deployment.Environment, + RequiredContexts: ¬ification.GitHub.Deployment.RequiredContexts, + AutoMerge: notification.GitHub.Deployment.AutoMerge, + TransientEnvironment: notification.GitHub.Deployment.TransientEnvironment, + }, + ) + if err != nil { + return err + } + } _, _, err = g.client.Repositories.CreateDeploymentStatus( context.Background(), u[0],