fixing rubocop and removing original fetcher

This commit is contained in:
Andrew Waage 2016-05-25 12:46:47 -07:00 committed by Olle Jonsson
parent 0e948fb125
commit ef9867c122
16 changed files with 434 additions and 651 deletions

View File

@ -16,6 +16,7 @@ end
group :test do group :test do
gem "vcr" gem "vcr"
gem "multi_json"
gem "webmock" gem "webmock"
gem "coveralls", "~>0.8", require: false gem "coveralls", "~>0.8", require: false
gem "simplecov", "~>0.10", require: false gem "simplecov", "~>0.10", require: false

View File

@ -1,7 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require "github_api"
require "octokit" require "octokit"
require 'faraday-http-cache' require 'faraday-http-cache'
require "logger" require "logger"

View File

@ -1,13 +1,13 @@
class Array class Array
def stringify_keys_deep! def stringify_keys_deep!
new_ar = [] new_ar = []
self.each do |value| each do |value|
new_value = value new_value = value
if value.is_a? Hash or value.is_a? Array if value.is_a?(Hash) || value.is_a?(Array)
new_value = value.stringify_keys_deep! new_value = value.stringify_keys_deep!
end end
new_ar << new_value new_ar << new_value
end end
new_ar new_ar
end end
end end

View File

@ -1,226 +0,0 @@
# frozen_string_literal: true
module GitHubChangelogGenerator
# A Fetcher responsible for all requests to GitHub and all basic manipulation with related data
# (such as filtering, validating, e.t.c)
#
# Example:
# fetcher = GitHubChangelogGenerator::Fetcher.new options
class Fetcher
PER_PAGE_NUMBER = 30
MAX_SIMULTANEOUS_REQUESTS = 25
CHANGELOG_GITHUB_TOKEN = "CHANGELOG_GITHUB_TOKEN"
GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: Can't finish operation: GitHub API rate limit exceeded, change log may be " \
"missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."
NO_TOKEN_PROVIDED = "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found. " \
"This script can make only 50 requests to GitHub API per hour without token!"
def initialize(options = {})
@options = options || {}
@user = @options[:user]
@project = @options[:project]
@github_token = fetch_github_token
@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?
@github = check_github_response { Github.new @github_options }
end
# Returns GitHub token. First try to use variable, provided by --token option,
# otherwise try to fetch it from CHANGELOG_GITHUB_TOKEN env variable.
#
# @return [String]
def fetch_github_token
env_var = @options[:token] ? @options[:token] : (ENV.fetch CHANGELOG_GITHUB_TOKEN, nil)
Helper.log.warn NO_TOKEN_PROVIDED unless env_var
env_var
end
# Fetch all tags from repo
# @return [Array] array of tags
def get_all_tags
print "Fetching tags...\r" if @options[:verbose]
check_github_response { github_fetch_tags }
end
# This is wrapper with rescue block
# @return [Object] returns exactly the same, what you put in the block, but wrap it with begin-rescue block
def check_github_response
begin
value = yield
rescue Github::Error::Unauthorized => e
Helper.log.error e.response_message
abort "Error: wrong GitHub token"
rescue Github::Error::Forbidden => e
Helper.log.warn e.response_message
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
end
value
end
# Fill input array with tags
# @return [Array] array of tags in repo
def github_fetch_tags
tags = []
response = @github.repos.tags @options[:user], @options[:project]
page_i = 0
count_pages = response.count_pages
response.each_page do |page|
page_i += PER_PAGE_NUMBER
print_in_same_line("Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}")
tags.concat(page) unless page.nil?
end
print_empty_line
if tags.empty?
Helper.log.warn "Warning: Can't find any tags in repo.\
Make sure, that you push tags to remote repo via 'git push --tags'"
else
Helper.log.info "Found #{tags.count} tags"
end
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)
def fetch_closed_issues_and_pr
print "Fetching closed issues...\r" if @options[:verbose]
issues = []
begin
response = @github.issues.list user: @options[:user],
repo: @options[:project],
state: "closed",
filter: "all",
labels: nil
page_i = 0
count_pages = response.count_pages
response.each_page do |page|
page_i += PER_PAGE_NUMBER
print_in_same_line("Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}")
issues.concat(page)
break if @options[:max_issues] && issues.length >= @options[:max_issues]
end
print_empty_line
Helper.log.info "Received issues: #{issues.count}"
rescue Github::Error::Forbidden => e
Helper.log.warn e.error_messages.map { |m| m[:message] }.join(", ")
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
end
# separate arrays of issues and pull requests:
issues.partition do |x|
x[:pull_request].nil?
end
end
# Fetch all pull requests. We need them to detect :merged_at parameter
# @return [Array] all pull requests
def fetch_closed_pull_requests
pull_requests = []
begin
response = if @options[:release_branch].nil?
@github.pull_requests.list @options[:user],
@options[:project],
state: "closed"
else
@github.pull_requests.list @options[:user],
@options[:project],
state: "closed",
base: @options[:release_branch]
end
page_i = 0
count_pages = response.count_pages
response.each_page do |page|
page_i += PER_PAGE_NUMBER
log_string = "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}"
print_in_same_line(log_string)
pull_requests.concat(page)
end
print_empty_line
rescue Github::Error::Forbidden => e
Helper.log.warn e.error_messages.map { |m| m[:message] }.join(", ")
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
end
Helper.log.info "Fetching merged dates: #{pull_requests.count}"
pull_requests
end
# Print specified line on the same string
# @param [String] log_string
def print_in_same_line(log_string)
print log_string + "\r"
end
# Print long line with spaces on same line to clear prev message
def print_empty_line
print_in_same_line(" ")
end
# Fetch event for all issues and add them to events
# @param [Array] issues
# @return [Void]
def fetch_events_async(issues)
i = 0
threads = []
issues.each_slice(MAX_SIMULTANEOUS_REQUESTS) do |issues_slice|
issues_slice.each do |issue|
threads << Thread.new do
begin
response = @github.issues.events.list user: @options[:user],
repo: @options[:project],
issue_number: issue["number"]
issue['events'] = []
response.each_page do |page|
issue['events'].concat(page)
end
rescue Github::Error::Forbidden => e
Helper.log.warn e.error_messages.map { |m| m[:message] }.join(", ")
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
end
print_in_same_line("Fetching events for issues and PR: #{i + 1}/#{issues.count}")
i += 1
end
end
threads.each(&:join)
threads = []
end
# to clear line from prev print
print_empty_line
Helper.log.info "Fetching events for issues and PR: #{i}"
end
# Fetch tag time from repo
#
# @param [Hash] tag
# @return [Time] time of specified tag
def fetch_date_of_tag(tag)
begin
commit_data = @github.git_data.commits.get @options[:user],
@options[:project],
tag["commit"]["sha"]
rescue Github::Error::Forbidden => e
Helper.log.warn e.error_messages.map { |m| m[:message] }.join(", ")
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG
end
time_string = commit_data["committer"]["date"]
Time.parse(time_string)
end
# Fetch commit for specified event
# @return [Hash]
def fetch_commit(event)
@github.git_data.commits.get @options[:user], @options[:project], event['commit_id']
end
end
end

View File

@ -1,5 +1,4 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "../fetcher"
require_relative "../octo_fetcher" require_relative "../octo_fetcher"
require_relative "generator_generation" require_relative "generator_generation"
require_relative "generator_fetcher" require_relative "generator_fetcher"
@ -107,13 +106,13 @@ module GitHubChangelogGenerator
issues.each do |dict| issues.each do |dict|
added = false added = false
dict['labels'].each do |label| dict["labels"].each do |label|
if @options[:bug_labels].include? label['name'] if @options[:bug_labels].include? label["name"]
bugs_a.push dict bugs_a.push dict
added = true added = true
next next
end end
if @options[:enhancement_labels].include? label['name'] if @options[:enhancement_labels].include? label["name"]
enhancement_a.push dict enhancement_a.push dict
added = true added = true
next next
@ -124,13 +123,13 @@ module GitHubChangelogGenerator
added_pull_requests = [] added_pull_requests = []
pull_requests.each do |pr| pull_requests.each do |pr|
pr['labels'].each do |label| pr["labels"].each do |label|
if @options[:bug_labels].include? label['name'] if @options[:bug_labels].include? label["name"]
bugs_a.push pr bugs_a.push pr
added_pull_requests.push pr added_pull_requests.push pr
next next
end end
if @options[:enhancement_labels].include? label['name'] if @options[:enhancement_labels].include? label["name"]
enhancement_a.push pr enhancement_a.push pr
added_pull_requests.push pr added_pull_requests.push pr
next next

View File

