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