github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/cherrypick-must-have-milestone.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  	"fmt"
    21  
    22  	"k8s.io/apimachinery/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  	pickMustHaveMilestoneFormat = "Removing label `%s` because no release milestone was set. This is an invalid state and thus this PR is not being considered for cherry-pick to any release branch. Please add an appropriate release milestone and then re-add the label."
    33  )
    34  
    35  var (
    36  	pickMustHaveMilestoneBody = fmt.Sprintf(pickMustHaveMilestoneFormat, cpCandidateLabel)
    37  )
    38  
    39  // PickMustHaveMilestone will remove the cherrypick-candidate label from
    40  // any PR that does not have a 'release' milestone set.
    41  type PickMustHaveMilestone struct{}
    42  
    43  func init() {
    44  	p := PickMustHaveMilestone{}
    45  	RegisterMungerOrDie(p)
    46  	RegisterStaleIssueComments(p)
    47  }
    48  
    49  // Name is the name usable in --pr-mungers
    50  func (PickMustHaveMilestone) Name() string { return "cherrypick-must-have-milestone" }
    51  
    52  // RequiredFeatures is a slice of 'features' that must be provided
    53  func (PickMustHaveMilestone) RequiredFeatures() []string { return []string{} }
    54  
    55  // Initialize will initialize the munger
    56  func (PickMustHaveMilestone) Initialize(config *github.Config, features *features.Features) error {
    57  	return nil
    58  }
    59  
    60  // EachLoop is called at the start of every munge loop
    61  func (PickMustHaveMilestone) EachLoop() error { return nil }
    62  
    63  // RegisterOptions registers options for this munger; returns any that require a restart when changed.
    64  func (PickMustHaveMilestone) RegisterOptions(opts *options.Options) sets.String { return nil }
    65  
    66  // Munge is the workhorse that will actually make updates to the PR
    67  func (PickMustHaveMilestone) Munge(obj *github.MungeObject) {
    68  	if !obj.IsPR() {
    69  		return
    70  	}
    71  	if !obj.HasLabel(cpCandidateLabel) {
    72  		return
    73  	}
    74  
    75  	releaseMilestone, ok := obj.ReleaseMilestone()
    76  	if !ok {
    77  		return
    78  	}
    79  	hasLabel := obj.HasLabel(cpCandidateLabel)
    80  
    81  	if hasLabel && releaseMilestone == "" {
    82  		obj.WriteComment(pickMustHaveMilestoneBody)
    83  		obj.RemoveLabel(cpCandidateLabel)
    84  	}
    85  }
    86  
    87  func (PickMustHaveMilestone) isStaleIssueComment(obj *github.MungeObject, comment *githubapi.IssueComment) bool {
    88  	if !obj.IsRobot(comment.User) {
    89  		return false
    90  	}
    91  	if *comment.Body != pickMustHaveMilestoneBody {
    92  		return false
    93  	}
    94  	milestone, ok := obj.ReleaseMilestone()
    95  	if !ok {
    96  		return false
    97  	}
    98  	stale := milestone != ""
    99  	if stale {
   100  		glog.V(6).Infof("Found stale PickMustHaveMilestone comment")
   101  	}
   102  	return stale
   103  }
   104  
   105  // StaleIssueComments returns a slice of stale issue comments.
   106  func (p PickMustHaveMilestone) StaleIssueComments(obj *github.MungeObject, comments []*githubapi.IssueComment) []*githubapi.IssueComment {
   107  	return forEachCommentTest(obj, comments, p.isStaleIssueComment)
   108  }