github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/author_filter_wrapper.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package plugins 18 19 import ( 20 "github.com/spf13/cobra" 21 "k8s.io/test-infra/velodrome/sql" 22 ) 23 24 // AuthorFilterPluginWrapper ignore comments and events from some authors 25 type AuthorFilterPluginWrapper struct { 26 ignoredAuthors []string 27 28 plugin Plugin 29 } 30 31 var _ Plugin = &AuthorFilterPluginWrapper{} 32 33 // NewAuthorFilterPluginWrapper is the constructor for AuthorFilterPluginWrapper 34 func NewAuthorFilterPluginWrapper(plugin Plugin) *AuthorFilterPluginWrapper { 35 return &AuthorFilterPluginWrapper{ 36 plugin: plugin, 37 } 38 } 39 40 // AddFlags adds "ignore-authors" <authors> to the command help 41 func (a *AuthorFilterPluginWrapper) AddFlags(cmd *cobra.Command) { 42 cmd.Flags().StringSliceVar(&a.ignoredAuthors, "ignore-authors", []string{}, "Name of people to ignore") 43 } 44 45 func (a *AuthorFilterPluginWrapper) match(author string) bool { 46 for _, ignored := range a.ignoredAuthors { 47 if author == ignored { 48 return true 49 } 50 } 51 return false 52 } 53 54 // ReceiveIssue calls plugin.ReceiveIssue() if the author is not filtered 55 func (a *AuthorFilterPluginWrapper) ReceiveIssue(issue sql.Issue) []Point { 56 if a.match(issue.User) { 57 return nil 58 } 59 return a.plugin.ReceiveIssue(issue) 60 } 61 62 // ReceiveIssueEvent calls plugin.ReceiveIssueEvent() if the author is not filtered 63 func (a *AuthorFilterPluginWrapper) ReceiveIssueEvent(event sql.IssueEvent) []Point { 64 if event.Actor != nil && a.match(*event.Actor) { 65 return nil 66 } 67 return a.plugin.ReceiveIssueEvent(event) 68 } 69 70 // ReceiveComment calls plugin.ReceiveComment() if the author is not filtered 71 func (a *AuthorFilterPluginWrapper) ReceiveComment(comment sql.Comment) []Point { 72 if a.match(comment.User) { 73 return nil 74 } 75 return a.plugin.ReceiveComment(comment) 76 }