@ -50,12 +50,12 @@ module GitHubChangelogGenerator
# Fill :actual_date parameter of specified issue by closed date of the commit, if it was closed by commit. # Fill :actual_date parameter of specified issue by closed date of the commit, if it was closed by commit.
# @param [Hash] issue # @param [Hash] issue
def find_closed_date_by_commit(issue) def find_closed_date_by_commit(issue)
unless issue['events'].nil? 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" compare_string = issue["merged_at"].nil? ? "closed" : "merged"
# reverse! - to find latest closed event. (event goes in date order) # reverse! - to find latest closed event. (event goes in date order)
issue['events'].reverse!.each do |event| issue["events"].reverse!.each do |event|
if event['event'].eql? compare_string if event["event"].eql? compare_string
set_date_from_event(event, issue) set_date_from_event(event, issue)
break break
end end
@ -69,16 +69,17 @@ module GitHubChangelogGenerator
# @param [Hash] event # @param [Hash] event
# @param [Hash] issue # @param [Hash] issue
def set_date_from_event(event, issue) def set_date_from_event(event, issue)
if event['commit_id'].nil? if event["commit_id"].nil?
issue['actual_date'] = issue['closed_at'] issue["actual_date"] = issue["closed_at"]
else else
begin begin
commit = @fetcher.fetch_commit(event) commit = @fetcher.fetch_commit(event)
issue['actual_date'] = commit['commit']['author']['date'] issue["actual_date"] = commit["commit"]["author"]["date"]
# issue['actual_date'] = commit['author']['date'] # issue['actual_date'] = commit['author']['date']
rescue rescue
puts "Warning: Can't fetch commit #{event['commit_id']}. It is probably referenced from another repo." puts "Warning: Can't fetch commit #{event['commit_id']}. It is probably referenced from another repo."
issue['actual_date'] = issue['closed_at'] issue["actual_date"] = issue["closed_at"]
end end
end end
end end

View File

@ -119,9 +119,8 @@ module GitHubChangelogGenerator
# #
# @return [Array] filtered issues and pull requests # @return [Array] filtered issues and pull requests
def filter_issues_for_tags(newer_tag, older_tag) def filter_issues_for_tags(newer_tag, older_tag)
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)
newer_tag_name = newer_tag.nil? ? nil : newer_tag["name"] newer_tag_name = newer_tag.nil? ? nil : newer_tag["name"]
@ -167,7 +166,7 @@ module GitHubChangelogGenerator
# @param [Hash] issue Fetched issue from GitHub # @param [Hash] issue Fetched issue from GitHub
# @return [String] Markdown-formatted single issue # @return [String] Markdown-formatted single issue
def get_string_for_issue(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) issue_line_with_user(title_with_number, issue)
@ -178,13 +177,13 @@ module GitHubChangelogGenerator
def issue_line_with_user(line, issue) def issue_line_with_user(line, issue)
return line if !@options[:author] || issue.pull_request.nil? return line if !@options[:author] || issue.pull_request.nil?
user = issue['user'] user = issue["user"]
return "#{line} ({Null user})" unless user return "#{line} ({Null user})" unless user
if @options[:usernames_as_github_logins] if @options[:usernames_as_github_logins]
"#{line} (@#{user['login']})" "#{line} (@#{user["login"]})"
else else
"#{line} ([#{user['login']}](#{user['html_url']}))" "#{line} ([#{user["login"]}](#{user["html_url"]}))"
end end
end end
end end

View File

@ -8,7 +8,7 @@ module GitHubChangelogGenerator
return issues if !@options[:exclude_labels] || @options[:exclude_labels].empty? return issues if !@options[:exclude_labels] || @options[:exclude_labels].empty?
issues.reject do |issue| issues.reject do |issue|
labels = issue['labels'].map{|l| l['name'] } labels = issue["labels"].map { |l| l["name"] }
(labels & @options[:exclude_labels]).any? (labels & @options[:exclude_labels]).any?
end end
end end
@ -32,18 +32,18 @@ module GitHubChangelogGenerator
# @return [Array] issues with milestone #tag_name # @return [Array] issues with milestone #tag_name
def find_issues_to_add(all_issues, tag_name) def find_issues_to_add(all_issues, tag_name)
all_issues.select do |issue| all_issues.select do |issue|
if issue['milestone'].nil? if issue["milestone"].nil?
false false
else else
# check, that this milestone in tag list: # check, that this milestone in tag list:
milestone_is_tag = @filtered_tags.find do |tag| milestone_is_tag = @filtered_tags.find do |tag|
tag['name'] == issue['milestone']['title'] tag["name"] == issue["milestone"]["title"]
end end
if milestone_is_tag.nil? if milestone_is_tag.nil?
false false
else else
issue['milestone']['title'] == tag_name issue["milestone"]["title"] == tag_name
end end
end end
end end
@ -53,11 +53,11 @@ module GitHubChangelogGenerator
def remove_issues_in_milestones(filtered_issues) def remove_issues_in_milestones(filtered_issues)
filtered_issues.select! do |issue| filtered_issues.select! do |issue|
# leave issues without milestones # leave issues without milestones
if issue['milestone'].nil? if issue["milestone"].nil?
true true
else else
# check, that this milestone in tag list: # check, that this milestone in tag list:
@filtered_tags.find { |tag| tag['name'] == issue['milestone']['title'] }.nil? @filtered_tags.find { |tag| tag["name"] == issue["milestone"]["title"] }.nil?
end end
end end
end end
@ -68,7 +68,7 @@ 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] 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 # @param [String] newer_tag all issue after this tag will be excluded. May be nil for unreleased section
# @return [Array] filtered issues # @return [Array] filtered issues
def delete_by_time(issues, hash_key = 'actual_date', older_tag = nil, newer_tag = nil) def delete_by_time(issues, hash_key = "actual_date", older_tag = nil, newer_tag = nil)
# in case if not tags specified - return unchanged array # in case if not tags specified - return unchanged array
return issues if older_tag.nil? && newer_tag.nil? return issues if older_tag.nil? && newer_tag.nil?
@ -123,7 +123,7 @@ module GitHubChangelogGenerator
def filter_wo_labels(issues) def filter_wo_labels(issues)
if @options[:add_issues_wo_labels] if @options[:add_issues_wo_labels]
issues_wo_labels = issues.select do |issue| issues_wo_labels = issues.select do |issue|
!issue['labels'].map{|l| l['name'] }.any? !issue["labels"].map { |l| l["name"] }.any?
end end
return issues_wo_labels return issues_wo_labels
end end
@ -135,7 +135,7 @@ module GitHubChangelogGenerator
issues issues
else else
issues.select do |issue| issues.select do |issue|
labels = issue['labels'].map { |l| l['name'] } & @options[:include_labels] labels = issue["labels"].map { |l| l["name"] } & @options[:include_labels]
labels.any? labels.any?
end end
end end
@ -179,16 +179,16 @@ module GitHubChangelogGenerator
pull_requests.each do |pr| pull_requests.each do |pr|
fetched_pr = closed_pull_requests.find do |fpr| fetched_pr = closed_pull_requests.find do |fpr|
fpr['number'] == pr['number'] fpr["number"] == pr["number"]
end end
if fetched_pr if fetched_pr
pr['merged_at'] = fetched_pr['merged_at'] pr["merged_at"] = fetched_pr["merged_at"]
closed_pull_requests.delete(fetched_pr) closed_pull_requests.delete(fetched_pr)
end end
end end
pull_requests.select! do |pr| pull_requests.select! do |pr|
!pr['merged_at'].nil? !pr["merged_at"].nil?
end end
pull_requests pull_requests

View File

