Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor retries in gitlab set status #5293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,27 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
}
)

for i := 0; i < maxAttempts; i++ {
for {
attempt := int(retryer.Attempt()) + 1
logger := logger.With(
"attempt", i+1,
"attempt", attempt,
"max_attempts", maxAttempts,
"repo", repo.FullName,
"commit", commit.ShortID,
"state", state.String(),
)

_, resp, err = g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, setCommitStatusOptions)
_, resp, err := g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, setCommitStatusOptions)
if err == nil {
if retryer.Attempt() > 0 {
logger.Info("GitLab returned HTTP [200 OK] after updating commit status")
}

return nil
}
if attempt == maxAttempts {
return errors.Wrap(err, fmt.Sprintf("failed to update commit status for '%s' @ '%s' to '%s' after %d attempts", repo.FullName, pull.HeadCommit, src, attempt))
}

if resp != nil {
logger.Debug("POST /projects/%s/statuses/%s returned: %d", repo.FullName, pull.HeadCommit, resp.StatusCode)
Expand All @@ -481,26 +492,15 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
// GitLab does not allow merge requests to be merged when the pipeline status is "running."

if resp.StatusCode == http.StatusConflict {
sleep := retryer.ForAttempt(float64(i))

logger.With("retry_in", sleep).Warn("GitLab returned HTTP [409 Conflict] when updating commit status")
time.Sleep(sleep)

continue
logger.Warn("GitLab returned HTTP [409 Conflict] when updating commit status")
}
}

// Log we got a 200 OK response from GitLab after at least one retry to help with debugging/understanding delays/errors.
if err == nil && i > 0 {
logger.Info("GitLab returned HTTP [200 OK] after updating commit status")
}
sleep := retryer.Duration()

// Return the err, which might be nil if everything worked out
return err
logger.With("retry_in", sleep).Warn("GitLab errored when updating commit status: %w", err)
time.Sleep(sleep)
}

// If we got here, we've exhausted all attempts to update the commit status and still failed, so return the error upstream
return errors.Wrap(err, fmt.Sprintf("failed to update commit status for '%s' @ '%s' to '%s' after %d attempts", repo.FullName, pull.HeadCommit, src, maxAttempts))
}

func (g *GitlabClient) GetMergeRequest(logger logging.SimpleLogging, repoFullName string, pullNum int) (*gitlab.MergeRequest, error) {
Expand Down