github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/operators.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package matchers
    18  
    19  import "github.com/google/go-github/github"
    20  
    21  // True is a matcher that is always true
    22  type trueMatcher struct{}
    23  
    24  // True returns a matcher that is always true
    25  func True() Matcher {
    26  	return trueMatcher{}
    27  }
    28  
    29  // MatchEvent returns true no matter what
    30  func (t trueMatcher) MatchEvent(event *github.IssueEvent) bool {
    31  	return true
    32  }
    33  
    34  // MatchComment returns true no matter what
    35  func (t trueMatcher) MatchComment(comment *github.IssueComment) bool {
    36  	return true
    37  }
    38  
    39  // MatchReviewComment returns true no matter what
    40  func (t trueMatcher) MatchReviewComment(review *github.PullRequestComment) bool {
    41  	return true
    42  }
    43  
    44  // falseMatcher is a matcher that is always false
    45  type falseMatcher struct{}
    46  
    47  // False returns a matcher that is always false
    48  func False() Matcher {
    49  	return falseMatcher{}
    50  }
    51  
    52  // MatchEvent returns false no matter what
    53  func (t falseMatcher) MatchEvent(event *github.IssueEvent) bool {
    54  	return false
    55  }
    56  
    57  // MatchComment returns false no matter what
    58  func (t falseMatcher) MatchComment(comment *github.IssueComment) bool {
    59  	return false
    60  }
    61  
    62  // MatchReviewComment returns false no matter what
    63  func (t falseMatcher) MatchReviewComment(review *github.PullRequestComment) bool {
    64  	return false
    65  }
    66  
    67  // andMatcher makes sure that each match in the list matches (true if empty)
    68  type andMatcher []Matcher
    69  
    70  // And returns a matcher that verifies that all matchers match
    71  func And(matchers ...Matcher) Matcher {
    72  	and := andMatcher{}
    73  	for _, matcher := range matchers {
    74  		and = append(and, matcher)
    75  	}
    76  	return and
    77  }
    78  
    79  // MatchEvent returns true if all the matchers in the list matche
    80  func (a andMatcher) MatchEvent(event *github.IssueEvent) bool {
    81  	for _, matcher := range a {
    82  		if !matcher.MatchEvent(event) {
    83  			return false
    84  		}
    85  	}
    86  	return true
    87  }
    88  
    89  // MatchComment returns true if all the matchers in the list matche
    90  func (a andMatcher) MatchComment(comment *github.IssueComment) bool {
    91  	for _, matcher := range a {
    92  		if !matcher.MatchComment(comment) {
    93  			return false
    94  		}
    95  	}
    96  	return true
    97  }
    98  
    99  // MatchReviewComment returns true if all the matchers in the list matche
   100  func (a andMatcher) MatchReviewComment(review *github.PullRequestComment) bool {
   101  	for _, matcher := range a {
   102  		if !matcher.MatchReviewComment(review) {
   103  			return false
   104  		}
   105  	}
   106  	return true
   107  }
   108  
   109  // orMatcher makes sure that at least one element in the list matches (false if empty)
   110  type orMatcher []Matcher
   111  
   112  // Or returns a matcher that verifies that one of the matcher matches
   113  func Or(matchers ...Matcher) Matcher {
   114  	or := orMatcher{}
   115  	for _, matcher := range matchers {
   116  		or = append(or, matcher)
   117  	}
   118  	return or
   119  }
   120  
   121  // MatchEvent returns true if one of the matcher in the list matches
   122  func (o orMatcher) MatchEvent(event *github.IssueEvent) bool {
   123  	for _, matcher := range o {
   124  		if matcher.MatchEvent(event) {
   125  			return true
   126  		}
   127  	}
   128  	return false
   129  }
   130  
   131  // MatchComment returns true if one of the matcher in the list matches
   132  func (o orMatcher) MatchComment(comment *github.IssueComment) bool {
   133  	for _, matcher := range o {
   134  		if matcher.MatchComment(comment) {
   135  			return true
   136  		}
   137  	}
   138  	return false
   139  }
   140  
   141  // MatchReviewComment returns true no matter what
   142  func (o orMatcher) MatchReviewComment(review *github.PullRequestComment) bool {
   143  	for _, matcher := range o {
   144  		if matcher.MatchReviewComment(review) {
   145  			return true
   146  		}
   147  	}
   148  	return false
   149  }
   150  
   151  // Not reverses the effect of the matcher
   152  type notMatcher struct {
   153  	Matcher Matcher
   154  }
   155  
   156  func Not(matcher Matcher) Matcher {
   157  	return notMatcher{Matcher: matcher}
   158  }
   159  
   160  // MatchEvent returns true if the matcher would return false, and vice-versa
   161  func (n notMatcher) MatchEvent(event *github.IssueEvent) bool {
   162  	return !n.Matcher.MatchEvent(event)
   163  }
   164  
   165  // MatchComment returns true if the matcher would return false, and vice-versa
   166  func (n notMatcher) MatchComment(comment *github.IssueComment) bool {
   167  	return !n.Matcher.MatchComment(comment)
   168  }
   169  
   170  // MatchReviewComment returns true if the matcher would return false, and vice-versa
   171  func (n notMatcher) MatchReviewComment(review *github.PullRequestComment) bool {
   172  	return !n.Matcher.MatchReviewComment(review)
   173  }