@ -80,8 +80,8 @@ module GitHubChangelogGenerator
filtered_tags = all_tags filtered_tags = all_tags
tag = detect_since_tag tag = detect_since_tag
if tag if tag
if all_tags.map(&:name).include? tag if all_tags.map { |t| t["name"] }.include? tag
idx = all_tags.index { |t| t.name == tag } idx = all_tags.index { |t| t["name"] == tag }
filtered_tags = if idx > 0 filtered_tags = if idx > 0
all_tags[0..idx - 1] all_tags[0..idx - 1]
else else
@ -100,8 +100,8 @@ module GitHubChangelogGenerator
filtered_tags = all_tags filtered_tags = all_tags
tag = @options[:due_tag] tag = @options[:due_tag]
if tag if tag
if all_tags.any? && all_tags.map(&:name).include?(tag) if all_tags.any? && all_tags.map { |t| t["name"] }.include?(tag)
idx = all_tags.index { |t| t.name == tag } idx = all_tags.index { |t| t["name"] == tag }
last_index = all_tags.count - 1 last_index = all_tags.count - 1
filtered_tags = if idx > 0 && idx < last_index filtered_tags = if idx > 0 && idx < last_index
all_tags[idx + 1..last_index] all_tags[idx + 1..last_index]
@ -121,11 +121,11 @@ module GitHubChangelogGenerator
filtered_tags = all_tags filtered_tags = all_tags
if @options[:between_tags] if @options[:between_tags]
@options[:between_tags].each do |tag| @options[:between_tags].each do |tag|
unless all_tags.map(&:name).include? tag unless all_tags.map { |t| t["name"] }.include? tag
Helper.log.warn "Warning: can't find tag #{tag}, specified with --between-tags option." Helper.log.warn "Warning: can't find tag #{tag}, specified with --between-tags option."
end end
end end
filtered_tags = all_tags.select { |tag| @options[:between_tags].include? tag.name } filtered_tags = all_tags.select { |tag| @options[:between_tags].include? tag["name"] }
end end
filtered_tags filtered_tags
end end
@ -158,18 +158,18 @@ module GitHubChangelogGenerator
def filter_tags_with_regex(all_tags, regex) def filter_tags_with_regex(all_tags, regex)
warn_if_nonmatching_regex(all_tags) warn_if_nonmatching_regex(all_tags)
all_tags.reject { |tag| regex =~ tag.name } all_tags.reject { |tag| regex =~ tag["name"] }
end end
def filter_exact_tags(all_tags) def filter_exact_tags(all_tags)
@options[:exclude_tags].each do |tag| @options[:exclude_tags].each do |tag|
warn_if_tag_not_found(all_tags, tag) warn_if_tag_not_found(all_tags, tag)
end end
all_tags.reject { |tag| @options[:exclude_tags].include? tag.name } all_tags.reject { |tag| @options[:exclude_tags].include? tag["name"] }
end end
def warn_if_nonmatching_regex(all_tags) def warn_if_nonmatching_regex(all_tags)
unless all_tags.map(&:name).any? { |t| @options[:exclude_tags] =~ t } unless all_tags.map { |t| t["name"] }.any? { |t| @options[:exclude_tags] =~ t }
Helper.log.warn "Warning: unable to reject any tag, using regex "\ Helper.log.warn "Warning: unable to reject any tag, using regex "\
"#{@options[:exclude_tags].inspect} in --exclude-tags "\ "#{@options[:exclude_tags].inspect} in --exclude-tags "\
"option." "option."
@ -177,7 +177,7 @@ module GitHubChangelogGenerator
end end
def warn_if_tag_not_found(all_tags, tag) def warn_if_tag_not_found(all_tags, tag)
unless all_tags.map(&:name).include? tag unless all_tags.map { |t| t["name"] }.include? tag
Helper.log.warn "Warning: can't find tag #{tag}, specified with --exclude-tags option." Helper.log.warn "Warning: can't find tag #{tag}, specified with --exclude-tags option."
end end
end end

View File

@ -2,14 +2,14 @@ class Hash
def stringify_keys_deep! def stringify_keys_deep!
new_hash = {} new_hash = {}
keys.each do |k| keys.each do |k|
ks = k.respond_to?(:to_s) ? k.to_s : 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] = if values_at(k).first.is_a?(Hash) || values_at(k).first.is_a?(Array)
new_hash[ks] = values_at(k).first.send(:stringify_keys_deep!) values_at(k).first.send(:stringify_keys_deep!)
else else
new_hash[ks] = values_at(k).first values_at(k).first
end end
end end
new_hash new_hash
end end
end end

View File

@ -28,7 +28,7 @@ module GitHubChangelogGenerator
@github_token = fetch_github_token @github_token = fetch_github_token
@request_options = { :per_page => PER_PAGE_NUMBER } @request_options = { per_page: PER_PAGE_NUMBER }
@github_options = {} @github_options = {}
@github_options[:access_token] = @github_token unless @github_token.nil? @github_options[:access_token] = @github_token unless @github_token.nil?
@github_options[:api_endpoint] = @options[:github_endpoint] unless @options[:github_endpoint].nil? @github_options[:api_endpoint] = @options[:github_endpoint] unless @options[:github_endpoint].nil?
@ -63,7 +63,6 @@ module GitHubChangelogGenerator
check_github_response { github_fetch_tags } check_github_response { github_fetch_tags }
end end
# Returns the number of pages for a API call # Returns the number of pages for a API call
# #
# @return [Integer] number of pages for this API call in total # @return [Integer] number of pages for this API call in total
@ -75,23 +74,22 @@ module GitHubChangelogGenerator
last_response = client.last_response last_response = client.last_response
if last_pg = last_response.rels[:last] if (last_pg = last_response.rels[:last])
parse_url_for_vars(last_pg.href)['page'].to_i parse_url_for_vars(last_pg.href)["page"].to_i
else else
1 1
end end
end end
# Fill input array with tags # Fill input array with tags
# #
# @return [Array <Hash>] array of tags in repo # @return [Array <Hash>] array of tags in repo
def github_fetch_tags def github_fetch_tags
tags = [] tags = []
page_i = 0 page_i = 0
count_pages = calculate_pages(@client, 'tags', {}) count_pages = calculate_pages(@client, "tags", {})
iterate_pages(@client, 'tags', {}) do |new_tags| iterate_pages(@client, "tags", {}) do |new_tags|
page_i += PER_PAGE_NUMBER page_i += PER_PAGE_NUMBER
print_in_same_line("Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}") print_in_same_line("Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}")
tags.concat(new_tags) tags.concat(new_tags)
@ -105,7 +103,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
Helper.log.info "Found #{tags.count} tags" Helper.log.info "Found #{tags.count} tags"
end end
# tags are a Sawyer::Resource. Convert to hash # tags are a Sawyer::Resource. Convert to hash
tags = tags.map{|h| h.to_hash.stringify_keys_deep! } tags = tags.map { |h| h.to_hash.stringify_keys_deep! }
tags tags
end end
@ -117,16 +115,16 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
print "Fetching closed issues...\r" if @options[:verbose] print "Fetching closed issues...\r" if @options[:verbose]
issues = [] issues = []
options = { options = {
:state => "closed", state: "closed",
:filter => "all", filter: "all",
:labels => nil, labels: nil
} }
options[:since] = @since unless @since.nil? options[:since] = @since unless @since.nil?
page_i = 0 page_i = 0
count_pages = calculate_pages(@client, 'issues', options) count_pages = calculate_pages(@client, "issues", options)
iterate_pages(@client, 'issues', options) do |new_issues| iterate_pages(@client, "issues", options) do |new_issues|
page_i += PER_PAGE_NUMBER page_i += PER_PAGE_NUMBER
print_in_same_line("Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}") print_in_same_line("Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}")
issues.concat(new_issues) issues.concat(new_issues)
@ -135,11 +133,11 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
print_empty_line print_empty_line
Helper.log.info "Received issues: #{issues.count}" Helper.log.info "Received issues: #{issues.count}"
issues = issues.map{|h| h.to_hash.stringify_keys_deep! } issues = issues.map { |h| h.to_hash.stringify_keys_deep! }
# separate arrays of issues and pull requests: # separate arrays of issues and pull requests:
issues.partition do |x| issues.partition do |x|
x['pull_request'].nil? x["pull_request"].nil?
end end
end end
@ -148,17 +146,17 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# @return [Array <Hash>] all pull requests # @return [Array <Hash>] all pull requests
def fetch_closed_pull_requests def fetch_closed_pull_requests
pull_requests = [] pull_requests = []
options = { :state => 'closed' } options = { state: "closed" }
if !@options[:release_branch].nil? unless @options[:release_branch].nil?
options[:base] = @options[:release_branch] options[:base] = @options[:release_branch]
end end
page_i = 0 page_i = 0
count_pages = calculate_pages(@client, 'pull_requests', options) count_pages = calculate_pages(@client, "pull_requests", options)
iterate_pages(@client, 'pull_requests', options) do |new_pr| iterate_pages(@client, "pull_requests", options) do |new_pr|
page_i += PER_PAGE_NUMBER page_i += PER_PAGE_NUMBER
log_string = "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}" log_string = "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}"
print_in_same_line(log_string) print_in_same_line(log_string)
pull_requests.concat(new_pr) pull_requests.concat(new_pr)
@ -166,7 +164,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
print_empty_line print_empty_line
Helper.log.info "Pull Request count: #{pull_requests.count}" Helper.log.info "Pull Request count: #{pull_requests.count}"
pull_requests = pull_requests.map{|h| h.to_hash.stringify_keys_deep! } pull_requests = pull_requests.map { |h| h.to_hash.stringify_keys_deep! }
pull_requests pull_requests
end end
@ -181,11 +179,11 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
issues.each_slice(MAX_THREAD_NUMBER) do |issues_slice| issues.each_slice(MAX_THREAD_NUMBER) do |issues_slice|
issues_slice.each do |issue| issues_slice.each do |issue|
threads << Thread.new do threads << Thread.new do
issue['events'] = [] issue["events"] = []
iterate_pages(@client, 'issue_events', issue['number'], {}) do |new_event| iterate_pages(@client, "issue_events", issue["number"], {}) do |new_event|
issue['events'].concat(new_event) issue["events"].concat(new_event)
end end
issue['events'] = issue['events'].map{|h| h.to_hash.stringify_keys_deep! } 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}") print_in_same_line("Fetching events for issues and PR: #{i + 1}/#{issues.count}")
i += 1 i += 1
end end
@ -205,10 +203,10 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# @param [Hash] tag # @param [Hash] tag
# @return [Time] time of specified tag # @return [Time] time of specified tag
def fetch_date_of_tag(tag) def fetch_date_of_tag(tag)
commit_data = check_github_response { @client.commit(user_project, tag['commit']['sha']) } commit_data = check_github_response { @client.commit(user_project, tag["commit"]["sha"]) }
commit_data = commit_data.to_hash.stringify_keys_deep! commit_data = commit_data.to_hash.stringify_keys_deep!
commit_data['commit']['committer']['date'] commit_data["commit"]["committer"]["date"]
end end
# Fetch commit for specified event # Fetch commit for specified event
@ -216,8 +214,9 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# @return [Hash] # @return [Hash]
def fetch_commit(event) def fetch_commit(event)
check_github_response do check_github_response do
commit = @client.commit(user_project, event['commit_id']) commit = @client.commit(user_project, event["commit_id"])
commit = commit.to_hash.stringify_keys_deep! commit = commit.to_hash.stringify_keys_deep!
commit
end end
end end
@ -229,7 +228,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# @param [Octokit::Client] client # @param [Octokit::Client] client
# @param [String] method (eg. 'tags') # @param [String] method (eg. 'tags')
# @return [Integer] total number of pages # @return [Integer] total number of pages
def iterate_pages(client, method, *args, &block) def iterate_pages(client, method, *args)
if args.size == 1 && args.first.is_a?(Hash) if args.size == 1 && args.first.is_a?(Hash)
request_options = args.delete_at(0) request_options = args.delete_at(0)
elsif args.size > 1 && args.last.is_a?(Hash) elsif args.size > 1 && args.last.is_a?(Hash)
@ -247,8 +246,8 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
yield last_response.data yield last_response.data
while !(next_one = last_response.rels[:next]).nil? until (next_one = last_response.rels[:next]).nil?
pages +=1 pages += 1
last_response = check_github_response { next_one.get } last_response = check_github_response { next_one.get }
yield last_response.data yield last_response.data
@ -307,12 +306,11 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# @param [String] uri eg. https://api.github.com/repositories/43914960/tags?page=37&foo=1 # @param [String] uri eg. https://api.github.com/repositories/43914960/tags?page=37&foo=1
# @return [Hash] of all GET variables. eg. { 'page' => 37, 'foo' => 1 } # @return [Hash] of all GET variables. eg. { 'page' => 37, 'foo' => 1 }
def parse_url_for_vars(uri) def parse_url_for_vars(uri)
URI(uri).query.split("&").inject({}) do |params, get_var| URI(uri).query.split("&").each_with_object({}) do |params, get_var|
k,v = get_var.split("=") k, v = get_var.split("=")
params[k] = v params[k] = v
params params
end end
end end
end end
end end

