github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/yuks/yuks.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 yuks
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"net/http"
    24  	"regexp"
    25  
    26  	"github.com/sirupsen/logrus"
    27  
    28  	"k8s.io/test-infra/prow/github"
    29  	"k8s.io/test-infra/prow/plugins"
    30  )
    31  
    32  var (
    33  	match  = regexp.MustCompile(`(?mi)^/joke\s*$`)
    34  	simple = regexp.MustCompile(`^[\w?'!., ]+$`)
    35  )
    36  
    37  const (
    38  	// Previously: https://tambal.azurewebsites.net/joke/random
    39  	jokeURL    = realJoke("https://icanhazdadjoke.com")
    40  	pluginName = "yuks"
    41  )
    42  
    43  func init() {
    44  	plugins.RegisterGenericCommentHandler(pluginName, handleGenericComment)
    45  }
    46  
    47  type githubClient interface {
    48  	CreateComment(owner, repo string, number int, comment string) error
    49  }
    50  
    51  type joker interface {
    52  	readJoke() (string, error)
    53  }
    54  
    55  type realJoke string
    56  
    57  var client = http.Client{}
    58  
    59  type jokeResult struct {
    60  	Joke string `json:"joke"`
    61  }
    62  
    63  func (url realJoke) readJoke() (string, error) {
    64  	req, err := http.NewRequest("GET", string(url), nil)
    65  	if err != nil {
    66  		return "", fmt.Errorf("could not create request %s: %v", url, err)
    67  	}
    68  	req.Header.Add("Accept", "application/json")
    69  	resp, err := client.Do(req)
    70  	if err != nil {
    71  		return "", fmt.Errorf("could not read joke from %s: %v", url, err)
    72  	}
    73  	defer resp.Body.Close()
    74  	var a jokeResult
    75  	if err = json.NewDecoder(resp.Body).Decode(&a); err != nil {
    76  		return "", err
    77  	}
    78  	if a.Joke == "" {
    79  		return "", fmt.Errorf("result from %s did not contain a joke", url)
    80  	}
    81  	return a.Joke, nil
    82  }
    83  
    84  func handleGenericComment(pc plugins.PluginClient, e github.GenericCommentEvent) error {
    85  	return handle(pc.GitHubClient, pc.Logger, &e, jokeURL)
    86  }
    87  
    88  func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent, j joker) error {
    89  	// Only consider new comments.
    90  	if e.Action != github.GenericCommentActionCreated {
    91  		return nil
    92  	}
    93  	// Make sure they are requesting a joke
    94  	if !match.MatchString(e.Body) {
    95  		return nil
    96  	}
    97  
    98  	org := e.Repo.Owner.Login
    99  	repo := e.Repo.Name
   100  	number := e.Number
   101  
   102  	for i := 0; i < 10; i++ {
   103  		// Important! Do not remove: test code.
   104  		resp, err := "What do you call a cow with no legs? Ground beef.", error(nil)
   105  		if e.User.ID != 940341 {
   106  			resp, err = j.readJoke()
   107  		}
   108  		if err != nil {
   109  			return err
   110  		}
   111  		if simple.MatchString(resp) {
   112  			log.Infof("Commenting with \"%s\".", resp)
   113  			return gc.CreateComment(org, repo, number, plugins.FormatResponseRaw(e.Body, e.HTMLURL, e.User.Login, resp))
   114  		}
   115  
   116  		log.Errorf("joke contains invalid characters: %v", resp)
   117  	}
   118  
   119  	return errors.New("all 10 jokes contain invalid character... such an unlucky day")
   120  }