Skip to content

Commit 9648c1d

Browse files
committed
use Gitlab get commit statuses API to get previous pipeline
Signed-off-by: Ricky Hariady <[email protected]>
1 parent 86767f1 commit 9648c1d

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

server/events/vcs/gitlab_client.go

+18-25
Original file line numberDiff line numberDiff line change
@@ -416,41 +416,34 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
416416

417417
retries := 1
418418
delay := 2 * time.Second
419-
var commit *gitlab.Commit
420-
var statuses []*gitlab.CommitStatus
419+
var commitStatuses []*gitlab.CommitStatus
421420
var resp *gitlab.Response
422421
var err error
423422

423+
// get the last commit status with the same ref
424+
getCommitStatusesOptions := &gitlab.GetCommitStatusesOptions{
425+
ListOptions: gitlab.ListOptions{
426+
Sort: "desc",
427+
PerPage: 1,
428+
},
429+
Ref: gitlab.Ptr(pull.HeadBranch),
430+
}
431+
424432
// Try a couple of times to get the pipeline ID for the commit
425433
for i := 0; i <= retries; i++ {
426-
commit, resp, err = g.Client.Commits.GetCommit(repo.FullName, pull.HeadCommit, nil)
434+
commitStatuses, resp, err = g.Client.Commits.GetCommitStatuses(repo.FullName, pull.HeadCommit, getCommitStatusesOptions)
435+
427436
if resp != nil {
428437
logger.Debug("GET /projects/%s/repository/commits/%d: %d", pull.BaseRepo.ID(), pull.HeadCommit, resp.StatusCode)
429438
}
430439
if err != nil {
431440
return err
432441
}
433-
if commit.LastPipeline != nil {
434-
if commit.LastPipeline.Ref == pull.HeadBranch {
435-
logger.Info("Pipeline found for commit %s, setting pipeline ID to %d", pull.HeadCommit, commit.LastPipeline.ID)
436-
// Set the pipeline ID to the last pipeline that ran for the commit
437-
setCommitStatusOptions.PipelineID = gitlab.Ptr(commit.LastPipeline.ID)
438-
break
439-
} else {
440-
getCommitStatusesOptions := &gitlab.GetCommitStatusesOptions{
441-
Ref: gitlab.Ptr(pull.HeadBranch),
442-
Sort: gitlab.Ptr("desc"),
443-
PerPage: gitlab.Ptr(1)
444-
}
445-
446-
statuses, resp, err = g.Client.Commits.GetCommitStatus(repo.FullName, pull.HeadCommit, getCommitStatusesOptions)
447-
if len(statuses) > 0 {
448-
logger.Info("Pipeline found for commit %s, setting pipeline ID to %d", pull.HeadCommit, statuses[0].PipelineId)
449-
// Set the pipeline ID to the last pipeline that ran for the commit
450-
setCommitStatusOptions.PipelineID = gitlab.Ptr(statuses[0].PipelineId)
451-
break
452-
}
453-
}
442+
if len(commitStatuses) > 0 {
443+
logger.Info("Pipeline found for commit %s, setting pipeline ID to %d", pull.HeadCommit, commitStatuses[0].PipelineId)
444+
// Set the pipeline ID to the last pipeline that ran for the commit
445+
setCommitStatusOptions.PipelineID = gitlab.Ptr(commitStatuses[0].PipelineId)
446+
break
454447
}
455448
if i != retries {
456449
logger.Info("No pipeline found for commit %s, retrying in %s", pull.HeadCommit, delay)
@@ -475,7 +468,7 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
475468
"attempt", i+1,
476469
"max_attempts", maxAttempts,
477470
"repo", repo.FullName,
478-
"commit", commit.ShortID,
471+
"commit", pull.HeadCommit,
479472
"state", state.String(),
480473
)
481474

server/events/vcs/gitlab_client_test.go

+26-35
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ type UpdateStatusJsonBody struct {
4040
Ref string `json:"ref"`
4141
}
4242

43-
/* GetCommit response last_pipeline JSON object */
44-
type GetCommitResponseLastPipeline struct {
45-
ID int `json:"id"`
43+
type CommitStatus struct {
4644
Ref string `json:"ref"`
45+
PipelineID int `json:"pipeline_id"`
4746
}
4847

49-
/* GetCommit response JSON object */
50-
type GetCommitResponse struct {
51-
LastPipeline GetCommitResponseLastPipeline `json:"last_pipeline"`
52-
}
48+
/* GetCommitStatuses response JSON object */
49+
type GetCommitStatusesResponse []CommitStatus
5350

5451
/* Empty struct for JSON marshalling */
5552
type EmptyStruct struct{}
@@ -348,19 +345,19 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {
348345
_, err = w.Write(setStatusJsonResponse)
349346
Ok(t, err)
350347

351-
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha":
348+
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha/statuses?per_page=1&ref=test&sort=desc":
352349
w.WriteHeader(http.StatusOK)
353350

354-
getCommitResponse := GetCommitResponse{
355-
LastPipeline: GetCommitResponseLastPipeline{
356-
ID: gitlabPipelineSuccessMrID,
351+
getCommitStatusesResponse := GetCommitStatusesResponse{
352+
CommitStatus{
357353
Ref: updateStatusHeadBranch,
354+
PipelineID: gitlabPipelineSuccessMrID,
358355
},
359356
}
360-
getCommitJsonResponse, err := json.Marshal(getCommitResponse)
357+
getCommitStatusesJsonResponse, err := json.Marshal(getCommitStatusesResponse)
361358
Ok(t, err)
362359

363-
_, err = w.Write(getCommitJsonResponse)
360+
_, err = w.Write(getCommitStatusesJsonResponse)
364361
Ok(t, err)
365362

366363
case "/api/v4/":
@@ -471,25 +468,25 @@ func TestGitlabClient_UpdateStatusGetCommitRetryable(t *testing.T) {
471468
_, err = w.Write(getCommitJsonResponse)
472469
Ok(t, err)
473470

474-
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha":
471+
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha/statuses?per_page=1&ref=test&sort=desc":
475472
handledNumberOfRequests++
476473
noCommitLastPipeline := handledNumberOfRequests <= c.commitsWithNoLastPipeline
477474

478475
w.WriteHeader(http.StatusOK)
479476
if noCommitLastPipeline {
480-
getCommitJsonResponse, err := json.Marshal(EmptyStruct{})
477+
getCommitStatusesJsonResponse, err := json.Marshal([]EmptyStruct{})
481478
Ok(t, err)
482479

483-
_, err = w.Write(getCommitJsonResponse)
480+
_, err = w.Write(getCommitStatusesJsonResponse)
484481
Ok(t, err)
485482
} else {
486-
getCommitResponse := GetCommitResponse{
487-
LastPipeline: GetCommitResponseLastPipeline{
488-
ID: gitlabPipelineSuccessMrID,
483+
getCommitStatusesResponse := GetCommitStatusesResponse{
484+
CommitStatus{
489485
Ref: updateStatusHeadBranch,
486+
PipelineID: gitlabPipelineSuccessMrID,
490487
},
491488
}
492-
getCommitJsonResponse, err := json.Marshal(getCommitResponse)
489+
getCommitJsonResponse, err := json.Marshal(getCommitStatusesResponse)
493490
Ok(t, err)
494491

495492
_, err = w.Write(getCommitJsonResponse)
@@ -608,19 +605,19 @@ func TestGitlabClient_UpdateStatusSetCommitStatusConflictRetryable(t *testing.T)
608605
_, err = w.Write(getCommitJsonResponse)
609606
Ok(t, err)
610607

611-
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha":
608+
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha/statuses?per_page=1&ref=test&sort=desc":
612609
w.WriteHeader(http.StatusOK)
613610

614-
getCommitResponse := GetCommitResponse{
615-
LastPipeline: GetCommitResponseLastPipeline{
616-
ID: gitlabPipelineSuccessMrID,
611+
getCommitStatusesResponse := GetCommitStatusesResponse{
612+
CommitStatus{
617613
Ref: updateStatusHeadBranch,
614+
PipelineID: gitlabPipelineSuccessMrID,
618615
},
619616
}
620-
getCommitJsonResponse, err := json.Marshal(getCommitResponse)
617+
getCommitStatusesJsonResponse, err := json.Marshal(getCommitStatusesResponse)
621618
Ok(t, err)
622619

623-
_, err = w.Write(getCommitJsonResponse)
620+
_, err = w.Write(getCommitStatusesJsonResponse)
624621
Ok(t, err)
625622

626623
case "/api/v4/":
@@ -721,19 +718,13 @@ func TestGitlabClient_UpdateStatusDifferentRef(t *testing.T) {
721718
_, err = w.Write(setStatusJsonResponse)
722719
Ok(t, err)
723720

724-
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha":
721+
case "/api/v4/projects/runatlantis%2Fatlantis/repository/commits/sha/statuses?per_page=1&ref=test&sort=desc":
725722
w.WriteHeader(http.StatusOK)
726723

727-
getCommitResponse := GetCommitResponse{
728-
LastPipeline: GetCommitResponseLastPipeline{
729-
ID: gitlabPipelineSuccessMrID,
730-
Ref: updateStatusHeadBranchDuplicate,
731-
},
732-
}
733-
getCommitJsonResponse, err := json.Marshal(getCommitResponse)
724+
getCommitStatusesJsonResponse, err := json.Marshal([]EmptyStruct{})
734725
Ok(t, err)
735726

736-
_, err = w.Write(getCommitJsonResponse)
727+
_, err = w.Write(getCommitStatusesJsonResponse)
737728
Ok(t, err)
738729

739730
case "/api/v4/":

0 commit comments

Comments
 (0)