wrap github methods in another method

This commit is contained in:
Petr Korolev 2015-05-18 15:04:15 +03:00
parent 8a3ff1b799
commit 1ee1dfd50f

View File

@ -9,8 +9,10 @@ module GitHubChangelogGenerator
class Fetcher class Fetcher
PER_PAGE_NUMBER = 30 PER_PAGE_NUMBER = 30
CHANGELOG_GITHUB_TOKEN = "CHANGELOG_GITHUB_TOKEN" CHANGELOG_GITHUB_TOKEN = "CHANGELOG_GITHUB_TOKEN"
GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: GitHub API rate limit (5000 per hour) exceeded, change log may be " \ GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: Can't finish operation: GitHub API rate limit exceeded, change log may be " \
"missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument." "missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."
NO_TOKEN_PROVIDED = "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found. " \
"This script can make only 50 requests to GitHub API per hour without token!"
def initialize(options = {}) def initialize(options = {})
@options = options @options = options
@ -29,11 +31,7 @@ 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?
begin @github = check_github_response{Github.new github_options}
@github = Github.new github_options
rescue
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
end end
# Returns GitHub token. First try to use variable, provided by --token option, # Returns GitHub token. First try to use variable, provided by --token option,
@ -44,8 +42,7 @@ module GitHubChangelogGenerator
env_var = @options[:token] ? @options[:token] : (ENV.fetch CHANGELOG_GITHUB_TOKEN, nil) env_var = @options[:token] ? @options[:token] : (ENV.fetch CHANGELOG_GITHUB_TOKEN, nil)
unless env_var unless env_var
@logger.warn "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.".yellow @logger.warn NO_TOKEN_PROVIDED.yellow
@logger.warn "This script can make only 50 requests to GitHub API per hour without token!".yellow
end end
env_var env_var
@ -60,21 +57,24 @@ module GitHubChangelogGenerator
tags = [] tags = []
begin check_github_response { github_fetch_tags(tags) }
github_fetch_tags(tags)
rescue Github::Error::Unauthorized => e
@logger.error e.body.red
@logger.error "Error: wrong github token provided".red
rescue Github::Error::Forbidden => e
@logger.warn e.body.red
unless @options[:token].nil?
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
end
tags tags
end end
def check_github_response
begin
value = yield
rescue Github::Error::Unauthorized => e
@logger.error e.body.red
abort "Error: wrong GitHub token"
rescue Github::Error::Forbidden => e
@logger.warn e.body.red
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
value
end
def github_fetch_tags(tags) def github_fetch_tags(tags)
response = @github.repos.tags @options[:user], @options[:project] response = @github.repos.tags @options[:user], @options[:project]
page_i = 0 page_i = 0
@ -118,8 +118,6 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
break if @options[:max_issues] && issues.length >= @options[:max_issues] break if @options[:max_issues] && issues.length >= @options[:max_issues]
end end
rescue rescue
puts e.message
puts e.backtrace.inspect
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end end