@@ -19,7 +19,9 @@ package codeclient
19
19
20
20
import (
21
21
"context"
22
+ "fmt"
22
23
24
+ "github.com/google/uuid"
23
25
"github.com/pkg/errors"
24
26
"github.com/rs/zerolog"
25
27
"github.com/rs/zerolog/log"
@@ -29,19 +31,24 @@ import (
29
31
"github.com/snyk/code-client-go/internal/analysis"
30
32
"github.com/snyk/code-client-go/internal/bundle"
31
33
"github.com/snyk/code-client-go/internal/deepcode"
34
+ "github.com/snyk/code-client-go/internal/util"
35
+ workspaceClient "github.com/snyk/code-client-go/internal/workspace/2024-03-12"
36
+ externalRef3 "github.com/snyk/code-client-go/internal/workspace/2024-03-12/workspaces"
32
37
"github.com/snyk/code-client-go/observability"
33
38
"github.com/snyk/code-client-go/sarif"
34
39
)
35
40
36
41
type codeScanner struct {
37
42
bundleManager bundle.BundleManager
43
+ workspace * workspaceClient.ClientWithResponses
38
44
errorReporter observability.ErrorReporter
39
45
logger * zerolog.Logger
40
46
}
41
47
42
48
type CodeScanner interface {
43
49
UploadAndAnalyze (
44
50
ctx context.Context ,
51
+ orgId string ,
45
52
requestId string ,
46
53
path string ,
47
54
files <- chan string ,
@@ -56,14 +63,19 @@ func NewCodeScanner(
56
63
instrumentor observability.Instrumentor ,
57
64
errorReporter observability.ErrorReporter ,
58
65
logger * zerolog.Logger ,
59
- ) * codeScanner {
66
+ ) ( * codeScanner , error ) {
60
67
snykCode := deepcode .NewSnykCodeClient (logger , httpClient , instrumentor , errorReporter , config )
61
68
bundleManager := bundle .NewBundleManager (logger , snykCode , instrumentor , errorReporter )
69
+ workspace , err := workspaceClient .NewClientWithResponses (config .SnykApi (), workspaceClient .WithHTTPClient (httpClient ))
70
+ if err != nil {
71
+ return nil , err
72
+ }
62
73
return & codeScanner {
63
74
bundleManager : bundleManager ,
75
+ workspace : workspace ,
64
76
errorReporter : errorReporter ,
65
77
logger : logger ,
66
- }
78
+ }, nil
67
79
}
68
80
69
81
// WithBundleManager creates a new Code Scanner from the current one and replaces the bundle manager.
@@ -79,6 +91,7 @@ func (c *codeScanner) WithBundleManager(bundleManager bundle.BundleManager) *cod
79
91
// UploadAndAnalyze returns a fake SARIF response for testing. Use target-service to run analysis on.
80
92
func (c * codeScanner ) UploadAndAnalyze (
81
93
ctx context.Context ,
94
+ orgId string ,
82
95
requestId string ,
83
96
path string ,
84
97
files <- chan string ,
@@ -128,6 +141,56 @@ func (c *codeScanner) UploadAndAnalyze(
128
141
return nil , bundleHash , nil
129
142
}
130
143
144
+ orgUUID := uuid .MustParse (orgId )
145
+ repositoryUri , err := util .GetRepositoryUrl (path )
146
+ if err != nil {
147
+ if ctx .Err () != nil { // Only handle errors that are not intentional cancellations
148
+ msg := "error retrieving Git info..."
149
+ c .errorReporter .CaptureError (errors .Wrap (err , msg ), observability.ErrorReporterOptions {ErrorDiagnosticPath : path })
150
+ return nil , bundleHash , err
151
+ } else {
152
+ log .Info ().Msg ("Canceling Code scan - Code scanner received cancellation signal" )
153
+ return nil , bundleHash , nil
154
+ }
155
+ }
156
+
157
+ workspaceResponse , err := c .workspace .CreateWorkspaceWithApplicationVndAPIPlusJSONBodyWithResponse (ctx , orgUUID , & workspaceClient.CreateWorkspaceParams {
158
+ Version : "2024-03-12~experimental" ,
159
+ SnykRequestId : uuid .MustParse (requestId ),
160
+ }, workspaceClient.CreateWorkspaceApplicationVndAPIPlusJSONRequestBody {
161
+ Data : struct {
162
+ Attributes struct {
163
+ BundleId string `json:"bundle_id"`
164
+ RepositoryUri string `json:"repository_uri"`
165
+ WorkspaceType externalRef3.WorkspacePostRequestDataAttributesWorkspaceType `json:"workspace_type"`
166
+ } `json:"attributes"`
167
+ Type externalRef3.WorkspacePostRequestDataType `json:"type"`
168
+ }(struct {
169
+ Attributes struct {
170
+ BundleId string `json:"bundle_id"`
171
+ RepositoryUri string `json:"repository_uri"`
172
+ WorkspaceType externalRef3.WorkspacePostRequestDataAttributesWorkspaceType `json:"workspace_type"`
173
+ }
174
+ Type externalRef3.WorkspacePostRequestDataType
175
+ }{Attributes : struct {
176
+ BundleId string `json:"bundle_id"`
177
+ RepositoryUri string `json:"repository_uri"`
178
+ WorkspaceType externalRef3.WorkspacePostRequestDataAttributesWorkspaceType `json:"workspace_type"`
179
+ }(struct {
180
+ BundleId string
181
+ RepositoryUri string
182
+ WorkspaceType externalRef3.WorkspacePostRequestDataAttributesWorkspaceType
183
+ }{
184
+ BundleId : b .GetBundleHash (),
185
+ RepositoryUri : repositoryUri ,
186
+ WorkspaceType : "workspaceUri" ,
187
+ }),
188
+ Type : "workspace" ,
189
+ }),
190
+ })
191
+
192
+ fmt .Println (workspaceResponse .ApplicationvndApiJSON201 .Data .Id )
193
+
131
194
response , err := analysis .RunAnalysis ()
132
195
if ctx .Err () != nil {
133
196
c .logger .Info ().Msg ("Canceling Code scan - Code scanner received cancellation signal" )
0 commit comments