Skip to content

Commit 956c8fb

Browse files
committed
feat: show target directory and file as "Testing <target dir> (<target file>) ...", and simplify the JSON output's "org" code
- the "Testing ..." line now shows as "Testing <target dir> (target file) ...", which helps distinguish the project in cases like --all-projects. - "path" in both human-readable and JSON output is now the Target Directory instead of cwd - org and target directory are both obtainable from Configuration, so orgSlug is no longer passed around
1 parent a68ffc1 commit 956c8fb

File tree

8 files changed

+39
-38
lines changed

8 files changed

+39
-38
lines changed

internal/commands/ostest/depgraph_flow.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func RunUnifiedTestFlow(
2828
riskScoreThreshold *uint16,
2929
severityThreshold *testapi.Severity,
3030
orgID string,
31-
orgSlugOrID string,
3231
errFactory *errors.ErrorFactory,
3332
logger *zerolog.Logger,
3433
) ([]workflow.Data, error) {
@@ -47,7 +46,6 @@ func RunUnifiedTestFlow(
4746
ictx,
4847
testClient,
4948
orgID,
50-
orgSlugOrID,
5149
errFactory,
5250
logger,
5351
localPolicy,
@@ -67,7 +65,6 @@ func testAllDepGraphs(
6765
ictx workflow.InvocationContext,
6866
testClient testapi.TestClient,
6967
orgID string,
70-
orgSlugOrID string,
7168
errFactory *errors.ErrorFactory,
7269
logger *zerolog.Logger,
7370
localPolicy *testapi.LocalPolicy,
@@ -95,7 +92,7 @@ func testAllDepGraphs(
9592
// Run the test with the depgraph subject
9693
legacyFinding, outputData, err := RunTest(
9794
ctx, ictx, testClient, subject, projectName, packageManager, depCount,
98-
displayTargetFile, orgID, orgSlugOrID, errFactory, logger, localPolicy)
95+
displayTargetFile, orgID, errFactory, logger, localPolicy)
9996
if err != nil {
10097
return nil, nil, err
10198
}

internal/commands/ostest/sbom_reachability_flow.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func RunSbomReachabilityFlow(
2525
sourceCodePath string,
2626
bsClient bundlestore.Client,
2727
orgID string,
28-
orgSlugOrID string,
2928
) ([]workflow.Data, error) {
3029
if sourceCodePath == "" {
3130
sourceCodePath = "."
@@ -68,7 +67,7 @@ func RunSbomReachabilityFlow(
6867
return nil, fmt.Errorf("failed to create sbom test reachability subject: %w", err)
6968
}
7069

71-
findings, summary, err := RunTest(ctx, ictx, testClient, subject, "", "", int(0), sbomPath, orgID, orgSlugOrID, errFactory, logger, nil)
70+
findings, summary, err := RunTest(ctx, ictx, testClient, subject, "", "", int(0), sbomPath, orgID, errFactory, logger, nil)
7271
if err != nil {
7372
return nil, err
7473
}

internal/commands/ostest/sbom_reachability_flow_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func Test_RunSbomReachabilityFlow_JSON(t *testing.T) {
3939

4040
ctx := context.Background()
4141
ef := errors.NewErrorFactory(&nopLogger)
42-
mockIctx, mockTestClient, mockBsClient, orgID, orgSlug, sbomPath, sourceCodePath := setupTest(ctx, t, ctrl, true)
42+
mockIctx, mockTestClient, mockBsClient, orgID, sbomPath, sourceCodePath := setupTest(ctx, t, ctrl, true)
4343

4444
// This should now succeed with proper finding data
45-
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID, orgSlug)
45+
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID)
4646

4747
require.NoError(t, err)
4848
require.NotNil(t, result)
@@ -64,10 +64,10 @@ func Test_RunSbomReachabilityFlow_HumanReadable(t *testing.T) {
6464

6565
ctx := context.Background()
6666
ef := errors.NewErrorFactory(&nopLogger)
67-
mockIctx, mockTestClient, mockBsClient, orgID, orgSlug, sbomPath, sourceCodePath := setupTest(ctx, t, ctrl, false)
67+
mockIctx, mockTestClient, mockBsClient, orgID, sbomPath, sourceCodePath := setupTest(ctx, t, ctrl, false)
6868

6969
// This should now succeed with proper finding data
70-
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID, orgSlug)
70+
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID)
7171

7272
require.NoError(t, err)
7373
require.NotNil(t, result)
@@ -87,7 +87,6 @@ func setupTest(ctx context.Context, t *testing.T, ctrl *gomock.Controller, jsonO
8787
string,
8888
string,
8989
string,
90-
string,
9190
) {
9291
t.Helper()
9392
sbomPath := "./testdata/bom.json"
@@ -222,5 +221,5 @@ func setupTest(ctx context.Context, t *testing.T, ctrl *gomock.Controller, jsonO
222221
mockIctx.EXPECT().GetEnhancedLogger().Return(&nopLogger).AnyTimes()
223222
mockIctx.EXPECT().GetRuntimeInfo().Return(runtimeinfo.New()).AnyTimes()
224223

225-
return mockIctx, mockTestClient, mockBsClient, orgID, orgSlug, sbomPath, sourceCodePath
224+
return mockIctx, mockTestClient, mockBsClient, orgID, sbomPath, sourceCodePath
226225
}

internal/commands/ostest/test_execution.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
std_errors "errors"
77
"fmt"
88
"math"
9-
"os"
109
"sort"
1110
"strings"
1211

1312
"github.com/rs/zerolog"
1413
"github.com/snyk/go-application-framework/pkg/apiclients/testapi"
14+
"github.com/snyk/go-application-framework/pkg/configuration"
1515
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
1616
"github.com/snyk/go-application-framework/pkg/local_workflows/json_schemas"
1717
"github.com/snyk/go-application-framework/pkg/workflow"
@@ -43,7 +43,6 @@ func RunTest(
4343
depCount int,
4444
displayTargetFile string,
4545
orgID string,
46-
orgSlugOrID string,
4746
errFactory *errors.ErrorFactory,
4847
logger *zerolog.Logger,
4948
localPolicy *testapi.LocalPolicy,
@@ -53,17 +52,19 @@ func RunTest(
5352
return nil, nil, err
5453
}
5554

56-
// path should be the current working directory
57-
currentDir, wdErr := os.Getwd()
58-
if wdErr != nil {
59-
logger.Error().Err(wdErr).Msg("Failed to get current working directory")
60-
return nil, nil, fmt.Errorf("failed to get current working directory: %w", wdErr)
55+
config := ictx.GetConfiguration()
56+
targetDir := config.GetString(configuration.INPUT_DIRECTORY)
57+
58+
orgSlugOrID := config.GetString(configuration.ORGANIZATION_SLUG)
59+
if orgSlugOrID == "" {
60+
logger.Info().Msg("No organization slug provided; using organization ID.")
61+
orgSlugOrID = orgID
6162
}
6263

6364
uniqueCount := calculateUniqueIssueCount(findingsData)
6465

6566
// The summary is always needed for the exit code calculation.
66-
standardSummary, summaryData, summaryErr := NewSummaryData(finalResult, logger, currentDir)
67+
standardSummary, summaryData, summaryErr := NewSummaryData(finalResult, logger, targetDir)
6768
if summaryErr != nil && !std_errors.Is(summaryErr, ErrNoSummaryData) {
6869
// Log the error but continue, as this is not fatal.
6970
logger.Warn().Err(summaryErr).Msg("Failed to create test summary for exit code handling")
@@ -75,7 +76,7 @@ func RunTest(
7576
OrgSlugOrID: orgSlugOrID,
7677
ProjectName: projectName,
7778
PackageManager: packageManager,
78-
CurrentDir: currentDir,
79+
TargetDir: targetDir,
7980
UniqueCount: uniqueCount,
8081
DepCount: depCount,
8182
DisplayTargetFile: displayTargetFile,

internal/commands/ostest/workflow.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func setupSBOMReachabilityFlow(
7878
ictx workflow.InvocationContext,
7979
testClient testapi.TestClient,
8080
orgID string,
81-
orgSlugOrID string,
8281
errFactory *errors.ErrorFactory,
8382
logger *zerolog.Logger,
8483
sbom, sourceDir string,
@@ -105,7 +104,7 @@ func setupSBOMReachabilityFlow(
105104
)
106105

107106
bsClient := bundlestore.NewClient(ictx.GetNetworkAccess().GetHttpClient(), codeScannerConfig, cScanner, logger)
108-
return RunSbomReachabilityFlow(ctx, ictx, testClient, errFactory, logger, sbom, sourceDir, bsClient, orgID, orgSlugOrID)
107+
return RunSbomReachabilityFlow(ctx, ictx, testClient, errFactory, logger, sbom, sourceDir, bsClient, orgID)
109108
}
110109

111110
// setupDefaultTestFlow sets up and runs the default test flow with risk score and severity thresholds.
@@ -114,7 +113,6 @@ func setupDefaultTestFlow(
114113
ictx workflow.InvocationContext,
115114
testClient testapi.TestClient,
116115
orgID string,
117-
orgSlugOrID string,
118116
errFactory *errors.ErrorFactory,
119117
logger *zerolog.Logger,
120118
riskScoreThreshold int,
@@ -153,7 +151,7 @@ func setupDefaultTestFlow(
153151
severityThresholdPtr = &st
154152
}
155153

156-
return RunUnifiedTestFlow(ctx, ictx, testClient, riskScorePtr, severityThresholdPtr, orgID, orgSlugOrID, errFactory, logger)
154+
return RunUnifiedTestFlow(ctx, ictx, testClient, riskScorePtr, severityThresholdPtr, orgID, errFactory, logger)
157155
}
158156

159157
// OSWorkflow is the entry point for the Open Source Test workflow.
@@ -197,12 +195,6 @@ func OSWorkflow(
197195
return nil, errFactory.NewEmptyOrgError()
198196
}
199197

200-
orgSlugOrID := config.GetString(configuration.ORGANIZATION_SLUG)
201-
if orgSlugOrID == "" {
202-
logger.Info().Msg("No organization slug provided; using organization ID.")
203-
orgSlugOrID = orgID
204-
}
205-
206198
// Create Snyk client
207199
httpClient := ictx.GetNetworkAccess().GetHttpClient()
208200
snykClient := snykclient.NewSnykClient(httpClient, ictx.GetConfiguration().GetString(configuration.API_URL), orgID)
@@ -220,8 +212,8 @@ func OSWorkflow(
220212
// Route to the appropriate flow based on flags
221213
switch {
222214
case sbomReachabilityTest:
223-
return setupSBOMReachabilityFlow(ctx, ictx, testClient, orgID, orgSlugOrID, errFactory, logger, sbom, sourceDir)
215+
return setupSBOMReachabilityFlow(ctx, ictx, testClient, orgID, errFactory, logger, sbom, sourceDir)
224216
default:
225-
return setupDefaultTestFlow(ctx, ictx, testClient, orgID, orgSlugOrID, errFactory, logger, riskScoreThreshold)
217+
return setupDefaultTestFlow(ctx, ictx, testClient, orgID, errFactory, logger, riskScoreThreshold)
226218
}
227219
}

internal/legacy/transform/transform.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type SnykSchemaToLegacyParams struct {
2828
OrgSlugOrID string
2929
ProjectName string
3030
PackageManager string
31-
CurrentDir string
31+
TargetDir string
3232
UniqueCount int32
3333
DepCount int
3434
DisplayTargetFile string
@@ -398,7 +398,7 @@ func ConvertSnykSchemaFindingsToLegacy(params *SnykSchemaToLegacyParams) (*defin
398398
res := definitions.LegacyVulnerabilityResponse{
399399
Org: params.OrgSlugOrID,
400400
ProjectName: params.ProjectName,
401-
Path: params.CurrentDir,
401+
Path: params.TargetDir,
402402
PackageManager: params.PackageManager,
403403
DisplayTargetFile: params.DisplayTargetFile,
404404
UniqueCount: params.UniqueCount,

internal/presenters/funcs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func getDefaultTemplateFuncMap(config configuration.Configuration, ri runtimeinf
361361
defaultMap["getFindingId"] = getFindingID
362362
defaultMap["hasPrefix"] = strings.HasPrefix
363363
defaultMap["isLicenseFinding"] = isLicenseFinding
364+
defaultMap["constructDisplayPath"] = constructDisplayPath(config)
364365

365366
return defaultMap
366367
}
@@ -433,3 +434,14 @@ func formatDatetime(input, inputFormat, outputFormat string) string {
433434

434435
return datetime.Format(outputFormat)
435436
}
437+
438+
// constructDisplayPath constructs the display path from the summary path and display target file.
439+
func constructDisplayPath(config configuration.Configuration) func(displayTargetFile string) string {
440+
return func(displayTargetFile string) string {
441+
summaryPath := config.GetString(configuration.INPUT_DIRECTORY)
442+
if displayTargetFile == "" {
443+
return summaryPath
444+
}
445+
return fmt.Sprintf("%s (%s)", summaryPath, displayTargetFile)
446+
}
447+
}

internal/presenters/templates/unified_finding.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@
6666
{{- end}}{{/* end "details" */}}
6767

6868
{{define "header" }}
69-
{{ print "Testing " .Summary.Path " ..." | bold }}
69+
{{ $displayPath := constructDisplayPath .DisplayTargetFile -}}
70+
{{ print "Testing " $displayPath " ..." | bold }}
7071
{{end }}
7172

7273
{{- define "summary"}}{{ "Test Summary" | bold }}
7374

7475
Organization: {{ getValueFromConfig "internal_org_slug" }}
7576
Test type: {{ if eq .Summary.Type "sast" }}Static code analysis{{else}}{{ .Summary.Type }}{{ end}}
76-
Project path: {{ .Summary.Path }}
77-
77+
Project path: {{ getValueFromConfig "targetDirectory" }}
78+
7879
{{- $total := 0 }}{{- $open := 0 }}{{- $ignored := 0 }}
7980
{{- range $res := .Summary.Results }}
8081
{{- $total = add $total $res.Total }}

0 commit comments

Comments
 (0)