@@ -7,11 +7,15 @@ import (
77 "fmt"
88 "net/http"
99 "os"
10+ "regexp"
1011 "strconv"
1112 "testing"
13+ "time"
1214
1315 "github.com/google/go-github/v84/github"
16+ "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1417 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
18+ "github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
1519 tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
1620 "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
1721 twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
@@ -41,60 +45,99 @@ func TestGithubGHEPullRequestOkToTest(t *testing.T) {
4145 Organization : g .Options .Organization ,
4246 Repository : g .Options .Repo ,
4347 URL : repoinfo .GetHTMLURL (),
44- Sender : g .Options .Organization ,
4548 }
4649
50+ repo , err := g .Cnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (g .TargetNamespace ).Get (ctx , g .TargetNamespace , metav1.GetOptions {})
51+ assert .NilError (t , err )
52+ initialStatusCount := len (repo .Status )
53+
54+ pruns , err := g .Cnx .Clients .Tekton .TektonV1 ().PipelineRuns (g .TargetNamespace ).List (ctx , metav1.ListOptions {
55+ LabelSelector : fmt .Sprintf ("%s=%s" , keys .SHA , g .SHA ),
56+ })
57+ assert .NilError (t , err )
58+ initialPipelineRunCount := len (pruns .Items )
59+
4760 installID , err := strconv .ParseInt (os .Getenv ("TEST_GITHUB_SECOND_APPLICATION_ID" ), 10 , 64 )
4861 assert .NilError (t , err )
49- event := github.IssueCommentEvent {
50- Comment : & github.IssueComment {
51- Body : github .Ptr (`/ok-to-test` ),
52- },
53- Installation : & github.Installation {
54- ID : & installID ,
55- },
56- Action : github .Ptr ("created" ),
57- Issue : & github.Issue {
58- State : github .Ptr ("open" ),
59- PullRequestLinks : & github.PullRequestLinks {
60- HTMLURL : github .Ptr (fmt .Sprintf ("%s/pull/%d" , runevent .URL , g .PRNumber )),
62+
63+ sendIssueComment := func (t * testing.T , sender string ) {
64+ t .Helper ()
65+
66+ event := github.IssueCommentEvent {
67+ Comment : & github.IssueComment {
68+ Body : github .Ptr (`/ok-to-test` ),
69+ },
70+ Installation : & github.Installation {
71+ ID : & installID ,
72+ },
73+ Action : github .Ptr ("created" ),
74+ Issue : & github.Issue {
75+ State : github .Ptr ("open" ),
76+ PullRequestLinks : & github.PullRequestLinks {
77+ HTMLURL : github .Ptr (fmt .Sprintf ("%s/pull/%d" , runevent .URL , g .PRNumber )),
78+ },
79+ },
80+ Repo : & github.Repository {
81+ DefaultBranch : & runevent .DefaultBranch ,
82+ HTMLURL : & runevent .URL ,
83+ Name : & runevent .Repository ,
84+ Owner : & github.User {Login : & runevent .Organization },
85+ },
86+ Sender : & github.User {
87+ Login : github .Ptr (sender ),
6188 },
62- },
63- Repo : & github.Repository {
64- DefaultBranch : & runevent .DefaultBranch ,
65- HTMLURL : & runevent .URL ,
66- Name : & runevent .Repository ,
67- Owner : & github.User {Login : & runevent .Organization },
68- },
69- Sender : & github.User {
70- Login : & runevent .Sender ,
71- },
89+ }
90+
91+ err = payload .Send (ctx ,
92+ g .Cnx ,
93+ os .Getenv ("TEST_GITHUB_SECOND_EL_URL" ),
94+ os .Getenv ("TEST_GITHUB_SECOND_WEBHOOK_SECRET" ),
95+ os .Getenv ("TEST_GITHUB_SECOND_API_URL" ),
96+ os .Getenv ("TEST_GITHUB_SECOND_APPLICATION_ID" ),
97+ event ,
98+ "issue_comment" ,
99+ )
100+ assert .NilError (t , err )
72101 }
73102
74- err = payload .Send (ctx ,
75- g .Cnx ,
76- os .Getenv ("TEST_GITHUB_SECOND_EL_URL" ),
77- os .Getenv ("TEST_GITHUB_SECOND_WEBHOOK_SECRET" ),
78- os .Getenv ("TEST_GITHUB_SECOND_API_URL" ),
79- os .Getenv ("TEST_GITHUB_SECOND_APPLICATION_ID" ),
80- event ,
81- "issue_comment" ,
82- )
103+ g .Cnx .Clients .Log .Infof ("Sending /ok-to-test from untrusted sender on same-repo pull request" )
104+ sendIssueComment (t , "nonowner" )
105+
106+ time .Sleep (10 * time .Second )
107+
108+ pruns , err = g .Cnx .Clients .Tekton .TektonV1 ().PipelineRuns (g .TargetNamespace ).List (ctx , metav1.ListOptions {
109+ LabelSelector : fmt .Sprintf ("%s=%s" , keys .SHA , g .SHA ),
110+ })
111+ assert .NilError (t , err )
112+ assert .Equal (t , initialPipelineRunCount , len (pruns .Items ), "untrusted issue_comment must not create a new PipelineRun" )
113+
114+ repo , err = g .Cnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (g .TargetNamespace ).Get (ctx , g .TargetNamespace , metav1.GetOptions {})
83115 assert .NilError (t , err )
116+ assert .Equal (t , initialStatusCount , len (repo .Status ), "untrusted issue_comment must not add a new Repository status" )
84117
85- g .Cnx .Clients .Log .Infof ("Wait for the second repository update to be updated" )
118+ ctx , err = cctx .GetControllerCtxInfo (ctx , g .Cnx )
119+ assert .NilError (t , err )
120+ numLines := int64 (1000 )
121+ logRegex := regexp .MustCompile (`Skipping same-repo pull request shortcut for untrusted event \*github\.IssueCommentEvent` )
122+ err = twait .RegexpMatchingInControllerLog (ctx , g .Cnx , * logRegex , 10 , "ghe-controller" , & numLines , nil )
123+ assert .NilError (t , err )
124+
125+ g .Cnx .Clients .Log .Infof ("Sending /ok-to-test from trusted sender" )
126+ sendIssueComment (t , g .Options .Organization )
127+
128+ g .Cnx .Clients .Log .Infof ("Wait for the repository update triggered by trusted /ok-to-test" )
86129 waitOpts := twait.Opts {
87130 RepoName : g .TargetNamespace ,
88131 Namespace : g .TargetNamespace ,
89- MinNumberStatus : 1 ,
132+ MinNumberStatus : initialStatusCount + 1 ,
90133 PollTimeout : twait .DefaultTimeout ,
91134 TargetSHA : g .SHA ,
92135 }
93136 _ , err = twait .UntilRepositoryUpdated (ctx , g .Cnx .Clients , waitOpts )
94137 assert .NilError (t , err )
95138
96- g .Cnx .Clients .Log .Infof ("Check if we have the repository set as succeeded" )
97- repo , err : = g .Cnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (g .TargetNamespace ).Get (ctx , g .TargetNamespace , metav1.GetOptions {})
139+ g .Cnx .Clients .Log .Infof ("Check if we have the repository set as succeeded after trusted /ok-to-test " )
140+ repo , err = g .Cnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (g .TargetNamespace ).Get (ctx , g .TargetNamespace , metav1.GetOptions {})
98141 assert .NilError (t , err )
99142 assert .Assert (t , repo .Status [len (repo .Status )- 1 ].Conditions [0 ].Status == corev1 .ConditionTrue )
100143}
0 commit comments