forked from jfrog/frogbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrationutils.go
322 lines (279 loc) · 11.4 KB
/
integrationutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"context"
"fmt"
"github.com/go-git/go-git/v5"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/jfrog/frogbot/v2/scanpullrequest"
"github.com/jfrog/frogbot/v2/scanrepository"
"github.com/jfrog/frogbot/v2/utils"
"github.com/jfrog/frogbot/v2/utils/outputwriter"
"github.com/jfrog/froggit-go/vcsclient"
"github.com/jfrog/froggit-go/vcsutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"strconv"
"strings"
"testing"
"time"
)
const (
repoName = "integration"
issuesBranch = "issues-branch"
mainBranch = "main"
expectedNumberOfIssues = 10
)
type IntegrationTestDetails struct {
RepoName string
RepoOwner string
GitToken string
GitCloneURL string
GitProvider string
GitProject string
GitUsername string
ApiEndpoint string
PullRequestID string
CustomBranchName string
UseLocalRepo bool
}
func NewIntegrationTestDetails(token, gitProvider, gitCloneUrl, repoOwner string, useLocalRepo bool) *IntegrationTestDetails {
return &IntegrationTestDetails{
GitProject: repoName,
RepoOwner: repoOwner,
RepoName: repoName,
GitToken: token,
GitUsername: "frogbot",
GitProvider: gitProvider,
GitCloneURL: gitCloneUrl,
UseLocalRepo: useLocalRepo,
}
}
func buildGitManager(t *testing.T, testDetails *IntegrationTestDetails) *utils.GitManager {
gitManager, err := utils.NewGitManager().
SetAuth(testDetails.GitUsername, testDetails.GitToken).
SetEmailAuthor("[email protected]").
SetRemoteGitUrl(testDetails.GitCloneURL)
assert.NoError(t, err)
return gitManager
}
func getIssuesBranchName() string {
return fmt.Sprintf("%s-%s", issuesBranch, getTimestamp())
}
func getTimestamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
func setIntegrationTestEnvs(t *testing.T, testDetails *IntegrationTestDetails) func() {
// Frogbot sanitizes all the environment variables that start with 'JF',
// so we restore them at the end of the test to avoid collisions with other tests
envRestoreFunc := getJfrogEnvRestoreFunc(t)
unsetEnvs := utils.SetEnvsAndAssertWithCallback(t, map[string]string{
utils.RequirementsFileEnv: "requirements.txt",
utils.GitPullRequestIDEnv: testDetails.PullRequestID,
utils.GitProvider: testDetails.GitProvider,
utils.GitTokenEnv: testDetails.GitToken,
utils.GitRepoEnv: testDetails.RepoName,
utils.GitRepoOwnerEnv: testDetails.RepoOwner,
utils.BranchNameTemplateEnv: testDetails.CustomBranchName,
utils.GitApiEndpointEnv: testDetails.ApiEndpoint,
utils.GitProjectEnv: testDetails.GitProject,
utils.GitUsernameEnv: testDetails.GitUsername,
utils.GitBaseBranchEnv: mainBranch,
utils.GitUseLocalRepositoryEnv: fmt.Sprintf("%t", testDetails.UseLocalRepo),
})
return func() {
envRestoreFunc()
unsetEnvs()
}
}
func createAndCheckoutIssueBranch(t *testing.T, testDetails *IntegrationTestDetails, tmpDir, currentIssuesBranch string) func() {
gitManager := buildGitManager(t, testDetails)
// buildGitManager in an empty directory automatically creates a default .git folder, which prevents cloning.
// So we remove the default .git and clone the repository with its .git content
err := vcsutils.RemoveTempDir(".git")
require.NoError(t, err)
err = gitManager.Clone(tmpDir, issuesBranch)
require.NoError(t, err)
err = gitManager.CreateBranchAndCheckout(currentIssuesBranch, false)
require.NoError(t, err)
// This step is necessary because GitHub limits the number of pull requests from the same commit of the source branch
_, err = os.Create("emptyfile.txt")
assert.NoError(t, err)
err = gitManager.AddAllAndCommit("emptyfile added")
assert.NoError(t, err)
err = gitManager.Push(false, currentIssuesBranch)
require.NoError(t, err)
return func() {
// Remove the branch from remote
err := gitManager.RemoveRemoteBranch(currentIssuesBranch)
assert.NoError(t, err)
}
}
func findRelevantPrID(pullRequests []vcsclient.PullRequestInfo, branch string) (prId int) {
for _, pr := range pullRequests {
if pr.Source.Name == branch && pr.Target.Name == mainBranch {
prId = int(pr.ID)
return
}
}
return
}
func getOpenPullRequests(t *testing.T, client vcsclient.VcsClient, testDetails *IntegrationTestDetails) []vcsclient.PullRequestInfo {
ctx := context.Background()
pullRequests, err := client.ListOpenPullRequests(ctx, testDetails.RepoOwner, testDetails.RepoName)
require.NoError(t, err)
return pullRequests
}
func runScanPullRequestCmd(t *testing.T, client vcsclient.VcsClient, testDetails *IntegrationTestDetails) {
// Change working dir to temp dir for cloning and branch checkout
tmpDir, restoreFunc := utils.ChangeToTempDirWithCallback(t)
defer func() {
assert.NoError(t, restoreFunc())
}()
// Get a timestamp based issues-branch
currentIssuesBranch := getIssuesBranchName()
removeBranchFunc := createAndCheckoutIssueBranch(t, testDetails, tmpDir, currentIssuesBranch)
defer removeBranchFunc()
ctx := context.Background()
// Create a pull request from the timestamp based issue branch against the main branch
err := client.CreatePullRequest(ctx, testDetails.RepoOwner, testDetails.RepoName, currentIssuesBranch, mainBranch, "scan pull request integration test", "")
require.NoError(t, err)
// Find the relevant pull request id
pullRequests := getOpenPullRequests(t, client, testDetails)
prId := findRelevantPrID(pullRequests, currentIssuesBranch)
testDetails.PullRequestID = strconv.Itoa(prId)
require.NotZero(t, prId)
defer func() {
closePullRequest(t, client, testDetails, prId)
}()
// Set the required environment variables for the scan-pull-request command
unsetEnvs := setIntegrationTestEnvs(t, testDetails)
defer unsetEnvs()
err = Exec(&scanpullrequest.ScanPullRequestCmd{}, utils.ScanPullRequest)
// Validate that issues were found and the relevant error returned
require.Errorf(t, err, scanpullrequest.SecurityIssueFoundErr)
validateResults(t, ctx, client, testDetails, prId)
}
func runScanRepositoryCmd(t *testing.T, client vcsclient.VcsClient, testDetails *IntegrationTestDetails) {
testTempDir, restoreFunc := utils.ChangeToTempDirWithCallback(t)
defer func() {
assert.NoError(t, restoreFunc())
}()
// When testing using local repository clone the repository before the test starts so we can work with it as if it existed locally
if testDetails.UseLocalRepo {
cloneOptions := &git.CloneOptions{
URL: testDetails.GitCloneURL,
Auth: &githttp.BasicAuth{
Username: testDetails.GitUsername,
Password: testDetails.GitToken,
},
RemoteName: "origin",
ReferenceName: utils.GetFullBranchName("main"),
SingleBranch: true,
Depth: 1,
Tags: git.NoTags,
}
_, err := git.PlainClone(testTempDir, false, cloneOptions)
require.NoError(t, err)
}
timestamp := getTimestamp()
// Add a timestamp to the fixing pull requests, to identify them later
testDetails.CustomBranchName = "frogbot-{IMPACTED_PACKAGE}-{BRANCH_NAME_HASH}-" + timestamp
// Set the required environment variables for the scan-repository command
unsetEnvs := setIntegrationTestEnvs(t, testDetails)
defer unsetEnvs()
err := Exec(&scanrepository.ScanRepositoryCmd{}, utils.ScanRepository)
require.NoError(t, err)
gitManager := buildGitManager(t, testDetails)
pullRequests := getOpenPullRequests(t, client, testDetails)
expectedBranchName := "frogbot-pyjwt-45ebb5a61916a91ae7c1e3ff7ffb6112-" + timestamp
prId := findRelevantPrID(pullRequests, expectedBranchName)
assert.NotZero(t, prId)
closePullRequest(t, client, testDetails, prId)
assert.NoError(t, gitManager.RemoveRemoteBranch(expectedBranchName))
expectedBranchName = "frogbot-pyyaml-985622f4dbf3a64873b6b8440288e005-" + timestamp
prId = findRelevantPrID(pullRequests, expectedBranchName)
assert.NotZero(t, prId)
closePullRequest(t, client, testDetails, prId)
assert.NoError(t, gitManager.RemoveRemoteBranch(expectedBranchName))
}
func validateResults(t *testing.T, ctx context.Context, client vcsclient.VcsClient, testDetails *IntegrationTestDetails, prID int) {
comments, err := client.ListPullRequestComments(ctx, testDetails.RepoOwner, testDetails.RepoName, prID)
require.NoError(t, err)
switch actualClient := client.(type) {
case *vcsclient.GitHubClient:
validateGitHubComments(t, ctx, actualClient, testDetails, prID, comments)
case *vcsclient.AzureReposClient:
validateAzureComments(t, comments)
case *vcsclient.BitbucketServerClient:
validateBitbucketServerComments(t, comments)
case *vcsclient.GitLabClient:
validateGitLabComments(t, comments)
}
}
func validateGitHubComments(t *testing.T, ctx context.Context, client *vcsclient.GitHubClient, testDetails *IntegrationTestDetails, prID int, comments []vcsclient.CommentInfo) {
require.Len(t, comments, 1)
comment := comments[0]
assert.Contains(t, comment.Content, string(outputwriter.VulnerabilitiesPrBannerSource))
reviewComments, err := client.ListPullRequestReviewComments(ctx, testDetails.RepoOwner, testDetails.RepoName, prID)
assert.NoError(t, err)
assert.GreaterOrEqual(t, len(reviewComments), 11)
}
func validateAzureComments(t *testing.T, comments []vcsclient.CommentInfo) {
assert.GreaterOrEqual(t, len(comments), expectedNumberOfIssues)
assertBannerExists(t, comments, string(outputwriter.VulnerabilitiesPrBannerSource))
}
func validateBitbucketServerComments(t *testing.T, comments []vcsclient.CommentInfo) {
assert.GreaterOrEqual(t, len(comments), expectedNumberOfIssues)
assertBannerExists(t, comments, outputwriter.GetSimplifiedTitle(outputwriter.VulnerabilitiesPrBannerSource))
}
func validateGitLabComments(t *testing.T, comments []vcsclient.CommentInfo) {
assert.GreaterOrEqual(t, len(comments), expectedNumberOfIssues)
assertBannerExists(t, comments, string(outputwriter.VulnerabilitiesMrBannerSource))
}
func getJfrogEnvRestoreFunc(t *testing.T) func() {
jfrogEnvs := make(map[string]string)
for _, env := range os.Environ() {
envSplit := strings.Split(env, "=")
key := envSplit[0]
val := envSplit[1]
if strings.HasPrefix(key, "JF_") {
jfrogEnvs[key] = val
}
}
return func() {
for key, val := range jfrogEnvs {
assert.NoError(t, os.Setenv(key, val))
}
}
}
// This function retrieves the relevant VCS provider access token based on the corresponding environment variable.
// If the environment variable is empty, the test is skipped.
func getIntegrationToken(t *testing.T, tokenEnv string) string {
integrationRepoToken := os.Getenv(tokenEnv)
if integrationRepoToken == "" {
t.Skipf("%s is not set, skipping integration test", tokenEnv)
}
return integrationRepoToken
}
func assertBannerExists(t *testing.T, comments []vcsclient.CommentInfo, banner string) {
var isContains bool
var occurrences int
for _, c := range comments {
if strings.Contains(c.Content, banner) {
isContains = true
occurrences++
}
}
assert.True(t, isContains)
assert.Equal(t, 1, occurrences)
}
func closePullRequest(t *testing.T, client vcsclient.VcsClient, testDetails *IntegrationTestDetails, prID int) {
targetBranch := mainBranch
if _, isAzureClient := client.(*vcsclient.AzureReposClient); isAzureClient {
// The Azure API requires not adding parameters that won't be updated, so we omit the targetBranch in that case
targetBranch = ""
}
err := client.UpdatePullRequest(context.Background(), testDetails.RepoOwner, testDetails.RepoName, "integration test finished", "", targetBranch, prID, vcsutils.Closed)
assert.NoError(t, err)
}