Merge branch 'fetch-refactoring' into develop

This commit is contained in:
Petr Korolev 2015-05-19 09:31:38 +03:00
commit 7cfb182a82
3 changed files with 65 additions and 31 deletions

View File

@ -6,6 +6,8 @@
**Merged pull requests:** **Merged pull requests:**
- this pr will be closed and shouldn't appear in changelog [\#7](https://github.com/skywinder/changelog_test/pull/7) ([skywinder](https://github.com/skywinder))
- This PR SHOULD NOT appear in change log! [\#6](https://github.com/skywinder/changelog_test/pull/6) ([skywinder](https://github.com/skywinder)) - This PR SHOULD NOT appear in change log! [\#6](https://github.com/skywinder/changelog_test/pull/6) ([skywinder](https://github.com/skywinder))
- Add automatically generated change log file. [\#5](https://github.com/skywinder/changelog_test/pull/5) ([skywinder](https://github.com/skywinder)) - Add automatically generated change log file. [\#5](https://github.com/skywinder/changelog_test/pull/5) ([skywinder](https://github.com/skywinder))

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,31 +57,43 @@ module GitHubChangelogGenerator
tags = [] tags = []
begin check_github_response { github_fetch_tags(tags) }
response = @github.repos.tags @options[:user], @options[:project]
page_i = 0
count_pages = response.count_pages
response.each_page do |page|
page_i += PER_PAGE_NUMBER
print "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
tags.concat(page)
end
print " \r"
if tags.count == 0
@logger.warn "Warning: Can't find any tags in repo.\
Make sure, that you push tags to remote repo via 'git push --tags'".yellow
elsif @options[:verbose]
@logger.info "Found #{tags.count} tags"
end
rescue
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
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)
response = @github.repos.tags @options[:user], @options[:project]
page_i = 0
count_pages = response.count_pages
response.each_page do |page|
page_i += PER_PAGE_NUMBER
print "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
tags.concat(page)
end
print " \r"
if tags.count == 0
@logger.warn "Warning: Can't find any tags in repo.\
Make sure, that you push tags to remote repo via 'git push --tags'".yellow
elsif @options[:verbose]
@logger.info "Found #{tags.count} tags"
end
end
# This method fetch all closed issues and separate them to pull requests and pure issues # This method fetch all closed issues and separate them to pull requests and pure issues
# (pull request is kind of issue in term of GitHub) # (pull request is kind of issue in term of GitHub)
# @return [Tuple] with issues and pull requests # @return [Tuple] with issues and pull requests

View File

@ -1,5 +1,16 @@
VALID_TOKEN = "0123456789abcdef"
INVALID_TOKEN = "0000000000000000"
DEFAULT_OPTIONS = { user: "skywinder",
project: "changelog_test" }
def options_with_invalid_token
options = DEFAULT_OPTIONS
options[:token] = INVALID_TOKEN
options
end
describe GitHubChangelogGenerator::Fetcher do describe GitHubChangelogGenerator::Fetcher do
VALID_TOKEN = "0123456789abcdef"
before(:all) do before(:all) do
@fetcher = GitHubChangelogGenerator::Fetcher.new @fetcher = GitHubChangelogGenerator::Fetcher.new
end end
@ -33,4 +44,16 @@ describe GitHubChangelogGenerator::Fetcher do
it { is_expected.to eq(VALID_TOKEN) } it { is_expected.to eq(VALID_TOKEN) }
end end
end end
describe "#github_fetch_tags" do
context "when wrong token provided" do
before do
options = options_with_invalid_token
@fetcher = GitHubChangelogGenerator::Fetcher.new(options)
end
it "should raise Unauthorized error" do
expect { @fetcher.github_fetch_tags [] }.to raise_error Github::Error::Unauthorized
end
end
end
end end