2014-12-03 13:38:05 +00:00
|
|
|
module GitHubChangelogGenerator
|
|
|
|
class Generator
|
2015-05-22 11:06:48 +00:00
|
|
|
# A Generator responsible for all logic, related with change log generation from ready-to-parse issues
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# generator = GitHubChangelogGenerator::Generator.new
|
|
|
|
# content = generator.compound_changelog
|
|
|
|
|
2014-12-03 14:31:43 +00:00
|
|
|
def initialize(options = nil)
|
2014-12-03 13:38:05 +00:00
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2015-03-27 15:15:08 +00:00
|
|
|
# Parse issue and generate single line formatted issue line.
|
|
|
|
#
|
|
|
|
# Example output:
|
|
|
|
# - Add coveralls integration [\#223](https://github.com/skywinder/github-changelog-generator/pull/223) ([skywinder](https://github.com/skywinder))
|
|
|
|
#
|
|
|
|
# @param [Hash] issue Fetched issue from GitHub
|
|
|
|
# @return [String] Markdown-formatted single issue
|
2015-02-25 17:02:41 +00:00
|
|
|
def get_string_for_issue(issue)
|
2015-03-26 13:43:47 +00:00
|
|
|
encapsulated_title = encapsulate_string issue[:title]
|
2015-02-25 17:02:41 +00:00
|
|
|
|
2015-02-27 12:53:21 +00:00
|
|
|
title_with_number = "#{encapsulated_title} [\\##{issue[:number]}](#{issue.html_url})"
|
|
|
|
|
2015-02-25 17:02:41 +00:00
|
|
|
unless issue.pull_request.nil?
|
|
|
|
if @options[:author]
|
|
|
|
if issue.user.nil?
|
2015-04-02 09:05:26 +00:00
|
|
|
title_with_number += " ({Null user})"
|
2015-02-25 17:02:41 +00:00
|
|
|
else
|
2015-02-27 12:53:21 +00:00
|
|
|
title_with_number += " ([#{issue.user.login}](#{issue.user.html_url}))"
|
2015-02-25 17:02:41 +00:00
|
|
|
end
|
2014-12-15 13:54:11 +00:00
|
|
|
end
|
2014-12-03 13:38:05 +00:00
|
|
|
end
|
2015-02-27 12:53:21 +00:00
|
|
|
title_with_number
|
2014-12-03 13:38:05 +00:00
|
|
|
end
|
|
|
|
|
2015-05-22 10:37:06 +00:00
|
|
|
# Encapsulate characters to make markdown look as expected.
|
|
|
|
#
|
|
|
|
# @param [String] string
|
|
|
|
# @return [String] encapsulated input string
|
2014-12-03 14:31:43 +00:00
|
|
|
def encapsulate_string(string)
|
|
|
|
string.gsub! '\\', '\\\\'
|
|
|
|
|
2015-03-03 15:33:51 +00:00
|
|
|
encpas_chars = %w(> * _ \( \) [ ] #)
|
2015-03-26 13:43:47 +00:00
|
|
|
encpas_chars.each do |char|
|
2014-12-03 14:31:43 +00:00
|
|
|
string.gsub! char, "\\#{char}"
|
2015-03-26 13:43:47 +00:00
|
|
|
end
|
2014-12-03 14:31:43 +00:00
|
|
|
|
|
|
|
string
|
|
|
|
end
|
2015-05-22 11:06:48 +00:00
|
|
|
|
2015-05-22 11:11:29 +00:00
|
|
|
# Main function to start change log generation
|
|
|
|
#
|
|
|
|
# @return [String] Generated change log file
|
|
|
|
def compound_changelog
|
|
|
|
log = "# Change Log\n\n"
|
|
|
|
|
|
|
|
if @options[:unreleased_only]
|
|
|
|
log += generate_log_between_tags(all_tags[0], nil)
|
|
|
|
elsif @options[:tag1] and @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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-05-22 12:28:43 +00:00
|
|
|
# The full cycle of generation for whole project
|
|
|
|
# @return [String] The complete change log
|
|
|
|
def generate_log_for_all_tags
|
|
|
|
fetch_tags_dates
|
|
|
|
|
|
|
|
if @options[:verbose]
|
|
|
|
puts "Sorting tags..."
|
|
|
|
end
|
|
|
|
|
|
|
|
@all_tags.sort_by! { |x| @fetcher.get_time_of_tag(x) }.reverse!
|
|
|
|
|
|
|
|
if @options[:verbose]
|
|
|
|
puts "Generating log..."
|
|
|
|
end
|
|
|
|
|
|
|
|
log = ""
|
|
|
|
|
|
|
|
if @options[:unreleased] && @all_tags.count != 0
|
|
|
|
unreleased_log = generate_log_between_tags(all_tags[0], nil)
|
|
|
|
if unreleased_log
|
|
|
|
log += unreleased_log
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
(1...all_tags.size).each { |index|
|
|
|
|
log += generate_log_between_tags(all_tags[index], all_tags[index - 1])
|
|
|
|
}
|
|
|
|
if @all_tags.count != 0
|
|
|
|
log += generate_log_between_tags(nil, all_tags.last)
|
|
|
|
end
|
|
|
|
|
|
|
|
log
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-12-03 13:38:05 +00:00
|
|
|
end
|
2015-03-26 13:43:47 +00:00
|
|
|
end
|