Skip to content

Commit

Permalink
Merge pull request #5 from SpringMT/add-bundle-update-and-compare-link
Browse files Browse the repository at this point in the history
Add bundle update and compare link
  • Loading branch information
SpringMT committed Feb 17, 2017
2 parents f8cb45d + f3c5937 commit bcd212b
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 22 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -7,3 +7,4 @@ gem 'foreman'
gem 'rack-lineprof'
gem 'resque'
gem 'rake'
gem 'compare_linker'
48 changes: 32 additions & 16 deletions Gemfile.lock
@@ -1,59 +1,75 @@
GEM
remote: http://rubygems.org/
specs:
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
ast (2.3.0)
compare_linker (1.3.5)
httpclient
octokit
debugger-ruby_core_source (1.3.8)
foreman (0.82.0)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
foreman (0.83.0)
thor (~> 0.19.1)
httpclient (2.8.3)
mono_logger (1.1.0)
multi_json (1.12.1)
parser (2.3.1.2)
multipart-post (2.0.0)
octokit (4.6.2)
sawyer (~> 0.8.0, >= 0.5.3)
parser (2.4.0.0)
ast (~> 2.2)
powerpack (0.1.1)
rack (1.6.4)
public_suffix (2.0.5)
rack (1.6.5)
rack-lineprof (0.0.3)
rack (~> 1.5)
rblineprof (~> 0.3.6)
term-ansicolor (~> 1.3)
rack-protection (1.5.3)
rack
rainbow (2.1.0)
rake (11.3.0)
rainbow (2.2.1)
rake (12.0.0)
rblineprof (0.3.7)
debugger-ruby_core_source (~> 1.3)
redis (3.3.1)
redis-namespace (1.5.2)
redis (3.3.3)
redis-namespace (1.5.3)
redis (~> 3.0, >= 3.0.4)
resque (1.26.0)
resque (1.27.1)
mono_logger (~> 1.0)
multi_json (~> 1.0)
redis-namespace (~> 1.3)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
rubocop (0.42.0)
parser (>= 2.3.1.1, < 3.0)
rubocop (0.47.1)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
sinatra (1.4.7)
sawyer (0.8.1)
addressable (>= 2.3.5, < 2.6)
faraday (~> 0.8, < 1.0)
sinatra (1.4.8)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
term-ansicolor (1.3.2)
term-ansicolor (1.4.0)
tins (~> 1.0)
thor (0.19.1)
tilt (2.0.5)
tins (1.12.0)
unicode-display_width (1.1.1)
thor (0.19.4)
tilt (2.0.6)
tins (1.13.2)
unicode-display_width (1.1.3)
vegas (0.1.11)
rack (>= 1.0.0)

PLATFORMS
ruby

DEPENDENCIES
compare_linker
foreman
rack
rack-lineprof
Expand Down
27 changes: 27 additions & 0 deletions bundle_update_application.rb
@@ -0,0 +1,27 @@
require 'rubocop'
require 'json'

class BundleUpdateApplication
def call(env)
body_json = JSON.parse(env["rack.input"].gets)
organization = body_json.dig('organization', 'login')

return [ 200, { 'Content-Type' => 'text/plain' }, ['ONLY ALOW PULL REQUEST'] ] unless env['HTTP_X_GITHUB_EVENT'] == 'pull_request'
return [ 200, { 'Content-Type' => 'text/plain' }, ['THE PR IS NOT OPENED'] ] if body_json["action"] != "opened"
return [ 200, { 'Content-Type' => 'text/plain' }, ['THIS PR IS AUTO'] ] if body_json['pull_request']['title'].start_with?('[Automatic PR]')
return [ 200, { 'Content-Type' => 'text/plain' }, ['THIS PR IS AUTO'] ] if body_json['pull_request']['title'].start_with?('Automatic PR')

queue = {
organization: organization,
pr_number: body_json['number'],
git_url: body_json['repository']['ssh_url'],
repos_name: body_json['repository']['name'],
full_repos_name: body_json['repository']['full_name'],
branch_name: body_json['pull_request']['head']['ref'],
html_url: body_json['pull_request']['html_url'],
sender: body_json['pull_request']['user']['login']
}.to_json
Resque.enqueue(BundleUpdateJob, queue)
[ 200, { 'Content-Type' => 'text/plain' }, ['SUCCESS'] ]
end
end
9 changes: 5 additions & 4 deletions config.ru
Expand Up @@ -17,9 +17,10 @@ Resque.redis.namespace = "resque-hook"
#use Rack::Lineprof

run Rack::URLMap.new(
'/' => RubocopApplication.new,
'/resque' => Resque::Server.new,
'/hello' => HelloApplication.new,
'/favicon.ico' => HelloApplication.new
'/' => RubocopApplication.new,
'/bundle_update' => BundleUpdateApplication.new,
'/resque' => Resque::Server.new,
'/hello' => HelloApplication.new,
'/favicon.ico' => HelloApplication.new
)

109 changes: 109 additions & 0 deletions jobs/bundle_update_job.rb
@@ -0,0 +1,109 @@
require 'json'
require 'resque'
require 'compare_linker'

Resque.redis = 'redis://127.0.0.1:6379'
Resque.redis.namespace = "resque-hook"

class BundleUpdateJob
@queue = :bitting

TOKEN = ENV['TOKEN']
GITHUB_HOST = ENV['GITHUB_HOST'] || 'github.com'
GITHUB_API_BASE_PATH = ENV['GITHUB_API_BASE_PATH']
STDOUT.sync = true