View File

@ -38,16 +38,15 @@ end
require "github_changelog_generator" require "github_changelog_generator"
require "github_changelog_generator/task" require "github_changelog_generator/task"
VCR.configure do |c| VCR.configure do |c|
c.allow_http_connections_when_no_cassette = true c.allow_http_connections_when_no_cassette = true
c.cassette_library_dir = 'spec/vcr' c.cassette_library_dir = "spec/vcr"
c.ignore_localhost = true c.ignore_localhost = true
c.default_cassette_options = { c.default_cassette_options = {
:record => :new_episodes, record: :new_episodes,
:serialize_with => :json, serialize_with: :json,
:preserve_exact_body_bytes => true, preserve_exact_body_bytes: true,
:decode_compressed_response => true, decode_compressed_response: true
} }
c.hook_into :webmock, :faraday c.hook_into :webmock, :faraday
@ -74,6 +73,4 @@ RSpec.configure do |config|
Kernel.srand config.seed Kernel.srand config.seed
config.extend VCR::RSpec::Macros config.extend VCR::RSpec::Macros
end end

View File

@ -1,231 +0,0 @@
# frozen_string_literal: true
VALID_TOKEN = "0123456789abcdef"
INVALID_TOKEN = "0000000000000000"
describe GitHubChangelogGenerator::Fetcher do
let(:options) do
{
:user => "skywinder",
:project => "changelog_test",
}
end
let(:fetcher) { GitHubChangelogGenerator::Fetcher.new(options) }
describe "#fetch_github_token" do
token = GitHubChangelogGenerator::Fetcher::CHANGELOG_GITHUB_TOKEN
context "when token in ENV exist" do
before { stub_const("ENV", ENV.to_hash.merge(token => VALID_TOKEN)) }
subject { fetcher.fetch_github_token }
it { is_expected.to eq(VALID_TOKEN) }
end
context "when token in ENV is nil" do
before { stub_const("ENV", ENV.to_hash.merge(token => nil)) }
subject { fetcher.fetch_github_token }
it { is_expected.to be_nil }
end
context "when token in options and ENV is nil" do
let(:options) { { :token => VALID_TOKEN } }
before do
stub_const("ENV", ENV.to_hash.merge(token => nil))
end
subject { fetcher.fetch_github_token }
it { is_expected.to eq(VALID_TOKEN) }
end
context "when token in options and ENV specified" do
let(:options) { { :token => VALID_TOKEN } }
before do
stub_const("ENV", ENV.to_hash.merge(token => "no_matter_what"))
end
subject { fetcher.fetch_github_token }
it { is_expected.to eq(VALID_TOKEN) }
end
end
describe "#get_all_tags" do
context "when github_fetch_tags returns tags" do
it "returns tags" do
mock_tags = ['tag']
allow(fetcher).to receive(:github_fetch_tags).and_return(mock_tags)
expect(fetcher.get_all_tags).to eq(mock_tags)
end
end
end
describe "#github_fetch_tags" do
context "when wrong token provided" do
use_vcr_cassette
let(:options) do
{
:user => "skywinder",
:project => "changelog_test",
:token => INVALID_TOKEN
}
end
it "should raise Unauthorized error" do
expect { fetcher.github_fetch_tags }.to raise_error Github::Error::Unauthorized
end
end
context "when API call is valid" do
use_vcr_cassette
it "should return tags" do
expected_tags = [{"name"=>"v0.0.3", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3", "commit"=>{"sha"=>"a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90"}}, {"name"=>"v0.0.2", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.2", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.2", "commit"=>{"sha"=>"9b35bb13dcd15b68e7bcbf10cde5eb937a54f710", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/9b35bb13dcd15b68e7bcbf10cde5eb937a54f710"}}, {"name"=>"v0.0.1", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.1", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.1", "commit"=>{"sha"=>"4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d"}}, {"name"=>"0.0.4", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/0.0.4", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/0.0.4", "commit"=>{"sha"=>"ece0c3ab7142b21064b885061c55ede00ef6ce94", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/ece0c3ab7142b21064b885061c55ede00ef6ce94"}}]
expect(fetcher.github_fetch_tags).to eq(expected_tags)
end
end
end
describe "#fetch_closed_issues_and_pr" do
context "when API call is valid" do
use_vcr_cassette
it "returns issues" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expect(issues.size).to eq(7)
expect(pull_requests.size).to eq(14)
end
it "returns issue with proper key/values" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected_issue = {"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/events", "html_url"=>"https://github.com/skywinder/changelog_test/issues/14", "id"=>95419412, "number"=>14, "title"=>"Issue closed from commit from PR", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2015-07-16T12:06:08Z", "updated_at"=>"2015-07-16T12:21:42Z", "closed_at"=>"2015-07-16T12:21:42Z", "body"=>""}
expect(issues.first).to eq(expected_issue)
end
it "returns pull request with proper key/values" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected_pr = {"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/events", "html_url"=>"https://github.com/skywinder/changelog_test/pull/21", "id"=>124925759, "number"=>21, "title"=>"Merged br (should appear in change log with #20)", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2016-01-05T09:24:08Z", "updated_at"=>"2016-01-05T09:26:53Z", "closed_at"=>"2016-01-05T09:24:27Z", "pull_request"=>{"url"=>"https://api.github.com/repos/skywinder/changelog_test/pulls/21", "html_url"=>"https://github.com/skywinder/changelog_test/pull/21", "diff_url"=>"https://github.com/skywinder/changelog_test/pull/21.diff", "patch_url"=>"https://github.com/skywinder/changelog_test/pull/21.patch"}, "body"=>"to test https://github.com/skywinder/github-changelog-generator/pull/305\r\nshould appear in change log with #20"}
expect(pull_requests.first).to eq(expected_pr)
end
it "returns issues with labels" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected = [[], [], ["Bug"], [], ["enhancement"], ["some label"], []]
expect(issues.map{|i| i.labels.map(&:name) }).to eq(expected)
end
it "returns pull_requests with labels" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected = [[], [], [], [], [], ["enhancement"], [], [], ["invalid"], [], [], [], [], ["invalid"]]
expect(pull_requests.map{|i| i.labels.map(&:name) }).to eq(expected)
end
end
end
describe "#fetch_closed_pull_requests" do
context "when API call is valid" do
use_vcr_cassette
it "returns pull requests" do
pull_requests = fetcher.fetch_closed_pull_requests
expect(pull_requests.size).to eq(14)
end
it "returns correct pull request keys" do
pull_requests = fetcher.fetch_closed_pull_requests
pr = pull_requests.first
expect(pr.keys).to eq(["url", "id", "html_url", "diff_url", "patch_url", "issue_url", "number", "state", "locked", "title", "user", "body", "created_at", "updated_at", "closed_at", "merged_at", "merge_commit_sha", "assignee", "milestone", "commits_url", "review_comments_url", "review_comment_url", "comments_url", "statuses_url", "head", "base", "_links"])
end
end
end
describe "#fetch_events_async" do
context "when API call is valid" do
use_vcr_cassette
it "populates issues" do
issues = [{"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/events", "html_url"=>"https://github.com/skywinder/changelog_test/issues/14", "id"=>95419412, "number"=>14, "title"=>"Issue closed from commit from PR", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2015-07-16T12:06:08Z", "updated_at"=>"2015-07-16T12:21:42Z", "closed_at"=>"2015-07-16T12:21:42Z", "body"=>""}]
# Check that they are blank to begin with
expect(issues.first['events']).to be_nil
fetcher.fetch_events_async(issues)
issue_events = issues.first['events']
expected_events = [{"id"=>357462189, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"referenced", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:16Z"}, {"id"=>357462542, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462542", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"closed", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:42Z"}]
expect(issue_events).to eq(expected_events)
end
end
end
describe "#fetch_date_of_tag" do
context "when API call is valid" do
use_vcr_cassette
it "returns date" do
tag = {"name"=>"v0.0.3", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3", "commit"=>{"sha"=>"a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90"}}
dt = fetcher.fetch_date_of_tag(tag)
expect(dt).to eq(Time.parse("2015-03-04 19:01:48 UTC"))
end
end
end
describe "#fetch_commit" do
context "when API call is valid" do
use_vcr_cassette
it "returns commit" do
event = {"id"=>357462189, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"referenced", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:16Z"}
commit = fetcher.fetch_commit(event)
expectations = [
["sha", "decfe840d1a1b86e0c28700de5362d3365a29555"],
["url",
"https://api.github.com/repos/skywinder/changelog_test/git/commits/decfe840d1a1b86e0c28700de5362d3365a29555"],
["html_url",
"https://github.com/skywinder/changelog_test/commit/decfe840d1a1b86e0c28700de5362d3365a29555"],
["author",
{"name"=>"Petr Korolev",
"email"=>"sky4winder@gmail.com",
"date"=>"2015-07-16T12:11:01Z"}],
["committer",
{"name"=>"Petr Korolev",
"email"=>"sky4winder@gmail.com",
"date"=>"2015-07-16T12:11:01Z"}],
["tree",
{"sha"=>"0699c15258a7c2b2e157051fe19851d4f705cac8",
"url"=>
"https://api.github.com/repos/skywinder/changelog_test/git/trees/0699c15258a7c2b2e157051fe19851d4f705cac8"}],
["message", "fix #14"],
["parents",
[{"sha"=>"7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"url"=>
"https://api.github.com/repos/skywinder/changelog_test/git/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"html_url"=>
"https://github.com/skywinder/changelog_test/commit/7ec095e5e3caceacedabf44d0b9b10da17c92e51"}]
]
]
expectations.each do |property, val|
expect(commit.send(property)).to eq(val)
end
end
end
end
end

View File

@ -2,10 +2,10 @@
module GitHubChangelogGenerator module GitHubChangelogGenerator
describe Generator do describe Generator do
context "#exclude_issues_by_labels" do context "#exclude_issues_by_labels" do
let(:label) { { 'name' => "BAD" } } let(:label) { { "name" => "BAD" } }
let(:issue) { { 'labels' => [label] } } let(:issue) { { "labels" => [label] } }
let(:good_label) { { 'name' => "GOOD" } } let(:good_label) { { "name" => "GOOD" } }
let(:good_issue) { { 'labels' => [good_label] } } let(:good_issue) { { "labels" => [good_label] } }
let(:issues) { [issue, good_issue] } let(:issues) { [issue, good_issue] }
subject(:generator) { described_class.new(exclude_labels: %w(BAD BOO)) } subject(:generator) { described_class.new(exclude_labels: %w(BAD BOO)) }

View File

@ -1,12 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
describe GitHubChangelogGenerator::Generator do describe GitHubChangelogGenerator::Generator do
def tag_mash_with_name(tag) def tag_with_name(tag)
Hashie::Mash.new.tap { |mash_tag| mash_tag.name = tag } {}.tap { |mash_tag| mash_tag["name"] = tag }
end end
def tags_mash_from_strings(tags_strings) def tags_from_strings(tags_strings)
tags_strings.map do |tag| tags_strings.map do |tag|
tag_mash_with_name(tag) tag_with_name(tag)
end end
end end
@ -17,20 +17,20 @@ describe GitHubChangelogGenerator::Generator do
end end
subject do subject do
@generator.get_filtered_tags(tags_mash_from_strings(%w(1 2 3))) @generator.get_filtered_tags(tags_from_strings(%w(1 2 3)))
end end
it { is_expected.to be_a(Array) } it { is_expected.to be_a(Array) }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
context "when between_tags same as input array" do context "when between_tags same as input array" do
before do before do
@generator = GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2 3)) @generator = GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2 3))
end end
subject do subject do
@generator.get_filtered_tags(tags_mash_from_strings(%w(1 2 3))) @generator.get_filtered_tags(tags_from_strings(%w(1 2 3)))
end end
it { is_expected.to be_a(Array) } it { is_expected.to be_a(Array) }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
context "when between_tags filled with correct values" do context "when between_tags filled with correct values" do
@ -38,10 +38,10 @@ describe GitHubChangelogGenerator::Generator do
@generator = GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2)) @generator = GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2))
end end
subject do subject do
@generator.get_filtered_tags(tags_mash_from_strings(%w(1 2 3))) @generator.get_filtered_tags(tags_from_strings(%w(1 2 3)))
end end
it { is_expected.to be_a(Array) } it { is_expected.to be_a(Array) }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2))) } it { is_expected.to match_array(tags_from_strings(%w(1 2))) }
end end
context "when between_tags filled with invalid values" do context "when between_tags filled with invalid values" do
@ -50,133 +50,133 @@ describe GitHubChangelogGenerator::Generator do
end end
subject do subject do
@generator.get_filtered_tags(tags_mash_from_strings(%w(1 2 3))) @generator.get_filtered_tags(tags_from_strings(%w(1 2 3)))
end end
it { is_expected.to be_a(Array) } it { is_expected.to be_a(Array) }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) } it { is_expected.to match_array(tags_from_strings(%w(1))) }
end end
end end
describe "#get_filtered_tags" do describe "#get_filtered_tags" do
subject do subject do
generator.get_filtered_tags(tags_mash_from_strings(%w(1 2 3 4 5))) generator.get_filtered_tags(tags_from_strings(%w(1 2 3 4 5)))
end end
context "with excluded and between tags" do context "with excluded and between tags" do
let(:generator) { GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2 3), exclude_tags: %w(2)) } let(:generator) { GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2 3), exclude_tags: %w(2)) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 3))) }
end end
end end
describe "#filter_excluded_tags" do describe "#filter_excluded_tags" do
subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_excluded_tags(tags_from_strings(%w(1 2 3))) }
context "with matching string" do context "with matching string" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(3)) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(3)) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2))) } it { is_expected.to match_array(tags_from_strings(%w(1 2))) }
end end
context "with non-matching string" do context "with non-matching string" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(invalid tags)) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(invalid tags)) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
context "with matching regex" do context "with matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[23]/) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[23]/) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) } it { is_expected.to match_array(tags_from_strings(%w(1))) }
end end
context "with non-matching regex" do context "with non-matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[abc]/) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[abc]/) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
end end
describe "#filter_excluded_tags_regex" do describe "#filter_excluded_tags_regex" do
subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_excluded_tags(tags_from_strings(%w(1 2 3))) }
context "with matching regex" do context "with matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: "[23]") } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: "[23]") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) } it { is_expected.to match_array(tags_from_strings(%w(1))) }
end end
context "with non-matching regex" do context "with non-matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: "[45]") } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: "[45]") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
end end
describe "#filter_since_tag" do describe "#filter_since_tag" do
context "with filled array" do context "with filled array" do
subject { generator.filter_since_tag(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_since_tag(tags_from_strings(%w(1 2 3))) }
context "with valid since tag" do context "with valid since tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "2") } let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "2") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) } it { is_expected.to match_array(tags_from_strings(%w(1))) }
end end
context "with invalid since tag" do context "with invalid since tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "Invalid tag") } let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "Invalid tag") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
end end
context "with empty array" do context "with empty array" do
subject { generator.filter_since_tag(tags_mash_from_strings(%w())) } subject { generator.filter_since_tag(tags_from_strings(%w())) }
context "with valid since tag" do context "with valid since tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "2") } let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "2") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w())) } it { is_expected.to match_array(tags_from_strings(%w())) }
end end
context "with invalid since tag" do context "with invalid since tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "Invalid tag") } let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: "Invalid tag") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w())) } it { is_expected.to match_array(tags_from_strings(%w())) }
end end
end end
end end
describe "#filter_due_tag" do describe "#filter_due_tag" do
context "with filled array" do context "with filled array" do
subject { generator.filter_due_tag(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_due_tag(tags_from_strings(%w(1 2 3))) }
context "with valid due tag" do context "with valid due tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "2") } let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "2") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(3))) } it { is_expected.to match_array(tags_from_strings(%w(3))) }
end end
context "with invalid due tag" do context "with invalid due tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "Invalid tag") } let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "Invalid tag") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) }
end end
end end
context "with empty array" do context "with empty array" do
subject { generator.filter_due_tag(tags_mash_from_strings(%w())) } subject { generator.filter_due_tag(tags_from_strings(%w())) }
context "with valid due tag" do context "with valid due tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "2") } let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "2") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w())) } it { is_expected.to match_array(tags_from_strings(%w())) }
end end
context "with invalid due tag" do context "with invalid due tag" do
let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "Invalid tag") } let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: "Invalid tag") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w())) } it { is_expected.to match_array(tags_from_strings(%w())) }
end end
end end
end end
@ -191,7 +191,7 @@ describe GitHubChangelogGenerator::Generator do
end end
context "fetch already filled tag" do context "fetch already filled tag" do
before { @generator.instance_variable_set :@tag_times_hash, "valid_tag" => current_time } before { @generator.instance_variable_set :@tag_times_hash, "valid_tag" => current_time }
subject { @generator.get_time_of_tag tag_mash_with_name("valid_tag") } subject { @generator.get_time_of_tag tag_with_name("valid_tag") }
it { is_expected.to be_a_kind_of(Time) } it { is_expected.to be_a_kind_of(Time) }
it { is_expected.to eq(current_time) } it { is_expected.to eq(current_time) }
end end
@ -202,7 +202,7 @@ describe GitHubChangelogGenerator::Generator do
@generator.instance_variable_set :@fetcher, mock @generator.instance_variable_set :@fetcher, mock
end end
subject do subject do
of_tag = @generator.get_time_of_tag(tag_mash_with_name("valid_tag")) of_tag = @generator.get_time_of_tag(tag_with_name("valid_tag"))
of_tag of_tag
end end
it { is_expected.to be_a_kind_of(Time) } it { is_expected.to be_a_kind_of(Time) }
@ -229,13 +229,13 @@ describe GitHubChangelogGenerator::Generator do
@generator.sort_tags_by_date(tags) @generator.sort_tags_by_date(tags)
end end
context "sort unsorted tags" do context "sort unsorted tags" do
let(:tags) { tags_mash_from_strings %w(valid_tag1 valid_tag2 valid_tag3) } let(:tags) { tags_from_strings %w(valid_tag1 valid_tag2 valid_tag3) }
it { is_expected.to be_a_kind_of(Array) } it { is_expected.to be_a_kind_of(Array) }
it { is_expected.to match_array(tags.reverse!) } it { is_expected.to match_array(tags.reverse!) }
end end
context "sort sorted tags" do context "sort sorted tags" do
let(:tags) { tags_mash_from_strings %w(valid_tag3 valid_tag2 valid_tag1) } let(:tags) { tags_from_strings %w(valid_tag3 valid_tag2 valid_tag1) }
it { is_expected.to be_a_kind_of(Array) } it { is_expected.to be_a_kind_of(Array) }
it { is_expected.to match_array(tags) } it { is_expected.to match_array(tags) }

