-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjenkins.go
More file actions
83 lines (73 loc) · 3.05 KB
/
jenkins.go
File metadata and controls
83 lines (73 loc) · 3.05 KB
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
package jenkins
import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/github"
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/gitlab"
"github.com/aquasec-com/go-environments/enums"
"github.com/aquasec-com/go-environments/environments/jenkins"
env_utils "github.com/aquasec-com/go-environments/environments/utils"
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter"
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/bitbucket"
bitbucket_server "github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/bitbucket-server"
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/utils"
bitbucketutils "github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/utils/bitbucket"
)
func NewJenkins(baseRef string) (commenter.Repository, error) {
cloneUrl, _ := utils.GetRepositoryCloneURL()
sanitizedCloneUrl := env_utils.StripCredentialsFromUrl(cloneUrl)
scmSource, scmApiUrl := jenkins.GetRepositorySource(sanitizedCloneUrl)
if _, exists := bitbucketutils.GetBitbucketPayload(); strings.Contains(cloneUrl, "bitbucket") || exists {
username, ok := os.LookupEnv("USERNAME")
if !ok {
return nil, fmt.Errorf("USERNAME env var is not set")
}
password, ok := os.LookupEnv("PASSWORD")
if !ok {
return nil, fmt.Errorf("PASSWORD env var is not set")
}
if strings.Contains(cloneUrl, "bitbucket.org") {
return bitbucket.CreateClient(username, password, bitbucketutils.GetPrId(), bitbucketutils.GetRepositoryName(cloneUrl))
} else { // bitbucket server
repoName := bitbucketutils.GetRepositoryName(cloneUrl)
project, repo := bitbucketutils.GetProjectAndRepo(repoName)
return bitbucket_server.NewBitbucketServer(scmApiUrl, username, password, bitbucketutils.GetPrId(), project, repo, baseRef)
}
} else if scmSource == enums.GithubServer || scmSource == enums.Github {
_, org, repoName, _, err := env_utils.ParseDataFromCloneUrl(cloneUrl, scmApiUrl, scmSource)
if err != nil {
return nil, fmt.Errorf("failed parsing url with error: %s", err.Error())
}
token := os.Getenv("GITHUB_TOKEN")
prNumber := os.Getenv("CHANGE_ID")
// for gh single jenkins pipeline
if prNumber == "" {
prNumber = os.Getenv("ghprbPullId")
}
prNumberInt, err := strconv.Atoi(prNumber)
if err != nil {
return nil, fmt.Errorf("failed converting prNumber to int, %s: %s", prNumber, err.Error())
}
if scmSource == enums.Github {
return github.NewGithub(
token,
org,
repoName,
prNumberInt)
} else { //github server
return github.NewGithubServer(scmApiUrl, token, org, repoName, prNumberInt)
}
} else if scmSource == enums.GitlabServer || scmSource == enums.Gitlab {
_, org, repoName, _, err := env_utils.ParseDataFromCloneUrl(cloneUrl, scmApiUrl, scmSource)
if err != nil {
return nil, fmt.Errorf("failed parsing url with error: %s", err.Error())
}
token := os.Getenv("GITLAB_TOKEN")
prNumber := os.Getenv("CHANGE_ID")
return gitlab.NewGitlab(token, scmApiUrl, url.PathEscape(fmt.Sprintf("%s/%s", org, repoName)), prNumber)
}
return nil, nil
}