added specs for the new octo_fetcher. Also had to refactor a bit to deal with hashes and arrays instead of objects

This commit is contained in:
Andrew Waage
2016-05-19 23:46:13 -07:00
committed by Olle Jonsson
parent 8277aa4319
commit 2347cc4220
25 changed files with 345 additions and 36 deletions
+13
View File
@@ -0,0 +1,13 @@
class Array
def stringify_keys_deep!
new_ar = []
self.each do |value|
new_value = value
if value.is_a? Hash or value.is_a? Array
new_value = value.stringify_keys_deep!
end
new_ar << new_value
end
new_ar
end
end
@@ -107,13 +107,13 @@ module GitHubChangelogGenerator
issues.each do |dict|
added = false
dict.labels.each do |label|
if @options[:bug_labels].include? label.name
dict['labels'].each do |label|
if @options[:bug_labels].include? label['name']
bugs_a.push dict
added = true
next
end
if @options[:enhancement_labels].include? label.name
if @options[:enhancement_labels].include? label['name']
enhancement_a.push dict
added = true
next
@@ -123,16 +123,16 @@ module GitHubChangelogGenerator
end
added_pull_requests = []
pull_requests.each do |dict|
dict.labels.each do |label|
if @options[:bug_labels].include? label.name
bugs_a.push dict
added_pull_requests.push dict
pull_requests.each do |pr|
pr['labels'].each do |label|
if @options[:bug_labels].include? label['name']
bugs_a.push pr
added_pull_requests.push pr
next
end
if @options[:enhancement_labels].include? label.name
enhancement_a.push dict
added_pull_requests.push dict
if @options[:enhancement_labels].include? label['name']
enhancement_a.push pr
added_pull_requests.push pr
next
end
end
@@ -166,9 +166,9 @@ module GitHubChangelogGenerator
# @param [Hash] issue Fetched issue from GitHub
# @return [String] Markdown-formatted single issue
def get_string_for_issue(issue)
encapsulated_title = encapsulate_string issue[:title]
encapsulated_title = encapsulate_string issue['title']
title_with_number = "#{encapsulated_title} [\\##{issue[:number]}](#{issue.html_url})"
title_with_number = "#{encapsulated_title} [\\##{issue['number']}](#{issue['html_url']})"
issue_line_with_user(title_with_number, issue)
end
@@ -177,13 +177,13 @@ module GitHubChangelogGenerator
def issue_line_with_user(line, issue)
return line if !@options[:author] || issue.pull_request.nil?
user = issue.user
user = issue['user']
return "#{line} ({Null user})" unless user
if @options[:usernames_as_github_logins]
"#{line} (@#{user.login})"
"#{line} (@#{user['login']})"
else
"#{line} ([#{user.login}](#{user.html_url}))"
"#{line} ([#{user['login']}](#{user['html_url']}))"
end
end
end
@@ -8,7 +8,7 @@ module GitHubChangelogGenerator
return issues if !@options[:exclude_labels] || @options[:exclude_labels].empty?
issues.reject do |issue|
labels = issue.labels.map(&:name)
labels = issue['labels'].map{|l| l['name'] }
(labels & @options[:exclude_labels]).any?
end
end
@@ -123,7 +123,7 @@ module GitHubChangelogGenerator
def filter_wo_labels(issues)
if @options[:add_issues_wo_labels]
issues_wo_labels = issues.select do |issue|
!issue.labels.map(&:name).any?
!issue['labels'].map{|l| l['name'] }.any?
end
return issues_wo_labels
end
@@ -135,7 +135,7 @@ module GitHubChangelogGenerator
issues
else
issues.select do |issue|
labels = issue.labels.map(&:name) & @options[:include_labels]
labels = issue['labels'].map { |l| l['name'] } & @options[:include_labels]
labels.any?
end
end
@@ -179,16 +179,16 @@ module GitHubChangelogGenerator
pull_requests.each do |pr|
fetched_pr = closed_pull_requests.find do |fpr|
fpr.number == pr.number
fpr['number'] == pr['number']
end
if fetched_pr
pr[:merged_at] = fetched_pr[:merged_at]
pr['merged_at'] = fetched_pr['merged_at']
closed_pull_requests.delete(fetched_pr)
end
end
pull_requests.select! do |pr|
!pr[:merged_at].nil?
!pr['merged_at'].nil?
end
pull_requests
+15
View File
@@ -0,0 +1,15 @@
class Hash
def stringify_keys_deep!
new_hash = {}
keys.each do |k|
ks = k.respond_to?(:to_s) ? k.to_s : k
if values_at(k).first.kind_of? Hash or values_at(k).first.kind_of? Array
new_hash[ks] = values_at(k).first.send(:stringify_keys_deep!)
else
new_hash[ks] = values_at(k).first
end
end
new_hash
end
end
+15 -8
View File
@@ -50,7 +50,7 @@ module GitHubChangelogGenerator
# Fetch all tags from repo
#
# @return [Array] array of tags
# @return [Array <Hash>] array of tags
def get_all_tags
print "Fetching tags...\r" if @options[:verbose]
@@ -79,7 +79,7 @@ module GitHubChangelogGenerator
# Fill input array with tags
#
# @return [Array] array of tags in repo
# @return [Array <Hash>] array of tags in repo
def github_fetch_tags
tags = []
page_i = 0
@@ -98,13 +98,15 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
else
Helper.log.info "Found #{tags.count} tags"
end
# tags are a Sawyer::Resource. Convert to hash
tags = tags.map{|h| h.to_hash.stringify_keys_deep! }
tags
end
# This method fetch all closed issues and separate them to pull requests and pure issues
# (pull request is kind of issue in term of GitHub)
#
# @return [Tuple] with (issues, pull-requests)
# @return [Tuple] with (issues [Array <Hash>], pull-requests [Array <Hash>])
def fetch_closed_issues_and_pr
print "Fetching closed issues...\r" if @options[:verbose]
issues = []
@@ -127,15 +129,17 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
print_empty_line
Helper.log.info "Received issues: #{issues.count}"
issues = issues.map{|h| h.to_hash.stringify_keys_deep! }
# separate arrays of issues and pull requests:
issues.partition do |x|
x[:pull_request].nil?
x['pull_request'].nil?
end
end
# Fetch all pull requests. We need them to detect :merged_at parameter
#
# @return [Array] all pull requests
# @return [Array <Hash>] all pull requests
def fetch_closed_pull_requests
pull_requests = []
options = { :state => 'closed' }
@@ -156,6 +160,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
print_empty_line
Helper.log.info "Pull Request count: #{pull_requests.count}"
pull_requests = pull_requests.map{|h| h.to_hash.stringify_keys_deep! }
pull_requests
end
@@ -174,6 +179,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
iterate_pages(@client, 'issue_events', issue['number'], {}) do |new_event|
issue[:events].concat(new_event)
end
issue[:events] = issue[:events].map{|h| h.to_hash.stringify_keys_deep! }
print_in_same_line("Fetching events for issues and PR: #{i + 1}/#{issues.count}")
i += 1
end
@@ -203,7 +209,8 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# @return [Hash]
def fetch_commit(event)
check_github_response do
@client.commit(user_project, event[:commit_id])
commit = @client.commit(user_project, event['commit_id'])
commit = commit.to_hash.stringify_keys_deep!
end
end
@@ -250,10 +257,10 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
begin
value = yield
rescue Octokit::Unauthorized => e
Helper.log.error e.body.red
Helper.log.error e.message.red
abort "Error: wrong GitHub token"
rescue Octokit::Forbidden => e
Helper.log.warn e.body.red
Helper.log.warn e.message.red
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
value