github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/type_filter_wrapper.go (about) 1 /* 2 Copyright 2016 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 "fmt" 21 22 "k8s.io/test-infra/velodrome/sql" 23 24 "github.com/spf13/cobra" 25 ) 26 27 // TypeFilterWrapperPlugin allows ignoring either PR or issues from processing 28 type TypeFilterWrapperPlugin struct { 29 pullRequests bool 30 issues bool 31 32 plugin Plugin 33 34 // List of issues that we should ignore 35 pass map[string]bool 36 } 37 38 var _ Plugin = &TypeFilterWrapperPlugin{} 39 40 // NewTypeFilterWrapperPlugin is the constructor of TypeFilterWrapperPlugin 41 func NewTypeFilterWrapperPlugin(plugin Plugin) *TypeFilterWrapperPlugin { 42 return &TypeFilterWrapperPlugin{ 43 plugin: plugin, 44 pass: map[string]bool{}, 45 } 46 } 47 48 // AddFlags adds "no-pull-requests" and "no-issues" to the command help 49 func (t *TypeFilterWrapperPlugin) AddFlags(cmd *cobra.Command) { 50 cmd.Flags().BoolVar(&t.pullRequests, "no-pull-requests", false, "Ignore pull-requests") 51 cmd.Flags().BoolVar(&t.issues, "no-issues", false, "Ignore issues") 52 } 53 54 // CheckFlags makes sure not both PR and issues are ignored 55 func (t *TypeFilterWrapperPlugin) CheckFlags() error { 56 if t.pullRequests && t.issues { 57 return fmt.Errorf( 58 "you can't ignore both pull-requests and issues") 59 } 60 return nil 61 } 62 63 // ReceiveIssue calls plugin.ReceiveIssue() if issues are not ignored 64 func (t *TypeFilterWrapperPlugin) ReceiveIssue(issue sql.Issue) []Point { 65 if issue.IsPR && t.pullRequests { 66 return nil 67 } else if !issue.IsPR && t.issues { 68 return nil 69 } else { 70 t.pass[issue.ID] = true 71 return t.plugin.ReceiveIssue(issue) 72 } 73 } 74 75 // ReceiveIssueEvent calls plugin.ReceiveIssueEvent() if issues are not ignored 76 func (t *TypeFilterWrapperPlugin) ReceiveIssueEvent(event sql.IssueEvent) []Point { 77 if !t.pass[event.IssueID] { 78 return nil 79 } 80 return t.plugin.ReceiveIssueEvent(event) 81 } 82 83 // ReceiveComment calls plugin.ReceiveComment() if issues are not ignored 84 func (t *TypeFilterWrapperPlugin) ReceiveComment(comment sql.Comment) []Point { 85 if !t.pass[comment.IssueID] { 86 return nil 87 } 88 return t.plugin.ReceiveComment(comment) 89 }