github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/assign-fixes.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 "k8s.io/apimachinery/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 "github.com/golang/glog" 26 ) 27 28 // AssignFixesMunger will assign issues to users based on the config file 29 // provided by --assignfixes-config. 30 type AssignFixesMunger struct { 31 config *github.Config 32 features *features.Features 33 assignFixesReassign bool 34 } 35 36 func init() { 37 assignfixes := &AssignFixesMunger{} 38 RegisterMungerOrDie(assignfixes) 39 } 40 41 // Name is the name usable in --pr-mungers 42 func (a *AssignFixesMunger) Name() string { return "assign-fixes" } 43 44 // RequiredFeatures is a slice of 'features' that must be provided 45 func (a *AssignFixesMunger) RequiredFeatures() []string { return []string{} } 46 47 // Initialize will initialize the munger 48 func (a *AssignFixesMunger) Initialize(config *github.Config, features *features.Features) error { 49 a.features = features 50 a.config = config 51 return nil 52 } 53 54 // EachLoop is called at the start of every munge loop 55 func (a *AssignFixesMunger) EachLoop() error { return nil } 56 57 // RegisterOptions registers options for this munger; returns any that require a restart when changed. 58 func (a *AssignFixesMunger) RegisterOptions(opts *options.Options) sets.String { 59 opts.RegisterBool(&a.assignFixesReassign, "fixes-issue-reassign", false, "Assign fixes Issues even if they're already assigned") 60 return nil 61 } 62 63 // Munge is the workhorse the will actually make updates to the PR 64 func (a *AssignFixesMunger) Munge(obj *github.MungeObject) { 65 if !obj.IsPR() { 66 return 67 } 68 // we need the PR for the "User" (creator of the PR not the assignee) 69 pr, ok := obj.GetPR() 70 if !ok { 71 glog.Infof("Couldn't get PR %v", obj.Issue.Number) 72 return 73 } 74 prOwner := github.DescribeUser(pr.User) 75 76 issuesFixed := obj.GetPRFixesList() 77 if issuesFixed == nil { 78 return 79 } 80 for _, fixesNum := range issuesFixed { 81 // "issue" is the issue referenced by the "fixes #<num>" 82 issueObj, err := a.config.GetObject(fixesNum) 83 if err != nil { 84 glog.Infof("Couldn't get issue %v", fixesNum) 85 continue 86 } 87 issue := issueObj.Issue 88 if !a.assignFixesReassign && issue.Assignee != nil { 89 glog.V(6).Infof("skipping %v: reassign: %v assignee: %v", *issue.Number, a.assignFixesReassign, github.DescribeUser(issue.Assignee)) 90 continue 91 } 92 glog.Infof("Assigning %v to %v", *issue.Number, prOwner) 93 issueObj.AddAssignee(prOwner) 94 } 95 96 }