Skip to content

Commit c1761a8

Browse files
Chris McGeheenaveensrinivasan
authored andcommitted
Only download repo tarball when necessary
Previously, this was downloading the tarball for github.com/google/oss-fuzz every time scorecard was run
1 parent 0268747 commit c1761a8

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

clients/githubrepo/client.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string) error {
7171
}
7272

7373
// Init tarballHandler.
74-
if err := client.tarball.init(client.ctx, client.repo, commitSHA); err != nil {
75-
return fmt.Errorf("error during tarballHandler.init: %w", err)
76-
}
74+
client.tarball.init(client.ctx, client.repo, commitSHA)
7775

7876
// Setup GraphQL.
7977
client.graphClient.init(client.ctx, client.repourl)

clients/githubrepo/tarball.go

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6667
type 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

212228
func (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

226245
func (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)

clients/githubrepo/tarball_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"os"
2222
"strings"
23+
"sync"
2324
"testing"
2425

2526
"github.com/google/go-cmp/cmp"
@@ -58,10 +59,16 @@ func setup(inputFile string) (tarballHandler, error) {
5859
if _, err := io.Copy(tempFile, testFile); err != nil {
5960
return tarballHandler{}, fmt.Errorf("unable to do io.Copy: %w", err)
6061
}
61-
return tarballHandler{
62+
tarballHandler := tarballHandler{
6263
tempDir: tempDir,
6364
tempTarFile: tempFile.Name(),
64-
}, nil
65+
once: new(sync.Once),
66+
}
67+
tarballHandler.once.Do(func() {
68+
// We don't want to run the code in tarballHandler.setup(), so if we execute tarballHandler.once.Do() right
69+
// here, it won't get executed later when setup() is called.
70+
})
71+
return tarballHandler, nil
6572
}
6673

6774
// nolint: gocognit

0 commit comments

Comments
 (0)