Skip to content

Commit

Permalink
github: reuse existing deployment when sending new notification (#235)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: pasha-codefresh <[email protected]>
  • Loading branch information
MrFreezeex and pasha-codefresh authored Feb 7, 2024
1 parent 2daee60 commit c0913e2
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pkg/services/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &notification.GitHub.revision,
Environment: &notification.GitHub.Deployment.Environment,
RequiredContexts: &notification.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: &notification.GitHub.revision,
Environment: &notification.GitHub.Deployment.Environment,
RequiredContexts: &notification.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],
Expand Down

0 comments on commit c0913e2

Please sign in to comment.