-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
66 lines (55 loc) · 1.51 KB
/
github.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
package slinky
import (
"fmt"
"net/url"
"strings"
)
// GitHub User/Company: ^https://github\.com/[A-Za-z0-9\-]{1,39}$
func decodeGitHubURL(url *url.URL) (*URL, error) {
if url.Scheme == "http" {
url.Scheme = "https"
}
if url.Scheme != "https" {
return nil, fmt.Errorf("%w: invalid GitHub scheme", ErrInvalidURL)
}
switch {
case url.Host == "github.com":
path := strings.TrimSuffix(url.Path, "/")
if len(path) < 1 || path[0] != '/' {
return nil, fmt.Errorf("%w: invalid GitHub path", ErrInvalidURL)
}
username := strings.TrimPrefix(path, "/")
if strings.ContainsFunc(username, isNotGitHubHandleRune) {
return nil, fmt.Errorf("%w: invalid GitHub username", ErrInvalidURL)
}
return &URL{
Service: GitHub,
Type: "User",
ID: username,
Data: map[string]string{
"username": username,
},
URL: url,
}, nil
case strings.HasSuffix(url.Host, ".github.io"):
username := strings.TrimSuffix(url.Host, ".github.io")
if strings.ContainsFunc(username, isNotGitHubHandleRune) {
return nil, fmt.Errorf("%w: invalid GitHub username", ErrInvalidURL)
}
return &URL{
Service: GitHub,
Type: "User",
ID: username,
Data: map[string]string{
"username": username,
},
URL: url,
}, nil
default:
return nil, fmt.Errorf("%w: invalid GitHub host", ErrInvalidURL)
}
}
const githubHandleAlpha = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz0123456789-"
func isNotGitHubHandleRune(r rune) bool {
return !strings.ContainsRune(githubHandleAlpha, r)
}