-
Notifications
You must be signed in to change notification settings - Fork 1
/
giturl.go
73 lines (62 loc) · 1.18 KB
/
giturl.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
package main
import (
"fmt"
"net/url"
"strconv"
"strings"
)
const (
GITHUB_HOST string = "github.com"
)
type gitURL interface {
Host() string
Owner() string
Repository() string
PullRequest() int
}
func newGitURL(v string) (gitURL, error) {
url, err := url.Parse(v)
if err != nil {
return nil, err
}
host := url.Hostname()
switch host {
case GITHUB_HOST:
return newGithubURL(url), nil
default:
// TODO: Support GitHub Enterprise. Currently GitHub is only supported.
return nil, fmt.Errorf("unsupported host: %s", host)
}
}
type githubURL struct {
host string
owner string
repository string
pullRequest int
}
func newGithubURL(url *url.URL) *githubURL {
var (
paths = strings.Split(url.Path[1:], "/")
owner = paths[0]
repository = paths[1]
number, _ = strconv.Atoi(paths[3])
)
return &githubURL{
host: GITHUB_HOST,
owner: owner,
repository: repository,
pullRequest: number,
}
}
func (g *githubURL) Host() string {
return g.host
}
func (g *githubURL) Owner() string {
return g.owner
}
func (g *githubURL) Repository() string {
return g.repository
}
func (g *githubURL) PullRequest() int {
return g.pullRequest
}