From 177b7aa18f03c150306b48454f3e601cdc970e93 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Sat, 4 Apr 2015 03:10:16 +0300 Subject: [PATCH] code cleanup. remove code duplication --- lib/github_changelog_generator.rb | 57 ++++++++++++++----------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/lib/github_changelog_generator.rb b/lib/github_changelog_generator.rb index a82e949..c130c6d 100755 --- a/lib/github_changelog_generator.rb +++ b/lib/github_changelog_generator.rb @@ -110,6 +110,9 @@ module GitHubChangelogGenerator puts JSON.pretty_generate(json) end + # This method fetch missing required attributes for pull requests + # :merged_at - is a date, when issue PR was merged. + # More correct to use this date, not closed date. def fetch_merged_at_pull_requests if @options[:verbose] print "Fetching merged dates...\r" @@ -143,27 +146,19 @@ module GitHubChangelogGenerator end 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 + # And exclude all from :exclude_labels array. + # @return [Array] filtered PR's def get_filtered_pull_requests fetch_merged_at_pull_requests - filtered_pull_requests = @pull_requests.select { |pr| !pr[:merged_at].nil? } + # filtered_pull_requests = @pull_requests.select { |pr| !pr[:merged_at].nil? } - unless @options[:include_labels].nil? - filtered_pull_requests = @pull_requests.select { |issue| - # add all labels from @options[:include_labels] array - (issue.labels.map(&:name) & @options[:include_labels]).any? - } - end + filtered_pull_requests = include_issues_by_labels(@pull_requests) filtered_pull_requests = exclude_issues_by_labels(filtered_pull_requests) - if @options[:add_issues_wo_labels] - issues_wo_labels = @pull_requests.select { |issue| - !issue.labels.map(&:name).any? - } - filtered_pull_requests |= issues_wo_labels - end - if @options[:verbose] puts "Filtered pull requests: #{filtered_pull_requests.count}" end @@ -171,6 +166,22 @@ module GitHubChangelogGenerator filtered_pull_requests end + def include_issues_by_labels(issues) + unless @options[:include_labels].nil? + _issues = issues.select { |issue| + (issue.labels.map(&:name) & @options[:include_labels]).any? + } + end + + if @options[:add_issues_wo_labels] + issues_wo_labels = issues.select { |issue| + !issue.labels.map(&:name).any? + } + _issues |= issues_wo_labels + end + _issues + end + # delete all labels with labels from @options[:exclude_labels] array # @param [Array] issues # @return [Array] filtered array @@ -540,26 +551,10 @@ module GitHubChangelogGenerator end def get_filtered_issues - issues = @issues - - filtered_issues = issues - - unless @options[:include_labels].nil? - filtered_issues = issues.select { |issue| - # add all labels from @options[:include_labels] array - (issue.labels.map(&:name) & @options[:include_labels]).any? - } - end + filtered_issues = include_issues_by_labels(@issues) filtered_issues = exclude_issues_by_labels(filtered_issues) - if @options[:add_issues_wo_labels] - issues_wo_labels = issues.select { |issue| - !issue.labels.map(&:name).any? - } - filtered_issues |= issues_wo_labels - end - if @options[:verbose] puts "Filtered issues: #{filtered_issues.count}" end