Skip to content

Commit 47b4572

Browse files
authored
Merge pull request #7 from paveg/http-status
Add 5xx and 4xx handling (Frequent errors handle)
2 parents 48056ae + 92c87c6 commit 47b4572

6 files changed

Lines changed: 103 additions & 60 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
danger-lgtm (1.0.2)
4+
danger-lgtm (1.0.3)
55
danger-plugin-api (~> 1.0)
66

77
GEM

lib/lgtm/error_handleable.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Lgtm
2+
# ErrorHandleable is module of error handling
3+
module ErrorHandleable
4+
# 4xx http status.
5+
CLIENT_ERRORS = [
6+
Net::HTTPBadRequest,
7+
Net::HTTPForbidden,
8+
Net::HTTPNotFound
9+
].freeze
10+
# 5xx http status.
11+
SERVER_ERRORS = [
12+
Net::HTTPInternalServerError,
13+
Net::HTTPBadGateway,
14+
Net::HTTPServiceUnavailable,
15+
Net::HTTPGatewayTimeOut
16+
].freeze
17+
18+
# validate_response is response validating
19+
#
20+
# @param [Net::HTTPxxx] response Net::HTTP responses
21+
# @raise ::Lgtm::Errors::UnexpectedError
22+
# @return [void]
23+
#
24+
def validate_response(response)
25+
case response
26+
when *SERVER_ERRORS
27+
raise ::Lgtm::Errors::UnexpectedError
28+
when *CLIENT_ERRORS
29+
raise ::Lgtm::Errors::UnexpectedError
30+
end
31+
end
32+
end
33+
end

lib/lgtm/errors.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lgtm
2+
class Errors
3+
class UnexpectedError < StandardError
4+
end
5+
end
6+
end

lib/lgtm/gem_version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Lgtm
4-
VERSION = '1.0.2'.freeze
4+
VERSION = '1.0.3'.freeze
55
end

lib/lgtm/plugin.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'uri'
44
require 'json'
55
require 'net/http'
6+
require './lib/lgtm/errors'
7+
require './lib/lgtm/error_handleable'
68

79
module Danger
810
# Lgtm let danger say lgtm when there is no violations.
@@ -16,13 +18,14 @@ module Danger
1618
# @tags lgtm, github
1719
#
1820
class DangerLgtm < Plugin
21+
include ::Lgtm::ErrorHandleable
1922
RANDOM_LGTM_POST_URL = 'https://lgtm.in/g'.freeze
2023

2124
# Check status report, say lgtm if no violations
2225
# Generates a `markdown` of a lgtm image.
2326
#
24-
# @param [image_url] lgtm image url
25-
# @param [https_image_only] fetching https image only if true
27+
# @param [String] image_url lgtm image url
28+
# @param [Boolean] https_image_only https image only if true
2629
#
2730
# @return [void]
2831
#
@@ -41,7 +44,6 @@ def check_lgtm(image_url: nil, https_image_only: false)
4144

4245
# returns "<h1 align="center">LGTM</h1>" when ServiceTemporarilyUnavailable.
4346
def fetch_image_url(https_image_only: false)
44-
return unless lgtm_post_url
4547
lgtm_post_response = process_request(lgtm_post_url) do |req|
4648
req['Accept'] = 'application/json'
4749
end
@@ -53,6 +55,7 @@ def fetch_image_url(https_image_only: false)
5355
return fetch_image_url(https_image_only: true)
5456
end
5557
url
58+
rescue ::Lgtm::Errors::UnexpectedError; nil
5659
end
5760

5861
def process_request(url)
@@ -68,9 +71,9 @@ def process_request(url)
6871
end
6972

7073
def lgtm_post_url
71-
lgtm_post_req = process_request(RANDOM_LGTM_POST_URL)
72-
return if lgtm_post_req.code == '503'
73-
lgtm_post_req['location']
74+
res = process_request(RANDOM_LGTM_POST_URL)
75+
validate_response(res)
76+
res['location']
7477
end
7578

7679
def markdown_template(image_url)

spec/lgtm_spec.rb

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,73 @@
22

33
require File.expand_path('spec_helper', __dir__)
44

