From 41271b1ecbd0161ad45c0b48304c8e38fc6c72e0 Mon Sep 17 00:00:00 2001 From: Andrew Waage Date: Mon, 23 May 2016 18:28:00 -0700 Subject: [PATCH] cleaned up caching and added to options parser --- lib/github_changelog_generator/octo_fetcher.rb | 14 ++++++++++---- lib/github_changelog_generator/parser.rb | 14 +++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/github_changelog_generator/octo_fetcher.rb b/lib/github_changelog_generator/octo_fetcher.rb index 52e4c12..8934d3a 100644 --- a/lib/github_changelog_generator/octo_fetcher.rb +++ b/lib/github_changelog_generator/octo_fetcher.rb @@ -20,6 +20,12 @@ module GitHubChangelogGenerator @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 + @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 + init_cache if @http_cache + @github_token = fetch_github_token @request_options = { :per_page => PER_PAGE_NUMBER } @@ -27,16 +33,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? - init_middleware + client_type = @options[:github_endpoint].nil? ? Octokit::Client : Octokit::EnterpriseAdminClient @client = client_type.new(@github_options) end - def init_middleware + def init_cache middleware_opts = { :serializer => Marshal, - :store => ActiveSupport::Cache::FileStore.new('/tmp/cache'), - :logger => Logger.new('/tmp/github-changelog-logger.log'), + :store => ActiveSupport::Cache::FileStore.new(@cache_file), + :logger => Logger.new(@cache_log), :shared_cache => false } stack = Faraday::RackBuilder.new do |builder| diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index 70dde2d..14deb0a 100755 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -161,6 +161,15 @@ module GitHubChangelogGenerator opts.on("--release-branch [RELEASE-BRANCH]", "Limit pull requests to the release branch, such as master or release") do |release_branch| options[:release_branch] = release_branch end + opts.on("--[no-]http-cache", "Use HTTP Cache to cache Github API requests (useful for large repos) Default is true.") do |http_cache| + options[:http_cache] = http_cache + end + opts.on("--cache-file [CACHE-FILE]", "Filename to use for cache. Default is /tmp/github-changelog-http-cache") do |cache_file| + options[:cache_file] = cache_file + end + opts.on("--cache-log [CACHE-LOG]", "Filename to use for cache log. Default is /tmp/github-changelog-logger.log") do |cache_log| + options[:cache_log] = cache_log + end opts.on("--[no-]verbose", "Run verbosely. Default is true") do |v| options[:verbose] = v end @@ -204,7 +213,10 @@ module GitHubChangelogGenerator issue_prefix: "**Closed issues:**", bug_prefix: "**Fixed bugs:**", enhancement_prefix: "**Implemented enhancements:**", - git_remote: "origin" + git_remote: "origin", + http_cache: true, + cache_file: '/tmp/github-changelog-http-cache', + cache_log: '/tmp/github-changelog-logger.log', } end