Skip to content

Commit

Permalink
fix: workspace creation should tolerate missing repo url
Browse files Browse the repository at this point in the history
Workspace API is responsible for graceful degradation when a repo URL is not
available.

This should not be blocked in the client.
  • Loading branch information
cmars committed Jun 4, 2024
1 parent 5370be8 commit 3e008bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
14 changes: 6 additions & 8 deletions internal/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,17 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
return "", fmt.Errorf("target is nil")
}

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

host := a.host(true)
a.logger.Info().Str("host", host).Str("path", repositoryTarget.GetPath()).Str("repositoryUri", repositoryTarget.GetRepositoryUrl()).Msg("creating workspace")
a.logger.Info().Str("host", host).Str("path", repositoryTargetPath).Str("repositoryUri", repositoryTargetURL).Msg("creating workspace")

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

Expand Down Expand Up @@ -157,7 +155,7 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
WorkspaceType workspaces.WorkspacePostRequestDataAttributesWorkspaceType
}{
BundleId: bundleHash,
RepositoryUri: repositoryTarget.GetRepositoryUrl(),
RepositoryUri: repositoryTargetURL,
WorkspaceType: "file_bundle_workspace",
}),
Type: "workspace",
Expand Down
20 changes: 18 additions & 2 deletions internal/analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,26 @@ func TestAnalysis_CreateWorkspace(t *testing.T) {
func TestAnalysis_CreateWorkspace_NotARepository(t *testing.T) {
mockConfig, mockHTTPClient, mockInstrumentor, mockErrorReporter, mockTracker, mockTrackerFactory, logger := setup(t)

mockErrorReporter.EXPECT().CaptureError(gomock.Any(), gomock.Any())
mockTracker.EXPECT().Begin(gomock.Eq("Creating file bundle workspace"), gomock.Eq("")).Return()
mockTracker.EXPECT().End(gomock.Eq("")).Return()

mockHTTPClient.EXPECT().Do(
mock.MatchedBy(func(i interface{}) bool {
req := i.(*http.Request)
return req.URL.String() == "http://localhost/hidden/orgs/4a72d1db-b465-4764-99e1-ecedad03b06a/workspaces?version=2024-03-12~experimental" &&
req.Method == "POST" &&
req.Header.Get("Content-Type") == "application/vnd.api+json" &&
req.Header.Get("Snyk-Request-Id") == "b372d1db-b465-4764-99e1-ecedad03b06a" &&
req.Header.Get("User-Agent") == "cli"
}),
).Return(&http.Response{
StatusCode: http.StatusCreated,
Header: http.Header{
"Content-Type": []string{"application/vnd.api+json"},
},
Body: io.NopCloser(bytes.NewReader([]byte(`{"data":{"id": "c172d1db-b465-4764-99e1-ecedad03b06a"}}`))),
}, nil).Times(1)

repoDir := t.TempDir()
target, err := scan.NewRepositoryTarget(repoDir)
assert.ErrorContains(t, err, "open local repository")
Expand All @@ -120,7 +136,7 @@ func TestAnalysis_CreateWorkspace_NotARepository(t *testing.T) {
target,
"testBundleHash",
)
assert.ErrorContains(t, err, "workspace is not a repository")
assert.NoError(t, err)
}

func TestAnalysis_CreateWorkspace_Failure(t *testing.T) {
Expand Down

0 comments on commit 3e008bb

Please sign in to comment.