Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depetrify the cats. #10111

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions prow/plugins/cat/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
)

var (
match = regexp.MustCompile(`(?mi)^/meow( .+)?\s*$`)
match = regexp.MustCompile(`(?mi)^/meow(vie)?(?: (.+))?\s*$`)
meow = &realClowder{
url: "https://api.thecatapi.com/api/images/get?format=json&results_per_page=1",
}
Expand All @@ -57,11 +57,11 @@ func helpProvider(config *plugins.Configuration, enabledRepos []string) (*plugin
Description: "The cat plugin adds a cat image to an issue in response to the `/meow` command.",
}
pluginHelp.AddCommand(pluginhelp.Command{
Usage: "/meow",
Usage: "/meow(vie) [CATegory]",
Description: "Add a cat image to the issue",
Featured: false,
WhoCanUse: "Anyone",
Examples: []string{"/meow", "/meow caturday"},
Examples: []string{"/meow", "/meow caturday", "/meowvie clothes"},
})
return pluginHelp, nil
}
Expand All @@ -71,7 +71,7 @@ type githubClient interface {
}

type clowder interface {
readCat(string) (string, error)
readCat(string, bool) (string, error)
}

type realClowder struct {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (cr catResult) Format() (string, error) {
return fmt.Sprintf("[![cat image](%s)](%s)", img, src), nil
}

func (r *realClowder) Url(category string) string {
func (r *realClowder) Url(category string, movieCat bool) string {
r.lock.RLock()
defer r.lock.RUnlock()
uri := string(r.url)
Expand All @@ -136,11 +136,14 @@ func (r *realClowder) Url(category string) string {
if r.key != "" {
uri += "&api_key=" + url.QueryEscape(r.key)
}
if movieCat {
uri += "&mime_types=gif"
}
return uri
}

func (r *realClowder) readCat(category string) (string, error) {
uri := r.Url(category)
func (r *realClowder) readCat(category string, movieCat bool) (string, error) {
uri := r.Url(category, movieCat)
resp, err := http.Get(uri)
if err != nil {
return "", fmt.Errorf("could not read cat from %s: %v", uri, err)
Expand Down Expand Up @@ -191,20 +194,20 @@ func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent, c
return nil
}

category, movieCat, err := parseMatch(mat)
if err != nil {
return err
}

// Now that we know this is a relevant event we can set the key.
setKey()

category := mat[1]
if len(category) > 1 {
category = category[1:]
}

org := e.Repo.Owner.Login
repo := e.Repo.Name
number := e.Number

for i := 0; i < 3; i++ {
resp, err := c.readCat(category)
resp, err := c.readCat(category, movieCat)
if err != nil {
log.WithError(err).Error("Failed to get cat img")
continue
Expand All @@ -224,3 +227,13 @@ func handle(gc githubClient, log *logrus.Entry, e *github.GenericCommentEvent, c

return errors.New("could not find a valid cat image")
}

func parseMatch(mat []string) (string, bool, error) {
if len(mat) != 3 {
err := fmt.Errorf("expected 3 capture groups in regexp match, but got %d", len(mat))
return "", false, err
}
category := strings.TrimSpace(mat[2])
movieCat := len(mat[1]) > 0 // "vie" suffix is present.
return category, movieCat, nil
}
62 changes: 52 additions & 10 deletions prow/plugins/cat/cat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ type fakeClowder string

var human = flag.Bool("human", false, "Enable to run additional manual tests")
var category = flag.String("category", "", "Request a particular category if set")
var path = flag.String("key-path", "", "Path to api key if set")
var movieCat = flag.Bool("gif", false, "Specifically request a GIF image if set")
var keyPath = flag.String("key-path", "", "Path to api key if set")

func (c fakeClowder) readCat(category string) (string, error) {
func (c fakeClowder) readCat(category string, movieCat bool) (string, error) {
if category == "error" {
return "", errors.New(string(c))
}
Expand All @@ -50,11 +51,11 @@ func TestRealCat(t *testing.T) {
if !*human {
t.Skip("Real cats disabled for automation. Manual users can add --human [--category=foo]")
}
if *path != "" {
meow.setKey(*path, logrus.WithField("plugin", pluginName))
if *keyPath != "" {
meow.setKey(*keyPath, logrus.WithField("plugin", pluginName))
}

if cat, err := meow.readCat(*category); err != nil {
if cat, err := meow.readCat(*category, *movieCat); err != nil {
t.Errorf("Could not read cats from %#v: %v", meow, err)
} else {
fmt.Println(cat)
Expand All @@ -67,6 +68,7 @@ func TestUrl(t *testing.T) {
url string
category string
key string
movie bool
require []string
deny []string
}{
Expand All @@ -79,13 +81,28 @@ func TestUrl(t *testing.T) {
url: "http://foo",
key: "blah",
require: []string{"api_key=blah"},
deny: []string{"category="},
deny: []string{"category=", "mime_types=gif"},
},
{
name: "category",
url: "http://foo",
category: "bar",
require: []string{"category=bar"},
deny: []string{"api_key=", "mime_types=gif"},
},
{
name: "movie",
url: "http://foo",
movie: true,
require: []string{"mime_types=gif"},
deny: []string{"category=this", "api_key=that"},
},
{
name: "category and movie",
url: "http://foo",
category: "this",
movie: true,
require: []string{"mime_types=gif", "category=this", "&"},
deny: []string{"api_key="},
},
{
Expand All @@ -94,6 +111,15 @@ func TestUrl(t *testing.T) {
category: "this",
key: "that",
require: []string{"category=this", "api_key=that", "&"},
deny: []string{"mime_types=gif"},
},
{
name: "category, key, and movie",
url: "http://foo",
category: "this",
key: "that",
movie: true,
require: []string{"category=this", "api_key=that", "&", "mime_types=gif"},
},
}

Expand All @@ -102,7 +128,7 @@ func TestUrl(t *testing.T) {
url: tc.url,
key: tc.key,
}
url := rc.Url(tc.category)
url := rc.Url(tc.category, tc.movie)
for _, r := range tc.require {
if !strings.Contains(url, r) {
t.Errorf("%s: %s does not contain %s", tc.name, url, r)
Expand Down Expand Up @@ -275,7 +301,7 @@ Available variants:
// run test for each case
for _, testcase := range testcases {
fakemeow := &realClowder{url: ts.URL + testcase.path}
cat, err := fakemeow.readCat(*category)
cat, err := fakemeow.readCat(*category, *movieCat)
if testcase.valid && err != nil {
t.Errorf("For case %s, didn't expect error: %v", testcase.name, err)
} else if !testcase.valid && err == nil {
Expand All @@ -286,15 +312,15 @@ Available variants:
}

// fully test handling a comment
comment := "/meow"
comment := "/meowvie space"

e := &github.GenericCommentEvent{
Action: github.GenericCommentActionCreated,
Body: comment,
Number: 5,
IssueState: "open",
}
if err := handle(fc, logrus.WithField("plugin", pluginName), e, &realClowder{url: ts.URL}, func() {}); err != nil {
if err := handle(fc, logrus.WithField("plugin", pluginName), e, &realClowder{url: ts.URL + "/?format=json"}, func() {}); err != nil {
t.Errorf("didn't expect error: %v", err)
return
}
Expand Down Expand Up @@ -370,6 +396,22 @@ func TestCats(t *testing.T) {
shouldComment: true,
shouldError: true,
},
{
name: "movie cat",
state: "open",
action: github.GenericCommentActionCreated,
body: "/meowvie",
shouldComment: true,
shouldError: false,
},
{
name: "categorical movie cat",
state: "open",
action: github.GenericCommentActionCreated,
body: "/meowvie space",
shouldComment: true,
shouldError: false,
},
}
for _, tc := range testcases {
fc := &fakegithub.FakeClient{
Expand Down