@@ -420,6 +420,104 @@ func TestIssuesMultipleFilters(t *testing.T) {
420420 }
421421}
422422
423+ // TestIssuesPRFallbackUsesRunIssues verifies that when auto-branch resolution
424+ // detects a PR AND the current run is in-progress (fallback), the code fetches
425+ // issues via GetRunIssuesFlat (commit-scoped) instead of GetPRIssues.
426+ // Regression test for ticket #6884175.
427+ func TestIssuesPRFallbackUsesRunIssues (t * testing.T ) {
428+ cfgMgr := testutil .CreateTestConfigManager (t , "test-token" , "deepsource.com" , "test@example.com" )
429+
430+ // Load golden files for the multi-step mock.
431+ prFoundData , err := os .ReadFile (goldenPath ("get_pr_by_branch_found_response.json" ))
432+ if err != nil {
433+ t .Fatalf ("failed to read PR golden file: %v" , err )
434+ }
435+ runsFirstData , err := os .ReadFile (goldenPath ("get_analysis_runs_pr_fallback_first_response.json" ))
436+ if err != nil {
437+ t .Fatalf ("failed to read first runs golden file: %v" , err )
438+ }
439+ runsCompletedData , err := os .ReadFile (goldenPath ("get_analysis_runs_pr_fallback_completed_response.json" ))
440+ if err != nil {
441+ t .Fatalf ("failed to read completed runs golden file: %v" , err )
442+ }
443+ commitScopeData , err := os .ReadFile (goldenPath ("commit_scope_response.json" ))
444+ if err != nil {
445+ t .Fatalf ("failed to read commit scope golden file: %v" , err )
446+ }
447+
448+ mock := graphqlclient .NewMockClient ()
449+ analysisRunsCalls := 0
450+ mock .QueryFunc = func (_ context.Context , query string , _ map [string ]any , result any ) error {
451+ switch {
452+ case strings .Contains (query , "pullRequests(" ):
453+ return json .Unmarshal (prFoundData , result )
454+ case strings .Contains (query , "query GetAnalysisRuns(" ):
455+ analysisRunsCalls ++
456+ if analysisRunsCalls == 1 {
457+ // First call (ResolveLatestRunForBranch, limit=1): RUNNING run
458+ return json .Unmarshal (runsFirstData , result )
459+ }
460+ // Second call (ResolveLatestCompletedRun, limit=10): RUNNING + SUCCESS
461+ return json .Unmarshal (runsCompletedData , result )
462+ case strings .Contains (query , "checks {" ):
463+ // GetRunIssuesFlat for the fallback commit
464+ return json .Unmarshal (commitScopeData , result )
465+ default :
466+ t .Fatalf ("unexpected query: %s" , query )
467+ return nil
468+ }
469+ }
470+ client := deepsource .NewWithGraphQLClient (mock )
471+
472+ var buf bytes.Buffer
473+ deps := & cmddeps.Deps {
474+ Client : client ,
475+ ConfigMgr : cfgMgr ,
476+ Stdout : & buf ,
477+ BranchNameFunc : func () (string , error ) {
478+ return "feature/new-auth" , nil
479+ },
480+ HasUnpushedCommitsFunc : func () bool { return false },
481+ HasUncommittedChangesFunc : func () bool { return false },
482+ }
483+
484+ cmd := issuesCmd .NewCmdIssuesWithDeps (deps )
485+ cmd .SetArgs ([]string {"--repo" , "gh/testowner/testrepo" , "--output" , "json" })
486+
487+ if err := cmd .Execute (); err != nil {
488+ t .Fatalf ("unexpected error: %v" , err )
489+ }
490+
491+ got := buf .String ()
492+
493+ // Verify the fallback info message is present.
494+ if ! strings .Contains (got , "Analysis is running on commit" ) {
495+ t .Errorf ("expected fallback info message, got: %q" , got )
496+ }
497+
498+ // Extract the JSON array from the output (after the info message line).
499+ // The JSON starts at the first '[' character.
500+ jsonStart := strings .Index (got , "[" )
501+ if jsonStart < 0 {
502+ t .Fatalf ("no JSON array found in output: %s" , got )
503+ }
504+
505+ var issues []map [string ]any
506+ if err := json .Unmarshal ([]byte (got [jsonStart :]), & issues ); err != nil {
507+ t .Fatalf ("failed to parse JSON output: %v\n raw output: %s" , err , got )
508+ }
509+
510+ // Must NOT be empty — this was the bug.
511+ if len (issues ) == 0 {
512+ t .Fatal ("expected non-empty issues from fallback commit, got empty array" )
513+ }
514+
515+ // Verify we got the expected issues from commit_scope_response.json.
516+ if issues [0 ]["issue_code" ] != "GO-W1007" {
517+ t .Errorf ("expected first issue GO-W1007, got %v" , issues [0 ]["issue_code" ])
518+ }
519+ }
520+
423521func TestIssuesRunInProgress (t * testing.T ) {
424522 cfgMgr := testutil .CreateTestConfigManager (t , "test-token" , "deepsource.com" , "test@example.com" )
425523 mock := testutil .MockQueryFunc (t , map [string ]string {
0 commit comments