github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/shrug/shrug.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 shrug
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  
    23  	"github.com/sirupsen/logrus"
    24  
    25  	"k8s.io/test-infra/prow/github"
    26  	"k8s.io/test-infra/prow/plugins"
    27  )
    28  
    29  const pluginName = "shrug"
    30  
    31  var (
    32  	shrugLabel = "¯\\_(ツ)_/¯"
    33  	shrugRe    = regexp.MustCompile(`(?mi)^/shrug\s*$`)
    34  	unshrugRe  = regexp.MustCompile(`(?mi)^/unshrug\s*$`)
    35  )
    36  
    37  type event struct {
    38  	org           string
    39  	repo          string
    40  	number        int
    41  	prAuthor      string
    42  	commentAuthor string
    43  	body          string
    44  	assignees     []github.User
    45  	hasLabel      func(label string) (bool, error)
    46  	htmlurl       string
    47  }
    48  
    49  func init() {
    50  	plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment)
    51  }
    52  
    53  type githubClient interface {
    54  	AddLabel(owner, repo string, number int, label string) error
    55  	CreateComment(owner, repo string, number int, comment string) error
    56  	RemoveLabel(owner, repo string, number int, label string) error
    57  	GetIssueLabels(org, repo string, number int) ([]github.Label, error)
    58  }
    59  
    60  func handleGenericComment(pc plugins.PluginClient, e github.GenericCommentEvent) error {
    61  	return handle(pc.GitHubClient, pc.Logger, &e)
    62  }
    63  
    64  func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent) error {
    65  	if e.Action != github.GenericCommentActionCreated {
    66  		return nil
    67  	}
    68  
    69  	wantShrug := false
    70  	if shrugRe.MatchString(e.Body) {
    71  		wantShrug = true
    72  	} else if unshrugRe.MatchString(e.Body) {
    73  		wantShrug = false
    74  	} else {
    75  		return nil
    76  	}
    77  
    78  	org := e.Repo.Owner.Login
    79  	repo := e.Repo.Name
    80  
    81  	// Only add the label if it doesn't have it yet.
    82  	hasShrug := false
    83  	labels, err := gc.GetIssueLabels(org, repo, e.Number)
    84  	if err != nil {
    85  		log.WithError(err).Errorf("Failed to get the labels on %s/%s#%d.", org, repo, e.Number)
    86  	}
    87  	for _, candidate := range labels {
    88  		if candidate.Name == shrugLabel {
    89  			hasShrug = true
    90  			break
    91  		}
    92  	}
    93  	if hasShrug && !wantShrug {
    94  		log.Info("Removing Shrug label.")
    95  		resp := "¯\\\\\\_(ツ)\\_/¯"
    96  		log.Infof("Commenting with \"%s\".", resp)
    97  		if err := gc.CreateComment(org, repo, e.Number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, resp)); err != nil {
    98  			return fmt.Errorf("failed to comment on %s/%s#%d: %v", org, repo, e.Number, err)
    99  		}
   100  		return gc.RemoveLabel(org, repo, e.Number, shrugLabel)
   101  	} else if !hasShrug && wantShrug {
   102  		log.Info("Adding Shrug label.")
   103  		return gc.AddLabel(org, repo, e.Number, shrugLabel)
   104  	}
   105  	return nil
   106  }