From 43bcef88920b4f36f0d987683993cb4a51b9d8c3 Mon Sep 17 00:00:00 2001 From: Ryota Ikezawa Date: Tue, 31 Jul 2018 15:55:02 +0900 Subject: [PATCH 1/5] Add handling of 503 --- lib/lgtm/plugin.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/lgtm/plugin.rb b/lib/lgtm/plugin.rb index 6b19d68..4246876 100644 --- a/lib/lgtm/plugin.rb +++ b/lib/lgtm/plugin.rb @@ -40,7 +40,9 @@ def check_lgtm(image_url: nil, https_image_only: false) private def fetch_image_url(https_image_only: false) - lgtm_post_url = process_request(RANDOM_LGTM_POST_URL)['location'] + lgtm_post_req = process_request(RANDOM_LGTM_POST_URL) + return if lgtm_post_req.code == '503' # returns "img tag src='#'" when Service Temporarily Unavailable; Over Quota. + lgtm_post_url = lgtm_post_req['location'] lgtm_post_response = process_request(lgtm_post_url) do |req| req['Accept'] = 'application/json' @@ -68,7 +70,7 @@ def process_request(url) end def markdown_template(image_url) - "

LGTM

" + "

LGTM

" end end end From 549a1f9f53de7bdf94268fe5a1b0127d663262c8 Mon Sep 17 00:00:00 2001 From: Ryota Ikezawa Date: Tue, 31 Jul 2018 16:23:48 +0900 Subject: [PATCH 2/5] Add spec --- spec/lgtm_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spec/lgtm_spec.rb b/spec/lgtm_spec.rb index e7a8177..0d472bf 100644 --- a/spec/lgtm_spec.rb +++ b/spec/lgtm_spec.rb @@ -25,13 +25,21 @@ module Danger expect(@dangerfile.status_report[:markdowns].length).to eq(1) end + it 'lgtm with default url is OverQuota' do + allow(Net::HTTP).to receive(:start).and_return(mock(code: '503')) + + expect(@dangerfile.status_report[:markdowns]).to be_empty + end + def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q', - actual_image_url: 'https://example.com/image.jpg') + actual_image_url: 'https://example.com/image.jpg', + code: '200') double( :[] => request_url, body: JSON.generate( actualImageUrl: actual_image_url - ) + ), + code: code ) end From 5797fd00e11ddee7f2dad1d9064cab9e45399fb2 Mon Sep 17 00:00:00 2001 From: Ryota Ikezawa Date: Tue, 31 Jul 2018 17:50:53 +0900 Subject: [PATCH 3/5] Fix up --- lib/lgtm/plugin.rb | 8 ++++++-- spec/lgtm_spec.rb | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/lgtm/plugin.rb b/lib/lgtm/plugin.rb index 4246876..875b60d 100644 --- a/lib/lgtm/plugin.rb +++ b/lib/lgtm/plugin.rb @@ -41,7 +41,7 @@ def check_lgtm(image_url: nil, https_image_only: false) def fetch_image_url(https_image_only: false) lgtm_post_req = process_request(RANDOM_LGTM_POST_URL) - return if lgtm_post_req.code == '503' # returns "img tag src='#'" when Service Temporarily Unavailable; Over Quota. + return if lgtm_post_req.code == '503' # returns "

LGTM

" when Service Temporarily Unavailable; Over Quota. lgtm_post_url = lgtm_post_req['location'] lgtm_post_response = process_request(lgtm_post_url) do |req| @@ -70,7 +70,11 @@ def process_request(url) end def markdown_template(image_url) - "

LGTM

" + if image_url.nil? + "

LGTM

" + else + "

LGTM

" + end end end end diff --git a/spec/lgtm_spec.rb b/spec/lgtm_spec.rb index 0d472bf..678625b 100644 --- a/spec/lgtm_spec.rb +++ b/spec/lgtm_spec.rb @@ -24,11 +24,11 @@ module Danger @lgtm.check_lgtm expect(@dangerfile.status_report[:markdowns].length).to eq(1) end - it 'lgtm with default url is OverQuota' do allow(Net::HTTP).to receive(:start).and_return(mock(code: '503')) - - expect(@dangerfile.status_report[:markdowns]).to be_empty + @lgtm.check_lgtm + expect(@dangerfile.status_report[:markdowns][0].message) + .to eq("

LGTM

") end def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q', From 35ceacac32866153ac085892d11370735bf9e76c Mon Sep 17 00:00:00 2001 From: Ryota Ikezawa Date: Tue, 31 Jul 2018 17:51:32 +0900 Subject: [PATCH 4/5] bump up --- Gemfile.lock | 4 ++-- lib/lgtm/gem_version.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 88857a0..ee8db6a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - danger-lgtm (1.0.1) + danger-lgtm (1.0.2) danger-plugin-api (~> 1.0) GEM @@ -19,7 +19,7 @@ GEM colored2 (3.1.2) cork (0.3.0) colored2 (~> 3.1) - danger (5.6.2) + danger (5.6.4) claide (~> 1.0) claide-plugins (>= 0.9.2) colored2 (~> 3.1) diff --git a/lib/lgtm/gem_version.rb b/lib/lgtm/gem_version.rb index e26c979..2c6250d 100644 --- a/lib/lgtm/gem_version.rb +++ b/lib/lgtm/gem_version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Lgtm - VERSION = '1.0.1'.freeze + VERSION = '1.0.2'.freeze end From 1c19bb929864fe31c4eecaeacbe788898c9af2b1 Mon Sep 17 00:00:00 2001 From: Ryota Ikezawa Date: Tue, 31 Jul 2018 18:13:35 +0900 Subject: [PATCH 5/5] Pass travis.ci --- lib/lgtm/plugin.rb | 12 ++++++++---- spec/lgtm_spec.rb | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/lgtm/plugin.rb b/lib/lgtm/plugin.rb index 875b60d..a5f3bc4 100644 --- a/lib/lgtm/plugin.rb +++ b/lib/lgtm/plugin.rb @@ -39,11 +39,9 @@ def check_lgtm(image_url: nil, https_image_only: false) private + # returns "

LGTM

" when ServiceTemporarilyUnavailable. def fetch_image_url(https_image_only: false) - lgtm_post_req = process_request(RANDOM_LGTM_POST_URL) - return if lgtm_post_req.code == '503' # returns "

LGTM

" when Service Temporarily Unavailable; Over Quota. - lgtm_post_url = lgtm_post_req['location'] - + return unless lgtm_post_url lgtm_post_response = process_request(lgtm_post_url) do |req| req['Accept'] = 'application/json' end @@ -69,6 +67,12 @@ def process_request(url) end end + def lgtm_post_url + lgtm_post_req = process_request(RANDOM_LGTM_POST_URL) + return if lgtm_post_req.code == '503' + lgtm_post_req['location'] + end + def markdown_template(image_url) if image_url.nil? "

LGTM

" diff --git a/spec/lgtm_spec.rb b/spec/lgtm_spec.rb index 678625b..889beb2 100644 --- a/spec/lgtm_spec.rb +++ b/spec/lgtm_spec.rb @@ -24,16 +24,19 @@ module Danger @lgtm.check_lgtm expect(@dangerfile.status_report[:markdowns].length).to eq(1) end + it 'lgtm with default url is OverQuota' do allow(Net::HTTP).to receive(:start).and_return(mock(code: '503')) + @lgtm.check_lgtm + expect(@dangerfile.status_report[:markdowns][0].message) .to eq("

LGTM

") end def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q', actual_image_url: 'https://example.com/image.jpg', - code: '200') + code: '302') double( :[] => request_url, body: JSON.generate(