github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/comment/comment.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 comment
    18  
    19  import (
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/google/go-github/github"
    24  )
    25  
    26  // Comment is a struct that represents a generic text post on github.
    27  type Comment struct {
    28  	Body      *string
    29  	Author    *string
    30  	CreatedAt *time.Time
    31  	UpdatedAt *time.Time
    32  	HTMLURL   *string
    33  
    34  	Source interface{}
    35  }
    36  
    37  func FromIssueComment(ic *github.IssueComment) *Comment {
    38  	if ic == nil {
    39  		return nil
    40  	}
    41  	var login *string = nil
    42  	if ic.User != nil {
    43  		login = ic.User.Login
    44  	}
    45  	return &Comment{
    46  		Body:      ic.Body,
    47  		Author:    login,
    48  		CreatedAt: ic.CreatedAt,
    49  		UpdatedAt: ic.UpdatedAt,
    50  		HTMLURL:   ic.HTMLURL,
    51  		Source:    ic,
    52  	}
    53  }
    54  
    55  func FromIssueComments(ics []*github.IssueComment) []*Comment {
    56  	comments := []*Comment{}
    57  	for _, ic := range ics {
    58  		comments = append(comments, FromIssueComment(ic))
    59  	}
    60  	return comments
    61  }
    62  
    63  func FromReviewComment(rc *github.PullRequestComment) *Comment {
    64  	if rc == nil {
    65  		return nil
    66  	}
    67  	var login *string = nil
    68  	if rc.User != nil {
    69  		login = rc.User.Login
    70  	}
    71  	return &Comment{
    72  		Body:      rc.Body,
    73  		Author:    login,
    74  		CreatedAt: rc.CreatedAt,
    75  		UpdatedAt: rc.UpdatedAt,
    76  		HTMLURL:   rc.HTMLURL,
    77  		Source:    rc,
    78  	}
    79  }
    80  
    81  func FromReviewComments(rcs []*github.PullRequestComment) []*Comment {
    82  	comments := []*Comment{}
    83  	for _, rc := range rcs {
    84  		comments = append(comments, FromReviewComment(rc))
    85  	}
    86  	return comments
    87  }
    88  
    89  func FromReview(review *github.PullRequestReview) *Comment {
    90  	if review == nil {
    91  		return nil
    92  	}
    93  	var login *string = nil
    94  	if review.User != nil {
    95  		login = review.User.Login
    96  	}
    97  	return &Comment{
    98  		Body:      review.Body,
    99  		Author:    login,
   100  		CreatedAt: review.SubmittedAt,
   101  		UpdatedAt: review.SubmittedAt,
   102  		HTMLURL:   review.HTMLURL,
   103  		Source:    review,
   104  	}
   105  }
   106  
   107  func FromReviews(reviews []*github.PullRequestReview) []*Comment {
   108  	comments := []*Comment{}
   109  	for _, review := range reviews {
   110  		comments = append(comments, FromReview(review))
   111  	}
   112  	return comments
   113  }
   114  
   115  // Matcher is an interface to match a comment
   116  type Matcher interface {
   117  	Match(comment *Comment) bool
   118  }
   119  
   120  // CreatedAfter matches comments created after the time
   121  type CreatedAfter time.Time
   122  
   123  // Match returns true if the comment is created after the time
   124  func (c CreatedAfter) Match(comment *Comment) bool {
   125  	if comment == nil || comment.CreatedAt == nil {
   126  		return false
   127  	}
   128  	return comment.CreatedAt.After(time.Time(c))
   129  }
   130  
   131  // CreatedBefore matches comments created before the time
   132  type CreatedBefore time.Time
   133  
   134  // Match returns true if the comment is created before the time
   135  func (c CreatedBefore) Match(comment *Comment) bool {
   136  	if comment == nil || comment.CreatedAt == nil {
   137  		return false
   138  	}
   139  	return comment.CreatedAt.Before(time.Time(c))
   140  }
   141  
   142  // ValidAuthor validates that a comment has the author set
   143  type ValidAuthor struct{}
   144  
   145  // Match if the comment has a valid author
   146  func (ValidAuthor) Match(comment *Comment) bool {
   147  	return comment != nil && comment.Author != nil
   148  }
   149  
   150  // AuthorLogin matches comment made by this Author
   151  type AuthorLogin string
   152  
   153  // Match if the Author is a match (ignoring case)
   154  func (a AuthorLogin) Match(comment *Comment) bool {
   155  	if !(ValidAuthor{}).Match(comment) {
   156  		return false
   157  	}
   158  
   159  	return strings.ToLower(*comment.Author) == strings.ToLower(string(a))
   160  }
   161  
   162  // Author matches comment made by this github user.
   163  type Author github.User
   164  
   165  // Match if the Author is a match.
   166  func (a Author) Match(comment *Comment) bool {
   167  	if !(ValidAuthor{}).Match(comment) {
   168  		return false
   169  	}
   170  	return AuthorLogin(*a.Login).Match(comment)
   171  }