Skip to content

Change source reporter interfaces to not return errors #3900

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,7 @@ func handleChunksWithError(
if len(dataOrErr.Data) > 0 {
chunk := *chunkSkel
chunk.Data = dataOrErr.Data
if err := reporter.ChunkOk(ctx, chunk); err != nil {
return fmt.Errorf("error reporting chunk: %w", err)
}
reporter.ChunkOk(ctx, chunk)
}
case <-ctx.Done():
return ctx.Err()
Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,11 @@ func getGitCommitHash(t *testing.T, gitDir string) string {

type mockReporter struct{ reportedChunks int }

func (m *mockReporter) ChunkOk(context.Context, sources.Chunk) error {
func (m *mockReporter) ChunkOk(context.Context, sources.Chunk) {
m.reportedChunks++
return nil
}

func (m *mockReporter) ChunkErr(context.Context, error) error { return nil }
func (m *mockReporter) ChunkErr(context.Context, error) {}

func TestHandleChunksWithError(t *testing.T) {
tests := []struct {
Expand Down
27 changes: 11 additions & 16 deletions pkg/sources/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,18 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e
for _, path := range s.paths {
fileInfo, err := os.Lstat(filepath.Clean(path))
if err != nil {
if err := reporter.UnitErr(ctx, err); err != nil {
return err
}
reporter.UnitErr(ctx, err)
continue
}
if !fileInfo.IsDir() {
item := sources.CommonSourceUnit{ID: path}
if err := reporter.UnitOk(ctx, item); err != nil {
return err
}
reporter.UnitOk(ctx, item)
continue
}
err = fs.WalkDir(os.DirFS(path), ".", func(relativePath string, d fs.DirEntry, err error) error {
if err != nil {
return reporter.UnitErr(ctx, err)
reporter.UnitErr(ctx, err)
return nil
Comment on lines -212 to +209
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the cases where we were returning the reporter error, I changed it to return nil. This would be a behavior change for context cancellations, so maybe we should return ctx.Err() instead. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that returning ctx.Err() instead is a good idea!

}
if d.IsDir() {
return nil
Expand All @@ -219,12 +216,11 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e
return nil
}
item := sources.CommonSourceUnit{ID: fullPath}
return reporter.UnitOk(ctx, item)
reporter.UnitOk(ctx, item)
return nil
})
if err != nil {
if err := reporter.UnitErr(ctx, err); err != nil {
return err
}
reporter.UnitErr(ctx, err)
}
}
return nil
Expand All @@ -238,7 +234,8 @@ func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporte
cleanPath := filepath.Clean(path)
fileInfo, err := os.Lstat(cleanPath)
if err != nil {
return reporter.ChunkErr(ctx, fmt.Errorf("unable to get file info: %w", err))
reporter.ChunkErr(ctx, fmt.Errorf("unable to get file info: %w", err))
return nil
}

ch := make(chan *sources.Chunk)
Expand All @@ -259,16 +256,14 @@ func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporte
if chunk == nil {
continue
}
if err := reporter.ChunkOk(ctx, *chunk); err != nil {
return err
}
reporter.ChunkOk(ctx, *chunk)
}

if scanErr != nil && !errors.Is(scanErr, io.EOF) {
if !errors.Is(scanErr, skipSymlinkErr) {
logger.Error(scanErr, "error scanning filesystem")
}
return reporter.ChunkErr(ctx, scanErr)
reporter.ChunkErr(ctx, scanErr)
}
return nil
}
46 changes: 0 additions & 46 deletions pkg/sources/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,52 +252,6 @@ func TestEnumerateReporterErr(t *testing.T) {
s := Source{}
err = s.Init(ctx, "test enumerate", 0, 0, true, conn, 1)
assert.NoError(t, err)

// Enumerate should always return an error if the reporter returns an
// error.
reporter := sourcestest.ErrReporter{}
err = s.Enumerate(ctx, &reporter)
assert.Error(t, err)
}

func TestChunkUnitReporterErr(t *testing.T) {
t.Parallel()
ctx := context.Background()

// Setup test file to chunk.
tmpfile, err := os.CreateTemp("", "example.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

fileContents := []byte("TestChunkUnit")
_, err = tmpfile.Write(fileContents)
assert.NoError(t, err)
assert.NoError(t, tmpfile.Close())

conn, err := anypb.New(&sourcespb.Filesystem{})
assert.NoError(t, err)

// Initialize the source.
s := Source{}
err = s.Init(ctx, "test chunk unit", 0, 0, true, conn, 1)
assert.NoError(t, err)

// Happy path. ChunkUnit should always return an error if the reporter
// returns an error.
reporter := sourcestest.ErrReporter{}
err = s.ChunkUnit(ctx, sources.CommonSourceUnit{
ID: tmpfile.Name(),
}, &reporter)
assert.Error(t, err)

// Error path. ChunkUnit should always return an error if the reporter
// returns an error.
err = s.ChunkUnit(ctx, sources.CommonSourceUnit{
ID: "/file/not/found",
}, &reporter)
assert.Error(t, err)
}

// createTempFile is a helper function to create a temporary file in the given
Expand Down
40 changes: 14 additions & 26 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (s *Source) scanRepo(ctx context.Context, repoURI string, reporter sources.
return s.git.ScanRepo(ctx, repo, path, s.scanOptions, reporter)
}()
if err != nil {
return reporter.ChunkErr(ctx, err)
reporter.ChunkErr(ctx, err)
}
return nil
}
Expand Down Expand Up @@ -330,7 +330,8 @@ func (s *Source) scanDir(ctx context.Context, gitDir string, reporter sources.Ch
// try paths instead of url
repo, err := RepoFromPath(gitDir, s.scanOptions.Bare)
if err != nil {
return reporter.ChunkErr(ctx, err)
reporter.ChunkErr(ctx, err)
return nil
}

err = func() error {
Expand All @@ -341,7 +342,7 @@ func (s *Source) scanDir(ctx context.Context, gitDir string, reporter sources.Ch
return s.git.ScanRepo(ctx, repo, gitDir, s.scanOptions, reporter)
}()
if err != nil {
return reporter.ChunkErr(ctx, err)
reporter.ChunkErr(ctx, err)
}
return nil
}
Expand Down Expand Up @@ -626,9 +627,7 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
Data: []byte(sb.String()),
Verify: s.verify,
}
if err := reporter.ChunkOk(ctx, chunk); err != nil {
return err
}
reporter.ChunkOk(ctx, chunk)
}

fileName := diff.PathB
Expand Down Expand Up @@ -701,7 +700,8 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
Data: data,
Verify: s.verify,
}
return reporter.ChunkOk(ctx, chunk)
reporter.ChunkOk(ctx, chunk)
return nil
}
if err := chunkData(diff); err != nil {
return err
Expand Down Expand Up @@ -739,10 +739,7 @@ func (s *Git) gitChunk(ctx context.Context, diff *gitparse.Diff, fileName, email
Data: append([]byte{}, newChunkBuffer.Bytes()...),
Verify: s.verify,
}
if err := reporter.ChunkOk(ctx, chunk); err != nil {
// TODO: Return error.
return
}
reporter.ChunkOk(ctx, chunk)

newChunkBuffer.Reset()
lastOffset = offset
Expand All @@ -759,10 +756,7 @@ func (s *Git) gitChunk(ctx context.Context, diff *gitparse.Diff, fileName, email
Data: line,
Verify: s.verify,
}
if err := reporter.ChunkOk(ctx, chunk); err != nil {
// TODO: Return error.
return
}
reporter.ChunkOk(ctx, chunk)
continue
}
}
Expand All @@ -783,10 +777,7 @@ func (s *Git) gitChunk(ctx context.Context, diff *gitparse.Diff, fileName, email
Data: append([]byte{}, newChunkBuffer.Bytes()...),
Verify: s.verify,
}
if err := reporter.ChunkOk(ctx, chunk); err != nil {
// TODO: Return error.
return
}
reporter.ChunkOk(ctx, chunk)
}
}

Expand Down Expand Up @@ -903,7 +894,8 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
Data: data,
Verify: s.verify,
}
return reporter.ChunkOk(ctx, chunk)
reporter.ChunkOk(ctx, chunk)
return nil
}
if err := chunkData(diff); err != nil {
return err
Expand Down Expand Up @@ -1302,18 +1294,14 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e
continue
}
unit := SourceUnit{ID: repo, Kind: UnitDir}
if err := reporter.UnitOk(ctx, unit); err != nil {
return err
}
reporter.UnitOk(ctx, unit)
}
for _, repo := range s.conn.GetRepositories() {
if repo == "" {
continue
}
unit := SourceUnit{ID: repo, Kind: UnitRepo}
if err := reporter.UnitOk(ctx, unit); err != nil {
return err
}
reporter.UnitOk(ctx, unit)
}
return nil
}
Expand Down
Loading
Loading