Skip to content

Commit

Permalink
move existing path into fn
Browse files Browse the repository at this point in the history
  • Loading branch information
rosecodym committed Aug 29, 2024
1 parent ea6f5b1 commit 587dd47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
25 changes: 7 additions & 18 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,24 +1398,13 @@ func (s *Source) scanTarget(ctx context.Context, target sources.ChunkingTarget,
sha: meta.GetCommit(),
filename: meta.GetFile(),
}
res, err := s.getDiffForFileInCommit(ctx, qry)
if err != nil {
return err
}
chunk := &sources.Chunk{
SourceType: s.Type(),
SourceName: s.name,
SourceID: s.SourceID(),
JobID: s.JobID(),
SecretID: target.SecretID,
Data: []byte(stripLeadingPlusMinus(res)),
SourceMetadata: &source_metadatapb.MetaData{
Data: &source_metadatapb.MetaData_Github{Github: meta},
},
Verify: s.verify,
}

return common.CancellableWrite(ctx, chunksChan, chunk)
_, err = s.getDiffForFileInCommit(
ctx,
qry,
sources.ChanReporter{Ch: chunksChan},
target.SecretID,
&source_metadatapb.MetaData_Github{Github: meta})
return err
}

// stripLeadingPlusMinus removes leading + and - characters from lines in a diff string. These characters exist in the
Expand Down
22 changes: 20 additions & 2 deletions pkg/sources/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

gogit "github.com/go-git/go-git/v5"
"github.com/google/go-github/v63/github"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/giturl"
Expand Down Expand Up @@ -291,7 +292,13 @@ type commitQuery struct {

// getDiffForFileInCommit retrieves the diff for a specified file in a commit.
// If the file or its diff is not found, it returns an error.
func (s *Source) getDiffForFileInCommit(ctx context.Context, query commitQuery) (string, error) {
func (s *Source) getDiffForFileInCommit(
ctx context.Context,
query commitQuery,
reporter sources.ChunkReporter,
secretID int64,
meta *source_metadatapb.MetaData_Github,
) (string, error) {
var (
commit *github.RepositoryCommit
err error
Expand Down Expand Up @@ -332,7 +339,18 @@ func (s *Source) getDiffForFileInCommit(ctx context.Context, query commitQuery)
return "", fmt.Errorf("commit %s does not contain patch for file %s", query.sha, query.filename)
}

return res.String(), nil
return "", reporter.ChunkOk(ctx, sources.Chunk{
SourceType: s.Type(),
SourceName: s.name,
SourceID: s.SourceID(),
JobID: s.JobID(),
SecretID: secretID,
Data: []byte(stripLeadingPlusMinus(res.String())),
SourceMetadata: &source_metadatapb.MetaData{
Data: meta,
},
Verify: s.verify,
})
}

func (s *Source) normalizeRepo(repo string) (string, error) {
Expand Down

0 comments on commit 587dd47

Please sign in to comment.