@@ -16,6 +16,7 @@ import (
1616 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1717 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
1818 "github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
19+ testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
1920 ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
2021 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2122 "gotest.tools/v3/assert"
@@ -90,6 +91,45 @@ func TestGetExistingCheckRunIDFromMultiple(t *testing.T) {
9091 assert .Equal (t , * id , chosenID )
9192}
9293
94+ func TestGetExistingSkippedCheckRunID (t * testing.T ) {
95+ ctx , _ := rtesting .SetupFakeContext (t )
96+ client , mux , _ , teardown := ghtesthelper .SetupGH ()
97+ defer teardown ()
98+
99+ cnx := New ()
100+ cnx .Client = client
101+
102+ event := & info.Event {
103+ Organization : "owner" ,
104+ Repository : "repository" ,
105+ SHA : "sha" ,
106+ }
107+
108+ chosenOne := "chosenOne"
109+ chosenID := int64 (55555 )
110+ mux .HandleFunc (fmt .Sprintf ("/repos/%v/%v/commits/%v/check-runs" , event .Organization , event .Repository , event .SHA ), func (w http.ResponseWriter , r * http.Request ) {
111+ _ , _ = fmt .Fprintf (w , `{
112+ "total_count": 1,
113+ "check_runs": [
114+ {
115+ "id": %v,
116+ "external_id": "%s",
117+ "output": {
118+ "title": "Skipped",
119+ "summary": "My CI is skipping this commit"
120+ }
121+ }
122+ ]
123+ }` , chosenID , chosenOne )
124+ })
125+
126+ id , err := cnx .getExistingCheckRunID (ctx , event , provider.StatusOpts {
127+ PipelineRunName : chosenOne ,
128+ })
129+ assert .NilError (t , err )
130+ assert .Equal (t , * id , chosenID )
131+ }
132+
93133func TestGithubProviderCreateStatus (t * testing.T ) {
94134 checkrunid := int64 (2026 )
95135 resultid := int64 (666 )
@@ -117,11 +157,13 @@ func TestGithubProviderCreateStatus(t *testing.T) {
117157 githubApps bool
118158 }
119159 tests := []struct {
120- name string
121- args args
122- want * github.CheckRun
123- wantErr bool
124- notoken bool
160+ name string
161+ args args
162+ pr * v1beta1.PipelineRun
163+ want * github.CheckRun
164+ wantErr bool
165+ notoken bool
166+ addExistingCheckruns bool
125167 }{
126168 {
127169 name : "success" ,
@@ -137,6 +179,26 @@ func TestGithubProviderCreateStatus(t *testing.T) {
137179 want : & github.CheckRun {ID : & resultid },
138180 wantErr : false ,
139181 },
182+ {
183+ name : "success with using existing skipped run checkrun" ,
184+ args : args {
185+ runevent : runEvent ,
186+ status : "completed" ,
187+ conclusion : "success" ,
188+ text : "Yay" ,
189+ detailsURL : "https://cireport.com" ,
190+ titleSubstr : "Success" ,
191+ githubApps : true ,
192+ },
193+ pr : & v1beta1.PipelineRun {
194+ ObjectMeta : metav1.ObjectMeta {
195+ Name : prname ,
196+ },
197+ },
198+ addExistingCheckruns : true ,
199+ want : & github.CheckRun {ID : & resultid },
200+ wantErr : false ,
201+ },
140202 {
141203 name : "success coming from webhook" ,
142204 args : args {
@@ -219,9 +281,10 @@ func TestGithubProviderCreateStatus(t *testing.T) {
219281 defer teardown ()
220282
221283 ctx , _ := rtesting .SetupFakeContext (t )
222- gcvs := Provider {
223- Client : fakeclient ,
224- }
284+ gcvs := New ()
285+ gcvs .Client = fakeclient
286+ gcvs .Logger = getLogger ()
287+
225288 mux .HandleFunc ("/repos/check/run/statuses/sha" , func (rw http.ResponseWriter , r * http.Request ) {})
226289 mux .HandleFunc (fmt .Sprintf ("/repos/check/run/check-runs/%d" , checkrunid ), func (rw http.ResponseWriter , r * http.Request ) {
227290 bit , _ := io .ReadAll (r .Body )
@@ -242,6 +305,24 @@ func TestGithubProviderCreateStatus(t *testing.T) {
242305 _ , err = fmt .Fprintf (rw , `{"id": %d}` , resultid )
243306 assert .NilError (t , err )
244307 })
308+ if tt .addExistingCheckruns {
309+ tt .args .runevent .SHA = "sha"
310+ mux .HandleFunc (fmt .Sprintf ("/repos/%v/%v/commits/%v/check-runs" , tt .args .runevent .Organization , tt .args .runevent .Repository , tt .args .runevent .SHA ), func (w http.ResponseWriter , r * http.Request ) {
311+ _ , _ = fmt .Fprintf (w , `{
312+ "total_count": 1,
313+ "check_runs": [
314+ {
315+ "id": %v,
316+ "external_id": "%v",
317+ "output": {
318+ "title": "Skipped",
319+ "summary": "My CI is skipping this commit"
320+ }
321+ }
322+ ]
323+ }` , checkrunid , resultid )
324+ })
325+ }
245326
246327 status := provider.StatusOpts {
247328 PipelineRunName : prname ,
@@ -251,6 +332,9 @@ func TestGithubProviderCreateStatus(t *testing.T) {
251332 Text : tt .args .text ,
252333 DetailsURL : tt .args .detailsURL ,
253334 }
335+ if tt .pr != nil {
336+ status .PipelineRun = tt .pr
337+ }
254338 pacopts := & info.PacOpts {
255339 LogURL : "https://log" ,
256340 Settings : & settings.Settings {},
@@ -268,7 +352,15 @@ func TestGithubProviderCreateStatus(t *testing.T) {
268352 tt .args .runevent .SHA = "sha"
269353 }
270354 }
271- err := gcvs .CreateStatus (ctx , nil , tt .args .runevent , pacopts , status )
355+
356+ testData := testclient.Data {}
357+ if tt .pr != nil {
358+ testData = testclient.Data {
359+ PipelineRuns : []* v1beta1.PipelineRun {tt .pr },
360+ }
361+ }
362+ stdata , _ := testclient .SeedTestData (t , ctx , testData )
363+ err := gcvs .CreateStatus (ctx , stdata .Pipeline , tt .args .runevent , pacopts , status )
272364 if (err != nil ) != tt .wantErr {
273365 t .Errorf ("GithubProvider.CreateStatus() error = %v, wantErr %v" , err , tt .wantErr )
274366 return
0 commit comments