-
Notifications
You must be signed in to change notification settings - Fork 74
/
git_test.go
389 lines (372 loc) · 14.8 KB
/
git_test.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package utils
import (
"errors"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/jfrog/froggit-go/vcsutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
func TestGitManager_GenerateCommitMessage(t *testing.T) {
testCases := []struct {
gitManager GitManager
impactedPackage string
fixVersion VulnerabilityDetails
expected string
description string
}{
{
gitManager: GitManager{customTemplates: CustomTemplates{commitMessageTemplate: "<type>: bump ${IMPACTED_PACKAGE}"}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "<type>: bump mquery",
description: "Custom prefix",
},
{
gitManager: GitManager{customTemplates: CustomTemplates{commitMessageTemplate: "<type>[scope]: Upgrade package ${IMPACTED_PACKAGE} to ${FIX_VERSION}"}},
impactedPackage: "mquery", fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "<type>[scope]: Upgrade package mquery to 3.4.5",
description: "Default template",
}, {
gitManager: GitManager{customTemplates: CustomTemplates{commitMessageTemplate: ""}},
impactedPackage: "mquery", fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "Upgrade mquery to 3.4.5",
description: "Default template",
},
// Test template without $
{
gitManager: GitManager{customTemplates: CustomTemplates{commitMessageTemplate: "<type>[scope]: Upgrade package {IMPACTED_PACKAGE} to {FIX_VERSION}"}},
impactedPackage: "mquery", fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "<type>[scope]: Upgrade package mquery to 3.4.5",
description: "Default template",
},
{
gitManager: GitManager{customTemplates: CustomTemplates{commitMessageTemplate: "<type>[scope]: Upgrade package ${IMPACTED_PACKAGE} to {FIX_VERSION}"}},
impactedPackage: "mquery", fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "<type>[scope]: Upgrade package mquery to 3.4.5",
description: "Default template",
},
}
for _, test := range testCases {
t.Run(test.expected, func(t *testing.T) {
commitMessage := test.gitManager.GenerateCommitMessage(test.impactedPackage, test.fixVersion.SuggestedFixedVersion)
assert.Equal(t, test.expected, commitMessage)
})
}
}
func TestGitManager_GenerateFixBranchName(t *testing.T) {
testCases := []struct {
gitManager GitManager
impactedPackage string
fixVersion VulnerabilityDetails
expected string
description string
}{
{
gitManager: GitManager{customTemplates: CustomTemplates{branchNameTemplate: "[Feature]-${IMPACTED_PACKAGE}-${BRANCH_NAME_HASH}"}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "[Feature]-mquery-41b1f45136b25e3624b15999bd57a476",
description: "Custom template",
},
{
gitManager: GitManager{customTemplates: CustomTemplates{branchNameTemplate: ""}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "frogbot-mquery-41b1f45136b25e3624b15999bd57a476",
description: "No template",
}, {
gitManager: GitManager{customTemplates: CustomTemplates{branchNameTemplate: "just-a-branch-${BRANCH_NAME_HASH}"}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "just-a-branch-41b1f45136b25e3624b15999bd57a476",
description: "Custom template without inputs",
},
}
for _, test := range testCases {
t.Run(test.expected, func(t *testing.T) {
commitMessage, err := test.gitManager.GenerateFixBranchName("md5Branch", test.impactedPackage, test.fixVersion.SuggestedFixedVersion)
assert.NoError(t, err)
assert.Equal(t, test.expected, commitMessage)
})
}
}
func TestGitManager_GeneratePullRequestTitle(t *testing.T) {
testCases := []struct {
gitManager GitManager
impactedPackage string
fixVersion VulnerabilityDetails
expected string
description string
}{
{
gitManager: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[CustomPR] update ${IMPACTED_PACKAGE} to ${FIX_VERSION}"}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "[CustomPR] update mquery to 3.4.5",
description: "Custom template",
},
{
gitManager: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[CustomPR] update ${IMPACTED_PACKAGE}"}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "[CustomPR] update mquery",
description: "Custom template one var",
},
{
gitManager: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: ""}},
impactedPackage: "mquery",
fixVersion: VulnerabilityDetails{SuggestedFixedVersion: "3.4.5"},
expected: "[🐸 Frogbot] Update version of mquery to 3.4.5",
description: "No prefix",
},
}
for _, test := range testCases {
t.Run(test.expected, func(t *testing.T) {
titleOutput := test.gitManager.GeneratePullRequestTitle(test.impactedPackage, test.fixVersion.SuggestedFixedVersion)
assert.Equal(t, test.expected, titleOutput)
})
}
}
func TestGitManager_GenerateAggregatedFixBranchName(t *testing.T) {
testCases := []struct {
gitManager GitManager
baseBranch string
expected string
desc string
}{
{
expected: "frogbot-update-Go-dependencies-main",
baseBranch: "main",
desc: "No template",
gitManager: GitManager{},
}, {
expected: "frogbot-update-Go-dependencies-v2",
baseBranch: "v2",
desc: "No template",
gitManager: GitManager{},
},
{
expected: "[feature]-Go-main",
baseBranch: "main",
desc: "Custom template hash only",
gitManager: GitManager{customTemplates: CustomTemplates{branchNameTemplate: "[feature]-${BRANCH_NAME_HASH}"}},
}, {
expected: "[feature]-Go-master",
baseBranch: "master",
desc: "Custom template hash only",
gitManager: GitManager{customTemplates: CustomTemplates{branchNameTemplate: "[feature]-${BRANCH_NAME_HASH}"}},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
titleOutput := test.gitManager.GenerateAggregatedFixBranchName(test.baseBranch, []coreutils.Technology{coreutils.Go})
assert.Equal(t, test.expected, titleOutput)
})
}
}
func TestGitManager_GenerateAggregatedCommitMessage(t *testing.T) {
testCases := []struct {
gitManager GitManager
expected string
}{
{gitManager: GitManager{}, expected: "[🐸 Frogbot] Update Pipenv dependencies"},
{gitManager: GitManager{customTemplates: CustomTemplates{commitMessageTemplate: "custom_template"}}, expected: "custom_template"},
}
for _, test := range testCases {
t.Run(test.expected, func(t *testing.T) {
commit := test.gitManager.GenerateAggregatedCommitMessage([]coreutils.Technology{coreutils.Pipenv})
assert.Equal(t, commit, test.expected)
})
}
}
func TestGitManager_Checkout(t *testing.T) {
testCases := []struct {
withLocalChanges bool
}{
{
withLocalChanges: false,
},
{
withLocalChanges: true,
},
}
for _, test := range testCases {
t.Run(fmt.Sprintf("test branch checkout: local changes:%t", test.withLocalChanges), func(t *testing.T) {
tmpDir, err := fileutils.CreateTempDir()
assert.NoError(t, err)
defer func() {
assert.NoError(t, fileutils.RemoveTempDir(tmpDir))
}()
var restoreWd func() error
restoreWd, err = Chdir(tmpDir)
assert.NoError(t, err)
defer func() {
assert.NoError(t, restoreWd())
}()
gitManager := createFakeDotGit(t, tmpDir)
// Get the current branch that is set as HEAD
headRef, err := gitManager.localGitRepository.Head()
assert.NoError(t, err)
assert.Equal(t, headRef.Name().Short(), "master")
if test.withLocalChanges {
// Create new file in master branch
tempFilePath := filepath.Join(tmpDir, "myFile.txt")
var file *os.File
file, err = os.Create(tempFilePath)
assert.NoError(t, err)
assert.NoError(t, file.Close())
// Create 'dev' branch and checkout
err = gitManager.CreateBranchAndCheckout("dev", true)
assert.NoError(t, err)
// Verify that temp file exist in new branch
var fileExists bool
fileExists, err = fileutils.IsFileExists(tempFilePath, false)
assert.NoError(t, err)
assert.True(t, fileExists)
} else {
// Create 'dev' branch and checkout
err = gitManager.CreateBranchAndCheckout("dev", false)
assert.NoError(t, err)
}
var currBranch string
currBranch, err = getCurrentBranch(gitManager.localGitRepository)
assert.NoError(t, err)
assert.Equal(t, "dev", currBranch)
// Checkout back to 'master'
assert.NoError(t, gitManager.Checkout("master"))
currBranch, err = getCurrentBranch(gitManager.localGitRepository)
assert.NoError(t, err)
assert.Equal(t, "master", currBranch)
})
}
}
func createFakeDotGit(t *testing.T, testPath string) *GitManager {
// Initialize a new in-memory repository
repo, err := git.PlainInit(testPath, false)
assert.NoError(t, err)
// Create a new file and add it to the worktree
filename := "README.md"
content := []byte("# My New Repository\n\nThis is a sample repository created using go-git.")
err = os.WriteFile(filename, content, 0644)
assert.NoError(t, err)
worktree, err := repo.Worktree()
assert.NoError(t, err)
_, err = worktree.Add(filename)
assert.NoError(t, err)
// Commit the changes to the new main branch
_, err = worktree.Commit("Initial commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Your Name",
Email: "your@email.com",
},
})
assert.NoError(t, err)
manager := NewGitManager().SetDryRun(true, testPath)
manager.localGitRepository = repo
manager.remoteName = vcsutils.RemoteName
assert.NoError(t, err)
return manager
}
func TestGitManager_SetRemoteGitUrl(t *testing.T) {
testCases := []struct {
description string
dotGitExists bool
remoteGitUrl string
remoteHttpsGitUrl string
existingRemoteUrl string
expectedError error
expectedGitUrl string
}{
{
description: "DotGit does not exist",
dotGitExists: false,
remoteHttpsGitUrl: "https://example.com/owner/repo.git",
expectedGitUrl: "https://example.com/owner/repo.git",
},
{
description: "DotGit exists, no remote found",
dotGitExists: true,
remoteHttpsGitUrl: "https://example.com/owner/repo.git",
expectedError: errors.New("'git remote origin' failed with error: remote not found"),
},
{
description: "DotGit exists, remote URL exists with HTTPS protocol",
dotGitExists: true,
remoteHttpsGitUrl: "https://example.com/owner/repo.git",
existingRemoteUrl: "https://example.com/owner/repo.git",
expectedGitUrl: "https://example.com/owner/repo.git",
},
{
description: "DotGit exists, remote URL is not HTTPS",
dotGitExists: true,
remoteHttpsGitUrl: "https://example.com/owner/repo.git",
existingRemoteUrl: "ssh://example.com/owner/repo.git",
// Should be updated to the new HTTPS URL
expectedGitUrl: "https://example.com/owner/repo.git",
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
tmpDir, err := fileutils.CreateTempDir()
assert.NoError(t, err)
baseDir, err := os.Getwd()
assert.NoError(t, err)
restoreFunc := tests.ChangeDirWithCallback(t, baseDir, tmpDir)
defer restoreFunc()
gm := NewGitManager().SetDryRun(true, tmpDir)
if tc.dotGitExists {
gm = createFakeDotGit(t, tmpDir)
}
if tc.existingRemoteUrl != "" {
_, err = gm.localGitRepository.CreateRemote(&config.RemoteConfig{
Name: vcsutils.RemoteName,
URLs: []string{tc.existingRemoteUrl},
})
assert.NoError(t, err)
}
_, err = gm.SetRemoteGitUrl(tc.remoteHttpsGitUrl)
if err != nil {
assert.EqualError(t, tc.expectedError, err.Error())
} else {
assert.Nil(t, err)
}
assert.Equal(t, tc.expectedGitUrl, gm.remoteGitUrl)
})
}
}
func TestGetAggregatedPullRequestTitle(t *testing.T) {
defaultGm := GitManager{}
testsCases := []struct {
tech []coreutils.Technology
gm GitManager
expected string
}{
{gm: defaultGm, tech: []coreutils.Technology{}, expected: "[🐸 Frogbot] Update dependencies"},
{gm: defaultGm, tech: []coreutils.Technology{coreutils.Maven}, expected: "[🐸 Frogbot] Update Maven dependencies"},
{gm: defaultGm, tech: []coreutils.Technology{coreutils.Gradle}, expected: "[🐸 Frogbot] Update Gradle dependencies"},
{gm: defaultGm, tech: []coreutils.Technology{coreutils.Npm}, expected: "[🐸 Frogbot] Update npm dependencies"},
{gm: defaultGm, tech: []coreutils.Technology{coreutils.Yarn}, expected: "[🐸 Frogbot] Update Yarn dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Dependencies] My template "}}, tech: []coreutils.Technology{coreutils.Yarn}, expected: "[Dependencies] My template - Yarn Dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: ""}}, tech: []coreutils.Technology{coreutils.Yarn}, expected: "[🐸 Frogbot] Update Yarn dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Feature] %s hello"}}, tech: []coreutils.Technology{coreutils.Yarn}, expected: "[Feature] hello - Yarn Dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Feature] %s %d hello"}}, tech: []coreutils.Technology{coreutils.Yarn}, expected: "[Feature] hello - Yarn Dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Feature] %s %d hello"}}, tech: []coreutils.Technology{coreutils.Yarn}, expected: "[Feature] hello - Yarn Dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Feature] %s %f hello"}}, tech: []coreutils.Technology{coreutils.Yarn, coreutils.Go}, expected: "[Feature] hello - Yarn,Go Dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Feature] %s %d hello"}}, tech: []coreutils.Technology{coreutils.Yarn, coreutils.Go, coreutils.Npm}, expected: "[Feature] hello - Yarn,Go,npm Dependencies"},
{gm: GitManager{customTemplates: CustomTemplates{pullRequestTitleTemplate: "[Feature] %s %d hello"}}, tech: []coreutils.Technology{}, expected: "[Feature] hello"},
}
for _, test := range testsCases {
t.Run(test.expected, func(t *testing.T) {
title := test.gm.GenerateAggregatedPullRequestTitle(test.tech)
assert.Equal(t, test.expected, title)
})
}
}