chore: Refactor retries in gitlab set status (#5293)

Signed-off-by: Luke Massa <lukefrederickmassa@gmail.com>
This commit is contained in:
Luke Massa
2025-02-08 12:55:10 -05:00
committed by GitHub
parent 6f3c2b036a
commit e1dcc44918

View File

@@ -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)
@@ -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) {