@@ -7,16 +7,19 @@ 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"
1822 "gotest.tools/v3/assert"
19- corev1 "k8s.io/api/core/v1"
2023 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2124)
2225
@@ -41,60 +44,80 @@ func TestGithubGHEPullRequestOkToTest(t *testing.T) {
4144 Organization : g .Options .Organization ,
4245 Repository : g .Options .Repo ,
4346 URL : repoinfo .GetHTMLURL (),
44- Sender : g .Options .Organization ,
4547 }
4648
49+ repo , err := g .Cnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (g .TargetNamespace ).Get (ctx , g .TargetNamespace , metav1.GetOptions {})
50+ assert .NilError (t , err )
51+ initialStatusCount := len (repo .Status )
52+
53+ pruns , err := g .Cnx .Clients .Tekton .TektonV1 ().PipelineRuns (g .TargetNamespace ).List (ctx , metav1.ListOptions {
54+ LabelSelector : fmt .Sprintf ("%s=%s" , keys .SHA , g .SHA ),
55+ })
56+ assert .NilError (t , err )
57+ initialPipelineRunCount := len (pruns .Items )
58+
4759 installID , err := strconv .ParseInt (os .Getenv ("TEST_GITHUB_SECOND_APPLICATION_ID" ), 10 , 64 )
4860 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 )),
61+
62+ sendIssueComment := func (t * testing.T , sender string ) {
63+ t .Helper ()
64+
65+ event := github.IssueCommentEvent {
66+ Comment : & github.IssueComment {
67+ Body : github .Ptr (`/ok-to-test` ),
68+ },
69+ Installation : & github.Installation {
70+ ID : & installID ,
71+ },
72+ Action : github .Ptr ("created" ),
73+ Issue : & github.Issue {
74+ State : github .Ptr ("open" ),
75+ PullRequestLinks : & github.PullRequestLinks {
76+ HTMLURL : github .Ptr (fmt .Sprintf ("%s/pull/%d" , runevent .URL , g .PRNumber )),
77+ },
6178 },
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- },
79+ Repo : & github.Repository {
80+ DefaultBranch : & runevent .DefaultBranch ,
81+ HTMLURL : & runevent .URL ,
82+ Name : & runevent .Repository ,
83+ Owner : & github.User {Login : & runevent .Organization },
84+ },
85+ Sender : & github.User {
86+ Login : github .Ptr (sender ),
87+ },
88+ }
89+
90+ err = payload .Send (ctx ,
91+ g .Cnx ,
92+ os .Getenv ("TEST_GITHUB_SECOND_EL_URL" ),
93+ os .Getenv ("TEST_GITHUB_SECOND_WEBHOOK_SECRET" ),
94+ os .Getenv ("TEST_GITHUB_SECOND_API_URL" ),
95+ os .Getenv ("TEST_GITHUB_SECOND_APPLICATION_ID" ),
96+ event ,
97+ "issue_comment" ,
98+ )
99+ assert .NilError (t , err )
72100 }
73101
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- )
102+ g .Cnx .Clients .Log .Infof ("Sending /ok-to-test from untrusted sender on same-repo pull request" )
103+ sendIssueComment (t , "nonowner" )
104+
105+ time .Sleep (10 * time .Second )
106+
107+ pruns , err = g .Cnx .Clients .Tekton .TektonV1 ().PipelineRuns (g .TargetNamespace ).List (ctx , metav1.ListOptions {
108+ LabelSelector : fmt .Sprintf ("%s=%s" , keys .SHA , g .SHA ),
109+ })
83110 assert .NilError (t , err )
111+ assert .Equal (t , initialPipelineRunCount , len (pruns .Items ), "untrusted issue_comment must not create a new PipelineRun" )
84112
85- g .Cnx .Clients .Log .Infof ("Wait for the second repository update to be updated" )
86- waitOpts := twait.Opts {
87- RepoName : g .TargetNamespace ,
88- Namespace : g .TargetNamespace ,
89- MinNumberStatus : 1 ,
90- PollTimeout : twait .DefaultTimeout ,
91- TargetSHA : g .SHA ,
92- }
93- _ , err = twait .UntilRepositoryUpdated (ctx , g .Cnx .Clients , waitOpts )
113+ repo , err = g .Cnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (g .TargetNamespace ).Get (ctx , g .TargetNamespace , metav1.GetOptions {})
94114 assert .NilError (t , err )
115+ assert .Equal (t , initialStatusCount , len (repo .Status ), "untrusted issue_comment must not add a new Repository status" )
95116
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 {})
117+ ctx , err = cctx .GetControllerCtxInfo (ctx , g .Cnx )
118+ assert .NilError (t , err )
119+ numLines := int64 (1000 )
120+ logRegex := regexp .MustCompile (`Skipping same-repo pull request shortcut for untrusted event \*github\.IssueCommentEvent` )
121+ err = twait .RegexpMatchingInControllerLog (ctx , g .Cnx , * logRegex , 10 , "ghe-controller" , & numLines , nil )
98122 assert .NilError (t , err )
99- assert .Assert (t , repo .Status [len (repo .Status )- 1 ].Conditions [0 ].Status == corev1 .ConditionTrue )
100123}
0 commit comments