Skip to content

Commit c0913e2

Browse files
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 <arthur.outhenin-chalandre@ledger.fr> Co-authored-by: pasha-codefresh <pavel@codefresh.io>
1 parent 2daee60 commit c0913e2

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

pkg/services/github.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,21 +338,39 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
338338
if notification.GitHub.Deployment != nil {
339339
// maximum is 140 characters
340340
description := trunc(notification.Message, 140)
341-
deployment, _, err := g.client.Repositories.CreateDeployment(
341+
deployments, _, err := g.client.Repositories.ListDeployments(
342342
context.Background(),
343343
u[0],
344344
u[1],
345-
&github.DeploymentRequest{
346-
Ref: &notification.GitHub.revision,
347-
Environment: &notification.GitHub.Deployment.Environment,
348-
RequiredContexts: &notification.GitHub.Deployment.RequiredContexts,
349-
AutoMerge: notification.GitHub.Deployment.AutoMerge,
350-
TransientEnvironment: notification.GitHub.Deployment.TransientEnvironment,
345+
&github.DeploymentsListOptions{
346+
Ref: notification.GitHub.revision,
347+
Environment: notification.GitHub.Deployment.Environment,
351348
},
352349
)
353350
if err != nil {
354351
return err
355352
}
353+
354+
var deployment *github.Deployment
355+
if len(deployments) != 0 {
356+
deployment = deployments[0]
357+
} else {
358+
deployment, _, err = g.client.Repositories.CreateDeployment(
359+
context.Background(),
360+
u[0],
361+
u[1],
362+
&github.DeploymentRequest{
363+
Ref: &notification.GitHub.revision,
364+
Environment: &notification.GitHub.Deployment.Environment,
365+
RequiredContexts: &notification.GitHub.Deployment.RequiredContexts,
366+
AutoMerge: notification.GitHub.Deployment.AutoMerge,
367+
TransientEnvironment: notification.GitHub.Deployment.TransientEnvironment,
368+
},
369+
)
370+
if err != nil {
371+
return err
372+
}
373+
}
356374
_, _, err = g.client.Repositories.CreateDeploymentStatus(
357375
context.Background(),
358376
u[0],

0 commit comments

Comments
 (0)