-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscuttlebutt.go
60 lines (50 loc) · 1.54 KB
/
scuttlebutt.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
package scuttlebutt
import (
"fmt"
"net/url"
"path"
"strings"
)
// Repository represents a code repository.
type Repository struct {
ID string
Description string
Language string
Notified bool
Messages []*Message
}
// Name returns the name of the repository.
func (r *Repository) Name() string { return path.Base(r.ID) }
// URL returns the URL for the repository.
func (r *Repository) URL() string { return "https://" + r.ID }
// Repositories represents a sortable list of repositories.
type Repositories []*Repository
func (p Repositories) Len() int { return len(p) }
func (p Repositories) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p Repositories) Less(i, j int) bool { return p[i].ID < p[j].ID }
// Message represents a message associated with a project and language.
type Message struct {
ID uint64
Text string
RepositoryID string
}
// Extracts the repository identifier from a given URL.
func ExtractRepositoryID(u *url.URL) (string, error) {
sections := strings.Split(path.Clean(u.Path), "/")
if len(sections) != 3 {
return "", fmt.Errorf("invalid section count: %d", len(sections))
}
host, username, repositoryName := u.Host, sections[1], sections[2]
// Validate host & username.
switch host {
case "github.com", "www.github.com":
default:
return "", fmt.Errorf("invalid host: %s", host)
}
switch username {
case "blog", "explore":
return "", fmt.Errorf("invalid username: %s", username)
}
// Rejoin sections and return.
return path.Join(host, username, repositoryName), nil
}