danger-lgtm 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc895c5b5010b4c4a5f4d7447c0f1e8ae6c3e739
4
- data.tar.gz: 7e65307505e57e652024c53b27aa9e155f4115f3
3
+ metadata.gz: 96b81a57a626da94dcb035e081a9102303204040
4
+ data.tar.gz: f8c73962d2b81e6b4f4558004179fd03fcac0237
5
5
  SHA512:
6
- metadata.gz: feabb54a4e5d374387f0d64542565991115236d90a1cd098e54cb68250662368f12694f745f19900b1974a65f9abbec1aeb18836ca13f2251fa44324bfe6c606
7
- data.tar.gz: 66d8d3a8f931cad7346f4e3ef563da528b14c561cec252e4c11c4d0b556a3b7f4cb7b6bc3dd4f3317bdbb755f6278f14022c7c1fc2939f802f7be66cb1f2471f
6
+ metadata.gz: f42e21763b4d6bcfb20714948f6fd6517ab550bf94897e03cfb9033ec94e85246004d2d8f152c795cc7cf3e16d273d25434611296b0e29b9899f31b12ad4cc7a
7
+ data.tar.gz: 62b287ebaf3d065b402829e06fec6cb395045032b5ab66ae2636b4715ca2326f095fbd02c8eae3195c86adab4600733762ac0e64f6e1c43473df7a889204f8b3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danger-lgtm (1.0.2)
4
+ danger-lgtm (1.0.3)
5
5
  danger-plugin-api (~> 1.0)
6
6
 
7
7
  GEM
@@ -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
@@ -0,0 +1,6 @@
1
+ module Lgtm
2
+ class Errors
3
+ class UnexpectedError < StandardError
4
+ end
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lgtm
4
- VERSION = '1.0.2'.freeze
4
+ VERSION = '1.0.3'.freeze
5
5
  end
data/lib/lgtm/plugin.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'uri'
4
4
  require 'json'
5
5
  require 'net/http'
6
+ require './lib/lgtm/errors'
7
+ require './lib/lgtm/error_handleable'
6
8
 
7
9
  module Danger
8
10
  # Lgtm let danger say lgtm when there is no violations.
@@ -16,13 +18,14 @@ module Danger
16
18
  # @tags lgtm, github
17
19
  #
18
20
  class DangerLgtm < Plugin
21
+ include ::Lgtm::ErrorHandleable
19
22
  RANDOM_LGTM_POST_URL = 'https://lgtm.in/g'.freeze
20
23
 
21
24
  # Check status report, say lgtm if no violations
22
25
  # Generates a `markdown` of a lgtm image.
23
26
  #
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
26
29
  #
27
30
  # @return [void]
28
31
  #
@@ -41,7 +44,6 @@ module Danger
41
44
 
42
45
  # returns "<h1 align="center">LGTM</h1>" when ServiceTemporarilyUnavailable.
43
46
  def fetch_image_url(https_image_only: false)
44
- return unless lgtm_post_url
45
47
  lgtm_post_response = process_request(lgtm_post_url) do |req|
46
48
  req['Accept'] = 'application/json'
47
49
  end
@@ -53,6 +55,7 @@ module Danger
53
55
  return fetch_image_url(https_image_only: true)
54
56
  end
55
57
  url
58
+ rescue ::Lgtm::Errors::UnexpectedError; nil
56
59
  end
57
60
 
58
61
  def process_request(url)
@@ -68,9 +71,9 @@ module Danger
68
71
  end
69
72
 
70
73
  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']
74
77
  end
75
78
 
76
79
  def markdown_template(image_url)
data/spec/lgtm_spec.rb CHANGED
@@ -2,72 +2,73 @@
2
2
 
3
3
  require File.expand_path('spec_helper', __dir__)
4
4
 
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
19
15
 
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
22
19
 
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
27
27
 
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
30
32
 
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
32
37
 
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)
35
40
  end
41
+ end
36
42
 
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)
47
47
  end
48
48
 
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>")
56
55
  end
57
56
 
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
62
61
 
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'
65
66
  end
66
67
 
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'
71
72
  end
72
73
  end
73
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-lgtm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - leonhartX
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-01 00:00:00.000000000 Z
11
+ date: 2018-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -169,6 +169,8 @@ files:
169
169
  - danger-lgtm.gemspec
170
170
  - lib/danger_lgtm.rb
171
171
  - lib/danger_plugin.rb
172
+ - lib/lgtm/error_handleable.rb
173
+ - lib/lgtm/errors.rb
172
174
  - lib/lgtm/gem_version.rb
173
175
  - lib/lgtm/plugin.rb
174
176
  - spec/lgtm_spec.rb