Merge pull request #241 from SomtochiAma/gitlab-retried-job

Gitlab retried job
This commit is contained in:
Stefan Prodan 2021-09-30 10:33:18 +03:00 committed by GitHub
commit 8ff5f75a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"
"github.com/fluxcd/pkg/runtime/events"
@ -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
@ -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
}