Skip to content

Commit aa5a5a7

Browse files
feat: send reporting flags
1 parent e723980 commit aa5a5a7

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

internal/analysis/analysis.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ type AnalysisOrchestrator interface {
5252
RunAnalysis(ctx context.Context, orgId string, rootPath string, workspaceId string) (*sarif.SarifResponse, error)
5353
RunIncrementalAnalysis(ctx context.Context, orgId string, rootPath string, workspaceId string, limitToFiles []string) (*sarif.SarifResponse, error)
5454

55-
RunTest(ctx context.Context, orgId string, b bundle.Bundle, target scan.Target) (*sarif.SarifResponse, error)
55+
RunTest(ctx context.Context, orgId string, b bundle.Bundle, target scan.Target, reportingOptions ReportingConfig) (*sarif.SarifResponse, error)
5656
}
5757

58+
type ReportingConfig struct {
59+
Report *bool
60+
ProjectName *string
61+
TargetName *string
62+
}
5863
type analysisOrchestrator struct {
5964
httpClient codeClientHTTP.HTTPClient
6065
instrumentor observability.Instrumentor
@@ -477,7 +482,7 @@ func (a *analysisOrchestrator) host(isHidden bool) string {
477482
return fmt.Sprintf("%s/%s", apiUrl, path)
478483
}
479484

480-
func (a *analysisOrchestrator) RunTest(ctx context.Context, orgId string, b bundle.Bundle, target scan.Target) (*sarif.SarifResponse, error) {
485+
func (a *analysisOrchestrator) RunTest(ctx context.Context, orgId string, b bundle.Bundle, target scan.Target, reportingConfig ReportingConfig) (*sarif.SarifResponse, error) {
481486
tracker := a.trackerFactory.GenerateTracker()
482487
tracker.Begin("Snyk Code analysis for "+target.GetPath(), "Retrieving results...")
483488

@@ -498,8 +503,18 @@ func (a *analysisOrchestrator) RunTest(ctx context.Context, orgId string, b bund
498503
body := testApi.NewCreateTestApplicationBody(
499504
testApi.WithInputBundle(b.GetBundleHash(), target.GetPath(), repoUrl, b.GetLimitToFiles()),
500505
testApi.WithScanType(a.testType),
506+
testApi.WithProjectName(reportingConfig.ProjectName),
507+
testApi.WithTargetName(reportingConfig.TargetName),
508+
testApi.WithReporting(reportingConfig.Report),
501509
)
502510

511+
fmt.Println("Creating test")
512+
bodyBytes, err := json.MarshalIndent(body, "", " ")
513+
if err != nil {
514+
return nil, err
515+
}
516+
fmt.Println(string(bodyBytes))
517+
503518
// create test
504519
resp, err := client.CreateTestWithApplicationVndAPIPlusJSONBody(ctx, orgUuid, &params, *body)
505520
if err != nil {

internal/analysis/mocks/analysis.go

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/api/test/2024-12-21/helper.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v20241221
22

33
import (
4+
openapi_types "github.com/oapi-codegen/runtime/types"
45
v20241221 "github.com/snyk/code-client-go/internal/api/test/2024-12-21/models"
56
)
67

@@ -37,6 +38,56 @@ func WithScanType(t v20241221.Scan) CreateTestOption {
3738
}
3839
}
3940

41+
func ensureOutput(body *CreateTestApplicationVndAPIPlusJSONRequestBody) *struct {
42+
Label *string `json:"label,omitempty"`
43+
ProjectId *openapi_types.UUID `json:"project_id,omitempty"`
44+
ProjectName *string `json:"project_name,omitempty"`
45+
Report *bool `json:"report,omitempty"`
46+
TargetName *string `json:"target_name,omitempty"`
47+
TargetReference *string `json:"target_reference,omitempty"`
48+
} {
49+
if body.Data.Attributes.Configuration.Output == nil {
50+
body.Data.Attributes.Configuration.Output = &struct {
51+
Label *string `json:"label,omitempty"`
52+
ProjectId *openapi_types.UUID `json:"project_id,omitempty"`
53+
ProjectName *string `json:"project_name,omitempty"`
54+
Report *bool `json:"report,omitempty"`
55+
TargetName *string `json:"target_name,omitempty"`
56+
TargetReference *string `json:"target_reference,omitempty"`
57+
}{}
58+
}
59+
return body.Data.Attributes.Configuration.Output
60+
}
61+
62+
func WithProjectName(name *string) CreateTestOption {
63+
return func(body *CreateTestApplicationVndAPIPlusJSONRequestBody) {
64+
if name == nil {
65+
return
66+
}
67+
out := ensureOutput(body)
68+
out.ProjectName = name
69+
}
70+
}
71+
72+
func WithTargetName(name *string) CreateTestOption {
73+
return func(body *CreateTestApplicationVndAPIPlusJSONRequestBody) {
74+
if name == nil {
75+
return
76+
}
77+
out := ensureOutput(body)
78+
out.TargetName = name
79+
}
80+
}
81+
func WithReporting(report *bool) CreateTestOption {
82+
return func(body *CreateTestApplicationVndAPIPlusJSONRequestBody) {
83+
if report == nil {
84+
return
85+
}
86+
out := ensureOutput(body)
87+
out.Report = report
88+
}
89+
}
90+
4091
func NewCreateTestApplicationBody(options ...CreateTestOption) *CreateTestApplicationVndAPIPlusJSONRequestBody {
4192
result := &CreateTestApplicationVndAPIPlusJSONRequestBody{}
4293
result.Data.Type = v20241221.CreateTestRequestBodyDataTypeTest

scan.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type codeScanner struct {
4444
logger *zerolog.Logger
4545
config config.Config
4646
resultTypes testModels.Scan
47+
projectName *string
48+
targetName *string
4749
}
4850

4951
type CodeScanner interface {
@@ -53,6 +55,7 @@ type CodeScanner interface {
5355
target scan.Target,
5456
files <-chan string,
5557
changedFiles map[string]bool,
58+
options ...UploadAndAnalyzeOption,
5659
) (*sarif.SarifResponse, string, error)
5760
}
5861

@@ -95,6 +98,27 @@ func WithTrackerFactory(trackerFactory scan.TrackerFactory) OptionFunc {
9598
}
9699
}
97100

101+
func WithProjectName(projectName *string) OptionFunc {
102+
return func(c *codeScanner) {
103+
c.projectName = projectName
104+
}
105+
}
106+
107+
func WithTargetName(targetName *string) OptionFunc {
108+
return func(c *codeScanner) {
109+
c.targetName = targetName
110+
}
111+
}
112+
113+
type UploadAndAnalyzeOption func(*analysis.ReportingConfig)
114+
115+
func WithReportingConfig(projectName *string, targetName *string) UploadAndAnalyzeOption {
116+
return func(c *analysis.ReportingConfig) {
117+
c.ProjectName = projectName
118+
c.TargetName = targetName
119+
}
120+
}
121+
98122
// NewCodeScanner creates a Code Scanner which can be used to trigger Snyk Code on a folder.
99123
func NewCodeScanner(
100124
config config.Config,
@@ -169,7 +193,13 @@ func (c *codeScanner) UploadAndAnalyze(
169193
target scan.Target,
170194
files <-chan string,
171195
changedFiles map[string]bool,
196+
options ...UploadAndAnalyzeOption,
172197
) (*sarif.SarifResponse, string, error) {
198+
cfg := analysis.ReportingConfig{}
199+
for _, opt := range options {
200+
opt(&cfg)
201+
}
202+
173203
if ctx.Err() != nil {
174204
c.logger.Info().Msg("Canceling Code scan - Code scanner received cancellation signal")
175205
return nil, "", nil
@@ -209,7 +239,7 @@ func (c *codeScanner) UploadAndAnalyze(
209239
return nil, bundleHash, nil
210240
}
211241

212-
response, err := c.analysisOrchestrator.RunTest(ctx, c.config.Organization(), b, target)
242+
response, err := c.analysisOrchestrator.RunTest(ctx, c.config.Organization(), b, target, cfg)
213243

214244
if ctx.Err() != nil {
215245
c.logger.Info().Msg("Canceling Code scan - Code scanner received cancellation signal")

scan_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
104104
"4a72d1db-b465-4764-99e1-ecedad03b06a",
105105
gomock.Any(),
106106
gomock.Any(),
107+
gomock.Any(),
107108
).Return(&sarif.SarifResponse{Status: "COMPLETE"}, nil)
108109

109110
codeScanner := codeclient.NewCodeScanner(
@@ -140,6 +141,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
140141
"4a72d1db-b465-4764-99e1-ecedad03b06a",
141142
gomock.Any(),
142143
gomock.Any(),
144+
gomock.Any(),
143145
).Return(&sarif.SarifResponse{Status: "COMPLETE"}, nil)
144146

145147
codeScanner := codeclient.NewCodeScanner(

0 commit comments

Comments
 (0)