@@ -5,16 +5,23 @@ package test
55import (
66 "context"
77 "fmt"
8+ "net/http"
9+ "os"
810 "regexp"
911 "testing"
12+ "time"
1013
1114 "github.com/google/go-github/v81/github"
1215 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
16+ "github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
17+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
1318 "github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
1419 tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
1520 "github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
21+ "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
1622 twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
1723 tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
24+ "github.com/tektoncd/pipeline/pkg/names"
1825 "gotest.tools/v3/assert"
1926 corev1 "k8s.io/api/core/v1"
2027 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -217,3 +224,112 @@ func TestGithubSecondPushRequestGitOpsCommentCancel(t *testing.T) {
217224 }
218225 assert .Assert (t , cancelled , "No cancelled pipeline run found" )
219226}
227+
228+ func TestGithubPullRequestRetestPullRequestNumberSubstitution (t * testing.T ) {
229+ targetNS := names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("pac-e2e-ns" )
230+ ctx := context .Background ()
231+ g := & tgithub.PRTest {}
232+
233+ ctx , runcnx , opts , ghcnx , err := tgithub .Setup (ctx , g .SecondController , g .Webhook )
234+ assert .NilError (t , err )
235+ g .Logger = runcnx .Clients .Log
236+
237+ g .CommitTitle = fmt .Sprintf ("Testing %s with Github APPS integration on %s" , g .Label , targetNS )
238+ g .Logger .Info (g .CommitTitle )
239+
240+ repoinfo , resp , err := ghcnx .Client ().Repositories .Get (ctx , opts .Organization , opts .Repo )
241+ assert .NilError (t , err )
242+ if resp != nil && resp .StatusCode == http .StatusNotFound {
243+ t .Errorf ("Repository %s not found in %s" , opts .Organization , opts .Repo )
244+ }
245+
246+ if g .Options .Settings .Github != nil {
247+ opts .Settings = g .Options .Settings
248+ }
249+ err = tgithub .CreateCRD (ctx , t , repoinfo , runcnx , opts , targetNS )
250+ assert .NilError (t , err )
251+
252+ tempBaseBranch := names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("temp-base-branch" )
253+ tempBaseBranchRef := fmt .Sprintf ("refs/heads/%s" , tempBaseBranch )
254+
255+ // Get the SHA of the default branch
256+ defaultBranchRef , _ , err := ghcnx .Client ().Git .GetRef (ctx , opts .Organization , opts .Repo , fmt .Sprintf ("refs/heads/%s" , repoinfo .GetDefaultBranch ()))
257+ assert .NilError (t , err )
258+
259+ // Create the temporary base branch
260+ _ , _ , err = ghcnx .Client ().Git .CreateRef (ctx , opts .Organization , opts .Repo , github.CreateRef {
261+ Ref : tempBaseBranchRef ,
262+ SHA : * defaultBranchRef .Object .SHA ,
263+ })
264+ assert .NilError (t , err )
265+
266+ yamlEntries := map [string ]string {".tekton/pipelinerun-pr-number-variable.yaml" : "testdata/pipelinerun-pr-number-variable.yaml" }
267+
268+ entries , err := payload .GetEntries (yamlEntries , targetNS , tempBaseBranchRef , triggertype .Push .String (), map [string ]string {})
269+ assert .NilError (t , err )
270+
271+ targetRefName := fmt .Sprintf ("refs/heads/%s" ,
272+ names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("pac-e2e-test" ))
273+
274+ sha , vref , err := tgithub .PushFilesToRef (ctx , ghcnx .Client (), g .CommitTitle , tempBaseBranchRef , targetRefName ,
275+ opts .Organization , opts .Repo , entries )
276+ assert .NilError (t , err )
277+
278+ g .Logger .Infof ("Commit %s has been created and pushed to %s" , sha , vref .GetURL ())
279+ number , err := tgithub .PRCreate (ctx , runcnx , ghcnx , opts .Organization ,
280+ opts .Repo , targetRefName , tempBaseBranchRef , g .CommitTitle )
281+ assert .NilError (t , err )
282+
283+ g .Cnx = runcnx
284+ g .Options = opts
285+ g .Provider = ghcnx
286+ g .TargetNamespace = targetNS
287+ g .TargetRefName = targetRefName
288+ g .PRNumber = number
289+ g .SHA = sha
290+
291+ defer g .TearDown (ctx , t )
292+ defer func () {
293+ if os .Getenv ("TEST_NOCLEANUP" ) != "true" {
294+ g .Logger .Infof ("Deleting Ref %s" , tempBaseBranchRef )
295+ _ , err := ghcnx .Client ().Git .DeleteRef (ctx , opts .Organization , opts .Repo , tempBaseBranchRef )
296+ assert .NilError (t , err )
297+ }
298+ }()
299+
300+ mergeResult , _ , err := g .Provider .Client ().PullRequests .Merge (ctx , opts .Organization , opts .Repo , g .PRNumber , g .CommitTitle , & github.PullRequestOptions {
301+ MergeMethod : "rebase" ,
302+ SHA : sha ,
303+ })
304+ assert .NilError (t , err )
305+ g .Logger .Infof ("Pull request %d has been merged" , g .PRNumber )
306+
307+ // wait for API to reflect this PR in response
308+ time .Sleep (10 * time .Second )
309+
310+ mergedSHA := mergeResult .GetSHA ()
311+
312+ _ , _ , err = g .Provider .Client ().Repositories .CreateComment (ctx , opts .Organization , opts .Repo , mergedSHA , & github.RepositoryComment {Body : github .Ptr ("/retest pipelinerun-pr-number-variable branch:" + tempBaseBranch )})
313+ assert .NilError (t , err )
314+ g .Logger .Infof ("Comment %s has been created" , mergedSHA )
315+
316+ waitOpts := twait.Opts {
317+ RepoName : g .TargetNamespace ,
318+ Namespace : g .TargetNamespace ,
319+ MinNumberStatus : 2 ,
320+ PollTimeout : twait .DefaultTimeout ,
321+ TargetSHA : mergedSHA ,
322+ }
323+
324+ err = twait .UntilPipelineRunHasReason (ctx , g .Cnx .Clients , tektonv1 .PipelineRunReasonSuccessful , waitOpts )
325+ assert .NilError (t , err )
326+
327+ regex := regexp .MustCompile (fmt .Sprintf ("I don't know my PR number is %d" , g .PRNumber ))
328+
329+ // because there are two pipeline runs were created due to pull request merge, we need the one which is triggered on retest comment.
330+ err = twait .RegexpMatchingInPodLog (ctx , g .Cnx , g .TargetNamespace ,
331+ fmt .Sprintf ("pipelinesascode.tekton.dev/event-type=%s" ,
332+ opscomments .RetestSingleCommentEventType .String ()),
333+ "step-task" , * regex , "" , 2 )
334+ assert .NilError (t , err )
335+ }
0 commit comments