Skip to content

Commit

Permalink
Merge pull request #2 from sleekybadger/hotfix/redirect
Browse files Browse the repository at this point in the history
Follow lgtm redirect
  • Loading branch information
leonhartX authored Jun 2, 2017
2 parents fec0ddd + d56a866 commit a3456f5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Metrics/BlockLength:
Exclude:
- "**/*_spec.rb"
2 changes: 2 additions & 0 deletions lib/danger_lgtm.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require 'lgtm/gem_version'
2 changes: 2 additions & 0 deletions lib/danger_plugin.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require 'lgtm/plugin'
2 changes: 2 additions & 0 deletions lib/lgtm/gem_version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Lgtm
VERSION = '0.1.1'.freeze
end
43 changes: 39 additions & 4 deletions lib/lgtm/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

require 'uri'
require 'json'
require 'net/http'

module Danger
Expand All @@ -12,6 +16,8 @@ module Danger
# @tags lgtm, github
#
class DangerLgtm < Plugin
RANDOM_LGTM_POST_URL = 'http://lgtm.in/g'.freeze

# Check status report, say lgtm if no violations
# Generates a `markdown` of a lgtm iamge.
#
Expand All @@ -22,11 +28,40 @@ class DangerLgtm < Plugin
def check_lgtm(image_url: nil)
return unless status_report[:errors].length.zero? &&
status_report[:warnings].length.zero?
unless image_url
id = Net::HTTP.get_response('lgtm.in', '/g')['location'].split('/').last
image_url = "https://lgtm.in/p/#{id}"

image_url ||= fetch_image_url

markdown(
markdown_template(image_url)
)
end

private

def fetch_image_url
lgtm_post_url = process_request(RANDOM_LGTM_POST_URL)['location']

lgtm_post_response = process_request(lgtm_post_url) do |req|
req['Accept'] = 'application/json'
end
markdown("![LGTM](#{image_url})")

lgtm_post = JSON.parse(lgtm_post_response.body)

lgtm_post['actualImageUrl']
end

def process_request(url)
uri = URI(url)

req = Net::HTTP::Get.new(uri)

yield req if block_given?

Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
end

def markdown_template(image_url)
"<p align='center'><img src='#{image_url}' alt='LGTM' /></p>"
end
end
end
14 changes: 13 additions & 1 deletion spec/lgtm_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require File.expand_path('../spec_helper', __FILE__)

module Danger
Expand All @@ -24,9 +26,19 @@ module Danger
end

it 'pick random pic from lgtm.in' do
mock = double(
:[] => 'https://lgtm.in/p/sSuI4hm0q',
body: JSON.generate(
actualImageUrl: 'https://example.com/image.jpg'
)
)

allow(Net::HTTP).to receive(:start).and_return(mock)

@lgtm.check_lgtm

expect(@dangerfile.status_report[:markdowns][0].message)
.to match(%r{https:\/\/lgtm.\in\/p\/})
.to match(%r{https:\/\/example.com\/image.jpg})
end

it 'use given url' do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'pathname'
ROOT = Pathname.new(File.expand_path('../../', __FILE__))
$LOAD_PATH.unshift((ROOT + 'lib').to_s)
Expand Down

0 comments on commit a3456f5

Please sign in to comment.