Merge branch 'feature/custom-error' into develop

This commit is contained in:
Petr Korolev 2015-04-04 00:11:38 +03:00
commit 31c5ac4c55

View File

@ -11,6 +11,11 @@ require_relative "github_changelog_generator/version"
require_relative "github_changelog_generator/reader" require_relative "github_changelog_generator/reader"
module GitHubChangelogGenerator module GitHubChangelogGenerator
# Default error for ChangelogGenerator
class ChangelogGeneratorError < StandardError
end
# Main class and entry point for this script.
class ChangelogGenerator class ChangelogGenerator
attr_accessor :options, :all_tags, :github attr_accessor :options, :all_tags, :github
@ -18,6 +23,8 @@ module GitHubChangelogGenerator
GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: GitHub API rate limit (5000 per hour) exceeded, change log may be " \ GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: GitHub API rate limit (5000 per hour) exceeded, change log may be " \
"missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument." "missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."
# Class, responsible for whole change log generation cycle
# @return initialised insance of ChangelogGenerator
def initialize def initialize
@options = Parser.parse_options @options = Parser.parse_options
@ -176,6 +183,8 @@ module GitHubChangelogGenerator
filtered_pull_requests filtered_pull_requests
end end
# The entry point of this script to generate change log
# @raise (ChangelogGeneratorError) Is thrown when one of specified tags was not found in list of tags.
def compound_changelog def compound_changelog
log = "# Change Log\n\n" log = "# Change Log\n\n"
@ -195,12 +204,10 @@ module GitHubChangelogGenerator
index2 = hash[tag2] index2 = hash[tag2]
log += generate_log_between_tags(all_tags[index1], all_tags[index2]) log += generate_log_between_tags(all_tags[index1], all_tags[index2])
else else
puts "Can't find tag #{tag2} -> exit" fail ChangelogGeneratorError, "Can't find tag #{tag2} -> exit".red
exit
end end
else else
puts "Can't find tag #{tag1} -> exit" fail ChangelogGeneratorError, "Can't find tag #{tag1} -> exit".red
exit
end end
else else
log += generate_log_for_all_tags log += generate_log_for_all_tags
@ -317,8 +324,10 @@ module GitHubChangelogGenerator
@github_token ||= env_var @github_token ||= env_var
end end
# Generate log only between 2 specified tags
# @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) def generate_log_between_tags(older_tag, newer_tag)
# older_tag nil - means it's first tag, newer_tag nil - means it unreleased section
filtered_pull_requests = delete_by_time(@pull_requests, :actual_date, 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 = delete_by_time(@issues, :actual_date, older_tag, newer_tag)
@ -374,8 +383,12 @@ module GitHubChangelogGenerator
filtered_issues filtered_issues
end end
def delete_by_time(array, hash_key, older_tag = nil, newer_tag = nil) # @param [Array] array of issues to filter
fail "At least one of the tags should be not nil!" if older_tag.nil? && newer_tag.nil? # @param [Symbol] hash_key key of date value default is :actual_date
# @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 delete_by_time(array, hash_key = :actual_date, older_tag = nil, newer_tag = nil)
fail ChangelogGeneratorError, "At least one of the tags should be not nil!".red if older_tag.nil? && newer_tag.nil?
newer_tag_time = get_time_of_tag(newer_tag) newer_tag_time = get_time_of_tag(newer_tag)
older_tag_time = get_time_of_tag(older_tag) older_tag_time = get_time_of_tag(older_tag)