-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathevent.go
More file actions
108 lines (87 loc) · 2.15 KB
/
event.go
File metadata and controls
108 lines (87 loc) · 2.15 KB
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
package pb
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"reflect"
"gopkg.in/sourcegraph/go-vcsurl.v1"
)
type EventID [20]byte
// ComputeEventID compute the hash for a given provider and content.
func ComputeEventID(provider, content string) EventID {
var id EventID
h := sha1.New()
h.Write([]byte(provider))
h.Write([]byte("|"))
h.Write([]byte(content))
copy(id[:], h.Sum(nil))
return id
}
func (h EventID) IsZero() bool {
var empty EventID
return h == empty
}
func (h EventID) String() string {
return hex.EncodeToString(h[:])
}
type EventType int
const (
_ EventType = iota
PushEventType
ReviewEventType
)
// ID honors the Event interface.
func (e *ReviewEvent) ID() EventID {
return ComputeEventID(e.Provider, e.InternalID)
}
// 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
}
type RepositoryInfo = vcsurl.RepoInfo //TODO(mcuadros): improve repository references
// Repository returns the RepositoryInfo
func (e *ReferencePointer) Repository() *RepositoryInfo {
info, _ := vcsurl.Parse(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],
)
}