...

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

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

     1  // Copyright 2021 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  
    20  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    21  
    22  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    23  )
    24  
    25  type Title struct {
    26  	Matches    []common.Regexp `yaml:"matches"`
    27  	NotMatches []common.Regexp `yaml:"not_matches"`
    28  }
    29  
    30  var _ Predicate = Title{}
    31  
    32  func (pred Title) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
    33  	title := prctx.Title()
    34  
    35  	predicateResult := common.PredicateResult{
    36  		ValuePhrase:     "titles",
    37  		Values:          []string{title},
    38  		ConditionPhrase: "meet the pattern requirement",
    39  	}
    40  
    41  	var matchPatterns, notMatchPatterns []string
    42  
    43  	for _, reg := range pred.Matches {
    44  		matchPatterns = append(matchPatterns, reg.String())
    45  	}
    46  
    47  	for _, reg := range pred.NotMatches {
    48  		notMatchPatterns = append(notMatchPatterns, reg.String())
    49  	}
    50  
    51  	if len(pred.Matches) > 0 {
    52  		if anyMatches(pred.Matches, title) {
    53  			predicateResult.ConditionsMap = map[string][]string{"match": matchPatterns}
    54  			predicateResult.Description = "PR Title matches a Match pattern"
    55  			predicateResult.Satisfied = true
    56  			return &predicateResult, nil
    57  		}
    58  	}
    59  
    60  	if len(pred.NotMatches) > 0 {
    61  		if !anyMatches(pred.NotMatches, title) {
    62  			predicateResult.ConditionsMap = map[string][]string{"not match": notMatchPatterns}
    63  			predicateResult.Description = "PR Title doesn't match a NotMatch pattern"
    64  			predicateResult.Satisfied = true
    65  			return &predicateResult, nil
    66  		}
    67  	}
    68  	predicateResult.Satisfied = false
    69  	predicateResult.ConditionsMap = map[string][]string{"match": matchPatterns, "not match": notMatchPatterns}
    70  	return &predicateResult, nil
    71  }
    72  
    73  func (pred Title) Trigger() common.Trigger {
    74  	return common.TriggerPullRequest
    75  }
    76  

View as plain text