github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/branchprotector/request.go (about)

     1  /*
     2  Copyright 2018 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 main
    18  
    19  import (
    20  	branchprotection "k8s.io/test-infra/prow/config"
    21  	"k8s.io/test-infra/prow/github"
    22  
    23  	"github.com/sirupsen/logrus"
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  )
    26  
    27  // makeRequest renders a branch protection policy into the corresponding GitHub api request.
    28  func makeRequest(policy branchprotection.Policy) github.BranchProtectionRequest {
    29  	return github.BranchProtectionRequest{
    30  		EnforceAdmins:              makeAdmins(policy.Admins),
    31  		RequiredPullRequestReviews: makeReviews(policy.RequiredPullRequestReviews),
    32  		RequiredStatusChecks:       makeChecks(policy.RequiredStatusChecks),
    33  		Restrictions:               makeRestrictions(policy.Restrictions),
    34  	}
    35  
    36  }
    37  
    38  // makeAdmins returns true iff *val == true, else nil
    39  func makeAdmins(val *bool) *bool {
    40  	if v := makeBool(val); v {
    41  		return &v
    42  	}
    43  	return nil
    44  }
    45  
    46  // makeBool returns true iff *val == true
    47  func makeBool(val *bool) bool {
    48  	return val != nil && *val
    49  }
    50  
    51  // makeChecks renders a ContextPolicy into the corresponding GitHub api object.
    52  //
    53  // Returns nil when input policy is nil.
    54  // Otherwise returns non-nil Contexts (empty if unset) and Strict iff Strict is true
    55  func makeChecks(cp *branchprotection.ContextPolicy) *github.RequiredStatusChecks {
    56  	if cp == nil {
    57  		return nil
    58  	}
    59  	return &github.RequiredStatusChecks{
    60  		Contexts: append([]string{}, sets.NewString(cp.Contexts...).List()...),
    61  		Strict:   makeBool(cp.Strict),
    62  	}
    63  }
    64  
    65  // makeRestrictions renders restrictions into the corresponding GitHub api object.
    66  //
    67  // Returns nil when input restrictions is nil.
    68  // Otherwise Teams and Users are both non-nil (empty list if unset)
    69  func makeRestrictions(rp *branchprotection.Restrictions) *github.Restrictions {
    70  	if rp == nil {
    71  		return nil
    72  	}
    73  	teams := append([]string{}, sets.NewString(rp.Teams...).List()...)
    74  	users := append([]string{}, sets.NewString(rp.Users...).List()...)
    75  	return &github.Restrictions{
    76  		Teams: &teams,
    77  		Users: &users,
    78  	}
    79  }
    80  
    81  // makeReviews renders review policy into the corresponding GitHub api object.
    82  //
    83  // Returns nil if the policy is nil, or approvals is nil or 0.
    84  func makeReviews(rp *branchprotection.ReviewPolicy) *github.RequiredPullRequestReviews {
    85  	switch {
    86  	case rp == nil:
    87  		return nil
    88  	case rp.Approvals == nil:
    89  		logrus.Warn("WARNING: required_pull_request_reviews policy does not specify required_approving_review_count, disabling")
    90  		return nil
    91  	case *rp.Approvals == 0:
    92  		return nil
    93  	}
    94  	rprr := github.RequiredPullRequestReviews{
    95  		DismissStaleReviews:          makeBool(rp.DismissStale),
    96  		RequireCodeOwnerReviews:      makeBool(rp.RequireOwners),
    97  		RequiredApprovingReviewCount: *rp.Approvals,
    98  	}
    99  	if rp.DismissalRestrictions != nil {
   100  		rprr.DismissalRestrictions = *makeRestrictions(rp.DismissalRestrictions)
   101  	}
   102  	return &rprr
   103  }