@@ -252,10 +252,8 @@ func TestGetTektonDir(t *testing.T) {
252252 expectedString : "PipelineRun" ,
253253 treepath : "testdata/tree/simple" ,
254254 filterMessageSnippet : "Using PipelineRun definition from source pull_request tekton/cat#0" ,
255- // 1. Get Repo root objects
256- // 2. Get Tekton Dir objects
257- // 3. GraphQL batch fetch for 2 files (replaces 2 REST calls)
258- expectedGHApiCalls : 3 ,
255+ // 1. Single GraphQL call fetches tekton tree + inline blobs
256+ expectedGHApiCalls : 1 ,
259257 },
260258 {
261259 name : "test no subtree on push" ,
@@ -268,10 +266,8 @@ func TestGetTektonDir(t *testing.T) {
268266 expectedString : "PipelineRun" ,
269267 treepath : "testdata/tree/simple" ,
270268 filterMessageSnippet : "Using PipelineRun definition from source push" ,
271- // 1. Get Repo root objects
272- // 2. Get Tekton Dir objects
273- // 3. GraphQL batch fetch for 2 files (replaces 2 REST calls)
274- expectedGHApiCalls : 3 ,
269+ // 1. Single GraphQL call fetches tekton tree + inline blobs
270+ expectedGHApiCalls : 1 ,
275271 },
276272 {
277273 name : "test provenance default_branch " ,
@@ -285,10 +281,8 @@ func TestGetTektonDir(t *testing.T) {
285281 provenance : "default_branch" ,
286282 filterMessageSnippet : "Using PipelineRun definition from default_branch: main" ,
287283 // 1. Resolve default branch to a commit SHA
288- // 2. Get Repo root objects
289- // 3. Get Tekton Dir objects
290- // 4. GraphQL batch fetch for 2 files
291- expectedGHApiCalls : 4 ,
284+ // 2. Single GraphQL call fetches tekton tree + inline blobs
285+ expectedGHApiCalls : 2 ,
292286 },
293287 {
294288 name : "test with subtree" ,
@@ -299,10 +293,8 @@ func TestGetTektonDir(t *testing.T) {
299293 },
300294 expectedString : "FROMSUBTREE" ,
301295 treepath : "testdata/tree/subdir" ,
302- // 1. Get Repo root objects
303- // 2. Get Tekton Dir objects
304- // 3-5. Get object content for each object (foo/bar/pipeline.yaml)
305- expectedGHApiCalls : 3 ,
296+ // 1. Single GraphQL call fetches tekton tree + inline blobs (including subdirs)
297+ expectedGHApiCalls : 1 ,
306298 },
307299 {
308300 name : "test with badly formatted yaml" ,
@@ -314,10 +306,9 @@ func TestGetTektonDir(t *testing.T) {
314306 expectedString : "FROMSUBTREE" ,
315307 treepath : "testdata/tree/badyaml" ,
316308 wantErr : "error unmarshalling yaml file badyaml.yaml: yaml: line 2: did not find expected key" ,
317- // 1. Get Repo root objects
318- // 2. Get Tekton Dir objects
319- // 3. Get object content for object (badyaml.yaml)
320- expectedGHApiCalls : 3 ,
309+ // 1. Single GraphQL call fetches tekton tree + inline blobs
310+ // Error occurs during YAML validation after fetch
311+ expectedGHApiCalls : 1 ,
321312 },
322313 {
323314 name : "test no tekton directory" ,
@@ -432,15 +423,48 @@ func TestGetTektonDirGraphQL(t *testing.T) {
432423 skipSetupGitTree bool
433424 }{
434425 {
435- name : "graphql batch fetch reduces api calls " ,
426+ name : "graphql fetch tekton dir with inline blobs " ,
436427 event : & info.Event {
437428 Organization : "tekton" ,
438429 Repository : "cat" ,
439430 TriggerTarget : triggertype .PullRequest ,
440431 },
441- treepath : "testdata/tree/simple" ,
442- wantLogSnippet : "GraphQL batch fetch" ,
443- expectedAPICount : 3 ,
432+ skipSetupGitTree : true ,
433+ setup : func (t * testing.T , mux * http.ServeMux , event * info.Event ) {
434+ t .Helper ()
435+ shaDir := fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte ("testdata/tree/simple" )))
436+ event .SHA = shaDir
437+
438+ // Setup GraphQL endpoint with inline blob contents
439+ mux .HandleFunc ("/api/graphql" , func (w http.ResponseWriter , _ * http.Request ) {
440+ _ = json .NewEncoder (w ).Encode (map [string ]any {
441+ "data" : map [string ]any {
442+ "repository" : map [string ]any {
443+ "tektonTree" : map [string ]any {
444+ "entries" : []map [string ]any {
445+ {
446+ "name" : "pipeline.yaml" ,
447+ "type" : "blob" ,
448+ "path" : "pipeline.yaml" ,
449+ "oid" : "pipeline-sha" ,
450+ "object" : map [string ]any {"text" : "kind: Pipeline\n metadata:\n name: test\n " },
451+ },
452+ {
453+ "name" : "task.yaml" ,
454+ "type" : "blob" ,
455+ "path" : "task.yaml" ,
456+ "oid" : "task-sha" ,
457+ "object" : map [string ]any {"text" : "kind: Task\n metadata:\n name: test\n " },
458+ },
459+ },
460+ },
461+ },
462+ },
463+ })
464+ })
465+ },
466+ wantLogSnippet : "GitHub API call completed" ,
467+ expectedAPICount : 1 , // Single GraphQL call fetches tree + blobs
444468 },
445469 {
446470 name : "graphql error handling" ,
@@ -500,7 +524,7 @@ func TestGetTektonDirGraphQL(t *testing.T) {
500524 http .Error (w , "GraphQL endpoint not available" , http .StatusNotFound )
501525 })
502526 },
503- wantErr : "failed to fetch .tekton files via GraphQL " ,
527+ wantErr : "GraphQL request failed with status 404 " ,
504528 },
505529 {
506530 name : "default branch uses resolved sha for graphql" ,
@@ -514,7 +538,6 @@ func TestGetTektonDirGraphQL(t *testing.T) {
514538 setup : func (t * testing.T , mux * http.ServeMux , _ * info.Event ) {
515539 t .Helper ()
516540 resolvedSHA := "resolved-default-branch-sha"
517- tektonDirSHA := "tektondirsha"
518541
519542 mux .HandleFunc ("/repos/tekton/cat/branches/main" , func (rw http.ResponseWriter , _ * http.Request ) {
520543 branch := & github.Branch {
@@ -526,58 +549,80 @@ func TestGetTektonDirGraphQL(t *testing.T) {
526549 b , _ := json .Marshal (branch )
527550 fmt .Fprint (rw , string (b ))
528551 })
529- mux .HandleFunc ("/repos/tekton/cat/git/trees/" + resolvedSHA , func (rw http.ResponseWriter , _ * http.Request ) {
530- tree := & github.Tree {
531- SHA : github .Ptr (resolvedSHA ),
532- Entries : []* github.TreeEntry {
533- {
534- Path : github .Ptr (".tekton" ),
535- Type : github .Ptr ("tree" ),
536- SHA : github .Ptr (tektonDirSHA ),
537- },
538- },
539- }
540- b , _ := json .Marshal (tree )
541- fmt .Fprint (rw , string (b ))
542- })
543- mux .HandleFunc ("/repos/tekton/cat/git/trees/" + tektonDirSHA , func (rw http.ResponseWriter , _ * http.Request ) {
544- tree := & github.Tree {
545- SHA : github .Ptr (tektonDirSHA ),
546- Entries : []* github.TreeEntry {
547- {
548- Path : github .Ptr ("pipeline.yaml" ),
549- Type : github .Ptr ("blob" ),
550- SHA : github .Ptr ("pipeline-sha" ),
551- },
552- {
553- Path : github .Ptr ("pipelinerun.yaml" ),
554- Type : github .Ptr ("blob" ),
555- SHA : github .Ptr ("pipelinerun-sha" ),
556- },
557- },
558- }
559- b , _ := json .Marshal (tree )
560- fmt .Fprint (rw , string (b ))
561- })
562552 mux .HandleFunc ("/api/graphql" , func (w http.ResponseWriter , r * http.Request ) {
563553 var graphQLReq struct {
564- Query string `json:"query"`
554+ Query string `json:"query"`
555+ Variables map [string ]any `json:"variables"`
565556 }
566557 assert .NilError (t , json .NewDecoder (r .Body ).Decode (& graphQLReq ))
567- assert .Assert (t , strings .Contains (graphQLReq .Query , resolvedSHA + ":.tekton/pipeline.yaml" ), graphQLReq .Query )
568- assert .Assert (t , ! strings .Contains (graphQLReq .Query , `main:.tekton/pipeline.yaml` ), graphQLReq .Query )
558+ // New implementation uses tektonExpr variable: "resolvedSHA:.tekton"
559+ assert .Assert (t , strings .Contains (graphQLReq .Query , "tektonTree:" ), graphQLReq .Query )
560+ assert .Assert (t , graphQLReq .Variables ["tektonExpr" ] == resolvedSHA + ":.tekton" , "expected tektonExpr=%s:.tekton, got %v" , resolvedSHA , graphQLReq .Variables ["tektonExpr" ])
569561
562+ // Return tree structure with inline blob contents
570563 _ = json .NewEncoder (w ).Encode (map [string ]any {
571564 "data" : map [string ]any {
572565 "repository" : map [string ]any {
573- "file0" : map [string ]any {"text" : "kind: Pipeline\n metadata:\n name: pipeline\n " },
574- "file1" : map [string ]any {"text" : "kind: PipelineRun\n metadata:\n name: run\n " },
566+ "tektonTree" : map [string ]any {
567+ "entries" : []map [string ]any {
568+ {
569+ "name" : "pipeline.yaml" ,
570+ "type" : "blob" ,
571+ "path" : "pipeline.yaml" ,
572+ "oid" : "pipeline-sha" ,
573+ "object" : map [string ]any {"text" : "kind: Pipeline\n metadata:\n name: pipeline\n " },
574+ },
575+ {
576+ "name" : "pipelinerun.yaml" ,
577+ "type" : "blob" ,
578+ "path" : "pipelinerun.yaml" ,
579+ "oid" : "pipelinerun-sha" ,
580+ "object" : map [string ]any {"text" : "kind: PipelineRun\n metadata:\n name: run\n " },
581+ },
582+ },
583+ },
584+ },
585+ },
586+ })
587+ })
588+ },
589+ expectedAPICount : 2 , // 1 for get_default_branch + 1 for GraphQL
590+ },
591+ {
592+ name : "invalid yaml validation failure" ,
593+ event : & info.Event {
594+ Organization : "tekton" ,
595+ Repository : "cat" ,
596+ TriggerTarget : triggertype .PullRequest ,
597+ },
598+ skipSetupGitTree : true ,
599+ setup : func (t * testing.T , mux * http.ServeMux , event * info.Event ) {
600+ t .Helper ()
601+ shaDir := fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte ("testdata/tree/badyaml" )))
602+ event .SHA = shaDir
603+
604+ mux .HandleFunc ("/api/graphql" , func (w http.ResponseWriter , _ * http.Request ) {
605+ _ = json .NewEncoder (w ).Encode (map [string ]any {
606+ "data" : map [string ]any {
607+ "repository" : map [string ]any {
608+ "tektonTree" : map [string ]any {
609+ "entries" : []map [string ]any {
610+ {
611+ "name" : "badyaml.yaml" ,
612+ "type" : "blob" ,
613+ "path" : "badyaml.yaml" ,
614+ "oid" : "badyaml-sha" ,
615+ "object" : map [string ]any {"text" : "bad:\n yaml:\n - indent\n error" },
616+ },
617+ },
618+ },
575619 },
576620 },
577621 })
578622 })
579623 },
580- expectedAPICount : 4 ,
624+ wantErr : "error unmarshalling yaml file badyaml.yaml" ,
625+ expectedAPICount : 1 , // Single GraphQL call
581626 },
582627 }
583628
0 commit comments