Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Ignore missing tarballs for empty org .github repos #3433

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions clients/githubrepo/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import (
)

const (
repoDir = "repo*"
repoFilename = "githubrepo*.tar.gz"
repoDir = "repo*"
repoFilename = "githubrepo*.tar.gz"
orgGithubRepo = ".github"
)

var (
Expand Down Expand Up @@ -94,6 +95,11 @@ func (handler *tarballHandler) setup() error {

// Setup temp dir/files and download repo tarball.
if err := handler.getTarball(); errors.Is(err, errTarballNotFound) {
// don't warn for "someorg/.github" repos
// https://github.com/ossf/scorecard/issues/3076
if handler.repo.GetName() == orgGithubRepo {
return
}
log.Printf("unable to get tarball %v. Skipping...", err)
return
} else if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions clients/githubrepo/tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@
package githubrepo

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/go-github/v53/github"

"github.com/ossf/scorecard/v4/clients"
)

type listfileTest struct {
Expand Down Expand Up @@ -175,3 +183,59 @@ func TestExtractTarball(t *testing.T) {
})
}
}

// temporarily redirect default logger output somewhere else.
func setLogOutput(t *testing.T, w io.Writer) {
t.Helper()
old := log.Writer()
log.SetOutput(w)
t.Cleanup(func() { log.SetOutput(old) })
}

//nolint:paralleltest // modifying log output
func Test_setup_empty_repo(t *testing.T) {
tests := []struct {
name string
repo string
wantLog bool
}{
{
name: "org .github has no log message",
repo: ".github",
wantLog: false,
},
{
name: "non .github repo has log message",
repo: "foo",
wantLog: true,
},
}
// server always responds with bad request to trigger errTarballNotFound
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}))
t.Cleanup(ts.Close)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
h := tarballHandler{
httpClient: http.DefaultClient,
}
archiveURL := ts.URL + "/{archive_format}"
r := github.Repository{
Name: &tt.repo,
ArchiveURL: &archiveURL,
}
var buf bytes.Buffer
setLogOutput(t, &buf)
h.init(context.Background(), &r, clients.HeadSHA)
err := h.setup()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if (tt.wantLog) != (buf.Len() > 0) {
t.Errorf("wanted log: %t, log: %q", tt.wantLog, buf.String())
}
})
}
}
Loading