Skip to content

Commit 1f2a957

Browse files
aThorp96chmouel
authored andcommitted
fix: Cancel PipelineRuns on MR change in Gitlab
When a Merge Request in Gitlab triggers a PipelineRun, the TriggerTarget is updated to "pull_request" but the EventType label intentionally remains as "Merge Request", unchanged from the event. This appears to be done intentionally. As a result, when querying for PipelineRuns related to an event with a PullRequest Trigger type using the EventType label, we have to include 'Merge_Request' as a possible label value Note: 'Merge_Request' is used instead of the event's raw string 'Merge Request' because <space> is an illegal character in labels and is sanitized in labels.AddLabelsAndAnnotations using formatting.CleanValueKubernetes
1 parent a53a87e commit 1f2a957

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func (p *PacRun) cancelInProgressMatchingPR(ctx context.Context, matchPR *tekton
8888
}
8989
labelSelector := getLabelSelector(labelMap)
9090
if p.event.TriggerTarget == triggertype.PullRequest {
91-
labelSelector += fmt.Sprintf(",%s in (pull_request, %s)", keys.EventType, opscomments.AnyOpsKubeLabelInSelector())
91+
// "Merge_Request" included since EventType is not normalized to "Pull Request" like TriggerTarget
92+
labelSelector += fmt.Sprintf(",%s in (pull_request, Merge_Request, %s)", keys.EventType, opscomments.AnyOpsKubeLabelInSelector())
9293
}
9394
p.run.Clients.Log.Infof("cancel-in-progress: selecting pipelineRuns to cancel with labels: %v", labelSelector)
9495
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(matchPR.GetNamespace()).List(ctx, metav1.ListOptions{

test/gitlab_merge_request_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
2121
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
2222
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
23+
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
2324
"github.com/tektoncd/pipeline/pkg/names"
2425
clientGitlab "gitlab.com/gitlab-org/api/client-go"
2526
"gotest.tools/v3/assert"
@@ -277,6 +278,91 @@ func TestGitlabOnComment(t *testing.T) {
277278
assert.NilError(t, err)
278279
}
279280

281+
func TestGitlabCancelInProgressOnChange(t *testing.T) {
282+
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
283+
ctx := context.Background()
284+
runcnx, opts, glprovider, err := tgitlab.Setup(ctx)
285+
assert.NilError(t, err)
286+
ctx, err = cctx.GetControllerCtxInfo(ctx, runcnx)
287+
assert.NilError(t, err)
288+
runcnx.Clients.Log.Info("Testing Gitlab cancel in progress on pr close")
289+
projectinfo, resp, err := glprovider.Client.Projects.GetProject(opts.ProjectID, nil)
290+
assert.NilError(t, err)
291+
if resp != nil && resp.StatusCode == http.StatusNotFound {
292+
t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo)
293+
}
294+
295+
err = tgitlab.CreateCRD(ctx, projectinfo, runcnx, targetNS, nil)
296+
assert.NilError(t, err)
297+
298+
entries, err := payload.GetEntries(map[string]string{
299+
".tekton/in-progress.yaml": "testdata/pipelinerun-cancel-in-progress.yaml",
300+
}, targetNS, projectinfo.DefaultBranch,
301+
triggertype.PullRequest.String(), map[string]string{})
302+
assert.NilError(t, err)
303+
targetRefName := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")
304+
305+
gitCloneURL, err := scm.MakeGitCloneURL(projectinfo.WebURL, opts.UserName, opts.Password)
306+
assert.NilError(t, err)
307+
mrTitle := "TestCancelInProgress initial commit - " + targetRefName
308+
scmOpts := &scm.Opts{
309+
GitURL: gitCloneURL,
310+
Log: runcnx.Clients.Log,
311+
WebURL: projectinfo.WebURL,
312+
TargetRefName: targetRefName,
313+
BaseRefName: projectinfo.DefaultBranch,
314+
CommitTitle: mrTitle,
315+
}
316+
317+
oldSha := scm.PushFilesToRefGit(t, scmOpts, entries)
318+
runcnx.Clients.Log.Infof("Branch %s has been created and pushed with files", targetRefName)
319+
mrID, err := tgitlab.CreateMR(glprovider.Client, opts.ProjectID, targetRefName, projectinfo.DefaultBranch, mrTitle)
320+
assert.NilError(t, err)
321+
runcnx.Clients.Log.Infof("MergeRequest %s/-/merge_requests/%d has been created", projectinfo.WebURL, mrID)
322+
defer tgitlab.TearDown(ctx, t, runcnx, glprovider, mrID, targetRefName, targetNS, opts.ProjectID)
323+
324+
runcnx.Clients.Log.Infof("Waiting for the pipelinerun to be created")
325+
originalPipelineWaitOpts := twait.Opts{
326+
RepoName: targetNS,
327+
Namespace: targetNS,
328+
MinNumberStatus: 1,
329+
PollTimeout: twait.DefaultTimeout,
330+
TargetSHA: oldSha,
331+
}
332+
err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, originalPipelineWaitOpts)
333+
assert.NilError(t, err)
334+
335+
newEntries := map[string]string{
336+
"new-file.txt": "plz work",
337+
}
338+
339+
changeTitle := "TestCancelInProgress second commit - " + targetRefName
340+
scmOpts = &scm.Opts{
341+
GitURL: gitCloneURL,
342+
Log: runcnx.Clients.Log,
343+
WebURL: projectinfo.WebURL,
344+
TargetRefName: targetRefName,
345+
BaseRefName: targetRefName,
346+
CommitTitle: changeTitle,
347+
}
348+
newSha := scm.PushFilesToRefGit(t, scmOpts, newEntries)
349+
350+
runcnx.Clients.Log.Infof("Waiting for new pipeline to be created")
351+
newPipelineWaitOpts := twait.Opts{
352+
RepoName: targetNS,
353+
Namespace: targetNS,
354+
MinNumberStatus: 1,
355+
PollTimeout: twait.DefaultTimeout,
356+
TargetSHA: newSha,
357+
}
358+
err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, newPipelineWaitOpts)
359+
assert.NilError(t, err)
360+
361+
runcnx.Clients.Log.Infof("Waiting for old pipelinerun to be canceled")
362+
cancelledErr := twait.UntilPipelineRunHasReason(ctx, runcnx.Clients, v1.PipelineRunReasonCancelled, originalPipelineWaitOpts)
363+
assert.NilError(t, cancelledErr)
364+
}
365+
280366
func TestGitlabCancelInProgressOnPRClose(t *testing.T) {
281367
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
282368
ctx := context.Background()

0 commit comments

Comments
 (0)