@@ -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+
280366func TestGitlabCancelInProgressOnPRClose (t * testing.T ) {
281367 targetNS := names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("pac-e2e-ns" )
282368 ctx := context .Background ()
0 commit comments