forked from jfrog/frogbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrationutils.go
121 lines (109 loc) · 3.56 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
package main
import (
"context"
"fmt"
"github.com/jfrog/frogbot/utils"
"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 (
repoOwner = "frogbot-test"
repoName = "integration"
issuesBranch = "issues-branch"
mainBranch = "main"
)
type IntegrationTestDetails struct {
RepoName string
RepoOwner string
GitToken string
GitCloneURL string
GitProvider string
PullRequestID string
CustomBranchName string
}
func buildGitManager(t *testing.T, testDetails *IntegrationTestDetails) *utils.GitManager {
gitManager, err := utils.NewGitManager().SetAuth("", testDetails.GitToken).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.GitBaseBranchEnv: mainBranch,
})
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)
require.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 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))
}
}
}