@@ -4,13 +4,14 @@ package analyze
44import (
55 "context"
66 "fmt"
7- "github.com/boostsecurityio/poutine/models"
8- "golang.org/x/sync/semaphore"
97 "os"
108 "strings"
119 "sync"
1210 "time"
1311
12+ "github.com/boostsecurityio/poutine/models"
13+ "golang.org/x/sync/semaphore"
14+
1415 "github.com/boostsecurityio/poutine/opa"
1516 "github.com/boostsecurityio/poutine/providers/pkgsupply"
1617 "github.com/boostsecurityio/poutine/scanner"
@@ -129,14 +130,14 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
129130 defer sem .Release (1 )
130131 defer wg .Done ()
131132 repoNameWithOwner := repo .GetRepoIdentifier ()
132- tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken ())
133+ tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken (), "HEAD" )
133134 if err != nil {
134135 log .Error ().Err (err ).Str ("repo" , repoNameWithOwner ).Msg ("failed to clone repo" )
135136 return
136137 }
137138 defer os .RemoveAll (tempDir )
138139
139- pkg , err := a .generatePackageInsights (ctx , tempDir , repo )
140+ pkg , err := a .generatePackageInsights (ctx , tempDir , repo , "HEAD" )
140141 if err != nil {
141142 log .Error ().Err (err ).Str ("repo" , repoNameWithOwner ).Msg ("failed to generate package insights" )
142143 return
@@ -166,7 +167,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
166167 return a .finalizeAnalysis (ctx , inventory )
167168}
168169
169- func (a * Analyzer ) AnalyzeRepo (ctx context.Context , repoString string ) error {
170+ func (a * Analyzer ) AnalyzeRepo (ctx context.Context , repoString string , ref string ) error {
170171 org , repoName , err := a .ScmClient .ParseRepoAndOrg (repoString )
171172 if err != nil {
172173 return fmt .Errorf ("failed to parse repository: %w" , err )
@@ -200,13 +201,13 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string) error {
200201 progressbar .OptionSetWriter (os .Stderr ),
201202 )
202203
203- tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken ())
204+ tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken (), ref )
204205 if err != nil {
205206 return err
206207 }
207208 defer os .RemoveAll (tempDir )
208209
209- pkg , err := a .generatePackageInsights (ctx , tempDir , repo )
210+ pkg , err := a .generatePackageInsights (ctx , tempDir , repo , ref )
210211 if err != nil {
211212 return err
212213 }
@@ -255,7 +256,7 @@ func (a *Analyzer) AnalyzeLocalRepo(ctx context.Context, repoPath string) error
255256 progressbar .OptionSetWriter (os .Stderr ),
256257 )
257258
258- pkg , err := a .generatePackageInsights (ctx , repoPath , repo )
259+ pkg , err := a .generatePackageInsights (ctx , repoPath , repo , "" )
259260 if err != nil {
260261 return err
261262 }
@@ -288,7 +289,7 @@ func (a *Analyzer) finalizeAnalysis(ctx context.Context, inventory *scanner.Inve
288289 return nil
289290}
290291
291- func (a * Analyzer ) generatePackageInsights (ctx context.Context , tempDir string , repo Repository ) (* models.PackageInsights , error ) {
292+ func (a * Analyzer ) generatePackageInsights (ctx context.Context , tempDir string , repo Repository , ref string ) (* models.PackageInsights , error ) {
292293 commitDate , err := a .GitClient .LastCommitDate (ctx , tempDir )
293294 if err != nil {
294295 return nil , fmt .Errorf ("failed to get last commit date: %w" , err )
@@ -299,9 +300,12 @@ func (a *Analyzer) generatePackageInsights(ctx context.Context, tempDir string,
299300 return nil , fmt .Errorf ("failed to get commit SHA: %w" , err )
300301 }
301302
302- headBranchName , err := a .GitClient .GetRepoHeadBranchName (ctx , tempDir )
303- if err != nil {
304- return nil , fmt .Errorf ("failed to get head branch name: %w" , err )
303+ switch ref {
304+ case "HEAD" , "" :
305+ ref , err = a .GitClient .GetRepoHeadBranchName (ctx , tempDir )
306+ if err != nil {
307+ return nil , fmt .Errorf ("failed to get head branch name: %w" , err )
308+ }
305309 }
306310
307311 purl := fmt .Sprintf ("pkg:%s/%s" , repo .GetProviderName (), strings .ToLower (repo .GetRepoIdentifier ()))
@@ -311,7 +315,7 @@ func (a *Analyzer) generatePackageInsights(ctx context.Context, tempDir string,
311315 SourceGitCommitSha : commitSha ,
312316 SourceScmType : repo .GetProviderName (),
313317 SourceGitRepo : repo .GetRepoIdentifier (),
314- SourceGitRef : headBranchName ,
318+ SourceGitRef : ref ,
315319 }
316320 err = pkg .NormalizePurl ()
317321 if err != nil {
@@ -320,13 +324,13 @@ func (a *Analyzer) generatePackageInsights(ctx context.Context, tempDir string,
320324 return pkg , nil
321325}
322326
323- func (a * Analyzer ) cloneRepoToTemp (ctx context.Context , gitURL string , token string ) (string , error ) {
327+ func (a * Analyzer ) cloneRepoToTemp (ctx context.Context , gitURL string , token string , ref string ) (string , error ) {
324328 tempDir , err := os .MkdirTemp ("" , TEMP_DIR_PREFIX )
325329 if err != nil {
326330 return "" , fmt .Errorf ("failed to create temp directory: %w" , err )
327331 }
328332
329- err = a .GitClient .Clone (ctx , tempDir , gitURL , token , "HEAD" )
333+ err = a .GitClient .Clone (ctx , tempDir , gitURL , token , ref )
330334 if err != nil {
331335 os .RemoveAll (tempDir ) // Clean up if cloning fails
332336 return "" , fmt .Errorf ("failed to clone repo: %s" , err )
0 commit comments