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

     1  /*
     2  Copyright 2017 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 (
    20  	"sort"
    21  	"time"
    22  
    23  	"github.com/google/go-github/github"
    24  )
    25  
    26  type Items []Item
    27  
    28  // AddComments returns a new list with the added comments
    29  func (i Items) AddComments(comments ...*github.IssueComment) Items {
    30  	for _, comment := range comments {
    31  		i = append(i, (*Comment)(comment))
    32  	}
    33  
    34  	sort.Sort(i)
    35  	return i
    36  }
    37  
    38  // AddEvents returns a new list with the added events
    39  func (i Items) AddEvents(events ...*github.IssueEvent) Items {
    40  	for _, event := range events {
    41  		i = append(i, (*Event)(event))
    42  	}
    43  
    44  	sort.Sort(i)
    45  	return i
    46  }
    47  
    48  // AddReviewComments returns a new list with the added review comments
    49  func (i Items) AddReviewComments(reviews ...*github.PullRequestComment) Items {
    50  	for _, review := range reviews {
    51  		i = append(i, (*ReviewComment)(review))
    52  	}
    53  
    54  	sort.Sort(i)
    55  	return i
    56  }
    57  
    58  // Events returns the events from the list
    59  func (i Items) Events() []*github.IssueEvent {
    60  	events := []*github.IssueEvent{}
    61  
    62  	for _, item := range i {
    63  		events = item.AppendEvent(events)
    64  	}
    65  
    66  	return events
    67  }
    68  
    69  // Comments returns the comments from the list
    70  func (i Items) Comments() []*github.IssueComment {
    71  	comments := []*github.IssueComment{}
    72  
    73  	for _, item := range i {
    74  		comments = item.AppendComment(comments)
    75  	}
    76  
    77  	return comments
    78  }
    79  
    80  // ReviewComments returns the review comments from the list
    81  func (i Items) ReviewComments() []*github.PullRequestComment {
    82  	reviews := []*github.PullRequestComment{}
    83  
    84  	for _, item := range i {
    85  		reviews = item.AppendReviewComment(reviews)
    86  	}
    87  
    88  	return reviews
    89  }
    90  
    91  // Swap two Items
    92  func (i Items) Swap(x, y int) {
    93  	i[x], i[y] = i[y], i[x]
    94  }
    95  
    96  // Less compares two Items
    97  func (i Items) Less(x, y int) bool {
    98  	if i[x].Date() == nil {
    99  		return true
   100  	} else if i[y].Date() == nil {
   101  		return false
   102  	}
   103  
   104  	return i[x].Date().Before(*i[y].Date())
   105  }
   106  
   107  // Len is the number of Items
   108  func (i Items) Len() int {
   109  	return len(i)
   110  }
   111  
   112  // Empty checks for emptiness
   113  func (i Items) IsEmpty() bool {
   114  	return len(i) == 0
   115  }
   116  
   117  // GetLast returns the last item from the list
   118  func (i Items) GetLast() Item {
   119  	return i[len(i)-1]
   120  }
   121  
   122  // GetLast returns the first item from the list
   123  func (i Items) GetFirst() Item {
   124  	return i[0]
   125  }
   126  
   127  // Filter will return the list of matching Items
   128  func (i Items) Filter(matcher Matcher) Items {
   129  	matches := Items{}
   130  
   131  	for _, item := range i {
   132  		if item.Match(matcher) {
   133  			matches = append(matches, item)
   134  		}
   135  	}
   136  
   137  	return matches
   138  }
   139  
   140  // LastDate returns the date of the last matching event, or deflt if no match
   141  func (i Items) LastDate(deflt *time.Time) *time.Time {
   142  	if i.IsEmpty() {
   143  		return deflt
   144  	}
   145  	return i.GetLast().Date()
   146  }
   147  
   148  // FirstDate returns the date of the first matching event, or deflt if no match
   149  func (i Items) FirstDate(deflt *time.Time) *time.Time {
   150  	if i.IsEmpty() {
   151  		return deflt
   152  	}
   153  	return i.GetFirst().Date()
   154  }