Skip to content

Commit 77a51d4

Browse files
committed
check that all commitstatus have the same pipeline Id
1 parent 6cfdc38 commit 77a51d4

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

server/events/vcs/gitlab_client.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,6 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
424424
getCommitStatusesOptions := &gitlab.GetCommitStatusesOptions{
425425
ListOptions: gitlab.ListOptions{
426426
Sort: "desc",
427-
PerPage: 1,
428427
},
429428
Ref: gitlab.Ptr(pull.HeadBranch),
430429
}
@@ -440,10 +439,40 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
440439
return err
441440
}
442441
if len(commitStatuses) > 0 {
443-
logger.Info("Pipeline found for commit %s and ref %s, setting pipeline ID to %d", pull.HeadCommit, pull.HeadBranch, 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
442+
uniquePipelineIds := make(map[int]struct{})
443+
444+
// Loop through the commitStatuses to get unique pipeline Ids
445+
for _, commitStatus := range commitStatuses {
446+
// Ensure that commitStatus contain non-nil pipeline Id
447+
if commitStatus.PipelineId == nil {
448+
logger.Warn("Commit %s has status (id=%s) with no pipeline. Skipping.", pull.HeadCommit, commitStatus.ID)
449+
continue
450+
}
451+
uniquePipelineIds[commitStatus.PipelineId] = struct{}{}
452+
}
453+
454+
// Only set PipelineID if uniquePipelineIds is not empty
455+
if len(uniquePipelineIds) > 0 {
456+
457+
// Ensure that all commit statuses has identical pipeline ID
458+
if len(uniquePipelineIds) > 1 {
459+
// Create a slice to hold all the unique pipelineIds
460+
var pipelineIds []int
461+
462+
// Loop through the map and collect the pipelineIds
463+
for pipelineId := range uniquePipelineIds {
464+
pipelineIds = append(pipelineIds, pipelineId)
465+
}
466+
467+
logger.Warn("Commit %s has statuses with more than one PipelineID (id=%s) for ref %s. Set PipelineID from the last commit status: %s", pull.HeadCommit, pipelineIds, pull.HeadBranch, commitStatuses[0].PipelineId)
468+
} else {
469+
logger.Info("Exactly one PipelineID found for commit %s and ref %s, setting new status PipelineID to %d", pull.HeadCommit, pull.HeadBranch, commitStatuses[0].PipelineId)
470+
}
471+
472+
// Set the pipeline ID to the last pipeline that ran for the commit
473+
setCommitStatusOptions.PipelineID = gitlab.Ptr(commitStatuses[0].PipelineId)
474+
break
475+
}
447476
}
448477
if i != retries {
449478
logger.Info("No pipeline found for commit %s and ref %s, retrying in %s", pull.HeadCommit, pull.HeadBranch, delay)

0 commit comments

Comments
 (0)