forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
129 lines (104 loc) · 3.43 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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package event
import (
"strings"
"time"
"github.com/google/go-github/github"
)
// Matcher is an interface to match an event
type Matcher interface {
Match(event *github.IssueEvent) bool
}
// Actor searches for a specific actor
type Actor string
// Match if the event is from the specified actor
func (a Actor) Match(event *github.IssueEvent) bool {
if event == nil || event.Actor == nil || event.Actor.Login == nil {
return false
}
return strings.ToLower(*event.Actor.Login) == strings.ToLower(string(a))
}
// AddLabel searches for "labeled" event.
type AddLabel struct{}
// Match if the event is of type "labeled"
func (a AddLabel) Match(event *github.IssueEvent) bool {
if event == nil || event.Event == nil {
return false
}
return *event.Event == "labeled"
}
// RemoveLabel searches for "unlabeled" event.
type RemoveLabel struct{}
// Match if the event is of type "unlabeled"
func (r RemoveLabel) Match(event *github.IssueEvent) bool {
if event == nil || event.Event == nil {
return false
}
return *event.Event == "unlabeled"
}
// LabelPrefix searches for event whose label starts with the string
type LabelPrefix string
// Match if the label starts with the string
func (l LabelPrefix) Match(event *github.IssueEvent) bool {
if event == nil || event.Label == nil || event.Label.Name == nil {
return false
}
return strings.HasPrefix(*event.Label.Name, string(l))
}
// LabelName searches for event whose label starts with the string
type LabelName string
// Match if the label is exactly provided string
func (l LabelName) Match(event *github.IssueEvent) bool {
if event == nil || event.Label == nil || event.Label.Name == nil {
return false
}
return *event.Label.Name == string(l)
}
// CreatedAfter looks for event created after time
type CreatedAfter time.Time
// Match if the event is after the time
func (c CreatedAfter) Match(event *github.IssueEvent) bool {
if event == nil || event.CreatedAt == nil {
return false
}
return event.CreatedAt.After(time.Time(c))
}
// CreatedBefore looks for event created before time
type CreatedBefore time.Time
// Match if the event is before the time
func (c CreatedBefore) Match(event *github.IssueEvent) bool {
if event == nil || event.CreatedAt == nil {
return false
}
return event.CreatedAt.Before(time.Time(c))
}
// MungeBotActor returns a matcher that checks if the event was completed by MungeBot
func MungeBotActor() Matcher {
return Actor("k8s-merge-robot")
}
// JenkinsBotActor returns a matcher that checks if the event was completed by JenkinsBot
func JenkinsBotActor() Matcher {
return Actor("k8s-bot")
}
// BotActor returns a matcher that checks if the event was done by either of the Bots
func BotActor() Matcher {
return Or([]Matcher{
MungeBotActor(),
JenkinsBotActor(),
})
}
// HumanActor returns a matcher that checks if the event was done by a Human (Not a Bot)
func HumanActor() Matcher {
return Not{BotActor()}
}