github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/hook/events.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 hook
    18  
    19  import (
    20  	"github.com/sirupsen/logrus"
    21  
    22  	"k8s.io/test-infra/prow/github"
    23  	"k8s.io/test-infra/prow/plugins"
    24  )
    25  
    26  func (s *Server) handleReviewEvent(l *logrus.Entry, re github.ReviewEvent) {
    27  	l = l.WithFields(logrus.Fields{
    28  		"org":      re.Repo.Owner.Login,
    29  		"repo":     re.Repo.Name,
    30  		"pr":       re.PullRequest.Number,
    31  		"review":   re.Review.ID,
    32  		"reviewer": re.Review.User.Login,
    33  		"url":      re.Review.HTMLURL,
    34  	})
    35  	l.Infof("Review %s.", re.Action)
    36  	for p, h := range s.Plugins.ReviewEventHandlers(re.PullRequest.Base.Repo.Owner.Login, re.PullRequest.Base.Repo.Name) {
    37  		go func(p string, h plugins.ReviewEventHandler) {
    38  			pc := s.Plugins.PluginClient
    39  			pc.Logger = l.WithField("plugin", p)
    40  			pc.Config = s.ConfigAgent.Config()
    41  			pc.PluginConfig = s.Plugins.Config()
    42  			if err := h(pc, re); err != nil {
    43  				pc.Logger.WithError(err).Error("Error handling ReviewEvent.")
    44  			}
    45  		}(p, h)
    46  	}
    47  	action := genericCommentAction(string(re.Action))
    48  	if action == "" {
    49  		return
    50  	}
    51  	s.handleGenericComment(
    52  		l,
    53  		&github.GenericCommentEvent{
    54  			IsPR:        true,
    55  			Action:      action,
    56  			Body:        re.Review.Body,
    57  			HTMLURL:     re.Review.HTMLURL,
    58  			Number:      re.PullRequest.Number,
    59  			Repo:        re.Repo,
    60  			User:        re.Review.User,
    61  			IssueAuthor: re.PullRequest.User,
    62  			Assignees:   re.PullRequest.Assignees,
    63  			IssueState:  re.PullRequest.State,
    64  		},
    65  	)
    66  }
    67  
    68  func (s *Server) handleReviewCommentEvent(l *logrus.Entry, rce github.ReviewCommentEvent) {
    69  	l = l.WithFields(logrus.Fields{
    70  		"org":       rce.Repo.Owner.Login,
    71  		"repo":      rce.Repo.Name,
    72  		"pr":        rce.PullRequest.Number,
    73  		"review":    rce.Comment.ReviewID,
    74  		"commenter": rce.Comment.User.Login,
    75  		"url":       rce.Comment.HTMLURL,
    76  	})
    77  	l.Infof("Review comment %s.", rce.Action)
    78  	for p, h := range s.Plugins.ReviewCommentEventHandlers(rce.PullRequest.Base.Repo.Owner.Login, rce.PullRequest.Base.Repo.Name) {
    79  		go func(p string, h plugins.ReviewCommentEventHandler) {
    80  			pc := s.Plugins.PluginClient
    81  			pc.Logger = l.WithField("plugin", p)
    82  			pc.Config = s.ConfigAgent.Config()
    83  			pc.PluginConfig = s.Plugins.Config()
    84  			if err := h(pc, rce); err != nil {
    85  				pc.Logger.WithError(err).Error("Error handling ReviewCommentEvent.")
    86  			}
    87  		}(p, h)
    88  	}
    89  	action := genericCommentAction(string(rce.Action))
    90  	if action == "" {
    91  		return
    92  	}
    93  	s.handleGenericComment(
    94  		l,
    95  		&github.GenericCommentEvent{
    96  			IsPR:        true,
    97  			Action:      action,
    98  			Body:        rce.Comment.Body,
    99  			HTMLURL:     rce.Comment.HTMLURL,
   100  			Number:      rce.PullRequest.Number,
   101  			Repo:        rce.Repo,
   102  			User:        rce.Comment.User,
   103  			IssueAuthor: rce.PullRequest.User,
   104  			Assignees:   rce.PullRequest.Assignees,
   105  			IssueState:  rce.PullRequest.State,
   106  		},
   107  	)
   108  }
   109  
   110  func (s *Server) handlePullRequestEvent(l *logrus.Entry, pr github.PullRequestEvent) {
   111  	l = l.WithFields(logrus.Fields{
   112  		"org":    pr.Repo.Owner.Login,
   113  		"repo":   pr.Repo.Name,
   114  		"pr":     pr.Number,
   115  		"author": pr.PullRequest.User.Login,
   116  		"url":    pr.PullRequest.HTMLURL,
   117  	})
   118  	l.Infof("Pull request %s.", pr.Action)
   119  	for p, h := range s.Plugins.PullRequestHandlers(pr.PullRequest.Base.Repo.Owner.Login, pr.PullRequest.Base.Repo.Name) {
   120  		go func(p string, h plugins.PullRequestHandler) {
   121  			pc := s.Plugins.PluginClient
   122  			pc.Logger = l.WithField("plugin", p)
   123  			pc.Config = s.ConfigAgent.Config()
   124  			pc.PluginConfig = s.Plugins.Config()
   125  			if err := h(pc, pr); err != nil {
   126  				pc.Logger.WithError(err).Error("Error handling PullRequestEvent.")
   127  			}
   128  		}(p, h)
   129  	}
   130  	action := genericCommentAction(string(pr.Action))
   131  	if action == "" {
   132  		return
   133  	}
   134  	s.handleGenericComment(
   135  		l,
   136  		&github.GenericCommentEvent{
   137  			IsPR:        true,
   138  			Action:      action,
   139  			Body:        pr.PullRequest.Body,
   140  			HTMLURL:     pr.PullRequest.HTMLURL,
   141  			Number:      pr.PullRequest.Number,
   142  			Repo:        pr.Repo,
   143  			User:        pr.PullRequest.User,
   144  			IssueAuthor: pr.PullRequest.User,
   145  			Assignees:   pr.PullRequest.Assignees,
   146  			IssueState:  pr.PullRequest.State,
   147  		},
   148  	)
   149  }
   150  
   151  func (s *Server) handlePushEvent(l *logrus.Entry, pe github.PushEvent) {
   152  	l = l.WithFields(logrus.Fields{
   153  		"org":  pe.Repo.Owner.Name,
   154  		"repo": pe.Repo.Name,
   155  		"ref":  pe.Ref,
   156  		"head": pe.After,
   157  	})
   158  	l.Info("Push event.")
   159  	for p, h := range s.Plugins.PushEventHandlers(pe.Repo.Owner.Name, pe.Repo.Name) {
   160  		go func(p string, h plugins.PushEventHandler) {
   161  			pc := s.Plugins.PluginClient
   162  			pc.Logger = l.WithField("plugin", p)
   163  			pc.Config = s.ConfigAgent.Config()
   164  			pc.PluginConfig = s.Plugins.Config()
   165  			if err := h(pc, pe); err != nil {
   166  				pc.Logger.WithError(err).Error("Error handling PushEvent.")
   167  			}
   168  		}(p, h)
   169  	}
   170  }
   171  
   172  func (s *Server) handleIssueEvent(l *logrus.Entry, i github.IssueEvent) {
   173  	l = l.WithFields(logrus.Fields{
   174  		"org":    i.Repo.Owner.Login,
   175  		"repo":   i.Repo.Name,
   176  		"pr":     i.Issue.Number,
   177  		"author": i.Issue.User.Login,
   178  		"url":    i.Issue.HTMLURL,
   179  	})
   180  	l.Infof("Issue %s.", i.Action)
   181  	for p, h := range s.Plugins.IssueHandlers(i.Repo.Owner.Login, i.Repo.Name) {
   182  		go func(p string, h plugins.IssueHandler) {
   183  			pc := s.Plugins.PluginClient
   184  			pc.Logger = l.WithField("plugin", p)
   185  			pc.Config = s.ConfigAgent.Config()
   186  			pc.PluginConfig = s.Plugins.Config()
   187  			if err := h(pc, i); err != nil {
   188  				pc.Logger.WithError(err).Error("Error handleing IssueEvent.")
   189  			}
   190  		}(p, h)
   191  	}
   192  	action := genericCommentAction(string(i.Action))
   193  	if action == "" {
   194  		return
   195  	}
   196  	s.handleGenericComment(
   197  		l,
   198  		&github.GenericCommentEvent{
   199  			IsPR:        i.Issue.IsPullRequest(),
   200  			Action:      action,
   201  			Body:        i.Issue.Body,
   202  			HTMLURL:     i.Issue.HTMLURL,
   203  			Number:      i.Issue.Number,
   204  			Repo:        i.Repo,
   205  			User:        i.Issue.User,
   206  			IssueAuthor: i.Issue.User,
   207  			Assignees:   i.Issue.Assignees,
   208  			IssueState:  i.Issue.State,
   209  		},
   210  	)
   211  }
   212  
   213  func (s *Server) handleIssueCommentEvent(l *logrus.Entry, ic github.IssueCommentEvent) {
   214  	l = l.WithFields(logrus.Fields{
   215  		"org":    ic.Repo.Owner.Login,
   216  		"repo":   ic.Repo.Name,
   217  		"pr":     ic.Issue.Number,
   218  		"author": ic.Comment.User.Login,
   219  		"url":    ic.Comment.HTMLURL,
   220  	})
   221  	l.Infof("Issue comment %s.", ic.Action)
   222  	for p, h := range s.Plugins.IssueCommentHandlers(ic.Repo.Owner.Login, ic.Repo.Name) {
   223  		go func(p string, h plugins.IssueCommentHandler) {
   224  			pc := s.Plugins.PluginClient
   225  			pc.Logger = l.WithField("plugin", p)
   226  			pc.Config = s.ConfigAgent.Config()
   227  			pc.PluginConfig = s.Plugins.Config()
   228  			if err := h(pc, ic); err != nil {
   229  				pc.Logger.WithError(err).Error("Error handling IssueCommentEvent.")
   230  			}
   231  		}(p, h)
   232  	}
   233  	action := genericCommentAction(string(ic.Action))
   234  	if action == "" {
   235  		return
   236  	}
   237  	s.handleGenericComment(
   238  		l,
   239  		&github.GenericCommentEvent{
   240  			IsPR:        ic.Issue.IsPullRequest(),
   241  			Action:      action,
   242  			Body:        ic.Comment.Body,
   243  			HTMLURL:     ic.Comment.HTMLURL,
   244  			Number:      ic.Issue.Number,
   245  			Repo:        ic.Repo,
   246  			User:        ic.Comment.User,
   247  			IssueAuthor: ic.Issue.User,
   248  			Assignees:   ic.Issue.Assignees,
   249  			IssueState:  ic.Issue.State,
   250  		},
   251  	)
   252  }
   253  
   254  func (s *Server) handleStatusEvent(l *logrus.Entry, se github.StatusEvent) {
   255  	l = l.WithFields(logrus.Fields{
   256  		"org":     se.Repo.Owner.Login,
   257  		"repo":    se.Repo.Name,
   258  		"context": se.Context,
   259  		"sha":     se.SHA,
   260  		"state":   se.State,
   261  		"id":      se.ID,
   262  	})
   263  	l.Infof("Status description %s.", se.Description)
   264  	for p, h := range s.Plugins.StatusEventHandlers(se.Repo.Owner.Login, se.Repo.Name) {
   265  		go func(p string, h plugins.StatusEventHandler) {
   266  			pc := s.Plugins.PluginClient
   267  			pc.Logger = l.WithField("plugin", p)
   268  			pc.Config = s.ConfigAgent.Config()
   269  			pc.PluginConfig = s.Plugins.Config()
   270  			if err := h(pc, se); err != nil {
   271  				pc.Logger.WithError(err).Error("Error handling StatusEvent.")
   272  			}
   273  		}(p, h)
   274  	}
   275  }
   276  
   277  // genericCommentAction normalizes the action string to a GenericCommentEventAction or returns ""
   278  // if the action is unrelated to the comment text. (For example a PR 'label' action.)
   279  func genericCommentAction(action string) github.GenericCommentEventAction {
   280  	switch action {
   281  	case "created", "opened", "submitted":
   282  		return github.GenericCommentActionCreated
   283  	case "edited":
   284  		return github.GenericCommentActionEdited
   285  	case "deleted", "dismissed":
   286  		return github.GenericCommentActionDeleted
   287  	}
   288  	// The action is not related to the text body.
   289  	return ""
   290  }
   291  
   292  func (s *Server) handleGenericComment(l *logrus.Entry, ce *github.GenericCommentEvent) {
   293  	for p, h := range s.Plugins.GenericCommentHandlers(ce.Repo.Owner.Login, ce.Repo.Name) {
   294  		go func(p string, h plugins.GenericCommentHandler) {
   295  			pc := s.Plugins.PluginClient
   296  			pc.Logger = l.WithField("plugin", p)
   297  			pc.Config = s.ConfigAgent.Config()
   298  			pc.PluginConfig = s.Plugins.Config()
   299  			if err := h(pc, *ce); err != nil {
   300  				pc.Logger.WithError(err).Error("Error handling GenericCommentEvent.")
   301  			}
   302  		}(p, h)
   303  	}
   304  }