danger-lgtm 1.0.4 → 1.1.0

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
- SHA1:
3
- metadata.gz: 1c3fca298f48500eb273447a1f481edaac906dc7
4
- data.tar.gz: f804befd3788c349ac1f434f304140cac7e759fc
2
+ SHA256:
3
+ metadata.gz: cd172e647a8512ec42e6c9f93ad239417e2fe26dadabc3d74a674e54666e3ba0
4
+ data.tar.gz: f20897b8e4ac857e7e9e0a26ad108233947eb170bdd6606ef0a04a510c12cbba
5
5
  SHA512:
6
- metadata.gz: 29766ed8ad9ae7fa3eb06f2552047b017d2ec3dec6930ac8a32a821e124b4b9b743ebca9bfd1de2636c468126ebbe7807e54dfbfc0a258a9368f42a3b9d9c4cb
7
- data.tar.gz: 4e96ee1c51db0787293a5c30ff73441260762468708606e9e08844bbe8da9dc5565bb07efa2fce570881836427901a4ebee8ffddf069bd0471b2ba1c0f4251d6
6
+ metadata.gz: 1bf3c8c8f2b68ec72202b93a903c358edd8383d60e43e578d65be5f47762e9521b16d9d635b7dbbf8adb60cb868d8beeaea08d1ae7df6dfbd8eeab94766da205
7
+ data.tar.gz: 16814ecd00a3dd8fa55fedd10a869bc4e47f8ca1b41452f036d05efabf97d367a3c1d3d4943e61e01b0961981d0eb7b824efd33f487075d2aa91b1c013d5b981
@@ -1,11 +1,18 @@
1
- language: ruby
2
- cache:
3
- directories:
4
- - bundle
1
+ ---
2
+
3
+ sudo: false
5
4
 
5
+ language: ruby
6
6
  rvm:
7
- - 2.2
8
- - 2.3.1
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
11
+
12
+ before_install:
13
+ - travis_retry gem install bundler --no-document -v 1.17.3
14
+
15
+ cache: bundler
9
16
 
10
17
  script:
