-
Notifications
You must be signed in to change notification settings - Fork 114
/
plugin.go
76 lines (70 loc) · 2.26 KB
/
plugin.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
package plugins
import (
"github.com/jenkins-x/lighthouse/pkg/pluginhelp"
"github.com/jenkins-x/lighthouse/pkg/scmprovider"
"k8s.io/apimachinery/pkg/util/sets"
)
// Plugin defines a plugin and its handlers
type Plugin struct {
Description string
ExcludedProviders sets.String
ConfigHelpProvider ConfigHelpProvider
IssueHandler IssueHandler
PullRequestHandler PullRequestHandler
PushEventHandler PushEventHandler
ReviewEventHandler ReviewEventHandler
StatusEventHandler StatusEventHandler
GenericCommentHandler GenericCommentHandler
Commands []Command
}
// InvokeCommandHandler calls InvokeHandler on all commands
func (plugin Plugin) InvokeCommandHandler(ce *scmprovider.GenericCommentEvent, handler func(CommandEventHandler, *scmprovider.GenericCommentEvent, CommandMatch) error) error {
for _, cmd := range plugin.Commands {
if err := cmd.InvokeCommandHandler(ce, handler); err != nil {
return err
}
}
return nil
}
// GetHelp returns plugin help
func (plugin Plugin) GetHelp(config *Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) {
var err error
h := &pluginhelp.PluginHelp{
Description: plugin.Description,
Events: plugin.getEvents(),
ExcludedProviders: plugin.ExcludedProviders.List(),
}
if plugin.ConfigHelpProvider != nil {
h.Config, err = plugin.ConfigHelpProvider(config, enabledRepos)
}
for _, c := range plugin.Commands {
h.AddCommand(c.GetHelp())
}
return h, err
}
// IsProviderExcluded returns true if the given provider is excluded, false otherwise
func (plugin Plugin) IsProviderExcluded(provider string) bool {
return plugin.ExcludedProviders.Has(provider)
}
func (plugin Plugin) getEvents() []string {
var events []string
if plugin.IssueHandler != nil {
events = append(events, "issue")
}
if plugin.PullRequestHandler != nil {
events = append(events, "pull_request")
}
if plugin.PushEventHandler != nil {
events = append(events, "push")
}
if plugin.ReviewEventHandler != nil {
events = append(events, "pull_request_review")
}
if plugin.StatusEventHandler != nil {
events = append(events, "status")
}
if plugin.GenericCommentHandler != nil {
events = append(events, "GenericCommentEvent (any event for user text)")
}
return events
}