Add interfaces to allow passing data between functions
What does this MR do?
Introduces a new interface, Analyzer, for the argument of NewCommands (and commands Run, Search, ...) to enable passing data between steps of analysis without changing the API.
originally to support the events feature:
-
1️⃣ report module Add observability events and collect_sast_scan_... (report!102 - merged) • Julian Thome • 18.0 (Adding support for observability event data)-
2️⃣ command module (⭐ this MR) Add interfaces to allow passing data between fu... (!67 - merged) • Jason Leasure • 18.0 (Adding support for custom serialization and context data) -
3️⃣ tracking calculator https://gitlab.com/gitlab-org/security-products/post-analyzers/tracking-calculator/-/merge_requests/100+s (Bumping the report module in TC so that observability data is passed through the post analyzer)
-
Implementation
command.Config is split into two parts, an Analyzer and an Analysis. The Analyzer represents immutable configuration data and a factory function to create an Analysis, which is mutable.
-
Analyzermethods should remain pure, e.g. they shouldn't change theAnalyzerinstance. The only method that isn't a "getter" isNewAnalysis. -
Analysismethods, which subsume the callback functions, can modify their receiver. That's the point of this MR😄 .
Technically, it comes down to pointer versus non-pointer methods. The Analyzer methods on command.Config are non-pointer methods, while the Analyzer methods are pointer methods. E.g. consider the following:
Click to expand
analyzer := command.Config{}
analysis1 := analyzer.NewAnalysis()
analysis2 := analyzer.NewAnalysis()
fmt.Printf("original : %p\n", &analyzer)
fmt.Printf("analysis1: %p\n", analysis1.(*command.Config))
fmt.Printf("analysis2: %p\n", analysis2.(*command.Config))
All 3 pointers are different, because non-pointer receivers are struct copies. That's what we want, each call to Config.NewAnalysis returns a new copy.
In the previous implementation, assignment to RulesetConfig was local to the Run function's copy cfg only. The new design makes that explicit by moving the ruleset operations onto a new Analysis that's scoped to the Run function.
What are the relevant issue numbers?
Does this MR meet the acceptance criteria?
-
Changelog entry added -
Documentation created/updated for GitLab EE, if necessary -
Documentation created/updated for this project, if necessary -
Documentation reviewed by technical writer or follow-up review issue created -
Tests added for this feature/bug -
Job definition updated, if necessary -
Conforms to the code review guidelines -
Conforms to the Go guidelines -
Security reports checked/validated by reviewer