Merge pull request #432 from olleolleolle/refactor/octo-fetcher-and-options
OctoFetcher, Options: Refactoring
This commit is contained in:
commit
9f08e3b014
|
@ -10,13 +10,12 @@ require "multi_json"
|
|||
require "benchmark"
|
||||
|
||||
require_relative "github_changelog_generator/helper"
|
||||
require_relative "github_changelog_generator/options"
|
||||
require_relative "github_changelog_generator/parser"
|
||||
require_relative "github_changelog_generator/parser_file"
|
||||
require_relative "github_changelog_generator/generator/generator"
|
||||
require_relative "github_changelog_generator/version"
|
||||
require_relative "github_changelog_generator/reader"
|
||||
require_relative "github_changelog_generator/hash"
|
||||
require_relative "github_changelog_generator/array"
|
||||
|
||||
# The main module, where placed all classes (now, at least)
|
||||
module GitHubChangelogGenerator
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
class Array
|
||||
def stringify_keys_deep!
|
||||
new_ar = []
|
||||
each do |value|
|
||||
new_value = value
|
||||
if value.is_a?(Hash) || value.is_a?(Array)
|
||||
new_value = value.stringify_keys_deep!
|
||||
end
|
||||
new_ar << new_value
|
||||
end
|
||||
new_ar
|
||||
end
|
||||
end
|
|
@ -1,16 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
class Hash
|
||||
def stringify_keys_deep!
|
||||
new_hash = {}
|
||||
keys.each do |k|
|
||||
ks = k.respond_to?(:to_s) ? k.to_s : k
|
||||
new_hash[ks] = if values_at(k).first.is_a?(Hash) || values_at(k).first.is_a?(Array)
|
||||
values_at(k).first.send(:stringify_keys_deep!)
|
||||
else
|
||||
values_at(k).first
|
||||
end
|
||||
end
|
||||
|
||||
new_hash
|
||||
end
|
||||
end
|
|
@ -107,7 +107,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
|
|||
Helper.log.info "Found #{tags.count} tags"
|
||||
end
|
||||
# tags are a Sawyer::Resource. Convert to hash
|
||||
tags = tags.map { |h| h.to_hash.stringify_keys_deep! }
|
||||
tags = tags.map { |h| stringify_keys_deep(h.to_hash) }
|
||||
tags
|
||||
end
|
||||
|
||||
|
@ -137,7 +137,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
|
|||
print_empty_line
|
||||
Helper.log.info "Received issues: #{issues.count}"
|
||||
|
||||
issues = issues.map { |h| h.to_hash.stringify_keys_deep! }
|
||||
issues = issues.map { |h| stringify_keys_deep(h.to_hash) }
|
||||
|
||||
# separate arrays of issues and pull requests:
|
||||
issues.partition do |x|
|
||||
|
@ -168,7 +168,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
|
|||
print_empty_line
|
||||
|
||||
Helper.log.info "Pull Request count: #{pull_requests.count}"
|
||||
pull_requests = pull_requests.map { |h| h.to_hash.stringify_keys_deep! }
|
||||
pull_requests = pull_requests.map { |h| stringify_keys_deep(h.to_hash) }
|
||||
pull_requests
|
||||
end
|
||||
|
||||
|
@ -187,7 +187,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
|
|||
iterate_pages(@client, "issue_events", issue["number"], {}) do |new_event|
|
||||
issue["events"].concat(new_event)
|
||||
end
|
||||
issue["events"] = issue["events"].map { |h| h.to_hash.stringify_keys_deep! }
|
||||
issue["events"] = issue["events"].map { |h| stringify_keys_deep(h.to_hash) }
|
||||
print_in_same_line("Fetching events for issues and PR: #{i + 1}/#{issues.count}")
|
||||
i += 1
|
||||
end
|
||||
|
@ -209,7 +209,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
|
|||
# @return [Time] time of specified tag
|
||||
def fetch_date_of_tag(tag)
|
||||
commit_data = check_github_response { @client.commit(user_project, tag["commit"]["sha"]) }
|
||||
commit_data = commit_data.to_hash.stringify_keys_deep!
|
||||
commit_data = stringify_keys_deep(commit_data.to_hash)
|
||||
|
||||
commit_data["commit"]["committer"]["date"]
|
||||
end
|
||||
|
@ -220,13 +220,28 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
|
|||
def fetch_commit(event)
|
||||
check_github_response do
|
||||
commit = @client.commit(user_project, event["commit_id"])
|
||||
commit = commit.to_hash.stringify_keys_deep!
|
||||
commit = stringify_keys_deep(commit.to_hash)
|
||||
commit
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def stringify_keys_deep(indata)
|
||||
case indata
|
||||
when Array
|
||||
indata.map do |value|
|
||||
stringify_keys_deep(value)
|
||||
end
|
||||
when Hash
|
||||
indata.each_with_object({}) do |(k, v), output|
|
||||
output[k.to_s] = stringify_keys_deep(v)
|
||||
end
|
||||
else
|
||||
indata
|
||||
end
|
||||
end
|
||||
|
||||
# Iterates through all pages until there are no more :next pages to follow
|
||||
# yields the result per page
|
||||
#
|
||||
|
|
91
lib/github_changelog_generator/options.rb
Normal file
91
lib/github_changelog_generator/options.rb
Normal file
|
@ -0,0 +1,91 @@
|
|||
# frozen_string_literal: true
|
||||
require "delegate"
|
||||
module GitHubChangelogGenerator
|
||||
class Options < SimpleDelegator
|
||||
UnsupportedOptionError = Class.new(ArgumentError)
|
||||
|
||||
KNOWN_OPTIONS = [
|
||||
:add_issues_wo_labels,
|
||||
:add_pr_wo_labels,
|
||||
:author,
|
||||
:base,
|
||||
:between_tags,
|
||||
:bug_labels,
|
||||
:bug_prefix,
|
||||
:cache_file,
|
||||
:cache_log,
|
||||
:compare_link,
|
||||
:date_format,
|
||||
:due_tag,
|
||||
:enhancement_labels,
|
||||
:enhancement_prefix,
|
||||
:exclude_labels,
|
||||
:exclude_tags,
|
||||
:exclude_tags_regex,
|
||||
:filter_issues_by_milestone,
|
||||
:frontmatter,
|
||||
:future_release,
|
||||
:git_remote,
|
||||
:github_endpoint,
|
||||
:github_site,
|
||||
:header,
|
||||
:http_cache,
|
||||
:include_labels,
|
||||
:issue_prefix,
|
||||
:issues,
|
||||
:max_issues,
|
||||
:merge_prefix,
|
||||
:output,
|
||||
:project,
|
||||
:pulls,
|
||||
:release_branch,
|
||||
:release_url,
|
||||
:simple_list,
|
||||
:since_tag,
|
||||
:token,
|
||||
:unreleased,
|
||||
:unreleased_label,
|
||||
:unreleased_only,
|
||||
:user,
|
||||
:usernames_as_github_logins,
|
||||
:verbose
|
||||
]
|
||||
|
||||
THESE_ARE_DIFFERENT = [
|
||||
:tag1,
|
||||
:tag2
|
||||
]
|
||||
|
||||
def initialize(values)
|
||||
super(values)
|
||||
unsupported_options.any? && raise(UnsupportedOptionError, unsupported_options.inspect)
|
||||
end
|
||||
|
||||
def []=(key, val)
|
||||
supported_option?(key) || raise(UnsupportedOptionError, key.inspect)
|
||||
values[key] = val
|
||||
end
|
||||
|
||||
def to_hash
|
||||
values
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def values
|
||||
__getobj__
|
||||
end
|
||||
|
||||
def unsupported_options
|
||||
values.keys - supported_options
|
||||
end
|
||||
|
||||
def supported_option?(key)
|
||||
supported_options.include?(key)
|
||||
end
|
||||
|
||||
def supported_options
|
||||
KNOWN_OPTIONS + THESE_ARE_DIFFERENT
|
||||
end
|
||||
end
|
||||
end
|
|
@ -187,7 +187,7 @@ module GitHubChangelogGenerator
|
|||
|
||||
# @return [Hash] Default options
|
||||
def self.default_options
|
||||
{
|
||||
Options.new(
|
||||
tag1: nil,
|
||||
tag2: nil,
|
||||
date_format: "%Y-%m-%d",
|
||||
|
@ -217,7 +217,7 @@ module GitHubChangelogGenerator
|
|||
http_cache: true,
|
||||
cache_file: "/tmp/github-changelog-http-cache",
|
||||
cache_log: "/tmp/github-changelog-logger.log"
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
# If `:user` or `:project` not set in options, try setting them
|
||||
|
|
42
spec/unit/options_spec.rb
Normal file
42
spec/unit/options_spec.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
# frozen_string_literal: true
|
||||
RSpec.describe GitHubChangelogGenerator::Options do
|
||||
describe "#initialize" do
|
||||
context "with known options" do
|
||||
it "instantiates successfully" do
|
||||
expect(described_class.new(user: "olle")[:user]).to eq("olle")
|
||||
end
|
||||
end
|
||||
|
||||
context "with unknown options" do
|
||||
it "raises an error" do
|
||||
expect do
|
||||
described_class.new(
|
||||
git_remote: "origin",
|
||||
strength: "super-strength",
|
||||
wisdom: "deep"
|
||||
)
|
||||
end.to raise_error("[:strength, :wisdom]")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#[]=(key, value)" do
|
||||
let(:options) { described_class.new(git_remote: "origin") }
|
||||
|
||||
context "with known options" do
|
||||
it "sets the option value" do
|
||||
expect do
|
||||
options[:git_remote] = "in the cloud"
|
||||
end.to change { options[:git_remote] }.to "in the cloud"
|
||||
end
|
||||
end
|
||||
|
||||
context "with unknown options" do
|
||||
it "raises an error" do
|
||||
expect do
|
||||
options[:charisma] = 8
|
||||
end.to raise_error(":charisma")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -85,10 +85,6 @@ describe GitHubChangelogGenerator::Reader do
|
|||
context "angular.js.md" do
|
||||
it { is_expected.to be_an(Array) }
|
||||
it { is_expected.not_to be_empty }
|
||||
it do
|
||||
pending("Implement heading_level for parser.")
|
||||
expect(subject.count).to eq(134)
|
||||
end
|
||||
# it do
|
||||
# pending('Implement heading_level for parser.')
|
||||
# expect(subject.first).to include('version' => '1.4.0-beta.6 cookie-liberation')
|
||||
|
|
Loading…
Reference in New Issue
Block a user