github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/docs-no-retest.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  	"path"
    21  	"regexp"
    22  
    23  	githubapi "github.com/google/go-github/github"
    24  	"k8s.io/kubernetes/pkg/util/sets"
    25  	"k8s.io/test-infra/mungegithub/features"
    26  	"k8s.io/test-infra/mungegithub/github"
    27  	"k8s.io/test-infra/mungegithub/options"
    28  )
    29  
    30  const (
    31  	labelSkipRetest = "retest-not-required-docs-only"
    32  )
    33  
    34  var (
    35  	docFilesRegex    = regexp.MustCompile("^.*\\.(md|png|svg|dia)$")
    36  	ownersFilesRegex = regexp.MustCompile("^OWNERS$")
    37  )
    38  
    39  // DocsNeedNoRetest automatically labels documentation only pull-requests as retest-not-required
    40  type DocsNeedNoRetest struct{}
    41  
    42  func init() {
    43  	munger := &DocsNeedNoRetest{}
    44  	RegisterMungerOrDie(munger)
    45  }
    46  
    47  // Name is the name usable in --pr-mungers
    48  func (DocsNeedNoRetest) Name() string { return "docs-need-no-retest" }
    49  
    50  // RequiredFeatures is a slice of 'features' that must be provided
    51  func (DocsNeedNoRetest) RequiredFeatures() []string { return []string{} }
    52  
    53  // Initialize will initialize the munger
    54  func (s *DocsNeedNoRetest) Initialize(config *github.Config, features *features.Features) error {
    55  	return nil
    56  }
    57  
    58  // EachLoop is called at the start of every munge loop
    59  func (DocsNeedNoRetest) EachLoop() error { return nil }
    60  
    61  // RegisterOptions registers options for this munger; returns any that require a restart when changed.
    62  func (DocsNeedNoRetest) RegisterOptions(opts *options.Options) sets.String { return nil }
    63  
    64  func areFilesDocOnly(files []*githubapi.CommitFile) bool {
    65  	for _, file := range files {
    66  		_, basename := path.Split(*file.Filename)
    67  		if docFilesRegex.MatchString(basename) {
    68  			continue
    69  		}
    70  		if ownersFilesRegex.MatchString(basename) {
    71  			continue
    72  		}
    73  		return false
    74  	}
    75  	return true
    76  }
    77  
    78  // Munge is the workhorse the will actually make updates to the PR
    79  func (DocsNeedNoRetest) Munge(obj *github.MungeObject) {
    80  	if !obj.IsPR() {
    81  		return
    82  	}
    83  
    84  	files, ok := obj.ListFiles()
    85  	if !ok {
    86  		return
    87  	}
    88  
    89  	docsOnly := areFilesDocOnly(files)
    90  	if docsOnly && !obj.HasLabel(labelSkipRetest) {
    91  		obj.AddLabel(labelSkipRetest)
    92  	} else if !docsOnly && obj.HasLabel(labelSkipRetest) {
    93  		obj.RemoveLabel(labelSkipRetest)
    94  	}
    95  }