From 4ebeb6fa7deb9671a869760337fe75e2b9d0e489 Mon Sep 17 00:00:00 2001 From: micnncim Date: Sat, 14 Dec 2019 15:30:51 +0900 Subject: [PATCH] Fix LGTM.app bug (#32) * Update README.md * Fix bug in LGTM.app * Fix README.md --- README.md | 2 +- pkg/lgtm/lgtmapp/lgtmapp.go | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3cd8923..710cd59 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ![](docs/assets/screen-record.gif) -Send LGTM reaction as image or GIF when we say `lgtm`. +Send LGTM reaction as image when we say `lgtm`. Currently supports [LGTM.app](https://www.lgtm.app) and [GIPHY](https://giphy.com). diff --git a/pkg/lgtm/lgtmapp/lgtmapp.go b/pkg/lgtm/lgtmapp/lgtmapp.go index ac1c133..2408a16 100644 --- a/pkg/lgtm/lgtmapp/lgtmapp.go +++ b/pkg/lgtm/lgtmapp/lgtmapp.go @@ -37,6 +37,14 @@ func NewClient() (*client, error) { } func (c *client) GetRandom() (string, error) { + imageURL, err := c.getRandomImageURL() + if err != nil { + return "", nil + } + return lgtm.MarkdownStyle(imageURL), nil +} + +func (c *client) getRandomImageURL() (string, error) { log := c.log req, err := http.NewRequest(http.MethodGet, randomURL, nil) @@ -50,9 +58,22 @@ func (c *client) GetRandom() (string, error) { return "", nil } + // strip image url from lgtm.app data url. + // e.g.) https://www.lgtm.app/i/4F5vFPNW3 -> https://www.lgtm.app/p/4F5vFPNW3 redirectedURL := resp.Request.URL.String() - slugs := strings.Split(redirectedURL, "/") - id := slugs[len(slugs)-1] - imageURL := fmt.Sprintf(imageURLFormat, id) - return lgtm.MarkdownStyle(imageURL), nil + s := strings.Split(redirectedURL, "/") + id := s[len(s)-1] + u := fmt.Sprintf(imageURLFormat, id) + + req, err = http.NewRequest(http.MethodGet, u, nil) + if err != nil { + log.Error("unable to create new http request", zap.Error(err)) + return "", nil + } + resp, err = c.httpClient.Do(req) + if err != nil { + log.Error("unable to do http request", zap.Error(err)) + return "", nil + } + return resp.Request.URL.String(), nil }