...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate/branch.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate

     1  // Copyright 2018 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package predicate
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    22  
    23  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    24  )
    25  
    26  type TargetsBranch struct {
    27  	Pattern common.Regexp `yaml:"pattern"`
    28  }
    29  
    30  var _ Predicate = &TargetsBranch{}
    31  
    32  func (pred *TargetsBranch) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
    33  	targetName, _ := prctx.Branches()
    34  	matches := pred.Pattern.Matches(targetName)
    35  
    36  	desc := ""
    37  	if !matches {
    38  		desc = fmt.Sprintf("Target branch %q does not match required pattern %q", targetName, pred.Pattern)
    39  	}
    40  
    41  	predicateResult := common.PredicateResult{
    42  		Satisfied:       matches,
    43  		Description:     desc,
    44  		Values:          []string{targetName},
    45  		ValuePhrase:     "target branches",
    46  		ConditionPhrase: "match the required pattern",
    47  		ConditionValues: []string{pred.Pattern.String()},
    48  	}
    49  
    50  	return &predicateResult, nil
    51  }
    52  
    53  func (pred *TargetsBranch) Trigger() common.Trigger {
    54  	return common.TriggerPullRequest
    55  }
    56  
    57  type FromBranch struct {
    58  	Pattern common.Regexp `yaml:"pattern"`
    59  }
    60  
    61  var _ Predicate = &FromBranch{}
    62  
    63  func (pred *FromBranch) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
    64  	_, sourceBranchName := prctx.Branches()
    65  	matches := pred.Pattern.Matches(sourceBranchName)
    66  
    67  	desc := ""
    68  	if !matches {
    69  		desc = fmt.Sprintf("Source branch %q does not match specified from_branch pattern %q", sourceBranchName, pred.Pattern)
    70  	}
    71  
    72  	predicateResult := common.PredicateResult{
    73  		Satisfied:       matches,
    74  		Description:     desc,
    75  		Values:          []string{sourceBranchName},
    76  		ValuePhrase:     "source branches",
    77  		ConditionPhrase: "match the required pattern",
    78  		ConditionValues: []string{pred.Pattern.String()},
    79  	}
    80  
    81  	return &predicateResult, nil
    82  }
    83  
    84  func (pred *FromBranch) Trigger() common.Trigger {
    85  	return common.TriggerStatic
    86  }
    87  

View as plain text