5-
module Danger
6-
describe Danger::DangerLgtm do
7-
it 'should be a plugin' do
8-
expect(Danger::DangerLgtm.new(nil)).to be_a Danger::Plugin
9-
end
10-
11-
#
12-
# You should test your custom attributes and methods here
13-
#
14-
describe 'with Dangerfile' do
15-
before do
16-
@dangerfile = testing_dangerfile
17-
@lgtm = @dangerfile.lgtm
18-
end
5+
describe Danger::DangerLgtm do
6+
def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q',
7+
actual_image_url: 'https://example.com/image.jpg')
8+
double(
9+
:[] => request_url,
10+
body: JSON.generate(
11+
actualImageUrl: actual_image_url
12+
)
13+
)
14+
end
1915

20-
# Some examples for writing tests
21-
# You should replace these with your own.
16+
it 'should be a plugin' do
17+
expect(Danger::DangerLgtm.new(nil)).to be_a(Danger::Plugin)
18+
end
2219

23-
it 'lgtm with no violation' do
24-
@lgtm.check_lgtm
25-
expect(@dangerfile.status_report[:markdowns].length).to eq(1)
26-
end
20+
describe '#check_lgtm' do
21+
subject do
22+
lgtm.check_lgtm(
23+
https_image_only: https_image_only,
24+
image_url: image_url
25+
)
26+
end
2727

28-
it 'lgtm with default url is OverQuota' do
29-
allow(Net::HTTP).to receive(:start).and_return(mock(code: '503'))
28+
let(:dangerfile) { testing_dangerfile }
29+
let(:lgtm) { dangerfile.lgtm }
30+
let(:https_image_only) { false } # default false
31+
let(:image_url) {} # default nil
3032

31-
@lgtm.check_lgtm
33+
shared_examples 'returns correctly message' do
34+
it do
35+
allow(Net::HTTP).to receive(:start).and_return(mock)
36+
is_expected
3237

33-
expect(@dangerfile.status_report[:markdowns][0].message)
34-
.to eq("<h1 align='center'>LGTM</h1>")
38+
expect(dangerfile.status_report[:markdowns][0].message)
39+
.to match(expected_message)
3540
end
41+
end
3642

37-
def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q',
38-
actual_image_url: 'https://example.com/image.jpg',
39-
code: '302')
40-
double(
41-
:[] => request_url,
42-
body: JSON.generate(
43-
actualImageUrl: actual_image_url
44-
),
45-
code: code
46-
)
43+
context 'with Dangerfile' do
44+
it 'when no violation' do
45+
is_expected
46+
expect(dangerfile.status_report[:markdowns].length).to eq(1)
4747
end
4848

49-
it 'pick random pic from lgtm.in' do
50-
allow(Net::HTTP).to receive(:start).and_return(mock)
51-
52-
@lgtm.check_lgtm
53-
54-
expect(@dangerfile.status_report[:markdowns][0].message)
55-
.to match(%r{https:\/\/example.com\/image.jpg})
49+
it 'lgtm with errors' do
50+
allow(lgtm).to receive(:validate_response)
51+
.and_raise(::Lgtm::Errors::UnexpectedError)
52+
is_expected
53+
expect(dangerfile.status_report[:markdowns][0].message)
54+
.to eq("<h1 align='center'>LGTM</h1>")
5655
end
5756

58-
it 'pick random pic from lgtm.in with https_image_only option' do
59-
allow(Net::HTTP).to receive(:start).and_return(mock)
60-
61-
@lgtm.check_lgtm https_image_only: true
57+
context 'pick random pic from lgtm.in' do
58+
let(:expected_message) { %r{https:\/\/example.com\/image.jpg} }
59+
it_behaves_like 'returns correctly message'
60+
end
6261

63-
expect(@dangerfile.status_report[:markdowns][0].message)
64-
.to match(%r{https:\/\/example.com\/image.jpg})
62+
context 'pick random pic from lgtm.in with https_image_only option' do
63+
let(:https_image_only) { true }
64+
let(:expected_message) { %r{https:\/\/example.com\/image.jpg} }
65+
it_behaves_like 'returns correctly message'
6566
end
6667

67-
it 'use given url' do
68-
@lgtm.check_lgtm image_url: 'http://imgur.com/Irk2wyX.jpg'
69-
expect(@dangerfile.status_report[:markdowns][0].message)
70-
.to match(%r{http:\/\/imgur\.com\/Irk2wyX\.jpg})
68+
context 'use given url' do
69+
let(:image_url) { 'http://imgur.com/Irk2wyX.jpg' }
70+
let(:expected_message) { %r{http:\/\/imgur\.com\/Irk2wyX\.jpg} }
71+
it_behaves_like 'returns correctly message'
7172
end
7273
end
7374
end

0 commit comments

Comments
 (0)