reduce complexity accouding codeclimate recomendations

This commit is contained in:
Petr Korolev 2015-05-25 15:17:51 +03:00
parent 8e5e7de71d
commit 8f4a931256
4 changed files with 134 additions and 80 deletions

View File

@ -74,14 +74,7 @@ module GitHubChangelogGenerator
# @param [String] older_tag_name Older tag, used for the links. Could be nil for last tag.
# @return [String] Ready and parsed section
def create_log(pull_requests, issues, newer_tag, older_tag_name = nil)
newer_tag_time = newer_tag.nil? ? Time.new : @fetcher.get_time_of_tag(newer_tag)
if newer_tag.nil? && @options[:future_release]
newer_tag_name = @options[:future_release]
newer_tag_link = @options[:future_release]
else
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 = detect_link_tag_time(newer_tag)
github_site = options[:github_site] || "https://github.com"
project_url = "#{github_site}/#{@options[:user]}/#{@options[:project]}"
@ -90,11 +83,7 @@ module GitHubChangelogGenerator
if @options[:issues]
# Generate issues:
bugs_a, enhancement_a, issues_a = parse_by_sections(issues)
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 += issues_to_log(issues)
end
if @options[:pulls]
@ -105,6 +94,40 @@ module GitHubChangelogGenerator
log
end
# Detect link, name and time for specified tag.
#
# @param [Hash] newer_tag newer tag. Can be nil, if it's Unreleased section.
# @return [Array] link, name and time of the tag
def detect_link_tag_time(newer_tag)
# if tag is nil - set current time
newer_tag_time = newer_tag.nil? ? Time.new : @fetcher.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]
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_link = newer_tag.nil? ? "HEAD" : newer_tag_name
end
[newer_tag_link, newer_tag_name, newer_tag_time]
end
# Generate ready-to-paste log from list of issues.
#
# @param [Array] issues
# @return [String] generated log for issues
def issues_to_log(issues)
log ""
bugs_a, enhancement_a, issues_a = parse_by_sections(issues)
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
# This method sort issues by types
# (bugs, features, or just closed issues) by labels
#

View File

@ -14,25 +14,19 @@ module GitHubChangelogGenerator
# Async fetching of all tags dates
def fetch_tags_dates
print "Fetching tag dates...\r" if @options[:verbose]
# Async fetching tags:
threads = []
i = 0
all = @all_tags.count
@all_tags.each do |tag|
print " \r"
threads << Thread.new do
@fetcher.get_time_of_tag(tag)
if @options[:verbose]
print "Fetching tags dates: #{i + 1}/#{all}\r"
i += 1
end
print "Fetching tags dates: #{i + 1}/#{all}\r" if @options[:verbose]
i += 1
end
end
print " \r"
threads.each(&:join)
puts "Fetching tags dates: #{i}" if @options[:verbose]
end

View File

@ -9,31 +9,36 @@ module GitHubChangelogGenerator
if @options[:unreleased_only]
log += generate_log_between_tags(all_tags[0], nil)
elsif @options[:tag1] && @options[:tag2]
tag1 = @options[:tag1]
tag2 = @options[:tag2]
tags_strings = []
all_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
fail ChangelogGeneratorError, "Can't find tag #{tag2} -> exit".red
end
else
fail ChangelogGeneratorError, "Can't find tag #{tag1} -> exit".red
end
log = generate_for_2_tags(log)
else
log += generate_log_for_all_tags
end
log += "\n\n\\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*"
@log = log
@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 = []
all_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
fail ChangelogGeneratorError, "Can't find tag #{tag2} -> exit".red
end
else
fail ChangelogGeneratorError, "Can't find tag #{tag1} -> exit".red
end
log
end
# @param [Array] issues List of issues on sub-section
@ -86,18 +91,10 @@ module GitHubChangelogGenerator
# @param [String] older_tag all issues before this tag date will be excluded. May be nil, if it's first tag
# @param [String] newer_tag all issue after this tag will be excluded. May be nil for unreleased section
def generate_log_between_tags(older_tag, newer_tag)
filtered_pull_requests = delete_by_time(@pull_requests, :actual_date, older_tag, newer_tag)
filtered_issues = delete_by_time(@issues, :actual_date, older_tag, newer_tag)
filtered_issues, filtered_pull_requests = filter_issues_for_tags(newer_tag, older_tag)
newer_tag_name = newer_tag.nil? ? nil : newer_tag["name"]
older_tag_name = older_tag.nil? ? nil : older_tag["name"]
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)
end
if newer_tag.nil? && filtered_issues.empty? && filtered_pull_requests.empty?
# do not generate empty unreleased section
return ""
@ -106,6 +103,23 @@ module GitHubChangelogGenerator
create_log(filtered_pull_requests, filtered_issues, newer_tag, older_tag_name)
end
# Apply all filters to issues and pull requests
#
# @return [Array] filtered issues and pull requests
def filter_issues_for_tags(newer_tag, older_tag)
filtered_pull_requests = delete_by_time(@pull_requests, :actual_date, older_tag, newer_tag)
filtered_issues = delete_by_time(@issues, :actual_date, older_tag, newer_tag)
newer_tag_name = newer_tag.nil? ? nil : newer_tag["name"]
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)
end
[filtered_issues, filtered_pull_requests]
end
# The full cycle of generation for whole project
# @return [String] The complete change log
def generate_log_for_all_tags

