...

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

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

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

View as plain text