This commit is contained in:
Olle Jonsson 2016-09-27 23:44:49 +02:00
parent 0189c2d736
commit db1ffdd59b
10 changed files with 40 additions and 39 deletions

View File

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Array
def stringify_keys_deep!
new_ar = []

View File

@ -75,7 +75,7 @@ module GitHubChangelogGenerator
begin
commit = @fetcher.fetch_commit(event)
issue["actual_date"] = commit["commit"]["author"]["date"]
# issue['actual_date'] = commit['author']['date']
rescue
puts "Warning: Can't fetch commit #{event['commit_id']}. It is probably referenced from another repo."

View File

@ -178,9 +178,9 @@ module GitHubChangelogGenerator
return "#{line} ({Null user})" unless user
if @options[:usernames_as_github_logins]
"#{line} (@#{user["login"]})"
"#{line} (@#{user['login']})"
else
"#{line} ([#{user["login"]}](#{user["html_url"]}))"
"#{line} ([#{user['login']}](#{user['html_url']}))"
end
end
end

View File

@ -101,7 +101,7 @@ module GitHubChangelogGenerator
def get_filtered_tags(all_tags)
filtered_tags = filter_since_tag(all_tags)
filtered_tags = filter_due_tag(filtered_tags)
filtered_tags = filter_between_tags(filtered_tags)
filter_between_tags(filtered_tags)
end
# @param [Array] all_tags all tags
@ -148,7 +148,7 @@ module GitHubChangelogGenerator
# @return [Array] filtered tags according :between_tags option
def filter_between_tags(all_tags)
filtered_tags = all_tags
tag_names = filtered_tags.map { |ft| ft['name'] }
tag_names = filtered_tags.map { |ft| ft["name"] }
if @options[:between_tags]
@options[:between_tags].each do |tag|

View File

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Hash
def stringify_keys_deep!
new_hash = {}

View File

@ -1,10 +1,10 @@
# frozen_string_literal: true
module GitHubChangelogGenerator
# A Fetcher responsible for all requests to GitHub and all basic manipulation with related data
# (such as filtering, validating, e.t.c)
#
# Example:
# fetcher = GitHubChangelogGenerator::OctoFetcher.new options
# fetcher = GitHubChangelogGenerator::OctoFetcher.new(options)
class OctoFetcher
PER_PAGE_NUMBER = 100
MAX_THREAD_NUMBER = 1
@ -14,16 +14,21 @@ module GitHubChangelogGenerator
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 = {})
# @param options [Hash] Options passed in
# @option options [String] :user GitHub username
# @option options [String] :project GitHub project
# @option options [String] :since Only issues updated at or after this time are returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. eg. Time.parse("2016-01-01 10:00:00").iso8601
# @option options [Boolean] :http_cache Use ActiveSupport::Cache::FileStore to cache http requests
# @option options [Boolean] :cache_file If using http_cache, this is the cache file path
# @option options [Boolean] :cache_log If using http_cache, this is the cache log file path
def initialize(options = {}) # rubocop:disable Metrics/CyclomaticComplexity
@options = options || {}
@user = @options[:user]
@project = @options[:project]
# Only issues updated at or after this time are returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
@since = @options[:since] # eg. Time.parse("2016-01-01 10:00:00").iso8601
# Use ActiveSupport::Cache::FileStore to cache http requests
@since = @options[:since]
@http_cache = @options[:http_cache]
@cache_file = @options.fetch(:cache_file, '/tmp/github-changelog-http-cache') if @http_cache
@cache_log = @options.fetch(:cache_log, '/tmp/github-changelog-logger.log') if @http_cache
@cache_file = @options.fetch(:cache_file, "/tmp/github-changelog-http-cache") if @http_cache
@cache_log = @options.fetch(:cache_log, "/tmp/github-changelog-logger.log") if @http_cache
init_cache if @http_cache
@github_token = fetch_github_token
@ -33,17 +38,16 @@ module GitHubChangelogGenerator
@github_options[:access_token] = @github_token unless @github_token.nil?
@github_options[:api_endpoint] = @options[:github_endpoint] unless @options[:github_endpoint].nil?
client_type = @options[:github_endpoint].nil? ? Octokit::Client : Octokit::EnterpriseAdminClient
@client = client_type.new(@github_options)
end
def init_cache
middleware_opts = {
:serializer => Marshal,
:store => ActiveSupport::Cache::FileStore.new(@cache_file),
:logger => Logger.new(@cache_log),
:shared_cache => false
serializer: Marshal,
store: ActiveSupport::Cache::FileStore.new(@cache_file),
logger: Logger.new(@cache_log),
shared_cache: false
}
stack = Faraday::RackBuilder.new do |builder|
builder.use Faraday::HttpCache, middleware_opts
@ -98,7 +102,7 @@ module GitHubChangelogGenerator
if tags.count == 0
Helper.log.warn "Warning: Can't find any tags in repo.\
Make sure, that you push tags to remote repo via 'git push --tags'".yellow
Make sure, that you push tags to remote repo via 'git push --tags'"
else
Helper.log.info "Found #{tags.count} tags"
end
@ -264,11 +268,11 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
begin
value = yield
rescue Octokit::Unauthorized => e
Helper.log.error e.message.red
Helper.log.error e.message
abort "Error: wrong GitHub token"
rescue Octokit::Forbidden => e
Helper.log.warn e.message.red
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
Helper.log.warn e.message
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
Helper.log.warn @client.rate_limit
end
value
@ -293,7 +297,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
def fetch_github_token
env_var = @options[:token] ? @options[:token] : (ENV.fetch CHANGELOG_GITHUB_TOKEN, nil)
Helper.log.warn NO_TOKEN_PROVIDED.yellow unless env_var
Helper.log.warn NO_TOKEN_PROVIDED unless env_var
env_var
end

