Add rescue fallback with warning message to prevent crash in case of exceed Rate Limit (temporary workaround for #71)

This commit is contained in:
Petr Korolev 2015-03-21 02:06:59 +02:00
parent 30f429f632
commit 729938b85d

View File

@ -27,7 +27,11 @@ module GitHubChangelogGenerator
github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil? github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil?
github_options[:site] = options[:github_endpoint] unless options[:github_site].nil? github_options[:site] = options[:github_endpoint] unless options[:github_site].nil?
@github = Github.new github_options begin
@github = Github.new github_options
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
@generator = Generator.new(@options) @generator = Generator.new(@options)
@ -88,7 +92,11 @@ module GitHubChangelogGenerator
issue[:actual_date] = issue[:closed_at] issue[:actual_date] = issue[:closed_at]
else else
begin begin
commit = @github.git_data.commits.get @options[:user], @options[:project], event[:commit_id] begin
commit = @github.git_data.commits.get @options[:user], @options[:project], event[:commit_id]
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
issue[:actual_date] = commit[:author][:date] issue[:actual_date] = commit[:author][:date]
rescue rescue
puts "Warning: can't fetch commit #{event[:commit_id]} probably it referenced from another repo." puts "Warning: can't fetch commit #{event[:commit_id]} probably it referenced from another repo."
@ -110,16 +118,22 @@ module GitHubChangelogGenerator
if @options[:verbose] if @options[:verbose]
print "Fetching merged dates...\r" print "Fetching merged dates...\r"
end end
response = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
pull_requests = [] pull_requests = []
page_i = 0 begin
response.each_page do |page| response = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
page_i += PER_PAGE_NUMBER page_i = 0
count_pages = response.count_pages response.each_page do |page|
print "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r" page_i += PER_PAGE_NUMBER
pull_requests.concat(page) count_pages = response.count_pages
end print "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
pull_requests.concat(page)
end
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
print " \r" print " \r"
@pull_requests.each { |pr| @pull_requests.each { |pr|
@ -276,7 +290,11 @@ module GitHubChangelogGenerator
end end
def is_megred(number) def is_megred(number)
@github.pull_requests.merged? @options[:user], @options[:project], number begin
@github.pull_requests.merged? @options[:user], @options[:project], number
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
end end
def get_all_tags def get_all_tags
@ -285,20 +303,26 @@ module GitHubChangelogGenerator
print "Fetching tags...\r" print "Fetching tags...\r"
end end
response = @github.repos.tags @options[:user], @options[:project]
tags = [] tags = []
page_i = 0
count_pages = response.count_pages begin
response.each_page do |page| response = @github.repos.tags @options[:user], @options[:project]
page_i += PER_PAGE_NUMBER page_i = 0
print "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r" count_pages = response.count_pages
tags.concat(page) response.each_page do |page|
end page_i += PER_PAGE_NUMBER
print " \r" print "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
if @options[:verbose] tags.concat(page)
puts "Found #{tags.count} tags" end
end print " \r"
if @options[:verbose]
puts "Found #{tags.count} tags"
end
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
tags tags
end end
@ -512,7 +536,11 @@ module GitHubChangelogGenerator
return @tag_times_hash[tag_name['name']] return @tag_times_hash[tag_name['name']]
end end
github_git_data_commits_get = @github.git_data.commits.get @options[:user], @options[:project], tag_name['commit']['sha'] begin
github_git_data_commits_get = @github.git_data.commits.get @options[:user], @options[:project], tag_name['commit']['sha']
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
time_string = github_git_data_commits_get['committer']['date'] time_string = github_git_data_commits_get['committer']['date']
@tag_times_hash[tag_name['name']] = Time.parse(time_string) @tag_times_hash[tag_name['name']] = Time.parse(time_string)
end end
@ -558,17 +586,22 @@ module GitHubChangelogGenerator
if @options[:verbose] if @options[:verbose]
print "Fetching closed issues...\r" print "Fetching closed issues...\r"
end end
response = @github.issues.list user: @options[:user], repo: @options[:project], state: 'closed', filter: 'all', labels: nil
issues = [] issues = []
page_i = 0
count_pages = response.count_pages begin
response.each_page do |page| response = @github.issues.list user: @options[:user], repo: @options[:project], state: 'closed', filter: 'all', labels: nil
page_i += PER_PAGE_NUMBER page_i = 0
print "Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r" count_pages = response.count_pages
issues.concat(page) response.each_page do |page|
end page_i += PER_PAGE_NUMBER
print "Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
issues.concat(page)
end
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
print " \r" print " \r"
@ -610,7 +643,11 @@ module GitHubChangelogGenerator
issues.each_slice(max_thread_number) { |issues_slice| issues.each_slice(max_thread_number) { |issues_slice|
issues_slice.each { |issue| issues_slice.each { |issue|
threads << Thread.new { threads << Thread.new {
obj = @github.issues.events.list user: @options[:user], repo: @options[:project], issue_number: issue['number'] begin
obj = @github.issues.events.list user: @options[:user], repo: @options[:project], issue_number: issue['number']
rescue
puts "Warning: GitHub API rate limit exceed (5000 per hour), change log may not contain some issues.".yellow
end
issue[:events] = obj.body issue[:events] = obj.body
print "Fetching events for issues and PR: #{i+1}/#{@issues.count + @pull_requests.count}\r" print "Fetching events for issues and PR: #{i+1}/#{@issues.count + @pull_requests.count}\r"
i +=1 i +=1