/
utility.go
78 lines (73 loc) · 2.33 KB
/
utility.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
package v1
import (
v1 "github.com/klovercloud-ci-cd/integration-manager/core/v1"
"github.com/klovercloud-ci-cd/integration-manager/enums"
"log"
"strings"
)
func UrlFormatter(url string) string {
if strings.Contains(url, "/src/") {
user, repo := getUsernameAndRepoNameFromBitbucketRepositoryUrl(url)
url = "https://bitbucket.org/" + user + "/" + repo
} else if strings.Contains(url, "@bitbucket.org") {
user, repo := getUsernameAndRepoNameFromGithubRepositoryUrl(url)
url = "https://bitbucket.org/" + user + "/" + repo
} else {
user, repo := getUsernameAndRepoNameFromGithubRepositoryUrl(url)
url = "https://github.com/" + user + "/" + repo
}
return url
}
// branchExists returns boolean for branch existence
func BranchExists(steps []v1.Step, resourceRef string, gitType enums.REPOSITORY_TYPE) bool {
if gitType == enums.GITHUB {
for _, step := range steps {
if step.Type == enums.BUILD && step.Params[enums.ALLOWED_BRANCHES] != "" {
branch := strings.Split(resourceRef, "/")[2]
branches := strings.Split(step.Params[enums.ALLOWED_BRANCHES], ",")
for _, each := range branches {
if branch == each {
return true
}
}
log.Println("[Forbidden]: Branch wasn't matched!")
return false
}
}
return true
} else {
for _, step := range steps {
if step.Type == enums.BUILD && step.Params[enums.ALLOWED_BRANCHES] != "" {
branches := strings.Split(step.Params[enums.ALLOWED_BRANCHES], ",")
for _, each := range branches {
if resourceRef == each {
return true
}
}
log.Println("[Forbidden]: Branch wasn't matched!")
return false
}
}
return true
}
}
func getUsernameAndRepoNameFromGithubRepositoryUrl(url string) (username string, repoName string) {
trim := strings.TrimSuffix(url, ".git")
urlArray := strings.Split(trim, "/")
if len(urlArray) < 3 {
return "", ""
}
repositoryName := urlArray[len(urlArray)-1]
usernameOrorgName := urlArray[len(urlArray)-2]
return usernameOrorgName, repositoryName
}
func getUsernameAndRepoNameFromBitbucketRepositoryUrl(url string) (username string, repoName string) {
trim := strings.TrimSuffix(url, ".git")
urlArray := strings.Split(trim, "/")
if len(urlArray) < 3 {
return "", ""
}
repositoryName := urlArray[len(urlArray)-1]
usernameOrorgName := urlArray[len(urlArray)-2]
return usernameOrorgName, repositoryName
}