11
- - bundle exec rake spec
18
+ - bundle exec rake spec
@@ -1,16 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danger-lgtm (1.0.3)
4
+ danger-lgtm (1.1.0)
5
5
  danger-plugin-api (~> 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- addressable (2.5.2)
11
- public_suffix (>= 2.0.2, < 4.0)
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
12
  ast (2.4.0)
13
- claide (1.0.2)
13
+ claide (1.0.3)
14
14
  claide-plugins (0.9.2)
15
15
  cork
16
16
  nap
@@ -19,28 +19,30 @@ GEM
19
19
  colored2 (3.1.2)
20
20
  cork (0.3.0)
21
21
  colored2 (~> 3.1)
22
- danger (5.6.4)
22
+ danger (6.2.2)
23
23
  claide (~> 1.0)
24
24
  claide-plugins (>= 0.9.2)
25
25
  colored2 (~> 3.1)
26
26
  cork (~> 0.1)
27
27
  faraday (~> 0.9)
28
- faraday-http-cache (~> 1.0)
29
- git (~> 1)
30
- kramdown (~> 1.5)
28
+ faraday-http-cache (~> 2.0)
29
+ git (~> 1.6)
30
+ kramdown (~> 2.0)
31
+ kramdown-parser-gfm (~> 1.0)
31
32
  no_proxy_fix
32
33
  octokit (~> 4.7)
33
34
  terminal-table (~> 1)
34
35
  danger-plugin-api (1.0.0)
35
36
  danger (> 2.0)
36
37
  diff-lcs (1.3)
37
- faraday (0.15.2)
38
+ faraday (0.17.3)
38
39
  multipart-post (>= 1.2, < 3)
39
- faraday-http-cache (1.3.1)
40
+ faraday-http-cache (2.0.0)
40
41
  faraday (~> 0.8)
41
42
  ffi (1.9.18)
42
43
  formatador (0.2.5)
43
- git (1.4.0)
44
+ git (1.6.0)
45
+ rchardet (~> 1.8)
44
46
  guard (2.14.1)
45
47
  formatador (>= 0.2.4)
46
48
  listen (>= 2.7, < 4.0)
@@ -56,20 +58,23 @@ GEM
56
58
  guard-compat (~> 1.1)
57
59
  rspec (>= 2.99.0, < 4.0)
58
60
  jaro_winkler (1.5.1)
59
- kramdown (1.17.0)
61
+ kramdown (2.1.0)
62
+ kramdown-parser-gfm (1.1.0)
63
+ kramdown (~> 2.0)
60
64
  listen (3.0.7)
61
65
  rb-fsevent (>= 0.9.3)
62
66
  rb-inotify (>= 0.9.7)
63
67
  lumberjack (1.0.11)
64
68
  method_source (0.8.2)
65
- multipart-post (2.0.0)
69
+ multipart-post (2.1.1)
66
70
  nap (1.1.0)
67
71
  nenv (0.3.0)
68
72
  no_proxy_fix (0.1.2)
69
73
  notiffany (0.1.1)
70
74
  nenv (~> 0.1)
71
75
  shellany (~> 0.0)
72
- octokit (4.9.0)
76
+ octokit (4.16.0)
77
+ faraday (>= 0.9)
73
78
  sawyer (~> 0.8.0, >= 0.5.3)
74
79
  open4 (1.3.4)
75
80
  parallel (1.12.1)
@@ -80,12 +85,13 @@ GEM
80
85
  coderay (~> 1.1.0)
81
86
  method_source (~> 0.8.1)
82
87
  slop (~> 3.4)
83
- public_suffix (3.0.2)
88
+ public_suffix (4.0.3)
84
89
  rainbow (3.0.0)
85
90
  rake (10.5.0)
86
91
  rb-fsevent (0.9.8)
87
92
  rb-inotify (0.9.8)
88
93
  ffi (>= 0.5.0)
94
+ rchardet (1.8.0)
89
95
  rspec (3.5.0)
90
96
  rspec-core (~> 3.5.0)
91
97
  rspec-expectations (~> 3.5.0)
@@ -108,9 +114,9 @@ GEM
108
114
  ruby-progressbar (~> 1.7)
109
115
  unicode-display_width (~> 1.0, >= 1.0.1)
110
116
  ruby-progressbar (1.9.0)
111
- sawyer (0.8.1)
112
- addressable (>= 2.3.5, < 2.6)
113
- faraday (~> 0.8, < 1.0)
117
+ sawyer (0.8.2)
118
+ addressable (>= 2.3.5)
119
+ faraday (> 0.8, < 2.0)
114
120
  shellany (0.0.1)
115
121
  slop (3.6.0)
116
122
  terminal-table (1.8.0)
@@ -1,7 +1,9 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'lgtm/gem_version.rb'
5
+
6
+ require 'lgtm/version.rb'
5
7
 
6
8
  Gem::Specification.new do |spec|
7
9
  spec.name = 'danger-lgtm'
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'lgtm/gem_version'
3
+ require 'lgtm/version'
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lgtm
4
+ # Network is module of HTTP communication
5
+ module Network
6
+ class NetworkError < StandardError; end
7
+
8
+ # process_request Send HTTP request
9
+ #
10
+ # @param [URI] uri Request uri
11
+ #
12
+ # @return [Net::HTTP] HTTP response
13
+ #
14
+ def process_request(uri)
15
+ request = Net::HTTP::Get.new(uri)
16
+ connection = Net::HTTP.start(uri.hostname, uri.port, use_ssl: https?(uri))
17
+
18
+ connection
19
+ .request(request)
20
+ .tap(&method(:validate_response!))
21
+ end
22
+
23
+ # parse_redirect_location Parse redirect location from response
24
+ #
25
+ # @param [Net::HTTP] response HTTP response
26
+ #
27
+ # @return [String] Redirect location
28
+ #
29
+ def parse_redirect_location(response)
30
+ response['location']
31
+ end
32
+
33
+ private
34
+
35
+ # validate_response! Validates HTTP response
36
+ #
37
+ # @param [Net::HTTP] response Net::HTTP response
38
+ #
39
+ # @raise NetworkError
40
+ #
41
+ # @return [nil]
42
+ #
43
+ def validate_response!(response)
44
+ raise NetworkError, response.message if response.is_a?(Net::HTTPError)
45
+ end
46
+
47
+ # https? Check that uri use HTTPS scheme
48
+ #
49
+ # @param [URI] uri Test uri
50
+ #
51
+ # @return [Boolean] Result of check
52
+ #
53
+ def https?(uri)
54
+ uri.instance_of?(URI::HTTPS)
55
+ end
56
+ end
57
+ end
@@ -1,10 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'uri'
4
- require 'json'
5
4
  require 'net/http'
6
- require_relative 'errors'
7
- require_relative 'error_handleable'
5
+
6
+ require_relative 'network'
8
7
 
9
8
  module Danger
10
9
  # Lgtm let danger say lgtm when there is no violations.
@@ -18,70 +17,102 @@ module Danger
18
17
  # @tags lgtm, github
19
18
  #
20
19
  class DangerLgtm < Plugin
21
- include ::Lgtm::ErrorHandleable
22
- RANDOM_LGTM_POST_URL = 'https://lgtm.in/g'.freeze
20
+ include ::Lgtm::Network
21
+
22
+ LGTM_URL = 'https://www.lgtm.app'.freeze
23
23
 
24
- # Check status report, say lgtm if no violations
25
- # Generates a `markdown` of a lgtm image.
24
+ RANDOM_POST_PATH = '/g'.freeze
25
+ POST_CONTENT_PATH = '/p'.freeze
26
+
27
+ # check_lgtm Comment LGTM markdown if report has no violations
26
28
  #
27
- # @param [String] image_url lgtm image url
28
- # @param [Boolean] https_image_only https image only if true
29
+ # @param [String] image_url LGTM image url
30
+ # @param [Boolean] https_image_only Use only secure url
29
31
  #
30
- # @return [void]
32
+ # @return [nil]
31
33
  #
32
34
  def check_lgtm(image_url: nil, https_image_only: false)
33
- return unless status_report[:errors].length.zero? &&
34
- status_report[:warnings].length.zero?
35
-
36
- image_url ||= fetch_image_url(https_image_only: https_image_only)
35
+ return if status_report[:errors].any?
36
+ return if status_report[:warnings].any?
37
37
 
38
- markdown(
39
- markdown_template(image_url)
40
- )
38
+ if image_url
39
+ comment image_url
40
+ else
41
+ comment fetch_image_url(https_image_only)
42
+ end
41
43
  end
42
44
 
43
45
  private
44
46
 
45
- # returns "<h1 align="center">LGTM</h1>" when ServiceTemporarilyUnavailable.
46
- def fetch_image_url(https_image_only: false)
47
- lgtm_post_response = process_request(lgtm_post_url) do |req|
48
- req['Accept'] = 'application/json'
47
+ # comment Place markdown comment
48
+ #
49
+ # @param [Boolean] url LGTM image url
50
+ #
51
+ # @return [nil]
52
+ #
53
+ def comment(url)
54
+ if url.nil?
55
+ markdown("<h1 align='center'>LGTM</h1>")
56
+ else
57
+ markdown("<p align='center'><img src='#{url}' alt='LGTM' /></p>")
49
58
  end
59
+ end
50
60
 
51
- lgtm_post = JSON.parse(lgtm_post_response.body)
61
+ # fetch_image_url Fetch LGTM image url from https://www.lgtm.app
62
+ #
63
+ # @param [Boolean] reject_insecure_url Eeturn only secure url
64
+ #
65
+ # @return [String] LGTM image url
66
+ #
67
+ def fetch_image_url(reject_insecure_url = false)
68
+ post_id = fetch_randon_post_id
69
+ return if post_id.empty?
52
70
 
53
- url = lgtm_post['actualImageUrl']
54
- if https_image_only && URI.parse(url).scheme != 'https'
55
- return fetch_image_url(https_image_only: true)
56
- end
57
- url
58
- rescue ::Lgtm::Errors::UnexpectedError; nil
59
- end
71
+ post_content_url = fetch_post_content_url(post_id)
72
+ return if post_content_url.empty?
60
73
 
61
- def process_request(url)
62
- uri = URI(url)
74
+ return fetch_image_url(reject_insecure_url) if retry?(reject_insecure_url,
75
+ post_content_url)
63
76
 
64
- req = Net::HTTP::Get.new(uri)
77
+ post_content_url
78
+ rescue ::Lgtm::Network::NetworkError => e
79
+ $stdout.puts e.message
80
+ end
65
81
 
66
- yield req if block_given?
82
+ # fetch_randon_post_id Fetch renadon LGTM post url from https://www.lgtm.app
83
+ #
84
+ # @return [String] LGTM post url
85
+ #
86
+ def fetch_randon_post_id
87
+ uri = URI.join(LGTM_URL, RANDOM_POST_PATH)
88
+ response = process_request(uri)
89
+ location = parse_redirect_location(response)
67
90
 
68
- Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
69
- http.request(req)
70
- end
91
+ location.split('/').last
71
92
  end
72
93
 
73
- def lgtm_post_url
74
- res = process_request(RANDOM_LGTM_POST_URL)
75
- validate_response(res)
76
- res['location']
94
+ # fetch_post_content_url Fetch LGTM image url from https://www.lgtm.app
95
+ #
96
+ # @param [String] post_id LGTM post identifier
97
+ #
98
+ # @return [String] LGTM image url
99
+ #
100
+ def fetch_post_content_url(post_id)
101
+ uri = URI.join(LGTM_URL, POST_CONTENT_PATH, post_id)
102
+ response = process_request(uri)
103
+
104
+ parse_redirect_location(response)
77
105
  end
78
106
 
79
- def markdown_template(image_url)
80
- if image_url.nil?
81
- "<h1 align='center'>LGTM</h1>"
82
- else
83
- "<p align='center'><img src='#{image_url}' alt='LGTM' /></p>"
84
- end
107
+ # retry? Check should be image url requested again
108
+ #
109
+ # @param [Boolean] reject_insecure_url Return only secure url
110
+ # @param [String] url LGTM image url
111
+ #
112
+ # @return [Boolean] should be image url requested again
113
+ #
114
+ def retry?(reject_insecure_url, url)
115
+ reject_insecure_url && https?(URI.parse(url))
85
116
  end
86
117
  end
87
118
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lgtm
4
- VERSION = '1.0.4'.freeze
4
+ VERSION = '1.1.0'.freeze
5
5
  end
@@ -3,71 +3,63 @@
3
3
  require File.expand_path('spec_helper', __dir__)
4
4
 
5
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
15
-
16
6
  it 'should be a plugin' do
17
7
  expect(Danger::DangerLgtm.new(nil)).to be_a(Danger::Plugin)
18
8
  end
19
9
 
20
10
  describe '#check_lgtm' do
21
- subject do
22
- lgtm.check_lgtm(
23
- https_image_only: https_image_only,
24
- image_url: image_url
25
- )
11
+ shared_examples 'returns correctly message' do
12
+ it { expect(markdowns.first.message).to match(/#{image_url}/) }
26
13
  end
27
14
 
28
- let(:dangerfile) { testing_dangerfile }
29
15
  let(:lgtm) { dangerfile.lgtm }
30
- let(:https_image_only) { false } # default false
31
- let(:image_url) {} # default nil
16
+ let(:dangerfile) { testing_dangerfile }
32
17
 
33
- shared_examples 'returns correctly message' do
34
- it do
35
- allow(Net::HTTP).to receive(:start).and_return(mock)
36
- is_expected
18
+ let(:https_only) { false }
19
+ let(:image_url) { nil }
37
20
 
38
- expect(dangerfile.status_report[:markdowns][0].message)
39
- .to match(expected_message)
40
- end
21
+ let(:connection_mock) { double('Connection', request: response) }
22
+
23
+ subject(:markdowns) do
24
+ dangerfile.status_report[:markdowns]
41
25
  end
42
26
 
43
- context 'with Dangerfile' do
44
- it 'when no violation' do
45
- is_expected
46
- expect(dangerfile.status_report[:markdowns].length).to eq(1)
27
+ context 'with HTTP errors' do
28
+ let(:message) { 'Hello World!' }
29
+ let(:response) { failure_response_mock(message) }
30
+
31
+ it 'handles error' do
32
+ expect($stdout).to receive(:puts).with(message)
33
+
34
+ allow(Net::HTTP).to receive(:start).and_return(connection_mock)
35
+ lgtm.check_lgtm(https_image_only: https_only, image_url: image_url)
36
+
37
+ expect(markdowns.first.message).to eq("<h1 align='center'>LGTM</h1>")
47
38
  end
39
+ end
48
40
 
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>")
41
+ context 'without HTTP errors' do
42
+ before do
43
+ allow(Net::HTTP).to receive(:start).and_return(connection_mock)
55
44
  end
56
45
 
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'
46
+ before do
47
+ lgtm.check_lgtm(https_image_only: https_only, image_url: image_url)
60
48
  end
61
49
 
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} }
50
+ context 'with passed image url' do
51
+ let(:lgtm_image) { 'https://example.com/image1.jpg' }
52
+ let(:image_url) { 'https://example.com/image2.jpg' }
53
+ let(:response) { success_response_mock(lgtm_image) }
54
+
65
55
  it_behaves_like 'returns correctly message'
66
56
  end
67
57
 
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} }
58
+ context 'without passed image url' do
59
+ let(:lgtm_image) { 'https://example.com/image.jpg' }
60
+ let(:image_url) { 'https://example.com/image.jpg' }
61
+ let(:response) { success_response_mock(lgtm_image) }
62
+
71
63
  it_behaves_like 'returns correctly message'
