github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/comment/notification.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  	"regexp"
    21  	"strings"
    22  
    23  	mgh "k8s.io/test-infra/mungegithub/github"
    24  )
    25  
    26  // Notification is a message sent by the bot. Easy to find and create.
    27  type Notification struct {
    28  	Name      string
    29  	Arguments string
    30  	Context   string
    31  }
    32  
    33  var (
    34  	// Matches a notification: [NOTIFNAME] Arguments\n\nContext
    35  	notificationRegex = regexp.MustCompile(`(?s)^\[([^\]\s]+)\] *?([^\n]*)(?:\n\n(.*))?`)
    36  )
    37  
    38  // ParseNotification attempts to read a notification from a comment
    39  // Returns nil if the comment doesn't contain a notification
    40  func ParseNotification(comment *Comment) *Notification {
    41  	if comment == nil || comment.Body == nil {
    42  		return nil
    43  	}
    44  
    45  	match := notificationRegex.FindStringSubmatch(*comment.Body)
    46  	if match == nil {
    47  		return nil
    48  	}
    49  
    50  	return NewNotification(match[1], match[2], match[3])
    51  }
    52  
    53  // NewNotification creates a new notification
    54  func NewNotification(name, arguments, context string) *Notification {
    55  	return &Notification{
    56  		Name:      strings.ToUpper(name),
    57  		Arguments: strings.TrimSpace(arguments),
    58  		Context:   strings.TrimSpace(context),
    59  	}
    60  }
    61  
    62  // String converts the notification
    63  func (n *Notification) String() string {
    64  	str := "[" + strings.ToUpper(n.Name) + "]"
    65  
    66  	args := strings.TrimSpace(n.Arguments)
    67  	if args != "" {
    68  		str += " " + args
    69  	}
    70  
    71  	context := strings.TrimSpace(n.Context)
    72  	if context != "" {
    73  		str += "\n\n" + context
    74  	}
    75  
    76  	return str
    77  }
    78  
    79  // Post a new notification on Github
    80  func (n Notification) Post(obj *mgh.MungeObject) error {
    81  	return obj.WriteComment(n.String())
    82  }
    83  
    84  // Equal compares this notification to the given notification for equivalence
    85  func (n *Notification) Equal(o *Notification) bool {
    86  	return (o != nil && n.Name == o.Name && n.Arguments == o.Arguments && n.Context == o.Context)
    87  }