View File

@ -13,7 +13,44 @@ module GitHubChangelogGenerator
issues
end
def filter_by_milestone(filtered_issues, newer_tag_name, src_array)
# @return [Array] filtered issues accourding milestone
def filter_by_milestone(filtered_issues, tag_name, all_issues)
remove_issues_in_milestones(filtered_issues)
unless tag_name.nil?
# add missed issues (according milestones)
issues_to_add = find_issues_to_add(all_issues, tag_name)
filtered_issues |= issues_to_add
end
filtered_issues
end
# Add all issues, that should be in that tag, according milestone
#
# @param [Array] all_issues
# @param [String] tag_name
# @return [Array] issues with milestone #tag_name
def find_issues_to_add(all_issues, tag_name)
all_issues.select do |issue|
if issue.milestone.nil?
false
else
# check, that this milestone in tag list:
milestone_is_tag = @all_tags.find do |tag|
tag.name == issue.milestone.title
end
if milestone_is_tag.nil?
false
else
issue.milestone.title == tag_name
end
end
end
end
# @return [Array] array with removed issues, that contain milestones with same name as a tag
def remove_issues_in_milestones(filtered_issues)
filtered_issues.select! do |issue|
# leave issues without milestones
if issue.milestone.nil?
@ -23,29 +60,6 @@ module GitHubChangelogGenerator
@all_tags.find { |tag| tag.name == issue.milestone.title }.nil?
end
end
unless newer_tag_name.nil?
# add missed issues (according milestones)
issues_to_add = src_array.select do |issue|
if issue.milestone.nil?
false
else
# check, that this milestone in tag list:
milestone_is_tag = @all_tags.find do |tag|
tag.name == issue.milestone.title
end
if milestone_is_tag.nil?
false
else
issue.milestone.title == newer_tag_name
end
end
end
filtered_issues |= issues_to_add
end
filtered_issues
end
# Method filter issues, that belong only specified tag range
@ -89,18 +103,27 @@ module GitHubChangelogGenerator
# @param [Array] issues to filter
# @return [Array] filtered array of issues
def include_issues_by_labels(issues)
filtered_issues = @options[:include_labels].nil? ? issues : issues.select do |issue|
labels = issue.labels.map(&:name) & @options[:include_labels]
(labels).any?
end
filtered_issues = filter_by_include_labels(issues)
filtered_issues |= filter_wo_labels(issues)
filtered_issues
end
# @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]
issues_wo_labels = issues.select do |issue|
!issue.labels.map(&:name).any?
end
filtered_issues |= issues_wo_labels
issues_wo_labels
end
[]
end
def filter_by_include_labels(issues)
@options[:include_labels].nil? ? issues : issues.select do |issue|
labels = issue.labels.map(&:name) & @options[:include_labels]
(labels).any?
end
filtered_issues
end
# Return tags after filtering tags in lists provided by option: --between-tags & --exclude-tags