github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/needs_rebase.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  	"regexp"
    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  var (
    33  	rebaseRE = regexp.MustCompile(`@\S+ PR needs rebase`)
    34  )
    35  
    36  const needsRebase = "needs-rebase"
    37  
    38  // NeedsRebaseMunger will add the "needs-rebase" label to any issue which is
    39  // unable to be automatically merged
    40  type NeedsRebaseMunger struct{}
    41  
    42  const (
    43  	needsRebaseLabel = "needs-rebase"
    44  )
    45  
    46  func init() {
    47  	n := NeedsRebaseMunger{}
    48  	RegisterMungerOrDie(n)
    49  	RegisterStaleIssueComments(n)
    50  }
    51  
    52  // Name is the name usable in --pr-mungers
    53  func (NeedsRebaseMunger) Name() string { return "needs-rebase" }
    54  
    55  // RequiredFeatures is a slice of 'features' that must be provided
    56  func (NeedsRebaseMunger) RequiredFeatures() []string { return []string{} }
    57  
    58  // Initialize will initialize the munger
    59  func (NeedsRebaseMunger) Initialize(config *github.Config, features *features.Features) error {
    60  	return nil
    61  }
    62  
    63  // EachLoop is called at the start of every munge loop
    64  func (NeedsRebaseMunger) EachLoop() error { return nil }
    65  
    66  // RegisterOptions registers options for this munger; returns any that require a restart when changed.
    67  func (NeedsRebaseMunger) RegisterOptions(opts *options.Options) sets.String { return nil }
    68  
    69  // Munge is the workhorse the will actually make updates to the PR
    70  func (NeedsRebaseMunger) Munge(obj *github.MungeObject) {
    71  	if !obj.IsPR() {
    72  		return
    73  	}
    74  
    75  	mergeable, ok := obj.IsMergeable()
    76  	if !ok {
    77  		glog.V(2).Infof("Skipping %d - problem determining mergeable", *obj.Issue.Number)
    78  		return
    79  	}
    80  	if mergeable && obj.HasLabel(needsRebaseLabel) {
    81  		obj.RemoveLabel(needsRebaseLabel)
    82  	}
    83  	if !mergeable && !obj.HasLabel(needsRebaseLabel) {
    84  		obj.AddLabels([]string{needsRebaseLabel})
    85  
    86  		body := fmt.Sprintf("@%s PR needs rebase", *obj.Issue.User.Login)
    87  		if err := obj.WriteComment(body); err != nil {
    88  			return
    89  		}
    90  	}
    91  }
    92  
    93  func (NeedsRebaseMunger) isStaleIssueComment(obj *github.MungeObject, comment *githubapi.IssueComment) bool {
    94  	if !obj.IsRobot(comment.User) {
    95  		return false
    96  	}
    97  	if !rebaseRE.MatchString(*comment.Body) {
    98  		return false
    99  	}
   100  	stale := !obj.HasLabel(needsRebaseLabel)
   101  	if stale {
   102  		glog.V(6).Infof("Found stale NeedsRebaseMunger comment")
   103  	}
   104  	return stale
   105  }
   106  
   107  // StaleIssueComments returns a slice of stale issue comments.
   108  func (n NeedsRebaseMunger) StaleIssueComments(obj *github.MungeObject, comments []*githubapi.IssueComment) []*githubapi.IssueComment {
   109  	return forEachCommentTest(obj, comments, n.isStaleIssueComment)
   110  }