github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/trigger/trigger.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 trigger
    18  
    19  import (
    20  	"github.com/sirupsen/logrus"
    21  
    22  	"k8s.io/test-infra/prow/config"
    23  	"k8s.io/test-infra/prow/github"
    24  	"k8s.io/test-infra/prow/kube"
    25  	"k8s.io/test-infra/prow/plugins"
    26  )
    27  
    28  const (
    29  	pluginName = "trigger"
    30  	lgtmLabel  = "lgtm"
    31  )
    32  
    33  func init() {
    34  	plugins.RegisterIssueCommentHandler(pluginName, handleIssueComment)
    35  	plugins.RegisterPullRequestHandler(pluginName, handlePullRequest)
    36  	plugins.RegisterPushEventHandler(pluginName, handlePush)
    37  }
    38  
    39  type githubClient interface {
    40  	AddLabel(org, repo string, number int, label string) error
    41  	BotName() (string, error)
    42  	IsMember(org, user string) (bool, error)
    43  	GetPullRequest(org, repo string, number int) (*github.PullRequest, error)
    44  	GetRef(org, repo, ref string) (string, error)
    45  	CreateComment(owner, repo string, number int, comment string) error
    46  	ListIssueComments(owner, repo string, issue int) ([]github.IssueComment, error)
    47  	CreateStatus(owner, repo, ref string, status github.Status) error
    48  	GetCombinedStatus(org, repo, ref string) (*github.CombinedStatus, error)
    49  	GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
    50  	RemoveLabel(org, repo string, number int, label string) error
    51  }
    52  
    53  type kubeClient interface {
    54  	CreateProwJob(kube.ProwJob) (kube.ProwJob, error)
    55  }
    56  
    57  type client struct {
    58  	GitHubClient githubClient
    59  	KubeClient   kubeClient
    60  	Config       *config.Config
    61  	Logger       *logrus.Entry
    62  }
    63  
    64  func getClient(pc plugins.PluginClient) client {
    65  	return client{
    66  		GitHubClient: pc.GitHubClient,
    67  		Config:       pc.Config,
    68  		KubeClient:   pc.KubeClient,
    69  		Logger:       pc.Logger,
    70  	}
    71  }
    72  
    73  func handlePullRequest(pc plugins.PluginClient, pr github.PullRequestEvent) error {
    74  	org, repo := pr.PullRequest.Base.Repo.Owner.Login, pr.PullRequest.Base.Repo.Name
    75  	config := pc.PluginConfig.TriggerFor(org, repo)
    76  	var trustedOrg string
    77  	if config == nil || config.TrustedOrg == "" {
    78  		trustedOrg = org
    79  	} else {
    80  		trustedOrg = config.TrustedOrg
    81  	}
    82  	return handlePR(getClient(pc), trustedOrg, pr)
    83  }
    84  
    85  func handleIssueComment(pc plugins.PluginClient, ic github.IssueCommentEvent) error {
    86  	org, repo := ic.Repo.Owner.Login, ic.Repo.Name
    87  	config := pc.PluginConfig.TriggerFor(org, repo)
    88  	var trustedOrg string
    89  	if config == nil || config.TrustedOrg == "" {
    90  		trustedOrg = org
    91  	} else {
    92  		trustedOrg = config.TrustedOrg
    93  	}
    94  	return handleIC(getClient(pc), trustedOrg, ic)
    95  }
    96  
    97  func handlePush(pc plugins.PluginClient, pe github.PushEvent) error {
    98  	return handlePE(getClient(pc), pe)
    99  }