From bd024f6b63ce0e70282b3da33d29b1fe2440200e Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Wed, 28 Sep 2016 00:49:10 +0200 Subject: [PATCH] Style: Use attr_reader for options - prefer parentheses --- .../generator/generator.rb | 46 ++++++++--------- .../generator/generator_fetcher.rb | 12 ++--- .../generator/generator_generation.rb | 51 +++++-------------- .../generator/generator_processor.rb | 20 ++++---- .../generator/generator_tags.rb | 42 +++++++-------- 5 files changed, 74 insertions(+), 97 deletions(-) diff --git a/lib/github_changelog_generator/generator/generator.rb b/lib/github_changelog_generator/generator/generator.rb index 7425a64..04eefad 100644 --- a/lib/github_changelog_generator/generator/generator.rb +++ b/lib/github_changelog_generator/generator/generator.rb @@ -18,18 +18,18 @@ module GitHubChangelogGenerator # Example: # generator = GitHubChangelogGenerator::Generator.new # content = generator.compound_changelog - def initialize(options = nil) - @options = options || {} + def initialize(options = {}) + @options = options @tag_times_hash = {} - @fetcher = GitHubChangelogGenerator::OctoFetcher.new @options + @fetcher = GitHubChangelogGenerator::OctoFetcher.new(options) end def fetch_issues_and_pr issues, pull_requests = @fetcher.fetch_closed_issues_and_pr - @pull_requests = @options[:pulls] ? get_filtered_pull_requests(pull_requests) : [] + @pull_requests = options[:pulls] ? get_filtered_pull_requests(pull_requests) : [] - @issues = @options[:issues] ? get_filtered_issues(issues) : [] + @issues = options[:issues] ? get_filtered_issues(issues) : [] fetch_events_for_issues_and_pr detect_actual_closed_dates(@issues + @pull_requests) @@ -61,18 +61,18 @@ module GitHubChangelogGenerator newer_tag_link, newer_tag_name, newer_tag_time = detect_link_tag_time(newer_tag) github_site = options[:github_site] || "https://github.com" - project_url = "#{github_site}/#{@options[:user]}/#{@options[:project]}" + project_url = "#{github_site}/#{options[:user]}/#{options[:project]}" log = generate_header(newer_tag_name, newer_tag_link, newer_tag_time, older_tag_name, project_url) - if @options[:issues] + if options[:issues] # Generate issues: log += issues_to_log(issues, pull_requests) end - if @options[:pulls] + if options[:pulls] # Generate pull requests: - log += generate_sub_section(pull_requests, @options[:merge_prefix]) + log += generate_sub_section(pull_requests, options[:merge_prefix]) end log @@ -87,9 +87,9 @@ module GitHubChangelogGenerator log = "" bugs_a, enhancement_a, issues_a = parse_by_sections(issues, pull_requests) - log += generate_sub_section(enhancement_a, @options[:enhancement_prefix]) - log += generate_sub_section(bugs_a, @options[:bug_prefix]) - log += generate_sub_section(issues_a, @options[:issue_prefix]) + log += generate_sub_section(enhancement_a, options[:enhancement_prefix]) + log += generate_sub_section(bugs_a, options[:bug_prefix]) + log += generate_sub_section(issues_a, options[:issue_prefix]) log end @@ -107,31 +107,31 @@ module GitHubChangelogGenerator issues.each do |dict| added = false dict["labels"].each do |label| - if @options[:bug_labels].include? label["name"] - bugs_a.push dict + if options[:bug_labels].include?(label["name"]) + bugs_a.push(dict) added = true next end - if @options[:enhancement_labels].include? label["name"] - enhancement_a.push dict + if options[:enhancement_labels].include?(label["name"]) + enhancement_a.push(dict) added = true next end end - issues_a.push dict unless added + issues_a.push(dict) unless added end added_pull_requests = [] pull_requests.each do |pr| pr["labels"].each do |label| - if @options[:bug_labels].include? label["name"] - bugs_a.push pr - added_pull_requests.push pr + if options[:bug_labels].include?(label["name"]) + bugs_a.push(pr) + added_pull_requests.push(pr) next end - if @options[:enhancement_labels].include? label["name"] - enhancement_a.push pr - added_pull_requests.push pr + if options[:enhancement_labels].include?(label["name"]) + enhancement_a.push(pr) + added_pull_requests.push(pr) next end end diff --git a/lib/github_changelog_generator/generator/generator_fetcher.rb b/lib/github_changelog_generator/generator/generator_fetcher.rb index 1425fb7..e4172f3 100644 --- a/lib/github_changelog_generator/generator/generator_fetcher.rb +++ b/lib/github_changelog_generator/generator/generator_fetcher.rb @@ -6,7 +6,7 @@ module GitHubChangelogGenerator # Fetch event for issues and pull requests # @return [Array] array of fetched issues def fetch_events_for_issues_and_pr - if @options[:verbose] + if options[:verbose] print "Fetching events for issues and PR: 0/#{@issues.count + @pull_requests.count}\r" end @@ -16,7 +16,7 @@ module GitHubChangelogGenerator # Async fetching of all tags dates def fetch_tags_dates(tags) - print "Fetching tag dates...\r" if @options[:verbose] + print "Fetching tag dates...\r" if options[:verbose] # Async fetching tags: threads = [] i = 0 @@ -25,17 +25,17 @@ module GitHubChangelogGenerator print " \r" threads << Thread.new do get_time_of_tag(tag) - print "Fetching tags dates: #{i + 1}/#{all}\r" if @options[:verbose] + print "Fetching tags dates: #{i + 1}/#{all}\r" if options[:verbose] i += 1 end end threads.each(&:join) - puts "Fetching tags dates: #{i}" if @options[:verbose] + puts "Fetching tags dates: #{i}" if options[:verbose] end # Find correct closed dates, if issues was closed by commits def detect_actual_closed_dates(issues) - print "Fetching closed dates for issues...\r" if @options[:verbose] + print "Fetching closed dates for issues...\r" if options[:verbose] issues.each_slice(MAX_THREAD_NUMBER) do |issues_slice| threads = [] @@ -44,7 +44,7 @@ module GitHubChangelogGenerator end threads.each(&:join) end - puts "Fetching closed dates for issues: Done!" if @options[:verbose] + puts "Fetching closed dates for issues: Done!" if options[:verbose] end # Fill :actual_date parameter of specified issue by closed date of the commit, if it was closed by commit. diff --git a/lib/github_changelog_generator/generator/generator_generation.rb b/lib/github_changelog_generator/generator/generator_generation.rb index d3f0c53..cef7abe 100644 --- a/lib/github_changelog_generator/generator/generator_generation.rb +++ b/lib/github_changelog_generator/generator/generator_generation.rb @@ -9,44 +9,21 @@ module GitHubChangelogGenerator fetch_issues_and_pr log = "" - log += @options[:frontmatter] if @options[:frontmatter] - log += "#{@options[:header]}\n\n" + log += options[:frontmatter] if options[:frontmatter] + log += "#{options[:header]}\n\n" - log += if @options[:unreleased_only] + log += if options[:unreleased_only] generate_log_between_tags(filtered_tags[0], nil) else generate_log_for_all_tags end - log += File.read(@options[:base]) if File.file?(@options[:base]) + log += File.read(options[:base]) if File.file?(options[:base]) log += "\n\n\\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*" @log = log end - # @return [String] temp method should be removed soon - def generate_for_2_tags(log) - tag1 = @options[:tag1] - tag2 = @options[:tag2] - tags_strings = [] - filtered_tags.each { |x| tags_strings.push(x["name"]) } - - if tags_strings.include?(tag1) - if tags_strings.include?(tag2) - to_a = tags_strings.map.with_index.to_a - hash = Hash[to_a] - index1 = hash[tag1] - index2 = hash[tag2] - log += generate_log_between_tags(all_tags[index1], all_tags[index2]) - else - raise ChangelogGeneratorError, "Can't find tag #{tag2} -> exit" - end - else - raise ChangelogGeneratorError, "Can't find tag #{tag1} -> exit" - end - log - end - # @param [Array] issues List of issues on sub-section # @param [String] prefix Nae of sub-section # @return [String] Generate ready-to-go sub-section @@ -76,21 +53,21 @@ module GitHubChangelogGenerator log = "" # Generate date string: - time_string = newer_tag_time.strftime @options[:date_format] + time_string = newer_tag_time.strftime(options[:date_format]) # Generate tag name and link - release_url = if @options[:release_url] - format(@options[:release_url], newer_tag_link) + release_url = if options[:release_url] + format(options[:release_url], newer_tag_link) else "#{project_url}/tree/#{newer_tag_link}" end - log += if newer_tag_name.equal? @options[:unreleased_label] + log += if newer_tag_name.equal?(options[:unreleased_label]) "## [#{newer_tag_name}](#{release_url})\n\n" else "## [#{newer_tag_name}](#{release_url}) (#{time_string})\n" end - if @options[:compare_link] && older_tag_link + if options[:compare_link] && older_tag_link # Generate compare link log += "[Full Changelog](#{project_url}/compare/#{older_tag_link}...#{newer_tag_link})\n\n" end @@ -123,7 +100,7 @@ module GitHubChangelogGenerator newer_tag_name = newer_tag.nil? ? nil : newer_tag["name"] - if @options[:filter_issues_by_milestone] + if options[:filter_issues_by_milestone] # delete excess irrelevant issues (according milestones). Issue #22. filtered_issues = filter_by_milestone(filtered_issues, newer_tag_name, @issues) filtered_pull_requests = filter_by_milestone(filtered_pull_requests, newer_tag_name, @pull_requests) @@ -134,7 +111,7 @@ module GitHubChangelogGenerator # The full cycle of generation for whole project # @return [String] The complete change log def generate_log_for_all_tags - puts "Generating log..." if @options[:verbose] + puts "Generating log..." if options[:verbose] log = generate_unreleased_section @@ -148,7 +125,7 @@ module GitHubChangelogGenerator def generate_unreleased_section log = "" - if @options[:unreleased] + if options[:unreleased] unreleased_log = generate_log_between_tags(filtered_tags[0], nil) log += unreleased_log if unreleased_log end @@ -172,12 +149,12 @@ module GitHubChangelogGenerator private def issue_line_with_user(line, issue) - return line if !@options[:author] || issue.pull_request.nil? + return line if !options[:author] || issue.pull_request.nil? user = issue["user"] return "#{line} ({Null user})" unless user - if @options[:usernames_as_github_logins] + if options[:usernames_as_github_logins] "#{line} (@#{user['login']})" else "#{line} ([#{user['login']}](#{user['html_url']}))" diff --git a/lib/github_changelog_generator/generator/generator_processor.rb b/lib/github_changelog_generator/generator/generator_processor.rb index e863f82..5bbc11a 100644 --- a/lib/github_changelog_generator/generator/generator_processor.rb +++ b/lib/github_changelog_generator/generator/generator_processor.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true module GitHubChangelogGenerator class Generator - # delete all labels with labels from @options[:exclude_labels] array + # delete all labels with labels from options[:exclude_labels] array # @param [Array] issues # @return [Array] filtered array def exclude_issues_by_labels(issues) - return issues if !@options[:exclude_labels] || @options[:exclude_labels].empty? + return issues if !options[:exclude_labels] || options[:exclude_labels].empty? issues.reject do |issue| labels = issue["labels"].map { |l| l["name"] } - (labels & @options[:exclude_labels]).any? + (labels & options[:exclude_labels]).any? end end @@ -121,7 +121,7 @@ module GitHubChangelogGenerator # @return [Array] issues without labels or empty array if add_issues_wo_labels is false def filter_wo_labels(issues) - if @options[:add_issues_wo_labels] + if options[:add_issues_wo_labels] issues_wo_labels = issues.select do |issue| !issue["labels"].map { |l| l["name"] }.any? end @@ -131,11 +131,11 @@ module GitHubChangelogGenerator end def filter_by_include_labels(issues) - if @options[:include_labels].nil? + if options[:include_labels].nil? issues else issues.select do |issue| - labels = issue["labels"].map { |l| l["name"] } & @options[:include_labels] + labels = issue["labels"].map { |l| l["name"] } & options[:include_labels] labels.any? end end @@ -154,18 +154,18 @@ module GitHubChangelogGenerator # @return [Array] Filtered issues def get_filtered_issues(issues) issues = filter_array_by_labels(issues) - puts "Filtered issues: #{issues.count}" if @options[:verbose] + puts "Filtered issues: #{issues.count}" if options[:verbose] issues end # This method fetches missing params for PR and filter them by specified options - # It include add all PR's with labels from @options[:include_labels] array + # It include add all PR's with labels from options[:include_labels] array # And exclude all from :exclude_labels array. # @return [Array] filtered PR's def get_filtered_pull_requests(pull_requests) pull_requests = filter_array_by_labels(pull_requests) pull_requests = filter_merged_pull_requests(pull_requests) - puts "Filtered pull requests: #{pull_requests.count}" if @options[:verbose] + puts "Filtered pull requests: #{pull_requests.count}" if options[:verbose] pull_requests end @@ -174,7 +174,7 @@ module GitHubChangelogGenerator # :merged_at - is a date, when issue PR was merged. # More correct to use merged date, rather than closed date. def filter_merged_pull_requests(pull_requests) - print "Fetching merged dates...\r" if @options[:verbose] + print "Fetching merged dates...\r" if options[:verbose] closed_pull_requests = @fetcher.fetch_closed_pull_requests pull_requests.each do |pr| diff --git a/lib/github_changelog_generator/generator/generator_tags.rb b/lib/github_changelog_generator/generator/generator_tags.rb index e74d01c..de721d6 100644 --- a/lib/github_changelog_generator/generator/generator_tags.rb +++ b/lib/github_changelog_generator/generator/generator_tags.rb @@ -34,7 +34,7 @@ module GitHubChangelogGenerator # Sort all tags by date, newest to oldest def sort_tags_by_date(tags) - puts "Sorting tags..." if @options[:verbose] + puts "Sorting tags..." if options[:verbose] tags.sort_by! do |x| get_time_of_tag(x) end.reverse! @@ -68,12 +68,12 @@ module GitHubChangelogGenerator newer_tag_time = newer_tag.nil? ? Time.new : get_time_of_tag(newer_tag) # if it's future release tag - set this value - if newer_tag.nil? && @options[:future_release] - newer_tag_name = @options[:future_release] - newer_tag_link = @options[:future_release] + if newer_tag.nil? && options[:future_release] + newer_tag_name = options[:future_release] + newer_tag_link = options[:future_release] else # put unreleased label if there is no name for the tag - newer_tag_name = newer_tag.nil? ? @options[:unreleased_label] : newer_tag["name"] + newer_tag_name = newer_tag.nil? ? options[:unreleased_label] : newer_tag["name"] newer_tag_link = newer_tag.nil? ? "HEAD" : newer_tag_name end [newer_tag_link, newer_tag_name, newer_tag_time] @@ -81,17 +81,17 @@ module GitHubChangelogGenerator # @return [Object] try to find newest tag using #Reader and :base option if specified otherwise returns nil def detect_since_tag - @since_tag ||= @options.fetch(:since_tag) { version_of_first_item } + @since_tag ||= options.fetch(:since_tag) { version_of_first_item } end def detect_due_tag - @due_tag ||= @options.fetch(:due_tag, nil) + @due_tag ||= options.fetch(:due_tag, nil) end def version_of_first_item - return unless File.file?(@options[:base].to_s) + return unless File.file?(options[:base].to_s) - sections = GitHubChangelogGenerator::Reader.new.read(@options[:base]) + sections = GitHubChangelogGenerator::Reader.new.read(options[:base]) sections.first["version"] if sections && sections.any? end @@ -150,13 +150,13 @@ module GitHubChangelogGenerator filtered_tags = all_tags tag_names = filtered_tags.map { |ft| ft["name"] } - if @options[:between_tags] - @options[:between_tags].each do |tag| + if options[:between_tags] + options[:between_tags].each do |tag| unless tag_names.include?(tag) Helper.log.warn "Warning: can't find tag #{tag}, specified with --between-tags option." end end - filtered_tags = all_tags.select { |tag| @options[:between_tags].include? tag["name"] } + filtered_tags = all_tags.select { |tag| options[:between_tags].include?(tag["name"]) } end filtered_tags end @@ -164,9 +164,9 @@ module GitHubChangelogGenerator # @param [Array] all_tags all tags # @return [Array] filtered tags according :exclude_tags or :exclude_tags_regex option def filter_excluded_tags(all_tags) - if @options[:exclude_tags] + if options[:exclude_tags] apply_exclude_tags(all_tags) - elsif @options[:exclude_tags_regex] + elsif options[:exclude_tags_regex] apply_exclude_tags_regex(all_tags) else all_tags @@ -176,15 +176,15 @@ module GitHubChangelogGenerator private def apply_exclude_tags(all_tags) - if @options[:exclude_tags].is_a?(Regexp) - filter_tags_with_regex(all_tags, @options[:exclude_tags]) + if options[:exclude_tags].is_a?(Regexp) + filter_tags_with_regex(all_tags, options[:exclude_tags]) else filter_exact_tags(all_tags) end end def apply_exclude_tags_regex(all_tags) - filter_tags_with_regex(all_tags, Regexp.new(@options[:exclude_tags_regex])) + filter_tags_with_regex(all_tags, Regexp.new(options[:exclude_tags_regex])) end def filter_tags_with_regex(all_tags, regex) @@ -193,16 +193,16 @@ module GitHubChangelogGenerator end def filter_exact_tags(all_tags) - @options[:exclude_tags].each do |tag| + options[:exclude_tags].each do |tag| warn_if_tag_not_found(all_tags, tag) end - all_tags.reject { |tag| @options[:exclude_tags].include? tag["name"] } + all_tags.reject { |tag| options[:exclude_tags].include?(tag["name"]) } end def warn_if_nonmatching_regex(all_tags) - unless all_tags.map { |t| t["name"] }.any? { |t| @options[:exclude_tags] =~ t } + unless all_tags.map { |t| t["name"] }.any? { |t| options[:exclude_tags] =~ t } Helper.log.warn "Warning: unable to reject any tag, using regex "\ - "#{@options[:exclude_tags].inspect} in --exclude-tags "\ + "#{options[:exclude_tags].inspect} in --exclude-tags "\ "option." end end