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

Pull gitparse config options out of pkg consts #1072

Merged
merged 2 commits into from
Feb 4, 2023
Merged

Conversation

bill-rich
Copy link
Collaborator

Make gitparse options configurable to make testing easier.

@bill-rich bill-rich requested review from a team as code owners February 4, 2023 18:43
Copy link
Collaborator

@ahrav ahrav left a comment

Choose a reason for hiding this comment

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

Love this 🙌

@@ -17,14 +17,6 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
)

const (
Copy link
Collaborator

@ahrav ahrav Feb 4, 2023

Choose a reason for hiding this comment

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

question: Can we actually leave the constants and then just use them in your factory function for NewParser, but also make them private? I think they are only ever used in this file.

Ex:

const (
	// defaultDateFormat is the standard date format for git.
	defaultDateFormat = "Mon Jan 02 15:04:05 2006 -0700"

	// maxDiffSize is the maximum size for a diff. Larger diffs will be cut off.
	maxDiffSize = 1 * 1024 * 1024 * 1024 // 1GB
	// maxCommitSize is the maximum size for a commit. Larger commits will be cut off.
	maxCommitSize = maxDiffSize
)
// NewParser creates a GitParse config from options and sets defaults.
func NewParser(options ...Option) *Parser {
	parser := &Parser{
		dateFormat:    defaultDateFormat,
		maxDiffSize:   maxDiffSize,   // 1GB
		maxCommitSize: maxCommitSize, // 1GB
	}
	for _, option := range options {
		option(parser)
	}
	return parser
}

@@ -42,6 +34,46 @@ type Diff struct {
IsBinary bool
}

// Parser sets values used in GitParse.
type Parser struct {
MaxDiffSize int
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I think we can make all the fields in here private to reduce the exposure? If we can then we would just update the reaming functions to account for this change.


// MaxDiffSize sets MaxDiffSize option. Diffs larger than MaxDiffSize will
// be truncated.
func MaxDiffSize(MaxDiffSize int) Option {
Copy link
Collaborator

Choose a reason for hiding this comment

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

super nit: Rename theses to WithMaxDiffSize and WithMaxCommitSize that way we are consistent with our engine and few other places we use functional variadic args.

Also the param in this function can be camelcased?
func MaxDiffSize(MaxDiffSize int) Option -> func MaxDiffSize(maxDiffSize int) Option

Same with MaxCommitSize.

I feel like making all the things private, keeps the global namespace a tiny bit cleaner.

func WithMaxDiffSize(maxDiffSize int) Option {
	return func(parser *Parser) {
		parser.maxDiffSize = maxDiffSize
	}
}

@bill-rich
Copy link
Collaborator Author

Good call on all the feedback. Got it all fixed up.

@bill-rich bill-rich merged commit af6e3f8 into main Feb 4, 2023
@bill-rich bill-rich deleted the pull_gitparse_options branch February 4, 2023 21:19
javajawa added a commit to mewbotorg/mewbot that referenced this pull request Feb 12, 2023
Sourced from trufflesecurity/trufflehog's releases.

v3.27.0
What's Changed
Bump golang.org/x/oauth2 from 0.3.0 to 0.4.0 by @​dependabot in trufflesecurity/trufflehog#1039
add bodyclose linter to help prevent file handle leaks by @​dustin-decker in trufflesecurity/trufflehog#1048
braintree detector: use production API URL instead of the test sandbo… by @​swdbo in trufflesecurity/trufflehog#1054
Update float detector with correct User-Agent and regex by @​ahrav in trufflesecurity/trufflehog#1061
update webex detector regex by @​ahrav in trufflesecurity/trufflehog#1062
Handle errors in a thread safe manner by @​ahrav in trufflesecurity/trufflehog#1052
Add TruffleHog version input for GitHub action by @​mcastorina in trufflesecurity/trufflehog#1064
Revert "Add TruffleHog version input for GitHub action (#1064)" by @​mcastorina in trufflesecurity/trufflehog#1068
Pull gitparse config options out of pkg consts by @​bill-rich in trufflesecurity/trufflehog#1072
Add include exclude spaces for confluence source. by @​ahrav in trufflesecurity/trufflehog#1073
Add max commit size by @​bill-rich in trufflesecurity/trufflehog#1079
Make archive handler configurable by @​bill-rich in trufflesecurity/trufflehog#1077
[chore] - Add tests for errors by @​ahrav in trufflesecurity/trufflehog#1071
Skip repo and continue scanning when encountering an error by @​mcastorina in trufflesecurity/trufflehog#1080
[chore] - Dont pre-allocate errors slice by @​ahrav in trufflesecurity/trufflehog#1083
Add Type() to detector interface by @​trufflesteeeve in trufflesecurity/trufflehog#1088
[chore] Remove logrus from engine package by @​mcastorina in trufflesecurity/trufflehog#1085
[chore] Remove logrus from github source by @​mcastorina in trufflesecurity/trufflehog#1086
Bump github.com/joho/godotenv from 1.4.0 to 1.5.1 by @​dependabot in trufflesecurity/trufflehog#1075
[chore] Remove logrus from circleci, filesystem, gitlab, and s3 sources by @​mcastorina in trufflesecurity/trufflehog#1089
[chore] - Remove monolithic config struct by @​ahrav in trufflesecurity/trufflehog#1091
Make detectors configurable by @​ahrav in trufflesecurity/trufflehog#1084
New Contributors
@​swdbo made their first contribution in trufflesecurity/trufflehog#1054
Full Changelog: trufflesecurity/trufflehog@v3.26.0...v3.27.0

v3.26.0
What's Changed
Add openssh-client to trufflehog container by @​mcastorina in trufflesecurity/trufflehog#1045
Bump github.com/rabbitmq/amqp091-go from 1.5.0 to 1.6.0 by @​dependabot in trufflesecurity/trufflehog#1036
filesystem support for exclude and include filters (2nd attemp) by @​mac2000 in trufflesecurity/trufflehog#1033
Fix the typo "programatic" by @​nezakoo in trufflesecurity/trufflehog#1046
Add file to confluence proto. by @​ahrav in trufflesecurity/trufflehog#1049
Remove false positive detection for CustomRegex by @​mcastorina in trufflesecurity/trufflehog#1050
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants