Skip to content

Commit

Permalink
fix: change some log levels, increase retries and use pipeline id for…
Browse files Browse the repository at this point in the history
… commit status
  • Loading branch information
fitz7 committed Mar 8, 2024
1 parent 3ce6d18 commit 08fda6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
39 changes: 23 additions & 16 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/vcs/common"

version "github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/logging"

"github.com/runatlantis/atlantis/server/events/models"
gitlab "github.com/xanzy/go-gitlab"
"github.com/xanzy/go-gitlab"
)

// gitlabMaxCommentLength is the maximum number of chars allowed by Gitlab in a
Expand Down Expand Up @@ -397,14 +397,13 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
gitlabState = gitlab.Success
}

// refTarget is set to the head pipeline of the MR if it exists, or else it is set to the head branch
// of the MR. This is needed because the commit status is only shown in the MR if the pipeline is
// assigned to an MR reference.
// Try to get the MR details a couple of times in case the pipeline is not yet assigned to the MR
refTarget := pull.HeadBranch
// create these pointers here so that they can be nil when they get to the request and are omitted
var refTarget *string
var pipelineID *int

retries := 2
delay := 5 * time.Second

retries := 1
delay := 2 * time.Second
var mr *gitlab.MergeRequest
var err error

Expand All @@ -414,17 +413,24 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
return err
}
if mr.HeadPipeline != nil {
logger.Debug("Head pipeline found for merge request %d, source '%s'. refTarget '%s'",
pull.Num, mr.HeadPipeline.Source, mr.HeadPipeline.Ref)
refTarget = mr.HeadPipeline.Ref
logger.Info("Head pipeline found for merge request %d, source '%s'. pipelineID '%s'",
pull.Num, mr.HeadPipeline.Source, mr.HeadPipeline.ID)
pipelineID = gitlab.Ptr(mr.HeadPipeline.ID)

// let's check to see if the pipeline sha matches the head commit.
if mr.HeadPipeline.SHA != pull.HeadCommit {
logger.Err("Head pipeline SHA does not match pull head commit")
}
break
}
if i != retries {
logger.Debug("Head pipeline not found for merge request %d. Retrying in %s",
logger.Info("Head pipeline not found for merge request %d. Retrying in %s",
pull.Num, delay)
time.Sleep(delay)
} else {
logger.Debug("Head pipeline not found for merge request %d.",
headBranch := pull.HeadBranch
refTarget = gitlab.Ptr(headBranch)
logger.Warn("Head pipeline not found for merge request %d.",
pull.Num)
}
}
Expand All @@ -434,7 +440,8 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
Context: gitlab.Ptr(src),
Description: gitlab.Ptr(description),
TargetURL: &url,
Ref: gitlab.Ptr(refTarget),
PipelineID: pipelineID,
Ref: refTarget,
})
if resp != nil {
logger.Debug("POST /projects/%s/statuses/%s returned: %d", repo.FullName, pull.HeadCommit, resp.StatusCode)
Expand All @@ -461,7 +468,7 @@ func (g *GitlabClient) WaitForSuccessPipeline(logger logging.SimpleLogging, ctx
case <-ctx.Done():
// validation check time out
cancel()
return //ctx.Err()
return // ctx.Err()

default:
mr, _ := g.GetMergeRequest(logger, pull.BaseRepo.FullName, pull.Num)
Expand Down
6 changes: 3 additions & 3 deletions server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"testing"
"time"

version "github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
gitlab "github.com/xanzy/go-gitlab"
"github.com/xanzy/go-gitlab"

. "github.com/runatlantis/atlantis/testing"
)
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {

body, err := io.ReadAll(r.Body)
Ok(t, err)
exp := fmt.Sprintf(`{"state":"%s","ref":"patch-1-merger","context":"src","target_url":"https://google.com","description":"description"}`, c.expState)
exp := fmt.Sprintf(`{"state":"%s","context":"src","target_url":"https://google.com","description":"description","pipeline_id":488598}`, c.expState)
Equals(t, exp, string(body))
defer r.Body.Close() // nolint: errcheck
w.Write([]byte("{}")) // nolint: errcheck
Expand Down

0 comments on commit 08fda6d

Please sign in to comment.