Skip to content

Commit

Permalink
Merge branch 'hotfix/1.0.14-pr-override'
Browse files Browse the repository at this point in the history
  • Loading branch information
ckalpakoglu committed Aug 13, 2021
2 parents 691dd05 + d3707e1 commit 94b9d5e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 105 deletions.
14 changes: 11 additions & 3 deletions client/scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"time"

"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -47,8 +48,9 @@ type (
}

ScanPROptions struct {
From string `json:"from"`
To string `json:"to"`
From string `json:"from"`
To string `json:"to"`
OverrideOldAnalyze bool `json:"override-old-analyze"`
}

ResultSet struct {
Expand Down Expand Up @@ -217,7 +219,7 @@ func (c *Client) ScanByImage(project, branch, tool, image string) (string, error
return respBody.EventID, nil
}

func (c *Client) ImportScanResult(project, branch, tool string, file string) (string, error) {
func (c *Client) ImportScanResult(project, branch, tool string, file string, target string, override bool) (string, error) {

klog.Debugf("importing scan results using the file:%s", file)

Expand Down Expand Up @@ -254,6 +256,12 @@ func (c *Client) ImportScanResult(project, branch, tool string, file string) (st
if err = writer.WriteField("tool", tool); err != nil {
return "", err
}
if err = writer.WriteField("target", target); err != nil {
return "", err
}
if err = writer.WriteField("override-old-analyze", strconv.FormatBool(override)); err != nil {
return "", err
}
_ = writer.Close()

req, err := http.NewRequest(http.MethodPost, u.String(), body)
Expand Down
97 changes: 0 additions & 97 deletions cmd/import.go

This file was deleted.

28 changes: 23 additions & 5 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func init() {
scanCmd.Flags().StringP("meta", "m", "", "meta data")
scanCmd.Flags().StringP("file", "f", "", "scan file")
scanCmd.Flags().StringP("branch", "b", "", "branch")
scanCmd.Flags().StringP("merge-target", "M", "", "target branch name for pull request")
scanCmd.Flags().StringP("merge-target", "M", "", "source branch name for pull request")
scanCmd.Flags().Bool("override", false, "overrides old analysis results for the source branch")
scanCmd.Flags().String("image", "", "image to scan with container security products")

scanCmd.Flags().Bool("threshold-risk", false, "set risk score of last scan as threshold")
Expand Down Expand Up @@ -402,7 +403,19 @@ func scanByFile(cmd *cobra.Command, c *client.Client) (string, error) {
return "", fmt.Errorf("failed to parse branch flag: %w", err)
}

eventID, err := c.ImportScanResult(project, branch, tool, absoluteFilePath)
target, err := cmd.Flags().GetString("merge-target")
if err != nil {
return "", fmt.Errorf("failed to parse merge target flag: %w", err)
}
override, err := cmd.Flags().GetBool("override")
if err != nil {
return "", fmt.Errorf("failed to parse override flag: %w", err)
}
if override && target == "" {
return "", errors.New("overriding PR analysis requires a merge target")
}

eventID, err := c.ImportScanResult(project, branch, tool, absoluteFilePath, target, override)
if err != nil {
return "", fmt.Errorf("failed to import scan results: %w", err)
}
Expand Down Expand Up @@ -533,11 +546,15 @@ func findScanIDByProjectToolAndPR(cmd *cobra.Command, c *client.Client) (string,
}
branch, err := cmd.Flags().GetString("branch")
if err != nil {
return "", fmt.Errorf("failed to parse tool flag: %w", err)
return "", fmt.Errorf("failed to parse branch flag: %w", err)
}
if branch == "" {
return "", errors.New("missing branch field")
}
override, err := cmd.Flags().GetBool("override")
if err != nil {
return "", fmt.Errorf("failed to parse override flag: %w", err)
}

mergeTarget, err := cmd.Flags().GetString("merge-target")
if err != nil {
Expand All @@ -561,8 +578,9 @@ func findScanIDByProjectToolAndPR(cmd *cobra.Command, c *client.Client) (string,
scan, err := c.FindScan(project, params)
if err == nil {
opt := &client.ScanPROptions{
From: branch,
To: mergeTarget,
From: branch,
To: mergeTarget,
OverrideOldAnalyze: override,
}
eventID, err := c.RestartScanWithOption(scan.ID, opt)
if err != nil {
Expand Down

0 comments on commit 94b9d5e

Please sign in to comment.