Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from SpringMT/add-bundle-update-and-compare-link
Add bundle update and compare link
- Loading branch information
Showing
7 changed files
with
177 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ gem 'foreman' | |
gem 'rack-lineprof' | ||
gem 'resque' | ||
gem 'rake' | ||
gem 'compare_linker' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters