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

Add in exp backoff for finding MR pipelines #29

Merged
merged 1 commit into from
Jul 17, 2023
Merged
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
39 changes: 38 additions & 1 deletion pkg/vcs/gitlab/mr_status_updater.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package gitlab

import (
"errors"
"fmt"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/go-tfe"
"github.com/rs/zerolog/log"
gogitlab "github.com/xanzy/go-gitlab"
"github.com/zapier/tfbuddy/pkg/runstream"
)

// Sentinel error
var errNoPipelineStatus = errors.New("nil pipeline status")

func (p *RunStatusUpdater) updateCommitStatusForRun(run *tfe.Run, rmd runstream.RunMetadata) {
switch run.Status {
// https://www.terraform.io/cloud-docs/api-docs/run#run-states
Expand Down Expand Up @@ -92,7 +98,27 @@ func (p *RunStatusUpdater) updateStatus(state gogitlab.BuildStateValue, action s
TargetURL: runUrlForTFRunMetadata(rmd),
Description: descriptionForState(state),
State: state,
PipelineID: p.getLatestPipelineID(rmd),
}

// Look up the latest pipeline ID for this MR, since Gitlab is eventually consistent
// Once we have a pipeline ID returned, we know we have a valid pipeline to set commit status for
var pipelineID *int
getPipelineIDFn := func() error {
log.Debug().Msg("getting pipeline status")
pipelineID := p.getLatestPipelineID(rmd)
if pipelineID == nil {
return errNoPipelineStatus
}
return nil
}

err := backoff.Retry(getPipelineIDFn, configureBackOff())
if err != nil {
log.Warn().Msg("could not retrieve pipeline id after multiple attempts")
}
if pipelineID != nil {
log.Trace().Int("pipeline_id", *pipelineID).Msg("pipeline status")
status.PipelineID = pipelineID
}

log.Debug().Interface("new_status", status).Msg("updating Gitlab commit status")
Expand Down Expand Up @@ -151,3 +177,14 @@ func (p *RunStatusUpdater) getLatestPipelineID(rmd runstream.RunMetadata) *int {
}
return nil
}

// configureBackOff returns a backoff configuration to use to retry requests
func configureBackOff() *backoff.ExponentialBackOff {

// Lets setup backoff logic to retry this request for 30 seconds
expBackOff := backoff.NewExponentialBackOff()
expBackOff.MaxInterval = 10 * time.Second
expBackOff.MaxElapsedTime = 30 * time.Second

return expBackOff
}
Loading