-
Notifications
You must be signed in to change notification settings - Fork 73
/
repository.go
57 lines (52 loc) · 1.09 KB
/
repository.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
package main
import (
"net/url"
"strings"
)
type Repository struct {
User string
Name string
Path string
}
func NewRepositoryFromWebURL(u *url.URL) *Repository {
// TODO Check valid GitHub or GHE url
if u.Path == "" {
panic("Invalid https URL for GitHub repository: " + u.String())
}
split := strings.SplitN(u.Path[1:], "/", 2)
return &Repository{
split[0],
strings.TrimSuffix(split[1], ".git"),
GitRoot(),
}
}
func NewRepositoryFromSshURL(u string) *Repository {
if !strings.HasPrefix(u, "git@") || !strings.Contains(u, ":") {
panic("Invalid git@ URL for GitHub repository: " + u)
}
// TODO Check valid GitHub or GHE url
split := strings.SplitN(
strings.SplitN(u, ":", 2)[1],
"/",
2,
)
return &Repository{
split[0],
strings.TrimSuffix(split[1], ".git"),
GitRoot(),
}
}
func NewRepositoryFromURL(s string) *Repository {
u, err := url.Parse(s)
if err != nil {
return NewRepositoryFromSshURL(s)
}
switch u.Scheme {
case "https":
return NewRepositoryFromWebURL(u)
case "git":
return NewRepositoryFromWebURL(u)
default:
panic("Invalid URL for GitHub: " + s)
}
}