This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
event.go
99 lines (81 loc) · 1.92 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
package vcs
import (
"errors"
"fmt"
)
type EventType string
const (
EventTypePull EventType = "pull"
EventTypePush EventType = "push"
EventTypeTag EventType = "tag"
EventTypeInstallation EventType = "install" // github-app installation
)
type Action string
const (
ActionCreated Action = "created"
ActionDeleted Action = "deleted"
ActionMerged Action = "merged"
ActionUpdated Action = "updated"
)
// ErrIgnoreEvent informs an upstream vcs provider why an event it sent is
// ignored.
type ErrIgnoreEvent struct {
Reason string
}
func NewErrIgnoreEvent(msg string, args ...any) ErrIgnoreEvent {
return ErrIgnoreEvent{Reason: fmt.Sprintf(msg, args...)}
}
func (e ErrIgnoreEvent) Error() string {
return e.Reason
}
type (
// Event is a VCS event received from a cloud, e.g. a commit event from
// github
Event struct {
EventHeader
EventPayload
}
EventHeader struct {
VCSProviderID string
}
EventPayload struct {
RepoPath string
VCSKind Kind
Type EventType
Action Action
Tag string
CommitSHA string
CommitURL string
Branch string // head branch
DefaultBranch string
PullRequestNumber int
PullRequestURL string
PullRequestTitle string
SenderUsername string
SenderAvatarURL string
SenderHTMLURL string
// Paths of files that have been added/modified/removed. Only applicable
// to Push and Tag events types.
Paths []string
// Only set if event is from a github app
GithubAppInstallID *int64
}
)
func (e EventPayload) Validate() error {
if e.VCSKind == "" {
return errors.New("event missing vcs kind")
}
if e.Type == "" {
return errors.New("event missing event type")
}
if e.Action == "" {
return errors.New("event missing event action")
}
switch e.Type {
case EventTypePush, EventTypePull, EventTypeTag:
if e.RepoPath == "" {
return errors.New("event missing repo path")
}
}
return nil
}