github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/comment_counter.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  	"regexp"
    21  
    22  	"github.com/spf13/cobra"
    23  	"k8s.io/test-infra/velodrome/sql"
    24  )
    25  
    26  // CommentCounterPlugin counts comments
    27  type CommentCounterPlugin struct {
    28  	matcher []*regexp.Regexp
    29  	pattern []string
    30  }
    31  
    32  var _ Plugin = &CommentCounterPlugin{}
    33  
    34  // AddFlags adds "comments" <comments> to the command help
    35  func (c *CommentCounterPlugin) AddFlags(cmd *cobra.Command) {
    36  	cmd.Flags().StringSliceVar(&c.pattern, "comments", []string{}, "Regexps to match comments")
    37  }
    38  
    39  // CheckFlags looks for comments matching regexes
    40  func (c *CommentCounterPlugin) CheckFlags() error {
    41  	for _, pattern := range c.pattern {
    42  		matcher, err := regexp.Compile(pattern)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		c.matcher = append(c.matcher, matcher)
    47  	}
    48  	return nil
    49  }
    50  
    51  // ReceiveIssue is needed to implement a Plugin
    52  func (CommentCounterPlugin) ReceiveIssue(issue sql.Issue) []Point {
    53  	return nil
    54  }
    55  
    56  // ReceiveIssueEvent is needed to implement a Plugin
    57  func (CommentCounterPlugin) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    58  	return nil
    59  }
    60  
    61  // ReceiveComment adds matching comments to InfluxDB
    62  func (c *CommentCounterPlugin) ReceiveComment(comment sql.Comment) []Point {
    63  	points := []Point{}
    64  	for _, matcher := range c.matcher {
    65  		if matcher.MatchString(comment.Body) {
    66  			points = append(points, Point{
    67  				Values: map[string]interface{}{
    68  					"comment": 1,
    69  				},
    70  				Date: comment.CommentCreatedAt,
    71  			})
    72  		}
    73  	}
    74  	return points
    75  }