-
Notifications
You must be signed in to change notification settings - Fork 787
/
git_url.go
273 lines (240 loc) · 6.93 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
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
package gits
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/jenkins-x/jx/v2/pkg/util"
)
const (
GitHubHost = "github.com"
GitHubURL = "https://github.com"
gitPrefix = "git@"
)
func (i *GitRepository) 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 *GitRepository) PullRequestURL(prName string) string {
return util.UrlJoin("https://"+i.Host, i.Organisation, i.Name, "pull", prName)
}
// HttpURL returns the URL to browse this repository in a web browser
func (i *GitRepository) 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 *GitRepository) 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 *GitRepository) 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
}
// URLWithoutUser returns the URL without any user/password
func (i *GitRepository) URLWithoutUser() string {
u := i.URL
if u != "" {
if strings.HasPrefix(u, gitPrefix) {
return u
}
u2, err := url.Parse(u)
if err == nil {
u2.User = nil
return u2.String()
}
}
host := i.Host
if !strings.Contains(host, ":/") {
host = "https://" + host
}
return host
}
func (i *GitRepository) 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 *GitRepository) 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) (*GitRepository, error) {
answer := GitRepository{
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, true)
}
// 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[len(arr)-1]
return &answer, nil
}
}
return nil, fmt.Errorf("Could not parse Git URL %s", text)
}
// ParseGitOrganizationURL attempts to parse the given text as a URL or git URL-like string to determine
// the protocol, host, organisation
func ParseGitOrganizationURL(text string) (*GitRepository, error) {
answer := GitRepository{
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, false)
}
// 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]
return &answer, nil
}
}
return nil, fmt.Errorf("could not parse Git URL %s", text)
}
func parsePath(path string, info *GitRepository, requireRepo bool) (*GitRepository, error) {
// This is necessary for Bitbucket Server in some cases.
trimPath := strings.TrimPrefix(path, "/scm")
// This is necessary for Bitbucket Server, EG: /projects/ORG/repos/NAME/pull-requests/1/overview
reOverview := regexp.MustCompile("/pull-requests/[0-9]+/overview$")
if reOverview.MatchString(trimPath) {
trimPath = strings.TrimSuffix(trimPath, "/overview")
}
// This is necessary for Bitbucket Server in other cases
trimPath = strings.Replace(trimPath, "/projects/", "/", 1)
trimPath = strings.Replace(trimPath, "/repos/", "/", 1)
re := regexp.MustCompile("/pull.*/[0-9]+$")
trimPath = re.ReplaceAllString(trimPath, "")
// Remove leading and trailing slashes so that splitting on "/" won't result
// in empty strings at the beginning & end of the array.
trimPath = strings.TrimPrefix(trimPath, "/")
trimPath = strings.TrimSuffix(trimPath, "/")
trimPath = strings.TrimSuffix(trimPath, ".git")
arr := strings.Split(trimPath, "/")
if len(arr) >= 2 {
// We're assuming the beginning of the path is of the form /<org>/<repo> or /<org>/<subgroup>/.../<repo>
info.Organisation = arr[0]
info.Project = arr[0]
info.Name = arr[len(arr)-1]
return info, nil
} else if len(arr) == 1 && !requireRepo {
// We're assuming the beginning of the path is of the form /<org>/<repo>
info.Organisation = arr[0]
info.Project = arr[0]
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 {
gitServiceUrl = strings.TrimSuffix(gitServiceUrl, "/")
switch gitServiceUrl {
case "http://github.com":
return KindGitHub
case "https://github.com":
return KindGitHub
case "https://gitlab.com":
return KindGitlab
case "http://bitbucket.org":
return KindBitBucketCloud
case BitbucketCloudURL:
return KindBitBucketCloud
case "http://fake.git", FakeGitURL:
return KindGitFake
default:
if strings.HasPrefix(gitServiceUrl, "https://github") {
return KindGitHub
}
return ""
}
}
// HttpCloneURL returns the HTTPS git URL this repository
func HttpCloneURL(repo *GitRepository, kind string) string {
if kind == KindBitBucketServer {
host := repo.Host
if !strings.Contains(host, ":/") {
host = "https://" + host
}
return util.UrlJoin(host, "scm", repo.Organisation, repo.Name) + ".git"
}
return repo.HttpsURL() + ".git"
}