forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_url.go
179 lines (157 loc) · 4.22 KB
/
git_url.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
package gits
import (
"fmt"
"net/url"
"strings"
"github.com/jenkins-x/jx/pkg/util"
)
const (
GitHubHost = "github.com"
GitHubURL = "https://github.com"
gitPrefix = "git@"
)
type GitRepositoryInfo struct {
URL string
Scheme string
Host string
Organisation string
Name string
Project string
}
func (i *GitRepositoryInfo) IsGitHub() bool {
return GitHubHost == i.Host || strings.HasSuffix(i.URL, "https://github.com")
}
// PullRequestURL returns the URL of a pull request of the given name/number
func (i *GitRepositoryInfo) PullRequestURL(prName string) string {
return util.UrlJoin("https://"+i.Host, i.Organisation, i.Name, "pull", prName)
}
// HttpCloneURL returns the HTTPS git URL this repository
func (i *GitRepositoryInfo) HttpCloneURL() string {
return i.HttpsURL() + ".git"
}
// HttpURL returns the URL to browse this repository in a web browser
func (i *GitRepositoryInfo) HttpURL() string {
host := i.Host
if !strings.Contains(host, ":/") {
host = "http://" + host
}
return util.UrlJoin(host, i.Organisation, i.Name)
}
// HttpsURL returns the URL to browse this repository in a web browser
func (i *GitRepositoryInfo) HttpsURL() string {
host := i.Host
if !strings.Contains(host, ":/") {
host = "https://" + host
}
return util.UrlJoin(host, i.Organisation, i.Name)
}
// HostURL returns the URL to the host
func (i *GitRepositoryInfo) HostURL() string {
answer := i.Host
if !strings.Contains(answer, ":/") {
// lets find the scheme from the URL
u := i.URL
if u != "" {
u2, err := url.Parse(u)
if err != nil {
// probably a git@ URL
return "https://" + answer
}
s := u2.Scheme
if s != "" {
if !strings.HasSuffix(s, "://") {
s += "://"
}
return s + answer
}
}
return "https://" + answer
}
return answer
}
func (i *GitRepositoryInfo) HostURLWithoutUser() string {
u := i.URL
if u != "" {
u2, err := url.Parse(u)
if err == nil {
u2.User = nil
u2.Path = ""
return u2.String()
}
}
host := i.Host
if !strings.Contains(host, ":/") {
host = "https://" + host
}
return host
}
// PipelinePath returns the pipeline path for the master branch which can be used to query
// pipeline logs in `jx get build logs myPipelinePath`
func (i *GitRepositoryInfo) PipelinePath() string {
return i.Organisation + "/" + i.Name + "/master"
}
// ParseGitURL attempts to parse the given text as a URL or git URL-like string to determine
// the protocol, host, organisation and name
func ParseGitURL(text string) (*GitRepositoryInfo, error) {
answer := GitRepositoryInfo{
URL: text,
}
u, err := url.Parse(text)
if err == nil && u != nil {
answer.Host = u.Host
// lets default to github
if answer.Host == "" {
answer.Host = GitHubHost
}
if answer.Scheme == "" {
answer.Scheme = "https"
}
answer.Scheme = u.Scheme
return parsePath(u.Path, &answer)
}
// handle git@ kinds of URIs
if strings.HasPrefix(text, gitPrefix) {
t := strings.TrimPrefix(text, gitPrefix)
t = strings.TrimPrefix(t, "/")
t = strings.TrimPrefix(t, "/")
t = strings.TrimSuffix(t, "/")
t = strings.TrimSuffix(t, ".git")
arr := util.RegexpSplit(t, ":|/")
if len(arr) >= 3 {
answer.Scheme = "git"
answer.Host = arr[0]
answer.Organisation = arr[1]
answer.Name = arr[2]
return &answer, nil
}
}
return nil, fmt.Errorf("Could not parse git url %s", text)
}
func parsePath(path string, info *GitRepositoryInfo) (*GitRepositoryInfo, error) {
trimPath := strings.TrimSuffix(path, "/")
trimPath = strings.TrimSuffix(trimPath, ".git")
arr := strings.Split(trimPath, "/")
arrayLength := len(arr)
if arrayLength >= 2 {
info.Organisation = arr[arrayLength-2]
info.Project = arr[arrayLength-2]
info.Name = arr[arrayLength-1]
return info, nil
}
return info, fmt.Errorf("Invalid path %s could not determine organisation and repository name", path)
}
// SaasGitKind returns the kind for SaaS git providers or "" if the URL could not be deduced
func SaasGitKind(gitServiceUrl string) string {
switch gitServiceUrl {
case "http://github.com":
return KindGitHub
case "https://github.com":
return KindGitHub
case "http://bitbucket.org":
return KindBitBucketCloud
case BitbucketCloudURL:
return KindBitBucketCloud
default:
return ""
}
}