Skip to content

Commit 3e008bb

Browse files
committed
fix: workspace creation should tolerate missing repo url
Workspace API is responsible for graceful degradation when a repo URL is not available. This should not be blocked in the client.
1 parent 5370be8 commit 3e008bb

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

internal/analysis/analysis.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,17 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
111111
return "", fmt.Errorf("target is nil")
112112
}
113113

114-
repositoryTarget, ok := target.(*scan.RepositoryTarget)
115-
if !ok || repositoryTarget.GetRepositoryUrl() == "" {
116-
err := fmt.Errorf("workspace is not a repository, cannot scan")
117-
a.errorReporter.CaptureError(err, observability.ErrorReporterOptions{ErrorDiagnosticPath: target.GetPath()})
118-
return "", err
114+
var repositoryTargetPath, repositoryTargetURL string
115+
if repositoryTarget, ok := target.(*scan.RepositoryTarget); ok {
116+
repositoryTargetPath, repositoryTargetURL = repositoryTarget.GetPath(), repositoryTarget.GetRepositoryUrl()
119117
}
120118

121119
host := a.host(true)
122-
a.logger.Info().Str("host", host).Str("path", repositoryTarget.GetPath()).Str("repositoryUri", repositoryTarget.GetRepositoryUrl()).Msg("creating workspace")
120+
a.logger.Info().Str("host", host).Str("path", repositoryTargetPath).Str("repositoryUri", repositoryTargetURL).Msg("creating workspace")
123121

124122
workspace, err := workspaceClient.NewClientWithResponses(host, workspaceClient.WithHTTPClient(a.httpClient))
125123
if err != nil {
126-
a.errorReporter.CaptureError(err, observability.ErrorReporterOptions{ErrorDiagnosticPath: repositoryTarget.GetPath()})
124+
a.errorReporter.CaptureError(err, observability.ErrorReporterOptions{ErrorDiagnosticPath: repositoryTargetPath})
127125
return "", fmt.Errorf("failed to connect to the workspace API %w", err)
128126
}
129127

@@ -157,7 +155,7 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
157155
WorkspaceType workspaces.WorkspacePostRequestDataAttributesWorkspaceType
158156
}{
159157
BundleId: bundleHash,
160-
RepositoryUri: repositoryTarget.GetRepositoryUrl(),
158+
RepositoryUri: repositoryTargetURL,
161159
WorkspaceType: "file_bundle_workspace",
162160
}),
163161
Type: "workspace",

internal/analysis/analysis_test.go

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

107-
mockErrorReporter.EXPECT().CaptureError(gomock.Any(), gomock.Any())
108107
mockTracker.EXPECT().Begin(gomock.Eq("Creating file bundle workspace"), gomock.Eq("")).Return()
109108
mockTracker.EXPECT().End(gomock.Eq("")).Return()
110109

110+
mockHTTPClient.EXPECT().Do(
111+
mock.MatchedBy(func(i interface{}) bool {
112+
req := i.(*http.Request)
113+
return req.URL.String() == "http://localhost/hidden/orgs/4a72d1db-b465-4764-99e1-ecedad03b06a/workspaces?version=2024-03-12~experimental" &&
114+
req.Method == "POST" &&
115+
req.Header.Get("Content-Type") == "application/vnd.api+json" &&
116+
req.Header.Get("Snyk-Request-Id") == "b372d1db-b465-4764-99e1-ecedad03b06a" &&
117+
req.Header.Get("User-Agent") == "cli"
118+
}),
119+
).Return(&http.Response{
120+
StatusCode: http.StatusCreated,
121+
Header: http.Header{
122+
"Content-Type": []string{"application/vnd.api+json"},
123+
},
124+
Body: io.NopCloser(bytes.NewReader([]byte(`{"data":{"id": "c172d1db-b465-4764-99e1-ecedad03b06a"}}`))),
125+
}, nil).Times(1)
126+
111127
repoDir := t.TempDir()
112128
target, err := scan.NewRepositoryTarget(repoDir)
113129
assert.ErrorContains(t, err, "open local repository")
@@ -120,7 +136,7 @@ func TestAnalysis_CreateWorkspace_NotARepository(t *testing.T) {
120136
target,
121137
"testBundleHash",
122138
)
123-
assert.ErrorContains(t, err, "workspace is not a repository")
139+
assert.NoError(t, err)
124140
}
125141

126142
func TestAnalysis_CreateWorkspace_Failure(t *testing.T) {

0 commit comments

Comments
 (0)