def self.perform(*args)
queue = JSON.parse(args.first)

organization = queue['organization']
pr_number = queue['pr_number']
git_url = queue['git_url']
repos_name = queue['repos_name']
full_repos_name = queue['full_repos_name']
branch_name = queue['branch_name']
html_url = queue['html_url']
sender = queue['sender']

Dir.chdir "./tmp_bundle_update" do
`git clone #{git_url} #{repos_name}_#{pr_number}`
Dir.chdir "./#{repos_name}_#{pr_number}" do
execute_bundle_update(
organization: organization,
pr_number: pr_number,
repos_name: repos_name,
full_repos_name: full_repos_name,
branch_name: branch_name,
html_url: html_url,
sender: sender
)
end
FileUtils.rm_r("./#{repos_name}_#{pr_number}")
end
end

private
def self.execute_bundle_update(organization:, pr_number:, repos_name:, full_repos_name:, branch_name:, html_url:, sender:)
`git checkout #{branch_name}`

old_lockfile = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
`bundle install --path=vendor/bundle`
`bundle update`
new_lockfile = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))

octokit ||= Octokit::Client.new(access_token: ENV["BUNDLE_UPDATE_OCTOKIT_ACCESS_TOKEN"])
gem_dictionary = CompareLinker::GemDictionary.new
formatter = CompareLinker::Formatter::Markdown.new
comparator = CompareLinker::LockfileComparator.new
comparator.compare(old_lockfile, new_lockfile)

compare_links = comparator.updated_gems.map do |gem_name, gem_info|
if gem_info[:owner].nil?
finder = CompareLinker::GithubLinkFinder.new(octokit, gem_dictionary)
finder.find(gem_name)
gem_info[:homepage_uri] = finder.homepage_uri
if finder.repo_owner.nil?
formatter.format(gem_info)
else
gem_info[:repo_owner] = finder.repo_owner
gem_info[:repo_name] = finder.repo_name

tag_finder = CompareLinker::GithubTagFinder.new(octokit, gem_name, finder.repo_full_name)
old_tag = tag_finder.find(gem_info[:old_ver])
new_tag = tag_finder.find(gem_info[:new_ver])

if old_tag && new_tag
gem_info[:old_tag] = old_tag.name
gem_info[:new_tag] = new_tag.name
formatter.format(gem_info)
else
formatter.format(gem_info)
end
end
else
formatter.format(gem_info)
end
end

commit_result = `git commit -am 'bundle update'`

return [ 200, { 'Content-Type' => 'text/plain' }, ['UNNECESSARY RUBOCOP PERFECT!'] ] if commit_result.match(/nothing to commit \(working directory clean\)/)

p `git checkout -b #{branch_name}_bundle_update`
p `git push origin #{branch_name}_bundle_update`

https.start do |h|
req = Net::HTTP::Post.new("#{GITHUB_API_BASE_PATH}/#{full_repos_name}/pulls")
req["Authorization"] = "token #{TOKEN}"
req.body = {
title: "[Automatic PR] Bundle Update PR from #{full_repos_name}",
body: "@#{sender} bundle update result for #{html_url} \n#{compare_links}",
head: "#{organization}:#{branch_name}_bundle_update",
base: "#{branch_name}"
}.to_json
response = h.request(req)
return [ 401, { 'Content-Type' => 'text/plain' }, ['GITHUB API FAILED(CREATE PR)'] ] unless Net::HTTPSuccess === response
end
[ 200, { 'Content-Type' => 'text/plain' }, ['SUCCESS'] ]
end
end
2 changes: 1 addition & 1 deletion jobs/rubocop_job.rb
Expand Up @@ -86,7 +86,7 @@ def self.execute_rubocop(organization:, pr_number:, repos_name:, full_repos_name
req = Net::HTTP::Post.new("#{GITHUB_API_BASE_PATH}/#{full_repos_name}/pulls")
req["Authorization"] = "token #{TOKEN}"
req.body = {
title: "Automatic PR. Rubocopnilzed PR from #{full_repos_name}",
title: "[Automatic PR] Rubocopnilzed PR from #{full_repos_name}",
body: "@#{sender} Rubocop Result for #{html_url}",
head: "#{organization}:#{branch_name}_rubocop",
base: "#{branch_name}"
Expand Down
3 changes: 2 additions & 1 deletion rubocop_application.rb
Expand Up @@ -8,7 +8,8 @@ def call(env)

return [ 200, { 'Content-Type' => 'text/plain' }, ['ONLY ALOW PULL REQUEST'] ] unless env['HTTP_X_GITHUB_EVENT'] == 'pull_request'
return [ 200, { 'Content-Type' => 'text/plain' }, ['THE PR IS NOT OPENED'] ] if body_json["action"] != "opened"
return [ 200, { 'Content-Type' => 'text/plain' }, ['THIS PR IS AUTO'] ] if body_json['pull_request']['title'] == "Automatic PR. Rubocopnilzed PR from #{organization}"
return [ 200, { 'Content-Type' => 'text/plain' }, ['THIS PR IS AUTO'] ] if body_json['pull_request']['title'].start_with?('[Automatic PR]')
return [ 200, { 'Content-Type' => 'text/plain' }, ['THIS PR IS AUTO'] ] if body_json['pull_request']['title'].start_with?('Automatic PR')

queue = {
organization: organization,
Expand Down

0 comments on commit bcd212b

Please sign in to comment.