github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/owner-label.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 mungers
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/util/sets"
    21  	"k8s.io/test-infra/mungegithub/features"
    22  	"k8s.io/test-infra/mungegithub/github"
    23  	"k8s.io/test-infra/mungegithub/options"
    24  
    25  	githubapi "github.com/google/go-github/github"
    26  )
    27  
    28  // OwnerLabelMunger will label issues as specified in OWNERS files.
    29  type OwnerLabelMunger struct {
    30  	labeler fileLabeler
    31  }
    32  
    33  type fileLabeler interface {
    34  	AllPossibleOwnerLabels() sets.String
    35  	FindLabelsForPath(path string) sets.String
    36  }
    37  
    38  func init() {
    39  	ownerLabel := &OwnerLabelMunger{}
    40  	RegisterMungerOrDie(ownerLabel)
    41  }
    42  
    43  // Name is the name usable in --pr-mungers
    44  func (b *OwnerLabelMunger) Name() string { return "owner-label" }
    45  
    46  // RequiredFeatures is a slice of 'features' that must be provided
    47  func (b *OwnerLabelMunger) RequiredFeatures() []string {
    48  	return []string{features.RepoFeatureName}
    49  }
    50  
    51  // Initialize will initialize the munger
    52  func (b *OwnerLabelMunger) Initialize(config *github.Config, features *features.Features) error {
    53  	b.labeler = features.Repos
    54  	return nil
    55  }
    56  
    57  // EachLoop is called at the start of every munge loop
    58  func (b *OwnerLabelMunger) EachLoop() error { return nil }
    59  
    60  // RegisterOptions registers options for this munger; returns any that require a restart when changed.
    61  func (b *OwnerLabelMunger) RegisterOptions(opts *options.Options) sets.String { return nil }
    62  
    63  func (b *OwnerLabelMunger) getLabels(files []*githubapi.CommitFile) sets.String {
    64  	labels := sets.String{}
    65  	for _, file := range files {
    66  		if file == nil {
    67  			continue
    68  		}
    69  		if file.Changes == nil || *file.Changes == 0 {
    70  			continue
    71  		}
    72  		fileLabels := b.labeler.FindLabelsForPath(*file.Filename)
    73  		labels = labels.Union(fileLabels)
    74  	}
    75  	return labels
    76  }
    77  
    78  // Munge is the workhorse the will actually make updates to the PR
    79  func (b *OwnerLabelMunger) Munge(obj *github.MungeObject) {
    80  	if !obj.IsPR() {
    81  		return
    82  	}
    83  
    84  	files, ok := obj.ListFiles()
    85  	if !ok {
    86  		return
    87  	}
    88  
    89  	needsLabels := b.getLabels(files)
    90  
    91  	// TODO: make sure no other munger considers itself to own a label in
    92  	// AllPossibleOwnerLabels, and then pass that so that this will remove
    93  	// as well as add labels.
    94  	SyncLabels(needsLabels, needsLabels, obj)
    95  	// SyncLabels(b.labeler.AllPossibleOwnerLabels(), needsLabels, obj)
    96  }