View File

@ -4,14 +4,13 @@ INVALID_TOKEN = "0000000000000000"
describe GitHubChangelogGenerator::OctoFetcher do describe GitHubChangelogGenerator::OctoFetcher do
let(:options) do let(:options) do
{ {
:user => "skywinder", user: "skywinder",
:project => "changelog_test", project: "changelog_test"
} }
end end
let(:fetcher) { GitHubChangelogGenerator::OctoFetcher.new(options) } let(:fetcher) { GitHubChangelogGenerator::OctoFetcher.new(options) }
describe "#fetch_github_token" do describe "#fetch_github_token" do
token = GitHubChangelogGenerator::OctoFetcher::CHANGELOG_GITHUB_TOKEN token = GitHubChangelogGenerator::OctoFetcher::CHANGELOG_GITHUB_TOKEN
context "when token in ENV exist" do context "when token in ENV exist" do
@ -27,7 +26,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
end end
context "when token in options and ENV is nil" do context "when token in options and ENV is nil" do
let(:options) { { :token => VALID_TOKEN } } let(:options) { { token: VALID_TOKEN } }
before do before do
stub_const("ENV", ENV.to_hash.merge(token => nil)) stub_const("ENV", ENV.to_hash.merge(token => nil))
@ -38,7 +37,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
end end
context "when token in options and ENV specified" do context "when token in options and ENV specified" do
let(:options) { { :token => VALID_TOKEN } } let(:options) { { token: VALID_TOKEN } }
before do before do
stub_const("ENV", ENV.to_hash.merge(token => "no_matter_what")) stub_const("ENV", ENV.to_hash.merge(token => "no_matter_what"))
@ -52,7 +51,7 @@ describe GitHubChangelogGenerator::OctoFetcher do
describe "#get_all_tags" do describe "#get_all_tags" do
context "when github_fetch_tags returns tags" do context "when github_fetch_tags returns tags" do
it "returns tags" do it "returns tags" do
mock_tags = ['tag'] mock_tags = ["tag"]
allow(fetcher).to receive(:github_fetch_tags).and_return(mock_tags) allow(fetcher).to receive(:github_fetch_tags).and_return(mock_tags)
expect(fetcher.get_all_tags).to eq(mock_tags) expect(fetcher.get_all_tags).to eq(mock_tags)
end end
@ -65,9 +64,9 @@ describe GitHubChangelogGenerator::OctoFetcher do
let(:options) do let(:options) do
{ {
:user => "skywinder", user: "skywinder",
:project => "changelog_test", project: "changelog_test",
:token => INVALID_TOKEN token: INVALID_TOKEN
} }
end end
@ -80,7 +79,42 @@ describe GitHubChangelogGenerator::OctoFetcher do
use_vcr_cassette use_vcr_cassette
it "should return tags" do it "should return tags" do
expected_tags = [{"name"=>"v0.0.3", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3", "commit"=>{"sha"=>"a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90"}}, {"name"=>"v0.0.2", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.2", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.2", "commit"=>{"sha"=>"9b35bb13dcd15b68e7bcbf10cde5eb937a54f710", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/9b35bb13dcd15b68e7bcbf10cde5eb937a54f710"}}, {"name"=>"v0.0.1", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.1", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.1", "commit"=>{"sha"=>"4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d"}}, {"name"=>"0.0.4", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/0.0.4", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/0.0.4", "commit"=>{"sha"=>"ece0c3ab7142b21064b885061c55ede00ef6ce94", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/ece0c3ab7142b21064b885061c55ede00ef6ce94"}}] expected_tags = [{ "name" => "v0.0.3",
"zipball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3",
"tarball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3",
"commit" =>
{ "sha" => "a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90",
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90" } },
{ "name" => "v0.0.2",
"zipball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.2",
"tarball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.2",
"commit" =>
{ "sha" => "9b35bb13dcd15b68e7bcbf10cde5eb937a54f710",
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/9b35bb13dcd15b68e7bcbf10cde5eb937a54f710" } },
{ "name" => "v0.0.1",
"zipball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.1",
"tarball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.1",
"commit" =>
{ "sha" => "4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d",
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d" } },
{ "name" => "0.0.4",
"zipball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/zipball/0.0.4",
"tarball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/tarball/0.0.4",
"commit" =>
{ "sha" => "ece0c3ab7142b21064b885061c55ede00ef6ce94",
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/ece0c3ab7142b21064b885061c55ede00ef6ce94" } }]
expect(fetcher.github_fetch_tags).to eq(expected_tags) expect(fetcher.github_fetch_tags).to eq(expected_tags)
end end
@ -92,12 +126,10 @@ describe GitHubChangelogGenerator::OctoFetcher do
end end
end end
describe "#fetch_closed_issues_and_pr" do describe "#fetch_closed_issues_and_pr" do
context "when API call is valid" do context "when API call is valid" do
use_vcr_cassette use_vcr_cassette
it "returns issues" do it "returns issues" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expect(issues.size).to eq(7) expect(issues.size).to eq(7)
@ -105,42 +137,127 @@ describe GitHubChangelogGenerator::OctoFetcher do
end end
it "returns issue with proper key/values" do it "returns issue with proper key/values" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr issues, _pull_requests = fetcher.fetch_closed_issues_and_pr
expected_issue = {"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/events", "html_url"=>"https://github.com/skywinder/changelog_test/issues/14", "id"=>95419412, "number"=>14, "title"=>"Issue closed from commit from PR", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2015-07-16T12:06:08Z", "updated_at"=>"2015-07-16T12:21:42Z", "closed_at"=>"2015-07-16T12:21:42Z", "body"=>""} expected_issue = { "url" => "https://api.github.com/repos/skywinder/changelog_test/issues/14",
"repository_url" => "https://api.github.com/repos/skywinder/changelog_test",
"labels_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}",
"comments_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments",
"events_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/14/events",
"html_url" => "https://github.com/skywinder/changelog_test/issues/14",
"id" => 95_419_412,
"number" => 14,
"title" => "Issue closed from commit from PR",
"user" =>
{ "login" => "skywinder",
"id" => 3_356_474,
"avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3",
"gravatar_id" => "",
"url" => "https://api.github.com/users/skywinder",
"html_url" => "https://github.com/skywinder",
"followers_url" => "https://api.github.com/users/skywinder/followers",
"following_url" =>
"https://api.github.com/users/skywinder/following{/other_user}",
"gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}",
"starred_url" =>
"https://api.github.com/users/skywinder/starred{/owner}{/repo}",
"subscriptions_url" => "https://api.github.com/users/skywinder/subscriptions",
"organizations_url" => "https://api.github.com/users/skywinder/orgs",
"repos_url" => "https://api.github.com/users/skywinder/repos",
"events_url" => "https://api.github.com/users/skywinder/events{/privacy}",
"received_events_url" =>
"https://api.github.com/users/skywinder/received_events",
"type" => "User",
"site_admin" => false },
"labels" => [],
"state" => "closed",
"locked" => false,
"assignee" => nil,
"milestone" => nil,
"comments" => 0,
"created_at" => "2015-07-16T12:06:08Z",
"updated_at" => "2015-07-16T12:21:42Z",
"closed_at" => "2015-07-16T12:21:42Z",
"body" => "" }
# Convert times to Time # Convert times to Time
expected_issue.each_pair do |k,v| expected_issue.each_pair do |k, v|
if v =~ /^2015-/ expected_issue[k] = Time.parse(v) if v =~ /^2015-/
expected_issue[k] = Time.parse(v)
end
end end
expect(issues.first).to eq(expected_issue) expect(issues.first).to eq(expected_issue)
end end
it "returns pull request with proper key/values" do it "returns pull request with proper key/values" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr _issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected_pr = {"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/events", "html_url"=>"https://github.com/skywinder/changelog_test/pull/21", "id"=>124925759, "number"=>21, "title"=>"Merged br (should appear in change log with #20)", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2016-01-05T09:24:08Z", "updated_at"=>"2016-01-05T09:26:53Z", "closed_at"=>"2016-01-05T09:24:27Z", "pull_request"=>{"url"=>"https://api.github.com/repos/skywinder/changelog_test/pulls/21", "html_url"=>"https://github.com/skywinder/changelog_test/pull/21", "diff_url"=>"https://github.com/skywinder/changelog_test/pull/21.diff", "patch_url"=>"https://github.com/skywinder/changelog_test/pull/21.patch"}, "body"=>"to test https://github.com/skywinder/github-changelog-generator/pull/305\r\nshould appear in change log with #20"} expected_pr = { "url" => "https://api.github.com/repos/skywinder/changelog_test/issues/21",
"repository_url" => "https://api.github.com/repos/skywinder/changelog_test",
"labels_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/21/labels{/name}",
"comments_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/21/comments",
"events_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/21/events",
"html_url" => "https://github.com/skywinder/changelog_test/pull/21",
"id" => 124_925_759,
"number" => 21,
"title" => "Merged br (should appear in change log with #20)",
"user" =>
{ "login" => "skywinder",
"id" => 3_356_474,
"avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3",
"gravatar_id" => "",
"url" => "https://api.github.com/users/skywinder",
"html_url" => "https://github.com/skywinder",
"followers_url" => "https://api.github.com/users/skywinder/followers",
"following_url" =>
"https://api.github.com/users/skywinder/following{/other_user}",
"gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}",
"starred_url" =>
"https://api.github.com/users/skywinder/starred{/owner}{/repo}",
"subscriptions_url" => "https://api.github.com/users/skywinder/subscriptions",
"organizations_url" => "https://api.github.com/users/skywinder/orgs",
"repos_url" => "https://api.github.com/users/skywinder/repos",
"events_url" => "https://api.github.com/users/skywinder/events{/privacy}",
"received_events_url" =>
"https://api.github.com/users/skywinder/received_events",
"type" => "User",
"site_admin" => false },
"labels" => [],
"state" => "closed",
"locked" => false,
"assignee" => nil,
"milestone" => nil,
"comments" => 0,
"created_at" => "2016-01-05T09:24:08Z",
"updated_at" => "2016-01-05T09:26:53Z",
"closed_at" => "2016-01-05T09:24:27Z",
"pull_request" =>
{ "url" => "https://api.github.com/repos/skywinder/changelog_test/pulls/21",
"html_url" => "https://github.com/skywinder/changelog_test/pull/21",
"diff_url" => "https://github.com/skywinder/changelog_test/pull/21.diff",
"patch_url" => "https://github.com/skywinder/changelog_test/pull/21.patch" },
"body" =>
"to test https://github.com/skywinder/github-changelog-generator/pull/305\r\nshould appear in change log with #20" }
# Convert times to Time # Convert times to Time
expected_pr.each_pair do |k,v| expected_pr.each_pair do |k, v|
if v =~ /^2016-01/ expected_pr[k] = Time.parse(v) if v =~ /^2016-01/
expected_pr[k] = Time.parse(v)
end
end end
expect(pull_requests.first).to eq(expected_pr) expect(pull_requests.first).to eq(expected_pr)
end end
it "returns issues with labels" do it "returns issues with labels" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr issues, _pull_requests = fetcher.fetch_closed_issues_and_pr
expected = [[], [], ["Bug"], [], ["enhancement"], ["some label"], []] expected = [[], [], ["Bug"], [], ["enhancement"], ["some label"], []]
expect(issues.map{|i| i['labels'].map{|l| l['name']} }).to eq(expected) expect(issues.map { |i| i["labels"].map { |l| l["name"] } }).to eq(expected)
end end
it "returns pull_requests with labels" do it "returns pull_requests with labels" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr _issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected = [[], [], [], [], [], ["enhancement"], [], [], ["invalid"], [], [], [], [], ["invalid"]] expected = [[], [], [], [], [], ["enhancement"], [], [], ["invalid"], [], [], [], [], ["invalid"]]
expect(pull_requests.map{|i| i['labels'].map{|l| l['name']} }).to eq(expected) expect(pull_requests.map { |i| i["labels"].map { |l| l["name"] } }).to eq(expected)
end end
end end
end end
@ -158,93 +275,222 @@ describe GitHubChangelogGenerator::OctoFetcher do
pull_requests = fetcher.fetch_closed_pull_requests pull_requests = fetcher.fetch_closed_pull_requests
pr = pull_requests.first pr = pull_requests.first
expect(pr.keys).to eq(["url", "id", "html_url", "diff_url", "patch_url", "issue_url", "number", "state", "locked", "title", "user", "body", "created_at", "updated_at", "closed_at", "merged_at", "merge_commit_sha", "assignee", "milestone", "commits_url", "review_comments_url", "review_comment_url", "comments_url", "statuses_url", "head", "base", "_links"]) expect(pr.keys).to eq(%w(url id html_url diff_url patch_url issue_url number state locked title user body created_at updated_at closed_at merged_at merge_commit_sha assignee milestone commits_url review_comments_url review_comment_url comments_url statuses_url head base _links))
end end
end end
end end
describe "#fetch_events_async" do describe "#fetch_events_async" do
context "when API call is valid" do context "when API call is valid" do
use_vcr_cassette use_vcr_cassette
it "populates issues" do it "populates issues" do
issues = [{"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/events", "html_url"=>"https://github.com/skywinder/changelog_test/issues/14", "id"=>95419412, "number"=>14, "title"=>"Issue closed from commit from PR", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2015-07-16T12:06:08Z", "updated_at"=>"2015-07-16T12:21:42Z", "closed_at"=>"2015-07-16T12:21:42Z", "body"=>""}] issues = [{ "url" => "https://api.github.com/repos/skywinder/changelog_test/issues/14",
"repository_url" => "https://api.github.com/repos/skywinder/changelog_test",
"labels_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}",
"comments_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments",
"events_url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/14/events",
"html_url" => "https://github.com/skywinder/changelog_test/issues/14",
"id" => 95_419_412,
"number" => 14,
"title" => "Issue closed from commit from PR",
"user" =>
{ "login" => "skywinder",
"id" => 3_356_474,
"avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3",
"gravatar_id" => "",
"url" => "https://api.github.com/users/skywinder",
"html_url" => "https://github.com/skywinder",
"followers_url" => "https://api.github.com/users/skywinder/followers",
"following_url" =>
"https://api.github.com/users/skywinder/following{/other_user}",
"gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}",
"starred_url" =>
"https://api.github.com/users/skywinder/starred{/owner}{/repo}",
"subscriptions_url" =>
"https://api.github.com/users/skywinder/subscriptions",
"organizations_url" => "https://api.github.com/users/skywinder/orgs",
"repos_url" => "https://api.github.com/users/skywinder/repos",
"events_url" => "https://api.github.com/users/skywinder/events{/privacy}",
"received_events_url" =>
"https://api.github.com/users/skywinder/received_events",
"type" => "User",
"site_admin" => false },
"labels" => [],
"state" => "closed",
"locked" => false,
"assignee" => nil,
"milestone" => nil,
"comments" => 0,
"created_at" => "2015-07-16T12:06:08Z",
"updated_at" => "2015-07-16T12:21:42Z",
"closed_at" => "2015-07-16T12:21:42Z",
"body" => "" }]
# Check that they are blank to begin with # Check that they are blank to begin with
expect(issues.first['events']).to be_nil expect(issues.first["events"]).to be_nil
fetcher.fetch_events_async(issues) fetcher.fetch_events_async(issues)
issue_events = issues.first['events'] issue_events = issues.first["events"]
expected_events = [{"id"=>357462189, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"referenced", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:16Z"}, {"id"=>357462542, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462542", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"closed", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:42Z"}] expected_events = [{ "id" => 357_462_189,
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189",
"actor" =>
{ "login" => "skywinder",
"id" => 3_356_474,
"avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3",
"gravatar_id" => "",
"url" => "https://api.github.com/users/skywinder",
"html_url" => "https://github.com/skywinder",
"followers_url" => "https://api.github.com/users/skywinder/followers",
"following_url" =>
"https://api.github.com/users/skywinder/following{/other_user}",
"gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}",
"starred_url" =>
"https://api.github.com/users/skywinder/starred{/owner}{/repo}",
"subscriptions_url" =>
"https://api.github.com/users/skywinder/subscriptions",
"organizations_url" => "https://api.github.com/users/skywinder/orgs",
"repos_url" => "https://api.github.com/users/skywinder/repos",
"events_url" => "https://api.github.com/users/skywinder/events{/privacy}",
"received_events_url" =>
"https://api.github.com/users/skywinder/received_events",
"type" => "User",
"site_admin" => false },
"event" => "referenced",
"commit_id" => "decfe840d1a1b86e0c28700de5362d3365a29555",
"commit_url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555",
"created_at" => "2015-07-16T12:21:16Z" },
{ "id" => 357_462_542,
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462542",
"actor" =>
{ "login" => "skywinder",
"id" => 3_356_474,
"avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3",
"gravatar_id" => "",
"url" => "https://api.github.com/users/skywinder",
"html_url" => "https://github.com/skywinder",
"followers_url" => "https://api.github.com/users/skywinder/followers",
"following_url" =>
"https://api.github.com/users/skywinder/following{/other_user}",
"gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}",
"starred_url" =>
"https://api.github.com/users/skywinder/starred{/owner}{/repo}",
"subscriptions_url" =>
"https://api.github.com/users/skywinder/subscriptions",
"organizations_url" => "https://api.github.com/users/skywinder/orgs",
"repos_url" => "https://api.github.com/users/skywinder/repos",
"events_url" => "https://api.github.com/users/skywinder/events{/privacy}",
"received_events_url" =>
"https://api.github.com/users/skywinder/received_events",
"type" => "User",
"site_admin" => false },
"event" => "closed",
"commit_id" => "decfe840d1a1b86e0c28700de5362d3365a29555",
"commit_url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555",
"created_at" => "2015-07-16T12:21:42Z" }]
# Convert times to Time # Convert times to Time
expected_events.map! do |event| expected_events.map! do |event|
event.each_pair do |k, v| event.each_pair do |k, v|
if v =~ /^201[56]-/ event[k] = Time.parse(v) if v =~ /^201[56]-/
event[k] = Time.parse(v)
end
end end
end end
expect(issue_events).to eq(expected_events) expect(issue_events).to eq(expected_events)
end end
end end
end end
describe "#fetch_date_of_tag" do describe "#fetch_date_of_tag" do
context "when API call is valid" do context "when API call is valid" do
use_vcr_cassette use_vcr_cassette
it "returns date" do it "returns date" do
tag = {"name"=>"v0.0.3", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3", "commit"=>{"sha"=>"a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90"}} tag = { "name" => "v0.0.3",
"zipball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3",
"tarball_url" =>
"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3",
"commit" =>
{ "sha" => "a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90",
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90" } }
dt = fetcher.fetch_date_of_tag(tag) dt = fetcher.fetch_date_of_tag(tag)
expect(dt).to eq(Time.parse("2015-03-04 19:01:48 UTC")) expect(dt).to eq(Time.parse("2015-03-04 19:01:48 UTC"))
end end
end end
end end
describe "#fetch_commit" do describe "#fetch_commit" do
context "when API call is valid" do context "when API call is valid" do
use_vcr_cassette use_vcr_cassette
it "returns commit" do it "returns commit" do
event = {"id"=>357462189, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"referenced", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:16Z"} event = { "id" => 357_462_189,
"url" =>
"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189",
"actor" =>
{ "login" => "skywinder",
"id" => 3_356_474,
"avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3",
"gravatar_id" => "",
"url" => "https://api.github.com/users/skywinder",
"html_url" => "https://github.com/skywinder",
"followers_url" => "https://api.github.com/users/skywinder/followers",
"following_url" =>
"https://api.github.com/users/skywinder/following{/other_user}",
"gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}",
"starred_url" =>
"https://api.github.com/users/skywinder/starred{/owner}{/repo}",
"subscriptions_url" => "https://api.github.com/users/skywinder/subscriptions",
"organizations_url" => "https://api.github.com/users/skywinder/orgs",
"repos_url" => "https://api.github.com/users/skywinder/repos",
"events_url" => "https://api.github.com/users/skywinder/events{/privacy}",
"received_events_url" =>
"https://api.github.com/users/skywinder/received_events",
"type" => "User",
"site_admin" => false },
"event" => "referenced",
"commit_id" => "decfe840d1a1b86e0c28700de5362d3365a29555",
"commit_url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555",
"created_at" => "2015-07-16T12:21:16Z" }
commit = fetcher.fetch_commit(event) commit = fetcher.fetch_commit(event)
expectations = [ expectations = [
["sha", "decfe840d1a1b86e0c28700de5362d3365a29555"], %w(sha decfe840d1a1b86e0c28700de5362d3365a29555),
["url", ["url",
"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555"], "https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555"],
# OLD API: "https://api.github.com/repos/skywinder/changelog_test/git/commits/decfe840d1a1b86e0c28700de5362d3365a29555"], # OLD API: "https://api.github.com/repos/skywinder/changelog_test/git/commits/decfe840d1a1b86e0c28700de5362d3365a29555"],
["html_url", ["html_url",
"https://github.com/skywinder/changelog_test/commit/decfe840d1a1b86e0c28700de5362d3365a29555"], "https://github.com/skywinder/changelog_test/commit/decfe840d1a1b86e0c28700de5362d3365a29555"],
["author", ["author",
{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}], { "login" => "skywinder", "id" => 3_356_474, "avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id" => "", "url" => "https://api.github.com/users/skywinder", "html_url" => "https://github.com/skywinder", "followers_url" => "https://api.github.com/users/skywinder/followers", "following_url" => "https://api.github.com/users/skywinder/following{/other_user}", "gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url" => "https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url" => "https://api.github.com/users/skywinder/subscriptions", "organizations_url" => "https://api.github.com/users/skywinder/orgs", "repos_url" => "https://api.github.com/users/skywinder/repos", "events_url" => "https://api.github.com/users/skywinder/events{/privacy}", "received_events_url" => "https://api.github.com/users/skywinder/received_events", "type" => "User", "site_admin" => false }],
["committer", ["committer",
{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}], { "login" => "skywinder", "id" => 3_356_474, "avatar_url" => "https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id" => "", "url" => "https://api.github.com/users/skywinder", "html_url" => "https://github.com/skywinder", "followers_url" => "https://api.github.com/users/skywinder/followers", "following_url" => "https://api.github.com/users/skywinder/following{/other_user}", "gists_url" => "https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url" => "https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url" => "https://api.github.com/users/skywinder/subscriptions", "organizations_url" => "https://api.github.com/users/skywinder/orgs", "repos_url" => "https://api.github.com/users/skywinder/repos", "events_url" => "https://api.github.com/users/skywinder/events{/privacy}", "received_events_url" => "https://api.github.com/users/skywinder/received_events", "type" => "User", "site_admin" => false }],
["parents", ["parents",
[{"sha"=>"7ec095e5e3caceacedabf44d0b9b10da17c92e51", [{ "sha" => "7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"url"=> "url" =>
"https://api.github.com/repos/skywinder/changelog_test/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51", "https://api.github.com/repos/skywinder/changelog_test/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51",
# OLD API: "https://api.github.com/repos/skywinder/changelog_test/git/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51", # OLD API: "https://api.github.com/repos/skywinder/changelog_test/git/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"html_url"=> "html_url" =>
"https://github.com/skywinder/changelog_test/commit/7ec095e5e3caceacedabf44d0b9b10da17c92e51"}] "https://github.com/skywinder/changelog_test/commit/7ec095e5e3caceacedabf44d0b9b10da17c92e51" }]
] ]
] ]
expectations.each do |property, val| expectations.each do |property, val|
expect(commit[property]).to eq(val) expect(commit[property]).to eq(val)
end end
end end
end end
end end
end end