Add logging to forward-merge script

Add some logging to aid with development.
pull/22022/head
Phillip Webb 5 years ago
parent b6474f2ca1
commit 4a6e08c817

@ -2,6 +2,10 @@
require 'json' require 'json'
require 'net/http' require 'net/http'
require 'yaml' require 'yaml'
require 'logger'
$log = Logger.new(STDOUT)
$log.level = Logger::WARN
class ForwardMerge class ForwardMerge
attr_reader :issue, :milestone, :message, :line attr_reader :issue, :milestone, :message, :line
@ -14,48 +18,58 @@ class ForwardMerge
end end
def find_forward_merge(message_file) def find_forward_merge(message_file)
rev=`git rev-parse -q --verify MERGE_HEAD` $log.debug "Searching for for forward merge"
rev=`git rev-parse -q --verify MERGE_HEAD`.strip
$log.debug "Found #{rev} from git rev-parse"
return nil unless rev return nil unless rev
message = File.read(message_file) message = File.read(message_file)
message.each_line do |line| message.each_line do |line|
$log.debug "Checking #{line} for message"
match = /^(?:Fixes|Closes) gh-(\d+) in ([\d\.]+(?:(?:M|RC)\d)?)$/.match(line) match = /^(?:Fixes|Closes) gh-(\d+) in ([\d\.]+(?:(?:M|RC)\d)?)$/.match(line)
if match then if match then
issue = match[1] issue = match[1]
milestone = match[2] milestone = match[2]
$log.debug "Matched reference to issue #{issue} in milestone #{milestone}"
return ForwardMerge.new(issue, milestone, message, line) return ForwardMerge.new(issue, milestone, message, line)
end end
end end
$log.debug "No match in merge message"
return nil return nil
end end
def find_milestone(username, password, repository, title) def get_issue(username, password, repository, number)
uri = URI("https://api.github.com/repos/#{repository}/milestones") $log.debug "Getting issue #{number} from GitHub repository #{repository}"
uri = URI("https://api.github.com/repos/#{repository}/issues/#{number}")
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl=true http.use_ssl=true
request = Net::HTTP::Get.new(uri.path) request = Net::HTTP::Get.new(uri.path)
request.basic_auth(username, password) request.basic_auth(username, password)
response = http.request(request) response = http.request(request)
milestones = JSON.parse(response.body) $log.debug "Get HTTP response #{response.code}"
milestones.each do |milestone| return JSON.parse(response.body) unless response.code != '200'
return milestone['number'] if milestone['title'] == title puts "Failed to retrieve issue #{number}: #{response.message}"
end
puts "Milestone #{title} not found"
exit 1 exit 1
end end
def get_issue(username, password, repository, number) def find_milestone(username, password, repository, title)
uri = URI("https://api.github.com/repos/#{repository}/issues/#{number}") $log.debug "Finding milestone #{title} from GitHub repository #{repository}"
uri = URI("https://api.github.com/repos/#{repository}/milestones")
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl=true http.use_ssl=true
request = Net::HTTP::Get.new(uri.path) request = Net::HTTP::Get.new(uri.path)
request.basic_auth(username, password) request.basic_auth(username, password)
response = http.request(request) response = http.request(request)
return JSON.parse(response.body) unless response.code != '200' milestones = JSON.parse(response.body)
puts "Failed to retrieve issue #{number}: #{response.message}" milestones.each do |milestone|
$log.debug "Considering #{milestone['title']}"
return milestone['number'] if milestone['title'] == title
end
puts "Milestone #{title} not found"
exit 1 exit 1
end end
def create_issue(username, password, repository, original, title, labels, milestone, milestone_name, dry_run) def create_issue(username, password, repository, original, title, labels, milestone, milestone_name, dry_run)
$log.debug "Finding forward-merge issue in GitHub repository #{repository} for '#{title}'"
uri = URI("https://api.github.com/repos/#{repository}/issues") uri = URI("https://api.github.com/repos/#{repository}/issues")
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl=true http.use_ssl=true
@ -73,23 +87,32 @@ def create_issue(username, password, repository, original, title, labels, milest
return "dry-run" return "dry-run"
end end
response = JSON.parse(http.request(request).body) response = JSON.parse(http.request(request).body)
$log.debug "Created new issue #{response['number']}"
return response['number'] return response['number']
end end
$log.debug "Running forward-merge hook script"
message_file=ARGV[0] message_file=ARGV[0]
forward_merge = find_forward_merge(message_file) forward_merge = find_forward_merge(message_file)
exit 0 unless forward_merge exit 0 unless forward_merge
$log.debug "Loading config from ~/.spring-boot/forward_merge.yml"
config = YAML.load_file(File.join(Dir.home, '.spring-boot', 'forward-merge.yml')) config = YAML.load_file(File.join(Dir.home, '.spring-boot', 'forward-merge.yml'))
username = config['github']['credentials']['username'] username = config['github']['credentials']['username']
password = config['github']['credentials']['password'] password = config['github']['credentials']['password']
dry_run = config['dry_run'] dry_run = config['dry_run']
repository = 'spring-projects/spring-boot' repository = 'spring-projects/spring-boot'
existing_issue = get_issue(username, password, repository, forward_merge.issue) existing_issue = get_issue(username, password, repository, forward_merge.issue)
title = existing_issue['title'] title = existing_issue['title']
labels = existing_issue['labels'].map { |label| label['name'] } labels = existing_issue['labels'].map { |label| label['name'] }
labels << "status: forward-port" labels << "status: forward-port"
$log.debug "Processing issue '#{title}'"
milestone = find_milestone(username, password, repository, forward_merge.milestone) milestone = find_milestone(username, password, repository, forward_merge.milestone)
new_issue_number = create_issue(username, password, repository, forward_merge.issue, title, labels, milestone, forward_merge.milestone, dry_run) new_issue_number = create_issue(username, password, repository, forward_merge.issue, title, labels, milestone, forward_merge.milestone, dry_run)
puts "Created gh-#{new_issue_number} for forward port of gh-#{forward_merge.issue} into #{forward_merge.milestone}" puts "Created gh-#{new_issue_number} for forward port of gh-#{forward_merge.issue} into #{forward_merge.milestone}"
rewritten_message = forward_merge.message.sub(forward_merge.line, "Closes gh-#{new_issue_number}\n") rewritten_message = forward_merge.message.sub(forward_merge.line, "Closes gh-#{new_issue_number}\n")
File.write(message_file, rewritten_message) File.write(message_file, rewritten_message)

Loading…
Cancel
Save