rubocop auto-fixes

This commit is contained in:
Petr Korolev
2015-03-26 15:43:47 +02:00
parent a09b6affd4
commit cca8e5d810
8 changed files with 149 additions and 175 deletions
+62 -89
View File
@@ -11,20 +11,18 @@ require_relative 'github_changelog_generator/version'
module GitHubChangelogGenerator
class ChangelogGenerator
attr_accessor :options, :all_tags, :github
PER_PAGE_NUMBER = 30
GH_RATE_LIMIT_EXCEEDED_MSG = 'Warning: GitHub API rate limit exceed (5000 per hour), change log may not ' +
GH_RATE_LIMIT_EXCEEDED_MSG = 'Warning: GitHub API rate limit exceed (5000 per hour), change log may not ' \
'contain some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument'
def initialize
@options = Parser.parse_options
fetch_github_token
github_options = {per_page: PER_PAGE_NUMBER}
github_options = { per_page: PER_PAGE_NUMBER }
github_options[:oauth_token] = @github_token unless @github_token.nil?
github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil?
github_options[:site] = options[:github_endpoint] unless options[:github_site].nil?
@@ -37,17 +35,17 @@ module GitHubChangelogGenerator
@generator = Generator.new(@options)
@all_tags = self.get_all_tags
@issues, @pull_requests = self.fetch_issues_and_pull_requests
@all_tags = get_all_tags
@issues, @pull_requests = fetch_issues_and_pull_requests
if @options[:pulls]
@pull_requests = self.get_filtered_pull_requests
@pull_requests = get_filtered_pull_requests
else
@pull_requests = []
end
if @options[:issues]
@issues = self.get_filtered_issues
@issues = get_filtered_issues
else
@issues = []
end
@@ -58,7 +56,6 @@ module GitHubChangelogGenerator
end
def detect_actual_closed_dates
if @options[:verbose]
print "Fetching closed dates for issues...\r"
end
@@ -76,7 +73,7 @@ module GitHubChangelogGenerator
find_closed_date_by_commit(pull_request)
}
}
threads.each { |thr| thr.join }
threads.each(&:join)
if @options[:verbose]
puts 'Fetching closed dates for issues: Done!'
@@ -85,7 +82,7 @@ module GitHubChangelogGenerator
def find_closed_date_by_commit(issue)
unless issue['events'].nil?
#if it's PR -> then find "merged event", in case of usual issue -> fond closed date
# if it's PR -> then find "merged event", in case of usual issue -> fond closed date
compare_string = issue[:merged_at].nil? ? 'closed' : 'merged'
# reverse! - to find latest closed event. (event goes in date order)
issue['events'].reverse!.each { |event|
@@ -105,7 +102,7 @@ module GitHubChangelogGenerator
end
}
end
#TODO: assert issues, that remain without 'actual_date' hash for some reason.
# TODO: assert issues, that remain without 'actual_date' hash for some reason.
end
def print_json(json)
@@ -118,7 +115,7 @@ module GitHubChangelogGenerator
end
pull_requests = []
begin
response = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
response = @github.pull_requests.list @options[:user], @options[:project], state: 'closed'
page_i = 0
response.each_page do |page|
page_i += PER_PAGE_NUMBER
@@ -134,7 +131,8 @@ module GitHubChangelogGenerator
@pull_requests.each { |pr|
fetched_pr = pull_requests.find { |fpr|
fpr.number == pr.number }
fpr.number == pr.number
}
pr[:merged_at] = fetched_pr[:merged_at]
pull_requests.delete(fetched_pr)
}
@@ -142,38 +140,34 @@ module GitHubChangelogGenerator
if @options[:verbose]
puts 'Fetching merged dates... Done!'
end
end
def get_filtered_pull_requests
fetch_merged_at_pull_requests
self.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[:incluse_labels] array
(issue.labels.map { |label| label.name } & @options[:include_labels]).any?
# add all labels from @options[:incluse_labels] array
(issue.labels.map(&:name) & @options[:include_labels]).any?
}
end
unless @options[:exclude_labels].nil?
filtered_pull_requests = filtered_pull_requests.select { |issue|
#delete all labels from @options[:exclude_labels] array
!(issue.labels.map { |label| label.name } & @options[:exclude_labels]).any?
# delete all labels from @options[:exclude_labels] array
!(issue.labels.map(&:name) & @options[:exclude_labels]).any?
}
end
if @options[:add_issues_wo_labels]
issues_wo_labels = @pull_requests.select {
# add issues without any labels
|issue| !issue.labels.map { |label| label.name }.any?
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
@@ -182,16 +176,15 @@ module GitHubChangelogGenerator
end
def compund_changelog
log = "# Change Log\n\n"
if @options[:unreleased_only]
log += self.generate_log_between_tags(self.all_tags[0], nil)
log += generate_log_between_tags(all_tags[0], nil)
elsif @options[:tag1] and @options[:tag2]
tag1 = @options[:tag1]
tag2 = @options[:tag2]
tags_strings = []
self.all_tags.each { |x| tags_strings.push(x['name']) }
all_tags.each { |x| tags_strings.push(x['name']) }
if tags_strings.include?(tag1)
if tags_strings.include?(tag2)
@@ -199,7 +192,7 @@ module GitHubChangelogGenerator
hash = Hash[to_a]
index1 = hash[tag1]
index2 = hash[tag2]
log += self.generate_log_between_tags(self.all_tags[index1], self.all_tags[index2])
log += generate_log_between_tags(all_tags[index1], all_tags[index2])
else
puts "Can't find tag #{tag2} -> exit"
exit
@@ -209,7 +202,7 @@ module GitHubChangelogGenerator
exit
end
else
log += self.generate_log_for_all_tags
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)*"
@@ -218,38 +211,35 @@ module GitHubChangelogGenerator
File.open(output_filename, 'w') { |file| file.write(log) }
puts 'Done!'
puts "Generated log placed in #{`pwd`.strip!}/#{output_filename}"
end
def generate_log_for_all_tags
fetch_tags_dates
if @options[:verbose]
puts "Sorting tags.."
puts 'Sorting tags..'
end
@all_tags.sort_by! { |x| self.get_time_of_tag(x) }.reverse!
@all_tags.sort_by! { |x| get_time_of_tag(x) }.reverse!
if @options[:verbose]
puts "Generating log.."
puts 'Generating log..'
end
log = ''
if @options[:unreleased] && @all_tags.count != 0
unreleased_log = self.generate_log_between_tags(self.all_tags[0], nil)
unreleased_log = generate_log_between_tags(all_tags[0], nil)
if unreleased_log
log += unreleased_log
end
end
(1 ... self.all_tags.size).each { |index|
log += self.generate_log_between_tags(self.all_tags[index], self.all_tags[index-1])
(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, self.all_tags.last)
log += generate_log_between_tags(nil, all_tags.last)
end
log
@@ -267,18 +257,17 @@ module GitHubChangelogGenerator
@all_tags.each { |tag|
# explicit set @tag_times_hash to write data safety.
threads << Thread.new {
self.get_time_of_tag(tag, @tag_times_hash)
get_time_of_tag(tag, @tag_times_hash)
if @options[:verbose]
print "Fetching tags dates: #{i+1}/#{all}\r"
i+=1
print "Fetching tags dates: #{i + 1}/#{all}\r"
i += 1
end
}
}
print " \r"
threads.each { |thr| thr.join }
threads.each(&:join)
if @options[:verbose]
puts "Fetching tags dates: #{i} Done!"
@@ -292,7 +281,6 @@ module GitHubChangelogGenerator
end
def get_all_tags
if @options[:verbose]
print "Fetching tags...\r"
end
@@ -328,12 +316,11 @@ module GitHubChangelogGenerator
env_var = @options[:token] ? @options[:token] : (ENV.fetch 'CHANGELOG_GITHUB_TOKEN', nil)
unless env_var
puts "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.".yellow
puts "This script can make only 50 requests to GitHub API per hour without token!".yellow
puts 'Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.'.yellow
puts 'This script can make only 50 requests to GitHub API per hour without token!'.yellow
end
@github_token ||= env_var
end
def generate_log_between_tags(older_tag, newer_tag)
@@ -345,7 +332,7 @@ module GitHubChangelogGenerator
older_tag_name = older_tag.nil? ? nil : older_tag['name']
if @options[:filter_issues_by_milestone]
#delete excess irrelevant issues (according milestones)
# delete excess irrelevant issues (according milestones)
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
@@ -355,7 +342,7 @@ module GitHubChangelogGenerator
return ''
end
self.create_log(filtered_pull_requests, filtered_issues, newer_tag, older_tag_name)
create_log(filtered_pull_requests, filtered_issues, newer_tag, older_tag_name)
end
def filter_by_milestone(filtered_issues, newer_tag_name, src_array)
@@ -364,18 +351,18 @@ module GitHubChangelogGenerator
if issue.milestone.nil?
true
else
#check, that this milestone in tag list:
# check, that this milestone in tag list:
@all_tags.find { |tag| tag.name == issue.milestone.title }.nil?
end
}
unless newer_tag_name.nil?
#add missed issues (according milestones)
# add missed issues (according milestones)
issues_to_add = src_array.select { |issue|
if issue.milestone.nil?
false
else
#check, that this milestone in tag list:
# check, that this milestone in tag list:
milestone_is_tag = @all_tags.find { |tag|
tag.name == issue.milestone.title
}
@@ -394,11 +381,10 @@ module GitHubChangelogGenerator
end
def delete_by_time(array, hash_key, older_tag = nil, newer_tag = nil)
fail 'At least one of the tags should be not nil!' if older_tag.nil? && newer_tag.nil?
raise 'At least one of the tags should be not nil!' if (older_tag.nil? && newer_tag.nil?)
newer_tag_time = self.get_time_of_tag(newer_tag)
older_tag_time = self.get_time_of_tag(older_tag)
newer_tag_time = get_time_of_tag(newer_tag)
older_tag_time = get_time_of_tag(older_tag)
array.select { |req|
if req[hash_key]
@@ -416,7 +402,6 @@ module GitHubChangelogGenerator
tag_in_range_new = t <= newer_tag_time
end
tag_in_range = (tag_in_range_old) && (tag_in_range_new)
tag_in_range
@@ -431,8 +416,7 @@ module GitHubChangelogGenerator
# @param [String] older_tag_name
# @return [String]
def create_log(pull_requests, issues, newer_tag, older_tag_name = nil)
newer_tag_time = newer_tag.nil? ? nil : self.get_time_of_tag(newer_tag)
newer_tag_time = newer_tag.nil? ? nil : get_time_of_tag(newer_tag)
newer_tag_name = newer_tag.nil? ? nil : newer_tag['name']
github_site = options[:github_site] || 'https://github.com'
@@ -454,7 +438,7 @@ module GitHubChangelogGenerator
# Generate issues:
issues_a = []
enhancement_a = []
bugs_a =[]
bugs_a = []
issues.each { |dict|
added = false
@@ -504,8 +488,7 @@ module GitHubChangelogGenerator
end
def generate_header(log, newer_tag_name, newer_tag_name2, newer_tag_time, older_tag_name, project_url)
#Generate date string:
# Generate date string:
time_string = newer_tag_time.strftime @options[:format]
# Generate tag name and link
@@ -524,7 +507,6 @@ module GitHubChangelogGenerator
end
def get_time_of_tag(tag_name, tag_times_hash = @tag_times_hash)
if tag_name.nil?
return nil
end
@@ -543,40 +525,36 @@ 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[:incluse_labels] array
(issue.labels.map { |label| label.name } & @options[:include_labels]).any?
# add all labels from @options[:incluse_labels] array
(issue.labels.map(&:name) & @options[:include_labels]).any?
}
end
unless @options[:exclude_labels].nil?
filtered_issues = filtered_issues.select { |issue|
#delete all labels from @options[:exclude_labels] array
!(issue.labels.map { |label| label.name } & @options[:exclude_labels]).any?
# delete all labels from @options[:exclude_labels] array
!(issue.labels.map(&:name) & @options[:exclude_labels]).any?
}
end
if @options[:add_issues_wo_labels]
issues_wo_labels = issues.select {
# add issues without any labels
|issue| !issue.labels.map { |label| label.name }.any?
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
filtered_issues
end
def fetch_issues_and_pull_requests
@@ -607,12 +585,12 @@ module GitHubChangelogGenerator
# remove pull request from issues:
issues_wo_pr = issues.select { |x|
x.pull_request == nil
x.pull_request.nil?
}
pull_requests = issues.select { |x|
x.pull_request != nil
!x.pull_request.nil?
}
return issues_wo_pr, pull_requests
[issues_wo_pr, pull_requests]
end
def fetch_event_for_issues_and_pr
@@ -623,8 +601,6 @@ module GitHubChangelogGenerator
# Async fetching events:
fetch_events_async(@issues + @pull_requests)
end
def fetch_events_async(issues)
@@ -640,27 +616,24 @@ module GitHubChangelogGenerator
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
issue[:events] = obj.body
print "Fetching events for issues and PR: #{i+1}/#{@issues.count + @pull_requests.count}\r"
i +=1
print "Fetching events for issues and PR: #{i + 1}/#{@issues.count + @pull_requests.count}\r"
i += 1
}
}
threads.each { |thr| thr.join }
threads.each(&:join)
threads = []
}
#to clear line from prev print
# to clear line from prev print
print " \r"
if @options[:verbose]
puts "Fetching events for issues and PR: #{i} Done!"
end
end
end
if __FILE__ == $0
if __FILE__ == $PROGRAM_NAME
GitHubChangelogGenerator::ChangelogGenerator.new.compund_changelog
end
end
+5 -9
View File
@@ -1,19 +1,18 @@
module GitHubChangelogGenerator
class Generator
def initialize(options = nil)
@options = options
end
def get_string_for_issue(issue)
encapsulated_title = self.encapsulate_string issue[:title]
encapsulated_title = encapsulate_string issue[:title]
title_with_number = "#{encapsulated_title} [\\##{issue[:number]}](#{issue.html_url})"
unless issue.pull_request.nil?
if @options[:author]
if issue.user.nil?
title_with_number += " ({Null user})"
title_with_number += ' ({Null user})'
else
title_with_number += " ([#{issue.user.login}](#{issue.user.html_url}))"
end
@@ -23,17 +22,14 @@ module GitHubChangelogGenerator
end
def encapsulate_string(string)
string.gsub! '\\', '\\\\'
encpas_chars = %w(> * _ \( \) [ ] #)
encpas_chars.each { |char|
encpas_chars.each do |char|
string.gsub! char, "\\#{char}"
}
end
string
end
end
end
end
+28 -31
View File
@@ -6,32 +6,31 @@ require_relative 'version'
module GitHubChangelogGenerator
class Parser
def self.parse_options
options = {
:tag1 => nil,
:tag2 => nil,
:format => '%Y-%m-%d',
:output => 'CHANGELOG.md',
:exclude_labels => %w(duplicate question invalid wontfix),
:pulls => true,
:issues => true,
:verbose => true,
:add_issues_wo_labels => true,
:add_pr_wo_labels => true,
:merge_prefix => '**Merged pull requests:**',
:issue_prefix => '**Closed issues:**',
:bug_prefix => '**Fixed bugs:**',
:enhancement_prefix => '**Implemented enhancements:**',
:author => true,
:filter_issues_by_milestone => true,
:max_issues => nil,
:compare_link => true,
:unreleased => true,
:unreleased_label => 'Unreleased',
:branch => 'origin'
tag1: nil,
tag2: nil,
format: '%Y-%m-%d',
output: 'CHANGELOG.md',
exclude_labels: %w(duplicate question invalid wontfix),
pulls: true,
issues: true,
verbose: true,
add_issues_wo_labels: true,
add_pr_wo_labels: true,
merge_prefix: '**Merged pull requests:**',
issue_prefix: '**Closed issues:**',
bug_prefix: '**Fixed bugs:**',
enhancement_prefix: '**Implemented enhancements:**',
author: true,
filter_issues_by_milestone: true,
max_issues: nil,
compare_link: true,
unreleased: true,
unreleased_label: 'Unreleased',
branch: 'origin'
}
parser = OptionParser.new { |opts|
parser = OptionParser.new do |opts|
opts.banner = 'Usage: github_changelog_generator [options]'
opts.on('-u', '--user [USER]', 'Username of the owner of target GitHub repo') do |last|
options[:user] = last
@@ -99,7 +98,7 @@ module GitHubChangelogGenerator
opts.on('--[no-]verbose', 'Run verbosely. Default is true') do |v|
options[:verbose] = v
end
opts.on('-v', '--version', 'Print version number') do |v|
opts.on('-v', '--version', 'Print version number') do |_v|
puts "Version: #{GitHubChangelogGenerator::VERSION}"
exit
end
@@ -107,7 +106,7 @@ module GitHubChangelogGenerator
puts opts
exit
end
}
end
parser.parse!
@@ -126,10 +125,9 @@ module GitHubChangelogGenerator
exit
else
options[:user] = match[1]
options[:project]= match[2]
options[:project] = match[2]
end
end
if !options[:user] && !options[:project]
@@ -143,9 +141,9 @@ module GitHubChangelogGenerator
puts "Detected user:#{match[1]}, project:#{match[2]}"
options[:user], options[:project] = match[1], match[2]
else
# try to find repo in format:
# origin https://github.com/skywinder/ChangelogMerger (fetch)
# https://github.com/skywinder/ChangelogMerger
# try to find repo in format:
# origin https://github.com/skywinder/ChangelogMerger (fetch)
# https://github.com/skywinder/ChangelogMerger
match = /.*\/((?:-|\w|\.)*)\/((?:-|\w|\.)*).*/.match(remote)
if match && match[1] && match[2]
puts "Detected user:#{match[1]}, project:#{match[2]}"
@@ -154,7 +152,6 @@ module GitHubChangelogGenerator
end
end
if !options[:user] || !options[:project]
puts parser.banner
exit