@@ -26,6 +26,7 @@ import (
2626 "os"
2727 "path/filepath"
2828 "strings"
29+ "sync"
2930
3031 "github.com/google/go-github/v38/github"
3132
@@ -64,45 +65,60 @@ func extractAndValidateArchivePath(path, dest string) (string, error) {
6465}
6566
6667type tarballHandler struct {
68+ errSetup error
69+ once * sync.Once
70+ ctx context.Context
71+ repo * github.Repository
72+ commitSHA string
6773 tempDir string
6874 tempTarFile string
6975 files []string
7076}
7177
72- func (handler * tarballHandler ) init (ctx context.Context , repo * github.Repository , commitSHA string ) error {
73- // Cleanup any previous state.
74- if err := handler .cleanup (); err != nil {
75- return sce .WithMessage (sce .ErrScorecardInternal , err .Error ())
76- }
78+ func (handler * tarballHandler ) init (ctx context.Context , repo * github.Repository , commitSHA string ) {
79+ handler .errSetup = nil
80+ handler .once = new (sync.Once )
81+ handler .ctx = ctx
82+ handler .repo = repo
83+ handler .commitSHA = commitSHA
84+ }
7785
78- // Setup temp dir/files and download repo tarball.
79- if err := handler .getTarball ( ctx , repo , commitSHA ); errors . Is ( err , errTarballNotFound ) {
80- log . Printf ( "unable to get tarball %v. Skipping..." , err )
81- return nil
82- } else if err != nil {
83- return sce . WithMessage ( sce . ErrScorecardInternal , err . Error ())
84- }
86+ func ( handler * tarballHandler ) setup () error {
87+ handler .once . Do ( func ( ) {
88+ // Cleanup any previous state.
89+ if err := handler . cleanup (); err != nil {
90+ handler . errSetup = sce . WithMessage ( sce . ErrScorecardInternal , err . Error ())
91+ return
92+ }
8593
86- // Extract file names and content from tarball.
87- if err := handler .extractTarball (); errors .Is (err , errTarballCorrupted ) {
88- log .Printf ("unable to extract tarball %v. Skipping..." , err )
89- return nil
90- } else if err != nil {
91- return sce .WithMessage (sce .ErrScorecardInternal , err .Error ())
92- }
94+ // Setup temp dir/files and download repo tarball.
95+ if err := handler .getTarball (); errors .Is (err , errTarballNotFound ) {
96+ log .Printf ("unable to get tarball %v. Skipping..." , err )
97+ return
98+ } else if err != nil {
99+ handler .errSetup = sce .WithMessage (sce .ErrScorecardInternal , err .Error ())
100+ return
101+ }
93102
94- return nil
103+ // Extract file names and content from tarball.
104+ if err := handler .extractTarball (); errors .Is (err , errTarballCorrupted ) {
105+ log .Printf ("unable to extract tarball %v. Skipping..." , err )
106+ } else if err != nil {
107+ handler .errSetup = sce .WithMessage (sce .ErrScorecardInternal , err .Error ())
108+ }
109+ })
110+ return handler .errSetup
95111}
96112
97- func (handler * tarballHandler ) getTarball (ctx context. Context , repo * github. Repository , commitSHA string ) error {
98- url := repo .GetArchiveURL ()
113+ func (handler * tarballHandler ) getTarball () error {
114+ url := handler . repo .GetArchiveURL ()
99115 url = strings .Replace (url , "{archive_format}" , "tarball/" , 1 )
100- if strings .EqualFold (commitSHA , clients .HeadSHA ) {
116+ if strings .EqualFold (handler . commitSHA , clients .HeadSHA ) {
101117 url = strings .Replace (url , "{/ref}" , "" , 1 )
102118 } else {
103- url = strings .Replace (url , "{/ref}" , commitSHA , 1 )
119+ url = strings .Replace (url , "{/ref}" , handler . commitSHA , 1 )
104120 }
105- req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
121+ req , err := http .NewRequestWithContext (handler . ctx , http .MethodGet , url , nil )
106122 if err != nil {
107123 return fmt .Errorf ("http.NewRequestWithContext: %w" , err )
108124 }
@@ -210,6 +226,9 @@ func (handler *tarballHandler) extractTarball() error {
210226}
211227
212228func (handler * tarballHandler ) listFiles (predicate func (string ) (bool , error )) ([]string , error ) {
229+ if err := handler .setup (); err != nil {
230+ return nil , fmt .Errorf ("error during tarballHandler.setup: %w" , err )
231+ }
213232 ret := make ([]string , 0 )
214233 for _ , file := range handler .files {
215234 matches , err := predicate (file )
@@ -224,6 +243,9 @@ func (handler *tarballHandler) listFiles(predicate func(string) (bool, error)) (
224243}
225244
226245func (handler * tarballHandler ) getFileContent (filename string ) ([]byte , error ) {
246+ if err := handler .setup (); err != nil {
247+ return nil , fmt .Errorf ("error during tarballHandler.setup: %w" , err )
248+ }
227249 content , err := os .ReadFile (filepath .Join (handler .tempDir , filename ))
228250 if err != nil {
229251 return content , fmt .Errorf ("os.ReadFile: %w" , err )
0 commit comments