Skip to content

Commit 0532b0c

Browse files
authored
fix: workspace creation should tolerate missing repo url (#53)
2 parents 83581fd + 65b609d commit 0532b0c

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

internal/analysis/analysis.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,18 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
145145
return "", fmt.Errorf("target is nil")
146146
}
147147

148-
repositoryTarget, ok := target.(*scan.RepositoryTarget)
149-
if !ok || repositoryTarget.GetRepositoryUrl() == "" {
150-
err := fmt.Errorf("workspace is not a repository, cannot scan")
151-
a.errorReporter.CaptureError(err, observability.ErrorReporterOptions{ErrorDiagnosticPath: target.GetPath()})
152-
return "", err
148+
repositoryTargetPath := target.GetPath()
149+
var repositoryTargetURL string
150+
if repositoryTarget, ok := target.(*scan.RepositoryTarget); ok {
151+
repositoryTargetPath, repositoryTargetURL = repositoryTarget.GetPath(), repositoryTarget.GetRepositoryUrl()
153152
}
154153

155154
host := a.host(true)
156-
a.logger.Info().Str("host", host).Str("path", repositoryTarget.GetPath()).Str("repositoryUri", repositoryTarget.GetRepositoryUrl()).Msg("creating workspace")
155+
a.logger.Info().Str("host", host).Str("path", repositoryTargetPath).Str("repositoryUri", repositoryTargetURL).Msg("creating workspace")
157156

158157
workspace, err := workspaceClient.NewClientWithResponses(host, workspaceClient.WithHTTPClient(a.httpClient))
159158
if err != nil {
160-
a.errorReporter.CaptureError(err, observability.ErrorReporterOptions{ErrorDiagnosticPath: repositoryTarget.GetPath()})
159+
a.errorReporter.CaptureError(err, observability.ErrorReporterOptions{ErrorDiagnosticPath: repositoryTargetPath})
161160
return "", fmt.Errorf("failed to connect to the workspace API %w", err)
162161
}
163162

@@ -195,7 +194,7 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
195194
WorkspaceType workspaces.WorkspacePostRequestDataAttributesWorkspaceType
196195
}{
197196
BundleId: bundleHash,
198-
RepositoryUri: repositoryTarget.GetRepositoryUrl(),
197+
RepositoryUri: repositoryTargetURL,
199198
WorkspaceType: "file_bundle_workspace",
200199
RootFolderId: target.GetPath(),
201200
}),

internal/analysis/analysis_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,26 @@ func TestAnalysis_CreateWorkspace(t *testing.T) {
111111
func TestAnalysis_CreateWorkspace_NotARepository(t *testing.T) {
112112
mockConfig, mockHTTPClient, mockInstrumentor, mockErrorReporter, mockTracker, mockTrackerFactory, logger := setup(t)
113113

114-
mockErrorReporter.EXPECT().CaptureError(gomock.Any(), gomock.Any())
115114
mockTracker.EXPECT().Begin(gomock.Eq("Creating file bundle workspace"), gomock.Eq("")).Return()
116115
mockTracker.EXPECT().End(gomock.Eq("")).Return()
117116

117+
mockHTTPClient.EXPECT().Do(
118+
mock.MatchedBy(func(i interface{}) bool {
119+
req := i.(*http.Request)
120+
return req.URL.String() == "http://localhost/hidden/orgs/4a72d1db-b465-4764-99e1-ecedad03b06a/workspaces?version=2024-03-12~experimental" &&
121+
req.Method == "POST" &&
122+
req.Header.Get("Content-Type") == "application/vnd.api+json" &&
123+
req.Header.Get("Snyk-Request-Id") == "b372d1db-b465-4764-99e1-ecedad03b06a" &&
124+
req.Header.Get("User-Agent") == "cli"
125+
}),
126+
).Return(&http.Response{
127+
StatusCode: http.StatusCreated,
128+
Header: http.Header{
129+
"Content-Type": []string{"application/vnd.api+json"},
130+
},
131+
Body: io.NopCloser(bytes.NewReader([]byte(`{"data":{"id": "c172d1db-b465-4764-99e1-ecedad03b06a"}}`))),
132+
}, nil).Times(1)
133+
118134
repoDir := t.TempDir()
119135
target, err := scan.NewRepositoryTarget(repoDir)
120136
assert.ErrorContains(t, err, "open local repository")
@@ -135,7 +151,7 @@ func TestAnalysis_CreateWorkspace_NotARepository(t *testing.T) {
135151
target,
136152
"testBundleHash",
137153
)
138-
assert.ErrorContains(t, err, "workspace is not a repository")
154+
assert.NoError(t, err)
139155
}
140156

141157
func TestAnalysis_CreateWorkspace_Failure(t *testing.T) {

0 commit comments

Comments
 (0)