Skip to content

Commit f1e238b

Browse files
committed
fix: change some log levels, increase retries and use pipeline id for commit status
1 parent 3ce6d18 commit f1e238b

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

server/events/vcs/gitlab_client.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,13 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
397397
gitlabState = gitlab.Success
398398
}
399399

400-
// refTarget is set to the head pipeline of the MR if it exists, or else it is set to the head branch
401-
// of the MR. This is needed because the commit status is only shown in the MR if the pipeline is
402-
// assigned to an MR reference.
403-
// Try to get the MR details a couple of times in case the pipeline is not yet assigned to the MR
404-
refTarget := pull.HeadBranch
405-
406-
retries := 1
407-
delay := 2 * time.Second
400+
// create these pointers here so that they can be nil when they get to the request and are omitted
401+
var refTarget *string
402+
var pipelineID *int
403+
404+
retries := 2
405+
delay := 5 * time.Second
406+
408407
var mr *gitlab.MergeRequest
409408
var err error
410409

@@ -414,17 +413,24 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
414413
return err
415414
}
416415
if mr.HeadPipeline != nil {
417-
logger.Debug("Head pipeline found for merge request %d, source '%s'. refTarget '%s'",
418-
pull.Num, mr.HeadPipeline.Source, mr.HeadPipeline.Ref)
419-
refTarget = mr.HeadPipeline.Ref
416+
logger.Info("Head pipeline found for merge request %d, source '%s'. pipelineID '%s'",
417+
pull.Num, mr.HeadPipeline.Source, mr.HeadPipeline.ID)
418+
pipelineID = gitlab.Ptr(mr.HeadPipeline.ID)
419+
420+
// let's check to see if the pipeline sha matches the head commit.
421+
if mr.HeadPipeline.SHA != pull.HeadCommit {
422+
logger.Err("Head pipeline SHA does not match pull head commit")
423+
}
420424
break
421425
}
422426
if i != retries {
423-
logger.Debug("Head pipeline not found for merge request %d. Retrying in %s",
427+
logger.Info("Head pipeline not found for merge request %d. Retrying in %s",
424428
pull.Num, delay)
425429
time.Sleep(delay)
426430
} else {
427-
logger.Debug("Head pipeline not found for merge request %d.",
431+
headBranch := pull.HeadBranch
432+
refTarget = gitlab.Ptr(headBranch)
433+
logger.Warn("Head pipeline not found for merge request %d.",
428434
pull.Num)
429435
}
430436
}
@@ -434,7 +440,8 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
434440
Context: gitlab.Ptr(src),
435441
Description: gitlab.Ptr(description),
436442
TargetURL: &url,
437-
Ref: gitlab.Ptr(refTarget),
443+
PipelineID: pipelineID,
444+
Ref: refTarget,
438445
})
439446
if resp != nil {
440447
logger.Debug("POST /projects/%s/statuses/%s returned: %d", repo.FullName, pull.HeadCommit, resp.StatusCode)
@@ -461,7 +468,7 @@ func (g *GitlabClient) WaitForSuccessPipeline(logger logging.SimpleLogging, ctx
461468
case <-ctx.Done():
462469
// validation check time out
463470
cancel()
464-
return //ctx.Err()
471+
return // ctx.Err()
465472

466473
default:
467474
mr, _ := g.GetMergeRequest(logger, pull.BaseRepo.FullName, pull.Num)

server/events/vcs/gitlab_client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"testing"
1313
"time"
1414

15-
version "github.com/hashicorp/go-version"
15+
"github.com/hashicorp/go-version"
1616
"github.com/runatlantis/atlantis/server/events/command"
1717
"github.com/runatlantis/atlantis/server/events/models"
1818
"github.com/runatlantis/atlantis/server/logging"
19-
gitlab "github.com/xanzy/go-gitlab"
19+
"github.com/xanzy/go-gitlab"
2020

2121
. "github.com/runatlantis/atlantis/testing"
2222
)
@@ -303,7 +303,7 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {
303303

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

0 commit comments

Comments
 (0)