...

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

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

     1  // Copyright 2020 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 common
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  // Trigger is a flag set that marks the types of GitHub events that could
    23  // change the value of a predicate or evaluation. It is used to optimize
    24  // evaluation by skipping unnecessary work.
    25  type Trigger uint32
    26  
    27  const (
    28  	TriggerCommit Trigger = 1 << iota
    29  	TriggerComment
    30  	TriggerReview
    31  	TriggerLabel
    32  	TriggerStatus
    33  	TriggerPullRequest
    34  
    35  	// TriggerStatic is a name for the empty trigger set and means the
    36  	// computation never needs updating.
    37  	TriggerStatic Trigger = 0
    38  
    39  	// TriggerAll is a name for the full trigger set and means the computation
    40  	// should update after any changes to the pull request.
    41  	TriggerAll Trigger = TriggerCommit | TriggerComment | TriggerReview | TriggerLabel | TriggerStatus | TriggerPullRequest
    42  )
    43  
    44  // this is a slice instead of a map so the flags are always in a fixed order
    45  var triggerStrings = []struct {
    46  	T Trigger
    47  	S string
    48  }{
    49  	{TriggerCommit, "Commit"},
    50  	{TriggerComment, "Comment"},
    51  	{TriggerReview, "Review"},
    52  	{TriggerLabel, "Label"},
    53  	{TriggerStatus, "Status"},
    54  	{TriggerPullRequest, "PullRequest"},
    55  }
    56  
    57  // Matches returns true if flag contains any of the flags of this trigger.
    58  func (t Trigger) Matches(flags Trigger) bool {
    59  	return t&flags > 0
    60  }
    61  
    62  func (t Trigger) String() string {
    63  	if t == TriggerStatic {
    64  		return "Trigger(0x0=Static)"
    65  	}
    66  
    67  	var s strings.Builder
    68  	fmt.Fprintf(&s, "Trigger(0x%x", uint32(t))
    69  
    70  	i := 0
    71  	for _, ts := range triggerStrings {
    72  		if t.Matches(ts.T) {
    73  			if i == 0 {
    74  				s.WriteString("=")
    75  			} else {
    76  				s.WriteString("|")
    77  			}
    78  			s.WriteString(ts.S)
    79  			i++
    80  		}
    81  	}
    82  	s.WriteString(")")
    83  	return s.String()
    84  }
    85  
    86  // Triggered defines the Trigger method, which returns a set of conditions when
    87  // the implementor should be updated or evaluated.
    88  type Triggered interface {
    89  	Trigger() Trigger
    90  }
    91  

View as plain text