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 Feb 1, 2024
1 parent 9f3e875 commit f523570
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(

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L341

Added line #L341 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,
TransientEnvironment: notification.GitHub.Deployment.TransientEnvironment,
&github.DeploymentsListOptions{
Ref: notification.GitHub.revision,
Environment: notification.GitHub.Deployment.Environment,

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L345-L347

Added lines #L345 - L347 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,
TransientEnvironment: notification.GitHub.Deployment.TransientEnvironment,
},
)
if err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L354-L372

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

0 comments on commit f523570

Please sign in to comment.