Skip to content

Parse options file options into arrays, integers, flags, and other #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions lib/github_changelog_generator/parser_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def file

def parse_line!(line)
key_sym, value = extract_pair(line)
value = true if value =~ /^(true|t|yes|y|1)$/i
value = false if value =~ /^(false|f|no|n|0)$/i
@options[key_sym] = value
@options[translate_option_name(key_sym)] = convert_value(value, key_sym)
rescue
raise ParserError, "Config file #{file} is incorrect in line \"#{line.gsub(/[\n\r]+/, '')}\""
end
Expand All @@ -38,5 +36,39 @@ def extract_pair(line)
key, value = line.split("=", 2)
[key.sub("-", "_").to_sym, value.gsub(/[\n\r]+/, "")]
end

KNOWN_ARRAY_KEYS = [:exclude_labels, :include_labels, :bug_labels,
:enhancement_labels, :between_tags, :exclude_tags]
KNOWN_INTEGER_KEYS = [:max_issues]

def convert_value(value, key_sym)
if KNOWN_ARRAY_KEYS.include?(key_sym)
value.split(",")
elsif KNOWN_INTEGER_KEYS.include?(key_sym)
value.to_i
elsif value =~ /^(true|t|yes|y|1)$/i
true
elsif value =~ /^(false|f|no|n|0)$/i
false
else
value
end
end

def translate_option_name(key_sym)
{
bugs_label: :bug_prefix,
enhancement_label: :enhancement_prefix,
issues_label: :issue_prefix,
header_label: :header,
front_matter: :frontmatter,
pr_label: :merge_prefix,
issues_wo_labels: :add_issues_wo_labels,
pr_wo_labels: :add_pr_wo_labels,
pull_requests: :pulls,
filter_by_milestone: :filter_issues_by_milestone,
github_api: :github_endpoint
}.fetch(key_sym) { key_sym }
end
end
end
3 changes: 2 additions & 1 deletion spec/files/github_changelog_params_327
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exclude-labels=73a91042-da6f-11e5-9335-1040f38d7f90,7adf83b4-da6f-11e5-ae18-1040f38d7f90
exclude-labels=73a91042-da6f-11e5-9335-1040f38d7f90,7adf83b4-da6f-11e5-ae18-1040f38d7f90
header_label=# My changelog
8 changes: 7 additions & 1 deletion spec/unit/parse_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@
params_file: "spec/files/github_changelog_params_327"
}
end

it "reads exclude_labels into an Array" do
pending("Related with Bug #327.")
expect { parse.parse! }.to change { options[:exclude_labels] }
.from(nil)
.to(["73a91042-da6f-11e5-9335-1040f38d7f90", "7adf83b4-da6f-11e5-ae18-1040f38d7f90"])
end

it "translates given header_label into the :header option" do
expect { parse.parse! }.to change { options[:header] }
.from(nil)
.to("# My changelog")
end
end
end
end
Expand Down