View File

@ -215,8 +215,8 @@ module GitHubChangelogGenerator
enhancement_prefix: "**Implemented enhancements:**",
git_remote: "origin",
http_cache: true,
cache_file: '/tmp/github-changelog-http-cache',
cache_log: '/tmp/github-changelog-logger.log',
cache_file: "/tmp/github-changelog-http-cache",
cache_log: "/tmp/github-changelog-logger.log"
}
end

View File

@ -46,9 +46,9 @@ VCR.configure do |c|
preserve_exact_body_bytes: true,
decode_compressed_response: true
}
c.filter_sensitive_data('<GITHUB_TOKEN>') {
c.filter_sensitive_data("<GITHUB_TOKEN>") do
"token #{ENV.fetch('CHANGELOG_GITHUB_TOKEN')}"
}
end
c.configure_rspec_metadata!

View File

@ -2,7 +2,7 @@
describe GitHubChangelogGenerator::Generator do
def tag_with_name(tag)
{
'name' => tag
"name" => tag
}
end

View File

@ -1,3 +1,4 @@
# frozen_string_literal: true
VALID_TOKEN = "0123456789abcdef"
INVALID_TOKEN = "0000000000000000"
@ -60,7 +61,6 @@ describe GitHubChangelogGenerator::OctoFetcher do
describe "#github_fetch_tags" do
context "when wrong token provided", :vcr do
let(:options) do
{
user: "skywinder",
@ -75,7 +75,6 @@ describe GitHubChangelogGenerator::OctoFetcher do
end
context "when API call is valid", :vcr do
it "should return tags" do
expected_tags = [{ "name" => "v0.0.3",
"zipball_url" =>
@ -125,8 +124,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
end
describe "#fetch_closed_issues_and_pr" do
context "when API call is valid", :vcr do
context "when API call is valid", :vcr do
it "returns issues" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expect(issues.size).to eq(7)
@ -173,7 +171,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
"state" => "closed",
"locked" => false,
"assignee" => nil,
"assignees" => [],
"assignees" => [],
"milestone" => nil,
"comments" => 0,
"created_at" => "2015-07-16T12:06:08Z",
@ -229,7 +227,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
"state" => "closed",
"locked" => false,
"assignee" => nil,
"assignees" => [],
"assignees" => [],
"milestone" => nil,
"comments" => 0,
"created_at" => "2016-01-05T09:24:08Z",
@ -322,7 +320,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
"state" => "closed",
"locked" => false,
"assignee" => nil,
"assignees" => [],
"assignees" => [],
"milestone" => nil,
"comments" => 0,
"created_at" => "2015-07-16T12:06:08Z",
@ -411,7 +409,6 @@ describe GitHubChangelogGenerator::OctoFetcher do
describe "#fetch_date_of_tag" do
context "when API call is valid", :vcr do
it "returns date" do
tag = { "name" => "v0.0.3",
"zipball_url" =>
@ -431,7 +428,6 @@ describe GitHubChangelogGenerator::OctoFetcher do
describe "#fetch_commit" do
context "when API call is valid", :vcr do
it "returns commit" do
event = { "id" => 357_462_189,
"url" =>
@ -481,8 +477,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
"https://api.github.com/repos/skywinder/changelog_test/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51",
# OLD API: "https://api.github.com/repos/skywinder/changelog_test/git/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"html_url" =>
"https://github.com/skywinder/changelog_test/commit/7ec095e5e3caceacedabf44d0b9b10da17c92e51" }]
]
"https://github.com/skywinder/changelog_test/commit/7ec095e5e3caceacedabf44d0b9b10da17c92e51" }]]
]
expectations.each do |property, val|