Skip to content

Commit e1dcc44

Browse files
authored
chore: Refactor retries in gitlab set status (#5293)
Signed-off-by: Luke Massa <[email protected]>
1 parent 6f3c2b0 commit e1dcc44

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

server/events/vcs/gitlab_client.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,27 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
456456
}
457457
)
458458

459-
for i := 0; i < maxAttempts; i++ {
459+
for {
460+
attempt := int(retryer.Attempt()) + 1
460461
logger := logger.With(
461-
"attempt", i+1,
462+
"attempt", attempt,
462463
"max_attempts", maxAttempts,
463464
"repo", repo.FullName,
464465
"commit", commit.ShortID,
465466
"state", state.String(),
466467
)
467468

468-
_, resp, err = g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, setCommitStatusOptions)
469+
_, resp, err := g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, setCommitStatusOptions)
470+
if err == nil {
471+
if retryer.Attempt() > 0 {
472+
logger.Info("GitLab returned HTTP [200 OK] after updating commit status")
473+
}
474+
475+
return nil
476+
}
477+
if attempt == maxAttempts {
478+
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))
479+
}
469480

470481
if resp != nil {
471482
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
481492
// GitLab does not allow merge requests to be merged when the pipeline status is "running."
482493

483494
if resp.StatusCode == http.StatusConflict {
484-
sleep := retryer.ForAttempt(float64(i))
485-
486-
logger.With("retry_in", sleep).Warn("GitLab returned HTTP [409 Conflict] when updating commit status")
487-
time.Sleep(sleep)
488-
489-
continue
495+
logger.Warn("GitLab returned HTTP [409 Conflict] when updating commit status")
490496
}
491497
}
492498

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

498-
// Return the err, which might be nil if everything worked out
499-
return err
501+
logger.With("retry_in", sleep).Warn("GitLab errored when updating commit status: %w", err)
502+
time.Sleep(sleep)
500503
}
501-
502-
// If we got here, we've exhausted all attempts to update the commit status and still failed, so return the error upstream
503-
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))
504504
}
505505

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

0 commit comments

Comments
 (0)