Skip to content

Commit

Permalink
fix: workspace creation should tolerate missing repo url (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmars authored Jun 5, 2024
2 parents 83581fd + 65b609d commit 0532b0c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
15 changes: 7 additions & 8 deletions internal/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,18 @@ 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
repositoryTargetPath := target.GetPath()
var 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 @@ -195,7 +194,7 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
WorkspaceType workspaces.WorkspacePostRequestDataAttributesWorkspaceType
}{
BundleId: bundleHash,
RepositoryUri: repositoryTarget.GetRepositoryUrl(),
RepositoryUri: repositoryTargetURL,
WorkspaceType: "file_bundle_workspace",
RootFolderId: target.GetPath(),
}),
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 @@ -111,10 +111,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 @@ -135,7 +151,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 0532b0c

Please sign in to comment.