Skip to content

Commit

Permalink
bugfix: github fetch first (#78)
Browse files Browse the repository at this point in the history
In github we need fetch first the remote branch
Also added cleanup for base and head names in results

Co-authored-by: oranmoshai <oran.moshai@aquasec.com>
  • Loading branch information
oranmoshai and oranmoshai authored Apr 24, 2022
1 parent 90a0e25 commit df0f916
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pkg/processor/result_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func PrDiffResults(r report.Results) (reports report.Results, err error) {
}
}

// Cleanup tmp file names
for k, v := range reports {
if strings.Contains(v.Target, "head/") {
v.Target = strings.Replace(v.Target, "head/", "", 1)
reports[k] = v
}
if strings.Contains(v.Target, "base/") {
v.Target = strings.Replace(v.Target, "base/", "", 1)
reports[k] = v
}
}
return reports, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/processor/result_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestPrDiffResults(t *testing.T) {
name: "happy path - new file",
args: args{r: report.Results{
report.Result{Target: "head/cf/pr-bucket.yaml"}, report.Result{Target: "base/cf/bucket.yaml"}}},
wantReports: report.Results{report.Result{Target: "head/cf/pr-bucket.yaml"}},
wantReports: report.Results{report.Result{Target: "cf/pr-bucket.yaml"}},
},

{
Expand All @@ -143,7 +143,7 @@ func TestPrDiffResults(t *testing.T) {
types.DetectedMisconfiguration{ID: "AVD-001"}}}},
},
wantReports: report.Results{report.Result{
Target: "head/cf/bucket.yaml", Misconfigurations: []types.DetectedMisconfiguration{
Target: "cf/bucket.yaml", Misconfigurations: []types.DetectedMisconfiguration{
types.DetectedMisconfiguration{ID: "AVD-001"}}, Vulnerabilities: []types.DetectedVulnerability{}}},
},
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/scanner/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ func createDiffScanFs() error {
return errors.Wrap(err, "failed create aqua tmp dir")
}

// In GitHub we need fetch the remote branch first
if os.Getenv("GITHUB_BASE_REF") != "" {
_, err = gitExec("fetch", "origin", fmt.Sprintf("refs/heads/%s", os.Getenv("GITHUB_BASE_REF")))
if err != nil {
return errors.Wrap(err, "failed git fetch ref")
}
}

diffCmd := metadata.GetBaseRef()
out, err := gitExec("diff", "--name-status", diffCmd)
if err != nil {
return errors.Wrap(err, "failed git diff")
}

if out != "" {
diffFiles := strings.Split(out, "\n")
for _, v := range diffFiles {
Expand Down

0 comments on commit df0f916

Please sign in to comment.