@@ -12,13 +12,15 @@ import (
1212 "github.com/deepsourcelabs/cli/internal/vcs"
1313)
1414
15- // ResolveLatestRun finds the latest analysis run for the current git branch.
15+ // ResolveLatestRun finds the latest successful analysis run for the current git branch.
16+ // It walks recent commits from git history and looks up each via the run(commitOid) API.
1617// Returns the commitOid of the latest run and the branch name.
1718func ResolveLatestRun (
1819 ctx context.Context ,
1920 client * deepsource.Client ,
2021 remote * vcs.RemoteData ,
2122 branchNameFunc func () (string , error ),
23+ commitLogFunc func (branch string ) ([]string , error ),
2224) (commitOid string , branchName string , err error ) {
2325 if branchNameFunc != nil {
2426 branchName , err = branchNameFunc ()
@@ -29,64 +31,57 @@ func ResolveLatestRun(
2931 return "" , "" , fmt .Errorf ("failed to detect current branch: %w" , err )
3032 }
3133
32- const maxPages = 5
33- var after * string
34-
35- for page := 0 ; page < maxPages ; page ++ {
36- runs , pageInfo , err := client .GetAnalysisRuns (ctx , remote .Owner , remote .RepoName , remote .VCSProvider , 30 , after )
37- if err != nil {
38- return "" , "" , fmt .Errorf ("failed to fetch analysis runs: %w" , err )
39- }
40-
41- for _ , run := range runs {
42- if run .BranchName == branchName && run .Status == "SUCCESS" {
43- return run .CommitOid , branchName , nil
44- }
45- }
46-
47- if ! pageInfo .HasNextPage || pageInfo .EndCursor == nil {
48- break
49- }
50- after = pageInfo .EndCursor
34+ run , err := resolveRunFromCommits (ctx , client , branchName , commitLogFunc )
35+ if err != nil {
36+ return "" , branchName , err
5137 }
52-
53- return "" , branchName , fmt .Errorf (
54- "no analysis runs found for branch %q.\n Try: --default-branch, --commit <sha>, or push and analyze this branch first" ,
55- branchName ,
56- )
38+ return run .CommitOid , branchName , nil
5739}
5840
5941// ResolveLatestRunForBranch finds the latest successful analysis run for a given branch name.
42+ // It walks recent commits on the branch and looks up each via the run(commitOid) API.
6043// Returns the full AnalysisRun (which includes the ReportCard).
6144func ResolveLatestRunForBranch (
6245 ctx context.Context ,
6346 client * deepsource.Client ,
6447 remote * vcs.RemoteData ,
6548 branchName string ,
49+ commitLogFunc func (branch string ) ([]string , error ),
6650) (* runs.AnalysisRun , error ) {
67- const maxPages = 5
68- var after * string
51+ return resolveRunFromCommits ( ctx , client , branchName , commitLogFunc )
52+ }
6953
70- for page := 0 ; page < maxPages ; page ++ {
71- analysisRuns , pageInfo , err := client .GetAnalysisRuns (ctx , remote .Owner , remote .RepoName , remote .VCSProvider , 30 , after )
72- if err != nil {
73- return nil , fmt .Errorf ("failed to fetch analysis runs: %w" , err )
74- }
54+ // resolveRunFromCommits walks recent commits on a branch and returns the first
55+ // successful analysis run found via the run(commitOid) API.
56+ func resolveRunFromCommits (
57+ ctx context.Context ,
58+ client * deepsource.Client ,
59+ branchName string ,
60+ commitLogFunc func (branch string ) ([]string , error ),
61+ ) (* runs.AnalysisRun , error ) {
62+ var commits []string
63+ var err error
64+ if commitLogFunc != nil {
65+ commits , err = commitLogFunc (branchName )
66+ } else {
67+ commits , err = getCommitLog (branchName )
68+ }
69+ if err != nil {
70+ return nil , fmt .Errorf ("failed to get commit history for branch %q: %w" , branchName , err )
71+ }
7572
76- for i := range analysisRuns {
77- if analysisRuns [ i ]. BranchName == branchName && analysisRuns [ i ]. Status == "SUCCESS" {
78- return & analysisRuns [ i ], nil
79- }
73+ for _ , sha := range commits {
74+ run , err := client . GetRunByCommit ( ctx , sha )
75+ if err != nil {
76+ continue
8077 }
81-
82- if ! pageInfo .HasNextPage || pageInfo .EndCursor == nil {
83- break
78+ if run != nil && run .Status == "SUCCESS" && run .BranchName == branchName {
79+ return run , nil
8480 }
85- after = pageInfo .EndCursor
8681 }
8782
8883 return nil , fmt .Errorf (
89- "no successful analysis runs found for branch %q" ,
84+ "no analysis runs found for branch %q. \n Try: --default-branch, --commit <sha>, or push and analyze this branch first " ,
9085 branchName ,
9186 )
9287}
@@ -110,6 +105,15 @@ func ResolveCommitOid(commitOid string) string {
110105 return commitOid
111106}
112107
108+ // ResolveBranchName returns the current branch name using the provided function,
109+ // or falls back to git rev-parse if branchNameFunc is nil.
110+ func ResolveBranchName (branchNameFunc func () (string , error )) (string , error ) {
111+ if branchNameFunc != nil {
112+ return branchNameFunc ()
113+ }
114+ return getCurrentBranch ()
115+ }
116+
113117func getCurrentBranch () (string , error ) {
114118 cmd := exec .Command ("git" , "--no-pager" , "rev-parse" , "--abbrev-ref" , "HEAD" )
115119 var stdout , stderr bytes.Buffer
@@ -121,6 +125,22 @@ func getCurrentBranch() (string, error) {
121125 return strings .TrimSuffix (stdout .String (), "\n " ), nil
122126}
123127
128+ // getCommitLog returns recent commit SHAs for a branch using git log.
129+ func getCommitLog (branch string ) ([]string , error ) {
130+ cmd := exec .Command ("git" , "--no-pager" , "log" , branch , "--format=%H" , "-n" , "50" )
131+ var stdout , stderr bytes.Buffer
132+ cmd .Stdout = & stdout
133+ cmd .Stderr = & stderr
134+ if err := cmd .Run (); err != nil {
135+ return nil , fmt .Errorf ("%s: %s" , err , strings .TrimSpace (stderr .String ()))
136+ }
137+ lines := strings .TrimSpace (stdout .String ())
138+ if lines == "" {
139+ return nil , nil
140+ }
141+ return strings .Split (lines , "\n " ), nil
142+ }
143+
124144// GetDefaultBranch returns the default branch name of the origin remote
125145// (e.g. "master" or "main"). Returns an empty string if it cannot be determined.
126146func GetDefaultBranch () string {
0 commit comments