github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/cherrypick-label-unapproved.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 mungers
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/kubernetes/pkg/util/sets"
    23  	"k8s.io/test-infra/mungegithub/features"
    24  	"k8s.io/test-infra/mungegithub/github"
    25  	"k8s.io/test-infra/mungegithub/options"
    26  
    27  	"github.com/golang/glog"
    28  	githubapi "github.com/google/go-github/github"
    29  )
    30  
    31  const (
    32  	cherrypickUnapprovedLabel           = "do-not-merge/cherry-pick-not-approved"
    33  	deprecatedCherrypickUnapprovedLabel = "do-not-merge"
    34  	labelUnapprovedPicksName            = "label-unapproved-picks"
    35  	labelUnapprovedFormat               = "This PR is not for the master branch but does not have the `%s` label. Adding the `%s` label."
    36  )
    37  
    38  var (
    39  	labelUnapprovedBody           = fmt.Sprintf(labelUnapprovedFormat, cpApprovedLabel, cherrypickUnapprovedLabel)
    40  	deprecatedLabelUnapprovedBody = fmt.Sprintf(labelUnapprovedFormat, cpApprovedLabel, deprecatedCherrypickUnapprovedLabel)
    41  )
    42  
    43  // LabelUnapprovedPicks will add `do-not-merge` to PRs against a release branch which
    44  // do not have `cherrypick-approved`.
    45  type LabelUnapprovedPicks struct{}
    46  
    47  func init() {
    48  	l := LabelUnapprovedPicks{}
    49  	RegisterMungerOrDie(l)
    50  	RegisterStaleIssueComments(l)
    51  }
    52  
    53  // Name is the name usable in --pr-mungers
    54  func (LabelUnapprovedPicks) Name() string { return labelUnapprovedPicksName }
    55  
    56  // RequiredFeatures is a slice of 'features' that must be provided
    57  func (LabelUnapprovedPicks) RequiredFeatures() []string { return []string{} }
    58  
    59  // Initialize will initialize the munger
    60  func (LabelUnapprovedPicks) Initialize(config *github.Config, features *features.Features) error {
    61  	return nil
    62  }
    63  
    64  // EachLoop is called at the start of every munge loop
    65  func (LabelUnapprovedPicks) EachLoop() error { return nil }
    66  
    67  // RegisterOptions registers options for this munger; returns any that require a restart when changed.
    68  func (LabelUnapprovedPicks) RegisterOptions(opts *options.Options) sets.String { return nil }
    69  
    70  // Munge is the workhorse the will actually make updates to the PR
    71  func (LabelUnapprovedPicks) Munge(obj *github.MungeObject) {
    72  	if !obj.IsPR() {
    73  		return
    74  	}
    75  	//true=return
    76  	boolean, ok := obj.IsForBranch("master")
    77  	if !ok || boolean {
    78  		return
    79  	}
    80  
    81  	if obj.HasLabel(cpApprovedLabel) {
    82  		if obj.HasLabel(cherrypickUnapprovedLabel) {
    83  			obj.RemoveLabel(cherrypickUnapprovedLabel)
    84  		}
    85  		return
    86  	}
    87  
    88  	if obj.HasLabel(cherrypickUnapprovedLabel) {
    89  		return
    90  	}
    91  
    92  	obj.AddLabel(cherrypickUnapprovedLabel)
    93  
    94  	obj.WriteComment(labelUnapprovedBody)
    95  }
    96  
    97  func (LabelUnapprovedPicks) isStaleIssueComment(obj *github.MungeObject, comment *githubapi.IssueComment) bool {
    98  	if !obj.IsRobot(comment.User) {
    99  		return false
   100  	}
   101  	if *comment.Body != labelUnapprovedBody && *comment.Body != deprecatedLabelUnapprovedBody {
   102  		return false
   103  	}
   104  	stale := obj.HasLabel(cpApprovedLabel)
   105  	if stale {
   106  		glog.V(6).Infof("Found stale LabelUnapprovedPicks comment")
   107  	}
   108  	return stale
   109  }
   110  
   111  // StaleIssueComments returns a list of stale issue comments.
   112  func (l LabelUnapprovedPicks) StaleIssueComments(obj *github.MungeObject, comments []*githubapi.IssueComment) []*githubapi.IssueComment {
   113  	return forEachCommentTest(obj, comments, l.isStaleIssueComment)
   114  }