github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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  type CommentCounterPlugin struct {
    27  	matcher []*regexp.Regexp
    28  	pattern []string
    29  }
    30  
    31  var _ Plugin = &CommentCounterPlugin{}
    32  
    33  func (c *CommentCounterPlugin) AddFlags(cmd *cobra.Command) {
    34  	cmd.Flags().StringSliceVar(&c.pattern, "comments", []string{}, "Regexps to match comments")
    35  }
    36  
    37  func (c *CommentCounterPlugin) CheckFlags() error {
    38  	for _, pattern := range c.pattern {
    39  		matcher, err := regexp.Compile(pattern)
    40  		if err != nil {
    41  			return err
    42  		}
    43  		c.matcher = append(c.matcher, matcher)
    44  	}
    45  	return nil
    46  }
    47  
    48  func (CommentCounterPlugin) ReceiveIssue(issue sql.Issue) []Point {
    49  	return nil
    50  }
    51  
    52  func (CommentCounterPlugin) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    53  	return nil
    54  }
    55  
    56  func (c *CommentCounterPlugin) ReceiveComment(comment sql.Comment) []Point {
    57  	points := []Point{}
    58  	for _, matcher := range c.matcher {
    59  		if matcher.MatchString(comment.Body) {
    60  			points = append(points, Point{
    61  				Values: map[string]interface{}{
    62  					"comment": 1,
    63  				},
    64  				Date: comment.CommentCreatedAt,
    65  			})
    66  		}
    67  	}
    68  	return points
    69  }