Skip to content

Commit

Permalink
github: reuse existing deployment when sending new notification
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]>
  • Loading branch information
MrFreezeex committed Oct 19, 2023
1 parent 5aca046 commit 83df819
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions pkg/services/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,37 @@ 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(

Check warning on line 332 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L332

Added line #L332 was not covered by tests
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,
},
)
&github.DeploymentsListOptions{
Ref: notification.GitHub.revision,
Environment: notification.GitHub.Deployment.Environment,
})

Check warning on line 339 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L336-L339

Added lines #L336 - L339 were not covered by tests
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,
},
)
if err != nil {
return err
}

Check warning on line 361 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L344-L361

Added lines #L344 - L361 were not covered by tests
}
_, _, err = g.client.Repositories.CreateDeploymentStatus(
context.Background(),
u[0],
Expand Down

0 comments on commit 83df819

Please sign in to comment.