github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/mungegithub/features/branch-protection.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 features
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/util/sets"
    21  	"k8s.io/test-infra/mungegithub/github"
    22  	"k8s.io/test-infra/mungegithub/mungeopts"
    23  	"k8s.io/test-infra/mungegithub/options"
    24  )
    25  
    26  const (
    27  	// BranchProtectionFeature should update the branches with the required contexts
    28  	BranchProtectionFeature = "branch-protection"
    29  )
    30  
    31  // BranchProtection is a features that sets branches as protected
    32  type BranchProtection struct {
    33  	config *github.Config
    34  
    35  	branches      []string
    36  	extraContexts []string
    37  }
    38  
    39  func init() {
    40  	RegisterFeature(&BranchProtection{})
    41  }
    42  
    43  // Name is just going to return the name mungers use to request this feature
    44  func (bp *BranchProtection) Name() string {
    45  	return BranchProtectionFeature
    46  }
    47  
    48  // Initialize will initialize the feature.
    49  func (bp *BranchProtection) Initialize(config *github.Config) error {
    50  	bp.config = config
    51  	return nil
    52  }
    53  
    54  // EachLoop is called at the start of every munge loop
    55  func (bp *BranchProtection) EachLoop() error {
    56  	contexts := []string{}
    57  	contexts = append(contexts, bp.extraContexts...)
    58  	contexts = append(contexts, mungeopts.RequiredContexts.Merge...)
    59  	contexts = append(contexts, mungeopts.RequiredContexts.Retest...)
    60  
    61  	for _, branch := range bp.branches {
    62  		bp.config.SetBranchProtection(branch, contexts)
    63  	}
    64  	return nil
    65  }
    66  
    67  // RegisterOptions registers options for this feature; returns any that require a restart when changed.
    68  func (bp *BranchProtection) RegisterOptions(opts *options.Options) sets.String {
    69  	opts.RegisterStringSlice(&bp.branches, "protected-branches", []string{}, "branches to be marked 'protected'.  required-contexts, required-retest-contexts, and protected-branches-extra-contexts will be marked as required for non-admins")
    70  	opts.RegisterStringSlice(&bp.extraContexts, "protected-branches-extra-contexts", []string{}, "Contexts which will be marked as required in the Github UI but which the bot itself does not require")
    71  	return nil
    72  }