github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/heart/heart.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 heart
    18  
    19  import (
    20  	"math/rand"
    21  	"path/filepath"
    22  	"regexp"
    23  
    24  	"github.com/sirupsen/logrus"
    25  
    26  	"k8s.io/test-infra/prow/github"
    27  	"k8s.io/test-infra/prow/plugins"
    28  )
    29  
    30  const (
    31  	pluginName     = "heart"
    32  	ownersFilename = "OWNERS"
    33  )
    34  
    35  var mergeRe = regexp.MustCompile(`Automatic merge from submit-queue`)
    36  
    37  var reactions = []string{
    38  	github.ReactionThumbsUp,
    39  	github.ReactionLaugh,
    40  	github.ReactionHeart,
    41  	github.ReactionHooray,
    42  }
    43  
    44  func init() {
    45  	plugins.RegisterIssueCommentHandler(pluginName, handleIssueComment)
    46  	plugins.RegisterPullRequestHandler(pluginName, handlePullRequest)
    47  }
    48  
    49  type githubClient interface {
    50  	CreateCommentReaction(org, repo string, ID int, reaction string) error
    51  	CreateIssueReaction(org, repo string, ID int, reaction string) error
    52  	GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
    53  }
    54  
    55  type client struct {
    56  	GitHubClient githubClient
    57  	Logger       *logrus.Entry
    58  }
    59  
    60  func getClient(pc plugins.PluginClient) client {
    61  	return client{
    62  		GitHubClient: pc.GitHubClient,
    63  		Logger:       pc.Logger,
    64  	}
    65  }
    66  
    67  func handleIssueComment(pc plugins.PluginClient, ic github.IssueCommentEvent) error {
    68  	return handleIC(getClient(pc), pc.PluginConfig.Heart.Adorees, ic)
    69  }
    70  
    71  func handlePullRequest(pc plugins.PluginClient, pre github.PullRequestEvent) error {
    72  	return handlePR(getClient(pc), pre)
    73  }
    74  
    75  func handleIC(c client, adorees []string, ic github.IssueCommentEvent) error {
    76  	// Only consider new comments on PRs.
    77  	if !ic.Issue.IsPullRequest() || ic.Action != github.IssueCommentActionCreated {
    78  		return nil
    79  	}
    80  	adoredLogin := false
    81  	for _, login := range adorees {
    82  		if ic.Comment.User.Login == login {
    83  			adoredLogin = true
    84  			break
    85  		}
    86  	}
    87  	if !adoredLogin {
    88  		return nil
    89  	}
    90  
    91  	if !mergeRe.MatchString(ic.Comment.Body) {
    92  		return nil
    93  	}
    94  
    95  	c.Logger.Info("This is a wonderful thing!")
    96  	return c.GitHubClient.CreateCommentReaction(
    97  		ic.Repo.Owner.Login,
    98  		ic.Repo.Name,
    99  		ic.Comment.ID,
   100  		reactions[rand.Intn(len(reactions))])
   101  }
   102  
   103  func handlePR(c client, pre github.PullRequestEvent) error {
   104  	// Only consider newly opened PRs
   105  	if pre.Action != github.PullRequestActionOpened {
   106  		return nil
   107  	}
   108  
   109  	org := pre.PullRequest.Base.Repo.Owner.Login
   110  	repo := pre.PullRequest.Base.Repo.Name
   111  
   112  	changes, err := c.GitHubClient.GetPullRequestChanges(org, repo, pre.PullRequest.Number)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	// Smile at any change that adds to OWNERS files
   118  	for _, change := range changes {
   119  		_, filename := filepath.Split(change.Filename)
   120  		if filename == ownersFilename && change.Additions > 0 {
   121  			c.Logger.Info("Adding new OWNERS makes me happy!")
   122  			return c.GitHubClient.CreateIssueReaction(
   123  				pre.PullRequest.Base.Repo.Owner.Login,
   124  				pre.PullRequest.Base.Repo.Name,
   125  				pre.Number,
   126  				reactions[rand.Intn(len(reactions))])
   127  		}
   128  	}
   129  
   130  	return nil
   131  }