forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodehost.go
78 lines (66 loc) · 2.15 KB
/
codehost.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 extsvc
import (
"net/url"
"strings"
"github.com/sourcegraph/sourcegraph/internal/api"
)
type CodeHost struct {
ServiceID string
ServiceType string
BaseURL *url.URL
}
// Known public code hosts and their URLs
var (
GitHubDotComURL = mustParseURL("https://github.com")
GitHubDotCom = NewCodeHost(GitHubDotComURL, "github")
GitLabDotComURL = mustParseURL("https://gitlab.com")
GitLabDotCom = NewCodeHost(GitLabDotComURL, "gitlab")
PublicCodeHosts = []*CodeHost{
GitHubDotCom,
GitLabDotCom,
}
)
func NewCodeHost(baseURL *url.URL, serviceType string) *CodeHost {
return &CodeHost{
ServiceID: NormalizeBaseURL(baseURL).String(),
ServiceType: serviceType,
BaseURL: baseURL,
}
}
// IsHostOfRepo returns true if the repository belongs to given code host.
func IsHostOfRepo(c *CodeHost, repo *api.ExternalRepoSpec) bool {
return c.ServiceID == repo.ServiceID && c.ServiceType == repo.ServiceType
}
// IsHostOfAccount returns true if the account belongs to given code host.
func IsHostOfAccount(c *CodeHost, account *Account) bool {
return c.ServiceID == account.ServiceID && c.ServiceType == account.ServiceType
}
// NormalizeBaseURL modifies the input and returns a normalized form of the a base URL with insignificant
// differences (such as in presence of a trailing slash, or hostname case) eliminated. Its return value should be
// used for the (ExternalRepoSpec).ServiceID field (and passed to XyzExternalRepoSpec) instead of a non-normalized
// base URL.
func NormalizeBaseURL(baseURL *url.URL) *url.URL {
baseURL.Host = strings.ToLower(baseURL.Host)
if !strings.HasSuffix(baseURL.Path, "/") {
baseURL.Path += "/"
}
return baseURL
}
// CodeHostOf returns the CodeHost of the given repo, if any, as
// determined by a common prefix between the repo name and the
// code hosts' URL hostname component.
func CodeHostOf(name api.RepoName, codehosts ...*CodeHost) *CodeHost {
for _, c := range codehosts {
if strings.HasPrefix(strings.ToLower(string(name)), c.BaseURL.Hostname()) {
return c
}
}
return nil
}
func mustParseURL(rawurl string) *url.URL {
u, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
return u
}