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