Skip to content

Commit

Permalink
FWI-5436 - insights-plugins: Add files that were modified to scan res…
Browse files Browse the repository at this point in the history
…ponse (#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
  • Loading branch information
jdesouza authored Dec 18, 2023
1 parent c1ecddc commit 69da32a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions plugins/ci/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
15 changes: 14 additions & 1 deletion plugins/ci/pkg/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion plugins/ci/pkg/ci/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions plugins/ci/pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugins/ci/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.9
5.3.0

0 comments on commit 69da32a

Please sign in to comment.