-
Notifications
You must be signed in to change notification settings - Fork 11
/
event.go
176 lines (146 loc) · 3.7 KB
/
event.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package pb
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"net/url"
"path/filepath"
"reflect"
"strings"
)
// EventID unique hash id for an event
type EventID [20]byte
// ComputeEventID compute the hash for a given list of strings.
func ComputeEventID(content ...string) EventID {
var id EventID
h := sha1.New()
h.Write([]byte(strings.Join(content, "|")))
copy(id[:], h.Sum(nil))
return id
}
// IsZero checks if EventID is empty
func (h EventID) IsZero() bool {
var empty EventID
return h == empty
}
func (h EventID) String() string {
return hex.EncodeToString(h[:])
}
// EventType supported event types
type EventType int
const (
_ EventType = iota
// PushEventType is an event type when a repository branch gets updated
PushEventType
// ReviewEventType is an event type for proposed changes like pull request
ReviewEventType
)
// ID honors the Event interface.
func (e *ReviewEvent) ID() EventID {
return ComputeEventID(e.Provider, e.InternalID, e.Head.Hash)
}
// Type honors the Event interface.
func (e *ReviewEvent) Type() EventType {
return ReviewEventType
}
// Revision honors the Event interface.
func (e *ReviewEvent) Revision() *CommitRevision {
return &e.CommitRevision
}
// Validate honors the Event interface.
func (e *ReviewEvent) Validate() error {
var zeroVal ReviewEvent
if reflect.DeepEqual(*e, zeroVal) {
return errors.New("this ReviewEvent event is empty")
}
return nil
}
// ID honors the Event interface.
func (e *PushEvent) ID() EventID {
return ComputeEventID(e.Provider, e.InternalID)
}
// Type honors the Event interface.
func (e *PushEvent) Type() EventType {
return PushEventType
}
// Revision honors the Event interface.
func (e *PushEvent) Revision() *CommitRevision {
return &e.CommitRevision
}
// Validate honors the Event interface.
func (e *PushEvent) Validate() error {
var zeroVal PushEvent
if reflect.DeepEqual(*e, zeroVal) {
return errors.New("this PushEvent event is empty")
}
return nil
}
// RepositoryInfo contains information about a repository
type RepositoryInfo struct {
CloneURL string
Host string
FullName string
Owner string
Name string
}
// list of hosts we allow when parse string into RepositoryInfo
var supportedHosts = map[string]bool{
"github.com": true,
"gitlab.com": true,
"bitbucket.org": true,
}
// ParseRepositoryInfo creates RepositoryInfo from a string
func ParseRepositoryInfo(input string) (*RepositoryInfo, error) {
u, err := url.Parse(input)
if err != nil {
return nil, err
}
if u.Scheme == "" {
return ParseRepositoryInfo("https://" + input)
}
if u.Scheme == "file" {
return &RepositoryInfo{
CloneURL: input,
FullName: u.Path,
Name: filepath.Base(u.Path),
}, nil
}
if u.Scheme != "https" {
return nil, fmt.Errorf("only https urls are supported")
}
if u.Host == "" {
return nil, fmt.Errorf("host can't be empty")
}
if _, ok := supportedHosts[u.Host]; !ok {
return nil, fmt.Errorf("host %s is not supported", u.Host)
}
fullName := strings.TrimSuffix(strings.Trim(u.Path, "/"), ".git")
parts := strings.Split(fullName, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("unsupported path %s", fullName)
}
if !strings.HasSuffix(u.Path, ".git") {
u.Path = u.Path + ".git"
}
return &RepositoryInfo{
CloneURL: u.String(),
Host: u.Host,
FullName: fullName,
Owner: parts[0],
Name: parts[1],
}, nil
}
// Repository returns the RepositoryInfo
func (e *ReferencePointer) Repository() *RepositoryInfo {
info, _ := ParseRepositoryInfo(e.InternalRepositoryURL)
return info
}
// Short is a short string representation of a ReferencePointer.
func (e *ReferencePointer) Short() string {
return fmt.Sprintf(
"%s@%s",
e.ReferenceName.Short(),
e.Hash[0:6],
)
}