6
6
"os"
7
7
"path"
8
8
"strconv"
9
+ "strings"
9
10
10
11
"github.com/jfrog/frogbot/icons"
11
12
"github.com/jfrog/froggit-go/vcsclient"
@@ -24,17 +25,20 @@ import (
24
25
const (
25
26
frogbotVersion = "0.0.0"
26
27
// Env
27
- jfrogUser = "FROGBOT_JF_USER"
28
- jfrogUrl = "FROGBOT_JF_URL"
29
- jfrogXrayUrl = "FROGBOT_JF_XRAY_URL"
30
- jfrogArtifactoryUrl = "FROGBOT_JF_ARTIFACTORY_URL"
31
- jfrogPassword = "FROGBOT_JF_PASSWORD"
32
- jfrogToken = "FROGBOT_JF_TOKEN"
33
- gitRepoOwner = "FROGBOT_GIT_OWNER"
34
- gitRepo = "FROGBOT_GIT_REPO"
35
- gitToken = "FROGBOT_GIT_TOKEN"
36
- gitBaseBranch = "FROGBOT_GIT_BASE_BRANCH"
37
- prID = "FROGBOT_PR"
28
+ jfrogUser = "JF_USER"
29
+ jfrogUrl = "JF_URL"
30
+ jfrogXrayUrl = "JF_XRAY_URL"
31
+ jfrogArtifactoryUrl = "JF_ARTIFACTORY_URL"
32
+ jfrogPassword = "JF_PASSWORD"
33
+ jfrogToken = "JF_TOKEN"
34
+ jfrogWatches = "JF_WATCHES"
35
+ watchesDelimiter = ","
36
+ jfrogProject = "JF_PROJECT"
37
+ gitRepoOwner = "JF_GIT_OWNER"
38
+ gitRepo = "JF_GIT_REPO"
39
+ gitToken = "JF_GIT_TOKEN"
40
+ gitBaseBranch = "JF_GIT_BASE_BRANCH"
41
+ gitPullRequestID = "JF_GIT_PULL_REQUEST_ID"
38
42
)
39
43
40
44
func main () {
@@ -71,7 +75,7 @@ func getCommands() []*clitool.Command {
71
75
}
72
76
73
77
func scanPullRequest (c * clitool.Context ) error {
74
- server , repoOwner , token , repo , baseBranch , pullRequestID , err := extractParamsFromEnv ()
78
+ server , repoOwner , token , repo , baseBranch , watches , project , pullRequestID , err := extractParamsFromEnv ()
75
79
if err != nil {
76
80
return err
77
81
}
@@ -81,8 +85,7 @@ func scanPullRequest(c *clitool.Context) error {
81
85
}
82
86
83
87
// Audit PR code
84
- // TODO - fill contex according to env/flags
85
- xrayScanParams := services.XrayGraphScanParams {IncludeVulnerabilities : true }
88
+ xrayScanParams := createXrayScanParams (watches , project )
86
89
wd , err := os .Getwd ()
87
90
if err != nil {
88
91
return err
@@ -100,19 +103,20 @@ func scanPullRequest(c *clitool.Context) error {
100
103
}
101
104
// Get only the new issues added by this PR
102
105
var vulnerabilitiesRows []xrayutils.VulnerabilityRow
103
- // TODO - handle array of scan results!
104
- if len (currentScan [0 ].Violations ) > 0 {
105
- vulnerabilitiesRows = getNewViolations (previousScan [0 ], currentScan [0 ])
106
- } else if len (currentScan [0 ].Vulnerabilities ) > 0 {
107
- vulnerabilitiesRows = getNewVulnerabilities (previousScan [0 ], currentScan [0 ])
106
+ for i := 0 ; i < len (currentScan ); i += 1 {
107
+ if len (currentScan [i ].Violations ) > 0 {
108
+ vulnerabilitiesRows = append (vulnerabilitiesRows , getNewViolations (previousScan [i ], currentScan [i ])... )
109
+ } else if len (currentScan [i ].Vulnerabilities ) > 0 {
110
+ vulnerabilitiesRows = append (vulnerabilitiesRows , getNewVulnerabilities (previousScan [i ], currentScan [i ])... )
111
+ }
108
112
}
109
113
// Comment frogbot message on the PR
110
114
message := createPullRequestMessage (vulnerabilitiesRows )
111
115
return client .AddPullRequestComment (context .Background (), repoOwner , repo , message , pullRequestID )
112
116
113
117
}
114
118
115
- func extractParamsFromEnv () (server coreconfig.ServerDetails , repoOwner , token , repo , baseBranch string , pullRequestID int , err error ) {
119
+ func extractParamsFromEnv () (server coreconfig.ServerDetails , repoOwner , token , repo , baseBranch , project , watches string , pullRequestID int , err error ) {
116
120
url := os .Getenv (jfrogUrl )
117
121
xrUrl := os .Getenv (jfrogXrayUrl )
118
122
rtUrl := os .Getenv (jfrogArtifactoryUrl )
@@ -121,7 +125,7 @@ func extractParamsFromEnv() (server coreconfig.ServerDetails, repoOwner, token,
121
125
server .ArtifactoryUrl = rtUrl
122
126
} else {
123
127
if url == "" {
124
- err = fmt .Errorf ("%s or %s and %s are missing" , url , xrUrl , rtUrl )
128
+ err = fmt .Errorf ("%s or %s and %s are missing" , jfrogUrl , jfrogXrayUrl , jfrogArtifactoryUrl )
125
129
return
126
130
}
127
131
server .Url = url
@@ -156,15 +160,36 @@ func extractParamsFromEnv() (server coreconfig.ServerDetails, repoOwner, token,
156
160
err = fmt .Errorf ("%s is missing" , gitBaseBranch )
157
161
return
158
162
}
159
- pullRequestIDString := os .Getenv (prID )
163
+ pullRequestIDString := os .Getenv (gitPullRequestID )
160
164
if pullRequestIDString == "" {
161
- err = fmt .Errorf ("%s is missing" , prID )
165
+ err = fmt .Errorf ("%s is missing" , gitPullRequestID )
162
166
return
163
167
}
164
168
pullRequestID , err = strconv .Atoi (pullRequestIDString )
165
169
if err != nil {
166
170
return
167
171
}
172
+
173
+ // No mandatory Xray context params
174
+ watches = os .Getenv (jfrogWatches )
175
+ project = os .Getenv (jfrogProject )
176
+
177
+ return
178
+ }
179
+
180
+ func createXrayScanParams (watches , project string ) (params services.XrayGraphScanParams ) {
181
+ params .ScanType = services .Dependency
182
+ params .IncludeLicenses = false
183
+ if watches != "" {
184
+ params .Watches = strings .Split (watches , watchesDelimiter )
185
+ return
186
+ }
187
+ if project != "" {
188
+ params .ProjectKey = project
189
+ return
190
+ }
191
+ // No context was supplied, request from Xray to return all known vulnerabilities.
192
+ params .IncludeVulnerabilities = true
168
193
return
169
194
}
170
195
@@ -260,7 +285,7 @@ func createPullRequestMessage(vulnerabilitiesRows []xrayutils.VulnerabilityRow)
260
285
":--: | -- | -- | -- | -- | :--: | --"
261
286
var tableContent string
262
287
for _ , vulnerability := range vulnerabilitiesRows {
263
- tableContent += fmt .Sprintf ("\n | %s | %s | %s | %s | %s | %s | %s " , icons .GetIconTag (icons .GetIconSource (vulnerability .Severity )), vulnerability .ImpactedPackageName ,
288
+ tableContent += fmt .Sprintf ("\n | %s | %s | %s | %s | %s | %s | %s " , icons .GetIconTag (icons .GetIconSource (vulnerability .Severity ))+ " " + vulnerability . Severity , vulnerability .ImpactedPackageName ,
264
289
vulnerability .ImpactedPackageVersion , vulnerability .FixedVersions , vulnerability .Components [0 ].Name , vulnerability .Components [0 ].Version , vulnerability .Cves [0 ].Id )
265
290
}
266
291
return icons .GetIconTag (icons .VulnerabilitiesBannerSource ) + tableHeder + tableContent
0 commit comments