From 69da32a75ebe267f2b6c42bf48b287f3b8ab4c0f Mon Sep 17 00:00:00 2001 From: jdesouza Date: Mon, 18 Dec 2023 15:33:49 -0300 Subject: [PATCH] FWI-5436 - insights-plugins: Add files that were modified to scan response (#855) * Removed gcloud auth * Added files that were modified * Fixed version * Fixed version * Fixed version * Fixed version * Using current master hash implementation * Fixing logic * Unifying logic with master hash --- plugins/ci/CHANGELOG.md | 3 +++ plugins/ci/pkg/ci/ci.go | 15 ++++++++++++++- plugins/ci/pkg/ci/git.go | 17 ++++++++++++++++- plugins/ci/pkg/models/models.go | 7 ++++--- plugins/ci/version.txt | 2 +- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/plugins/ci/CHANGELOG.md b/plugins/ci/CHANGELOG.md index 7f9359b62..6c58c9ed7 100644 --- a/plugins/ci/CHANGELOG.md +++ b/plugins/ci/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 5.3.0 +* Added files that were modified to CI scan response + ## 5.2.9 * update trivy to 0.48.1 diff --git a/plugins/ci/pkg/ci/ci.go b/plugins/ci/pkg/ci/ci.go index 59c05e2ed..a6fbcea45 100644 --- a/plugins/ci/pkg/ci/ci.go +++ b/plugins/ci/pkg/ci/ci.go @@ -29,6 +29,8 @@ import ( const configFileName = "fairwinds-insights.yaml" const maxLinesForPrint = 8 +const filesModifiedFileName = "files_modified" + var podSpecFields = []string{"jobTemplate", "spec", "template"} var containerSpecFields = []string{"containers", "initContainers"} @@ -309,12 +311,23 @@ func (ci *CIScan) sendResults(reports []*models.ReportInfo) (*models.ScanResults logrus.Fatalf("Unable to write contents for %s: %v", file.field, err) } } - w.Close() repoDetails, err := getGitInfo(commands.ExecInDir, ci.config.Options.CIRunner, ci.repoBaseFolder, ci.config.Options.RepositoryName, ci.config.Options.BaseBranch) if err != nil { logrus.Fatalf("Unable to get git details: %v", err) } + if len(repoDetails.filesModified) > 0 { + fw, err := w.CreateFormFile(filesModifiedFileName, filesModifiedFileName) + if err != nil { + logrus.Fatalf("Unable to create form for %s: %v", "files_modified", err) + } + _, err = fw.Write([]byte(strings.Join(repoDetails.filesModified, "\n"))) + if err != nil { + logrus.Fatalf("Unable to write file for %s: %v", "files_modified", err) + } + } + + w.Close() url := fmt.Sprintf("%s/v0/organizations/%s/ci/scan-results", ci.config.Options.Hostname, ci.config.Options.Organization) req, err := http.NewRequest("POST", url, &b) diff --git a/plugins/ci/pkg/ci/git.go b/plugins/ci/pkg/ci/git.go index 7c1cd2490..877e82737 100644 --- a/plugins/ci/pkg/ci/git.go +++ b/plugins/ci/pkg/ci/git.go @@ -19,6 +19,7 @@ type gitInfo struct { currentHash string commitMessage string repoName string + filesModified []string } // cmdInDirExecutor was extracted to be able to test this function - as the main implementation execute real commands on the given path @@ -29,6 +30,7 @@ type cmdExecutor func(cmd *exec.Cmd, message string) (string, error) func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseRepoPath, repoName, baseBranch string) (*gitInfo, error) { var err error + var filesModified []string _, err = cmdExecutor(baseRepoPath, exec.Command("git", "config", "--global", "--add", "safe.directory", "/insights"), "marking directory as safe") if err != nil { logrus.Errorf("Unable to mark directory %s as safe: %v", baseRepoPath, err) @@ -87,9 +89,21 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR logrus.Warnf("Unable to get GIT branch name: %v", err) gitCommandFail = true } + files, err := cmdExecutor(baseRepoPath, exec.Command("git", "diff", "--name-only", "HEAD", masterHash), "modified files") + if err != nil { + logrus.Warnf("Unable to get git modified files: %v", err) + gitCommandFail = true + } + + splitted := strings.Split(files, "\n") + for _, f := range splitted { + if len(f) > 0 { + filesModified = append(filesModified, f) + } + } } logrus.Infof("Branch: %s", branch) - + logrus.Infof("Files modified: %s", filesModified) origin := os.Getenv("ORIGIN_URL") if origin == "" { origin, err = cmdExecutor(baseRepoPath, exec.Command("git", "remote", "get-url", "origin"), "getting origin url") @@ -117,6 +131,7 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR branch: strings.TrimSuffix(branch, "\n"), origin: strings.TrimSuffix(origin, "\n"), repoName: strings.TrimSuffix(repoName, "\n"), + filesModified: filesModified, }, nil } diff --git a/plugins/ci/pkg/models/models.go b/plugins/ci/pkg/models/models.go index e08c94488..5c01fd19c 100644 --- a/plugins/ci/pkg/models/models.go +++ b/plugins/ci/pkg/models/models.go @@ -26,9 +26,10 @@ type Resource struct { // ReportInfo is the information about a run of one of the reports. type ReportInfo struct { - Report string - Version string - Filename string + Report string + Version string + Filename string + FilesModified []string } // Configuration is a struct representing the config options for Insights CI/CD diff --git a/plugins/ci/version.txt b/plugins/ci/version.txt index 879fd9751..03f488b07 100644 --- a/plugins/ci/version.txt +++ b/plugins/ci/version.txt @@ -1 +1 @@ -5.2.9 +5.3.0