...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate/status.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  	"strings"
    20  
    21  	"github.com/pkg/errors"
    22  
    23  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    24  
    25  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    26  )
    27  
    28  type HasSuccessfulStatus []string
    29  
    30  var _ Predicate = HasSuccessfulStatus([]string{})
    31  
    32  func (pred HasSuccessfulStatus) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
    33  	statuses, err := prctx.LatestStatuses()
    34  
    35  	predicateResult := common.PredicateResult{
    36  		ValuePhrase:     "status checks",
    37  		ConditionPhrase: "exist and pass",
    38  	}
    39  
    40  	if err != nil {
    41  		return nil, errors.Wrap(err, "failed to list commit statuses")
    42  	}
    43  
    44  	var missingResults []string
    45  	var failingStatuses []string
    46  	for _, status := range pred {
    47  		result, ok := statuses[status]
    48  		if !ok {
    49  			missingResults = append(missingResults, status)
    50  		}
    51  		if result != "success" {
    52  			failingStatuses = append(failingStatuses, status)
    53  		}
    54  	}
    55  
    56  	if len(missingResults) > 0 {
    57  		predicateResult.Values = missingResults
    58  		predicateResult.Description = "One or more statuses is missing: " + strings.Join(missingResults, ", ")
    59  		predicateResult.Satisfied = false
    60  		return &predicateResult, nil
    61  	}
    62  
    63  	if len(failingStatuses) > 0 {
    64  		predicateResult.Values = failingStatuses
    65  		predicateResult.Description = "One or more statuses has not passed: " + strings.Join(failingStatuses, ",")
    66  		predicateResult.Satisfied = false
    67  		return &predicateResult, nil
    68  	}
    69  
    70  	predicateResult.Values = pred
    71  	predicateResult.Satisfied = true
    72  	return &predicateResult, nil
    73  }
    74  
    75  func (pred HasSuccessfulStatus) Trigger() common.Trigger {
    76  	return common.TriggerStatus
    77  }
    78  

View as plain text