@@ -9,12 +9,17 @@ import (
99 "os"
1010 "testing"
1111
12+ "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1213 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
1314 tbbdc "github.com/openshift-pipelines/pipelines-as-code/test/pkg/bitbucketdatacenter"
15+ "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
16+ "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
1417 "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
1518
19+ "github.com/jenkins-x/go-scm/scm"
1620 "github.com/tektoncd/pipeline/pkg/names"
1721 "gotest.tools/v3/assert"
22+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1823)
1924
2025func TestBitbucketDataCenterPullRequest (t * testing.T ) {
@@ -35,9 +40,11 @@ func TestBitbucketDataCenterPullRequest(t *testing.T) {
3540 files [fmt .Sprintf (".tekton/pipelinerun-%d.yaml" , i )] = "testdata/pipelinerun.yaml"
3641 }
3742
43+ files , err = payload .GetEntries (files , targetNS , options .MainBranch , triggertype .PullRequest .String (), map [string ]string {})
44+ assert .NilError (t , err )
45+
3846 pr := tbbdc .CreatePR (ctx , t , client , runcnx , opts , repo , files , bitbucketWSOwner , targetNS )
39- runcnx .Clients .Log .Infof ("Pull Request with title '%s' is created" , pr .Title )
40- defer tbbdc .TearDown (ctx , t , runcnx , client , pr .Number , bitbucketWSOwner , targetNS )
47+ defer tbbdc .TearDown (ctx , t , runcnx , client , pr , bitbucketWSOwner , targetNS )
4148
4249 successOpts := wait.SuccessOpt {
4350 TargetNS : targetNS ,
@@ -64,9 +71,11 @@ func TestBitbucketDataCenterCELPathChangeInPullRequest(t *testing.T) {
6471 ".tekton/pipelinerun.yaml" : "testdata/pipelinerun-cel-path-changed.yaml" ,
6572 }
6673
74+ files , err = payload .GetEntries (files , targetNS , options .MainBranch , triggertype .PullRequest .String (), map [string ]string {})
75+ assert .NilError (t , err )
76+
6777 pr := tbbdc .CreatePR (ctx , t , client , runcnx , opts , repo , files , bitbucketWSOwner , targetNS )
68- runcnx .Clients .Log .Infof ("Pull Request with title '%s' is created" , pr .Title )
69- defer tbbdc .TearDown (ctx , t , runcnx , client , pr .Number , bitbucketWSOwner , targetNS )
78+ defer tbbdc .TearDown (ctx , t , runcnx , client , pr , bitbucketWSOwner , targetNS )
7079
7180 successOpts := wait.SuccessOpt {
7281 TargetNS : targetNS ,
@@ -76,3 +85,64 @@ func TestBitbucketDataCenterCELPathChangeInPullRequest(t *testing.T) {
7685 }
7786 wait .Succeeded (ctx , t , runcnx , opts , successOpts )
7887}
88+
89+ func TestBitbucketDataCenterOnPathChangeAnnotationOnPRMerge (t * testing.T ) {
90+ targetNS := names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("pac-e2e-ns" )
91+ // this would be a temporary base branch for the pull request we're going to raise
92+ // we need this because we're going to merge the pull request so that after test
93+ // we can delete the temporary base branch and our main branch should not be affected
94+ // by this merge because we run the E2E frequently.
95+ tempBaseBranch := names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("pac-e2e-ns" )
96+
97+ ctx := context .Background ()
98+ bitbucketWSOwner := os .Getenv ("TEST_BITBUCKET_SERVER_E2E_REPOSITORY" )
99+
100+ ctx , runcnx , opts , client , err := tbbdc .Setup (ctx )
101+ assert .NilError (t , err )
102+
103+ repo := tbbdc .CreateCRD (ctx , t , client , runcnx , bitbucketWSOwner , targetNS )
104+ runcnx .Clients .Log .Infof ("Repository %s has been created" , repo .Name )
105+ defer tbbdc .TearDownNs (ctx , t , runcnx , targetNS )
106+
107+ branch , resp , err := client .Git .CreateRef (ctx , bitbucketWSOwner , tempBaseBranch , repo .Branch )
108+ assert .NilError (t , err , "error creating branch: http status code: %d : %v" , resp .Status , err )
109+ runcnx .Clients .Log .Infof ("Base branch %s has been created" , branch .Name )
110+
111+ opts .BaseBranch = branch .Name
112+
113+ if os .Getenv ("TEST_NOCLEANUP" ) != "true" {
114+ defer func () {
115+ _ , err := client .Git .DeleteRef (ctx , bitbucketWSOwner , tempBaseBranch )
116+ assert .NilError (t , err , "error deleting branch: http status code: %d : %v" , resp .Status , err )
117+ }()
118+ }
119+
120+ files := map [string ]string {
121+ ".tekton/pr.yaml" : "testdata/pipelinerun-on-path-change.yaml" ,
122+ "doc/foo/bar/README.md" : "README.md" ,
123+ }
124+
125+ files , err = payload .GetEntries (files , targetNS , tempBaseBranch , triggertype .Push .String (), map [string ]string {})
126+ assert .NilError (t , err )
127+
128+ pr := tbbdc .CreatePR (ctx , t , client , runcnx , opts , repo , files , bitbucketWSOwner , targetNS )
129+ defer tbbdc .TearDown (ctx , t , runcnx , client , nil , bitbucketWSOwner , targetNS )
130+
131+ // merge the pull request so that we can get push event.
132+ _ , err = client .PullRequests .Merge (ctx , bitbucketWSOwner , pr .Number , & scm.PullRequestMergeOptions {})
133+ assert .NilError (t , err )
134+
135+ successOpts := wait.SuccessOpt {
136+ TargetNS : targetNS ,
137+ OnEvent : triggertype .Push .String (),
138+ NumberofPRMatch : 1 ,
139+ MinNumberStatus : 1 ,
140+ }
141+ wait .Succeeded (ctx , t , runcnx , opts , successOpts )
142+
143+ pipelineRuns , err := runcnx .Clients .Tekton .TektonV1 ().PipelineRuns (targetNS ).List (ctx , metav1.ListOptions {})
144+ assert .NilError (t , err )
145+ assert .Equal (t , len (pipelineRuns .Items ), 1 )
146+ // check that pipeline run contains on-path-change annotation.
147+ assert .Equal (t , pipelineRuns .Items [0 ].GetAnnotations ()[keys .OnPathChange ], "[doc/***.md]" )
148+ }
0 commit comments