github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/issue-categorizer.go (about)

     1  /*
     2  Copyright 2015 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 mungers
    18  
    19  import (
    20  	"io/ioutil"
    21  	"net/http"
    22  	"net/url"
    23  	"strings"
    24  
    25  	"k8s.io/kubernetes/pkg/util/sets"
    26  	"k8s.io/test-infra/mungegithub/features"
    27  	"k8s.io/test-infra/mungegithub/github"
    28  	"k8s.io/test-infra/mungegithub/options"
    29  
    30  	"github.com/golang/glog"
    31  )
    32  
    33  // LabelMunger will update a label on a PR based on how many lines are changed.
    34  // It will exclude certain files in it's calculations based on the config
    35  // file provided in --generated-files-config
    36  type LabelMunger struct {
    37  	triagerUrl string
    38  }
    39  
    40  // Initialize will initialize the munger
    41  func (*LabelMunger) Initialize(config *github.Config, features *features.Features) error {
    42  	return nil
    43  }
    44  
    45  // Name is the name usable in --pr-mungers
    46  func (*LabelMunger) Name() string { return "issue-triager" }
    47  
    48  // RequiredFeatures is a slice of 'features' that must be provided
    49  func (*LabelMunger) RequiredFeatures() []string { return []string{} }
    50  
    51  // RegisterOptions registers options for this munger; returns any that require a restart when changed.
    52  func (lm *LabelMunger) RegisterOptions(opts *options.Options) sets.String {
    53  	opts.RegisterString(&lm.triagerUrl, "triager-url", "", "Url on which ml web service is listening")
    54  	return nil
    55  }
    56  
    57  func init() {
    58  	RegisterMungerOrDie(&LabelMunger{})
    59  }
    60  
    61  // EachLoop is called at the start of every munge loop
    62  func (*LabelMunger) EachLoop() error { return nil }
    63  
    64  // Munge is the workhorse the will actually make updates to the PR
    65  func (lm *LabelMunger) Munge(obj *github.MungeObject) {
    66  	//this munger only works on issues
    67  	if obj.IsPR() {
    68  		return
    69  	}
    70  	if obj.HasLabel("kind/flake") {
    71  		return
    72  	}
    73  
    74  	tLabels := github.GetLabelsWithPrefix(obj.Issue.Labels, "team/")
    75  	cLabels := github.GetLabelsWithPrefix(obj.Issue.Labels, "component/")
    76  
    77  	if len(tLabels) == 0 && len(cLabels) == 0 {
    78  		obj.AddLabels(getRoutingLabels(lm.triagerUrl, obj.Issue.Title, obj.Issue.Body))
    79  	}
    80  }
    81  
    82  func getRoutingLabels(triagerUrl string, title, body *string) []string {
    83  	glog.Infof("Asking the server for labels: %v", triagerUrl)
    84  
    85  	if title == nil || body == nil {
    86  		glog.Warning("Title or Body cannot be nil")
    87  		return []string{}
    88  	}
    89  	routingLabelsToApply, err := http.PostForm(triagerUrl,
    90  		url.Values{"title": {*title}, "body": {*body}})
    91  
    92  	if err != nil {
    93  		glog.Error(err)
    94  		return []string{}
    95  	}
    96  	defer routingLabelsToApply.Body.Close()
    97  	response, err := ioutil.ReadAll(routingLabelsToApply.Body)
    98  	if err != nil {
    99  		glog.Error(err)
   100  		return []string{}
   101  	}
   102  	if routingLabelsToApply.StatusCode != 200 {
   103  		glog.Errorf("%d: %s", routingLabelsToApply.StatusCode, response)
   104  		return []string{}
   105  	}
   106  	return strings.Split(string(response), ",")
   107  }