diff --git a/utils/resultwriter.go b/utils/resultwriter.go index f16222c1..2fca38b1 100644 --- a/utils/resultwriter.go +++ b/utils/resultwriter.go @@ -664,18 +664,23 @@ func getWorkflowFileLocationIfExists() (location string) { if exists, err := fileutils.IsDirExists(GithubBaseWorkflowDir, false); err != nil || !exists { return } + currentWd, err := os.Getwd() + if err != nil { + log.Warn(fmt.Sprintf("Failed to get the current working directory to get workflow file location: %s", err.Error())) + return + } // Check if exists in the .github/workflows directory as file name or in the content, return the file path or empty string if files, err := fileutils.ListFiles(GithubBaseWorkflowDir, false); err == nil && len(files) > 0 { for _, file := range files { if strings.Contains(file, workflowName) { log.Debug(fmt.Sprintf("Found workflow file %s at %s, replacing the location", workflowName, file)) - return file + return strings.TrimPrefix(file, currentWd) } } for _, file := range files { if content, err := fileutils.ReadFile(file); err == nil && strings.Contains(string(content), workflowName) { log.Debug(fmt.Sprintf("Found workflow name %s in %s, replacing the location", workflowName, file)) - return file + return strings.TrimPrefix(file, currentWd) } } } @@ -700,8 +705,8 @@ func getScaInBinaryMarkdownMsg(cmdResults *Results, result *sarif.Result) string func getBaseBinaryDescriptionMarkdown(subScanType SubScanType, cmdResults *Results, result *sarif.Result) (content string) { // If in github action, add the workflow name and run number - if os.Getenv(CurrentWorkflowNameEnvVar) != "" { - content += fmt.Sprintf("\nGithub Actions Workflow: %s", os.Getenv(CurrentWorkflowNameEnvVar)) + if workflowLocation := getWorkflowFileLocationIfExists(); workflowLocation != "" { + content += fmt.Sprintf("\nGithub Actions Workflow: %s", workflowLocation) } if os.Getenv(CurrentWorkflowRunNumberEnvVar) != "" { content += fmt.Sprintf("\nRun: %s", os.Getenv(CurrentWorkflowRunNumberEnvVar)) diff --git a/utils/resultwriter_test.go b/utils/resultwriter_test.go index e4e4cf8f..94712631 100644 --- a/utils/resultwriter_test.go +++ b/utils/resultwriter_test.go @@ -616,23 +616,28 @@ func preparePatchTestEnv(t *testing.T) (string, string, func()) { assert.NoError(t, err) wd, cleanUpTempDir := tests.CreateTempDirWithCallbackAndAssert(t) cleanUpWd := clientTests.ChangeDirWithCallback(t, currentWd, wd) - // Prepare dir content - // create dir named "DockerfileDir" in the temp dir and add a Dockerfile dockerfileDir := filepath.Join(wd, "DockerfileDir") err = fileutils.CreateDirIfNotExist(dockerfileDir) + // Prepare env content assert.NoError(t, err) - err = os.WriteFile(filepath.Join(dockerfileDir, "Dockerfile"), []byte("Dockerfile data"), 0644) - assert.NoError(t, err) - err = fileutils.CreateDirIfNotExist(filepath.Join(wd, GithubBaseWorkflowDir)) - assert.NoError(t, err) - err = os.WriteFile(filepath.Join(wd, GithubBaseWorkflowDir, "workflowFile.yml"), []byte("workflowFile.yml data"), 0644) - assert.NoError(t, err) + createDummyDockerfile(t, dockerfileDir) + createDummyGithubWorkflow(t, dockerfileDir) + createDummyGithubWorkflow(t, wd) return wd, dockerfileDir, func() { cleanUpWd() cleanUpTempDir() } } +func createDummyGithubWorkflow(t *testing.T, baseDir string) { + assert.NoError(t, fileutils.CreateDirIfNotExist(filepath.Join(baseDir, GithubBaseWorkflowDir))) + assert.NoError(t, os.WriteFile(filepath.Join(baseDir, GithubBaseWorkflowDir, "workflowFile.yml"), []byte("workflow name"), 0644)) +} + +func createDummyDockerfile(t *testing.T, baseDir string) { + assert.NoError(t, os.WriteFile(filepath.Join(baseDir, "Dockerfile"), []byte("Dockerfile data"), 0644)) +} + func TestPatchRunsToPassIngestionRules(t *testing.T) { wd, dockerfileDir, cleanUp := preparePatchTestEnv(t) defer cleanUp() @@ -695,7 +700,7 @@ func TestPatchRunsToPassIngestionRules(t *testing.T) { }, expectedResults: []*sarif.Run{ sarifutils.CreateRunWithDummyResultsInWd(wd, - sarifutils.CreateDummyResultWithFingerprint("some-msg\nGithub Actions Workflow: workflowFile.yml\nRun: 123\nImage: dockerImage:imageVersion\nLayer (sha256): f752cb05a39e65f231a3c47c2e08cbeac1c15e4daff0188cb129c12a3ea3049d", "some-msg", "jfrogFingerprintHash", "809cc81dc7cb39b84877606faae64f83", + sarifutils.CreateDummyResultWithFingerprint(fmt.Sprintf("some-msg\nGithub Actions Workflow: %s\nRun: 123\nImage: dockerImage:imageVersion\nLayer (sha256): f752cb05a39e65f231a3c47c2e08cbeac1c15e4daff0188cb129c12a3ea3049d", filepath.Join(GithubBaseWorkflowDir, "workflowFile.yml")), "some-msg", "jfrogFingerprintHash", "809cc81dc7cb39b84877606faae64f83", sarifutils.CreateDummyLocationWithPathAndLogicalLocation("", "f752cb05a39e65f231a3c47c2e08cbeac1c15e4daff0188cb129c12a3ea3049d", "layer", "algorithm", "sha256").WithPhysicalLocation( sarif.NewPhysicalLocation().WithArtifactLocation(sarif.NewSimpleArtifactLocation(filepath.Join(GithubBaseWorkflowDir, "workflowFile.yml"))), ), @@ -716,7 +721,7 @@ func TestPatchRunsToPassIngestionRules(t *testing.T) { }, expectedResults: []*sarif.Run{ sarifutils.CreateRunWithDummyResultsInWd(dockerfileDir, - sarifutils.CreateDummyResultWithFingerprint("some-msg\nGithub Actions Workflow: workflowFile.yml\nRun: 123\nImage: dockerImage:imageVersion\nLayer (sha256): f752cb05a39e65f231a3c47c2e08cbeac1c15e4daff0188cb129c12a3ea3049d", "some-msg", "jfrogFingerprintHash", "d5ff6a34398ff643223c1a09b06f29c4", + sarifutils.CreateDummyResultWithFingerprint(fmt.Sprintf("some-msg\nGithub Actions Workflow: %s\nRun: 123\nImage: dockerImage:imageVersion\nLayer (sha256): f752cb05a39e65f231a3c47c2e08cbeac1c15e4daff0188cb129c12a3ea3049d", filepath.Join(GithubBaseWorkflowDir, "workflowFile.yml")), "some-msg", "jfrogFingerprintHash", "d5ff6a34398ff643223c1a09b06f29c4", sarifutils.CreateDummyLocationWithPathAndLogicalLocation("", "f752cb05a39e65f231a3c47c2e08cbeac1c15e4daff0188cb129c12a3ea3049d", "layer", "algorithm", "sha256").WithPhysicalLocation( sarif.NewPhysicalLocation().WithArtifactLocation(sarif.NewSimpleArtifactLocation("Dockerfile")), ), @@ -805,10 +810,16 @@ func TestPatchRunsToPassIngestionRules(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { if tc.withEnvVars { - cleanFileEnv := clientTests.SetEnvWithCallbackAndAssert(t, CurrentWorkflowNameEnvVar, "workflowFile.yml") + cleanFileEnv := clientTests.SetEnvWithCallbackAndAssert(t, CurrentWorkflowNameEnvVar, "workflow name") defer cleanFileEnv() cleanRunNumEnv := clientTests.SetEnvWithCallbackAndAssert(t, CurrentWorkflowRunNumberEnvVar, "123") defer cleanRunNumEnv() + } else { + // Since the the env are provided by the + cleanFileEnv := clientTests.SetEnvWithCallbackAndAssert(t, CurrentWorkflowNameEnvVar, "") + defer cleanFileEnv() + cleanRunNumEnv := clientTests.SetEnvWithCallbackAndAssert(t, CurrentWorkflowRunNumberEnvVar, "") + defer cleanRunNumEnv() } if tc.withDockerfile { revertWd := clientTests.ChangeDirWithCallback(t, wd, dockerfileDir)