72
64
  end
73
65
  end
@@ -59,3 +59,23 @@ def testing_dangerfile
59
59
  env = Danger::EnvironmentManager.new(testing_env)
60
60
  Danger::Dangerfile.new(env, testing_ui)
61
61
  end
62
+
63
+ # Failute response mock
64
+ def failure_response_mock(message)
65
+ mock = double('Response')
66
+
67
+ allow(mock).to receive(:is_a?).with(Net::HTTPError).and_return(true)
68
+ allow(mock).to receive(:message).and_return(message)
69
+
70
+ mock
71
+ end
72
+
73
+ # Success response mock
74
+ def success_response_mock(url)
75
+ mock = double('Response')
76
+
77
+ allow(mock).to receive(:is_a?).with(Net::HTTPError).and_return(false)
78
+ allow(mock).to receive(:[]).with('location').and_return(url)
79
+
80
+ mock
81
+ 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.4
4
+ version: 1.1.0
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-29 00:00:00.000000000 Z
11
+ date: 2020-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -169,10 +169,9 @@ 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
174
- - lib/lgtm/gem_version.rb
172
+ - lib/lgtm/network.rb
175
173
  - lib/lgtm/plugin.rb
174
+ - lib/lgtm/version.rb
176
175
  - spec/lgtm_spec.rb
177
176
  - spec/spec_helper.rb
178
177
  homepage: https://github.com/leonhartX/danger-lgtm
@@ -194,8 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
193
  - !ruby/object:Gem::Version
195
194
  version: '0'
196
195
  requirements: []
197
- rubyforge_project:
198
- rubygems_version: 2.5.2.3
196
+ rubygems_version: 3.1.2
199
197
  signing_key:
200
198
  specification_version: 4
201
199
  summary: Danger Plugin used to post LGTM iamge when there is no violations
@@ -1,33 +0,0 @@
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
@@ -1,6 +0,0 @@
1
- module Lgtm
2
- class Errors
3
- class UnexpectedError < StandardError
4
- end
5
- end
6
- end