Skip to content

Commit

Permalink
Merge pull request #241 from SomtochiAma/gitlab-retried-job
Browse files Browse the repository at this point in the history
Gitlab retried job
  • Loading branch information
stefanprodan authored Sep 30, 2021
2 parents fdb79b5 + ae60d48 commit 8ff5f75
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/notifier/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"

"github.com/fluxcd/pkg/runtime/events"
Expand Down Expand Up @@ -91,6 +92,23 @@ func (g *GitLab) Post(event events.Event) error {
State: state,
}

listOpts := &gitlab.GetCommitStatusesOptions{}

status := &gitlab.CommitStatus{
Name: name,
SHA: rev,
Status: string(state),
Description: desc,
}

statuses, _, err := g.Client.Commits.GetCommitStatuses(g.Id, rev, listOpts)
if err != nil {
return fmt.Errorf("unable to list commit status: %s", err)
}
if duplicateGitlabStatus(statuses, status) {
return nil
}

_, _, err = g.Client.Commits.SetCommitStatus(g.Id, rev, options)
if err != nil {
return err
Expand All @@ -109,3 +127,15 @@ func toGitLabState(severity string) (gitlab.BuildStateValue, error) {
return "", errors.New("can't convert to gitlab state")
}
}

func duplicateGitlabStatus(statuses []*gitlab.CommitStatus, status *gitlab.CommitStatus) bool {
for _, s := range statuses {
if s.SHA == status.SHA {
if s.Status == status.Status && s.Description == status.Description && s.Name == status.Name {
return true
}
}
}

return false
}

0 comments on commit 8ff5f75

Please sign in to comment.