move methods

This commit is contained in:
Petr Korolev 2015-04-21 20:42:33 +03:00
parent 9ab84ac181
commit 088c98d0bf
2 changed files with 134 additions and 152 deletions

View File

@ -20,33 +20,18 @@ module GitHubChangelogGenerator
class ChangelogGenerator class ChangelogGenerator
attr_accessor :options, :all_tags, :github attr_accessor :options, :all_tags, :github
PER_PAGE_NUMBER = 30
GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: GitHub API rate limit (5000 per hour) exceeded, change log may be " \
"missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."
# Class, responsible for whole change log generation cycle # Class, responsible for whole change log generation cycle
# @return initialised insance of ChangelogGenerator # @return initialised instance of ChangelogGenerator
def initialize def initialize
@options = Parser.parse_options @options = Parser.parse_options
@fetcher = GitHubChangelogGenerator::Fetcher.new @options @fetcher = GitHubChangelogGenerator::Fetcher.new @options
github_options = { per_page: PER_PAGE_NUMBER } @generator = Generator.new @options
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?
begin @all_tags = @fetcher.get_filtered_tags
@github = Github.new github_options
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
@generator = Generator.new(@options) @issues, @pull_requests = @fetcher.fetch_issues_and_pull_requests
@all_tags = get_filtered_tags
@issues, @pull_requests = fetch_issues_and_pull_requests
@pull_requests = @options[:pulls] ? get_filtered_pull_requests : [] @pull_requests = @options[:pulls] ? get_filtered_pull_requests : []
@ -57,22 +42,6 @@ module GitHubChangelogGenerator
@tag_times_hash = {} @tag_times_hash = {}
end end
# Return tags after filtering tags in lists provided by option: --between-tags & --exclude-tags
#
# @return [Array]
def get_filtered_tags
all_tags = get_all_tags
filtered_tags = []
if @options[:between_tags]
@options[:between_tags].each do |tag|
unless all_tags.include? tag
puts "Warning: can't find tag #{tag}, specified with --between-tags option.".yellow
end
end
filtered_tags = all_tags.select { |tag| @options[:between_tags].include? tag }
end
filtered_tags
end
def detect_actual_closed_dates def detect_actual_closed_dates
if @options[:verbose] if @options[:verbose]
@ -128,42 +97,6 @@ module GitHubChangelogGenerator
puts JSON.pretty_generate(json) puts JSON.pretty_generate(json)
end end
# This method fetch missing required attributes for pull requests
# :merged_at - is a date, when issue PR was merged.
# More correct to use this date, not closed date.
def fetch_merged_at_pull_requests
if @options[:verbose]
print "Fetching merged dates...\r"
end
pull_requests = []
begin
response = @github.pull_requests.list @options[:user], @options[:project], state: "closed"
page_i = 0
response.each_page do |page|
page_i += PER_PAGE_NUMBER
count_pages = response.count_pages
print "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
pull_requests.concat(page)
end
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
print " \r"
@pull_requests.each { |pr|
fetched_pr = pull_requests.find { |fpr|
fpr.number == pr.number
}
pr[:merged_at] = fetched_pr[:merged_at]
pull_requests.delete(fetched_pr)
}
if @options[:verbose]
puts "Fetching merged dates: Done!"
end
end
# This method fetches missing params for PR and filter them by specified options # This method fetches missing params for PR and filter them by specified options
# It include add all PR's with labels from @options[:include_labels] array # It include add all PR's with labels from @options[:include_labels] array
# And exclude all from :exclude_labels array. # And exclude all from :exclude_labels array.
@ -311,48 +244,6 @@ module GitHubChangelogGenerator
end end
end end
def get_all_tags
if @options[:verbose]
print "Fetching tags...\r"
end
tags = []
begin
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 "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
tags.concat(page)
end
print " \r"
if tags.count == 0
puts "Warning: Can't find any tags in repo. Make sure, that you push tags to remote repo via 'git push --tags'".yellow
elsif @options[:verbose]
puts "Found #{tags.count} tags"
end
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
tags
end
def fetch_github_token
env_var = @options[:token] ? @options[:token] : (ENV.fetch "CHANGELOG_GITHUB_TOKEN", nil)
unless env_var
puts "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.".yellow
puts "This script can make only 50 requests per hour to GitHub API without a token!".yellow
end
@github_token ||= env_var
end
# Generate log only between 2 specified tags # Generate log only between 2 specified tags
# @param [String] older_tag all issues before this tag date will be excluded. May be nil, if it's first tag # @param [String] 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
@ -457,9 +348,9 @@ module GitHubChangelogGenerator
# @param [String] older_tag_name Older tag, used for the links. Could be nil for last tag. # @param [String] older_tag_name Older tag, used for the links. Could be nil for last tag.
# @return [String] Ready and parsed section # @return [String] Ready and parsed section
def create_log(pull_requests, issues, newer_tag, older_tag_name = nil) def create_log(pull_requests, issues, newer_tag, older_tag_name = nil)
newer_tag_time = newer_tag.nil? ? Time.new : get_time_of_tag(newer_tag) newer_tag_time = newer_tag.nil? ? Time.new : get_time_of_tag(newer_tag)
newer_tag_name = newer_tag.nil? ? @options[:unreleased_label] : newer_tag["name"] newer_tag_name = newer_tag.nil? ? @options[:unreleased_label] : newer_tag["name"]
newer_tag_link = newer_tag.nil? ? "HEAD" : newer_tag_name newer_tag_link = newer_tag.nil? ? "HEAD" : newer_tag_name
github_site = options[:github_site] || "https://github.com" github_site = options[:github_site] || "https://github.com"
project_url = "#{github_site}/#{@options[:user]}/#{@options[:project]}" project_url = "#{github_site}/#{@options[:user]}/#{@options[:project]}"
@ -584,42 +475,6 @@ module GitHubChangelogGenerator
filtered_issues filtered_issues
end 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 and pull requests
def fetch_issues_and_pull_requests
if @options[:verbose]
print "Fetching closed issues...\r"
end
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 "Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
issues.concat(page)
break if @options[:max_issues] && issues.length >= @options[:max_issues]
end
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
print " \r"
if @options[:verbose]
puts "Received issues: #{issues.count}"
end
# remove pull request from issues:
issues.partition { |x|
x[:pull_request].nil?
}
end
def fetch_event_for_issues_and_pr def fetch_event_for_issues_and_pr
if @options[:verbose] if @options[:verbose]
print "Fetching events for issues and PR: 0/#{@issues.count + @pull_requests.count}\r" print "Fetching events for issues and PR: 0/#{@issues.count + @pull_requests.count}\r"

View File

@ -4,6 +4,10 @@ module GitHubChangelogGenerator
# Example: # Example:
# fetcher = GitHubChangelogGenerator::Fetcher.new options # fetcher = GitHubChangelogGenerator::Fetcher.new options
class Fetcher class Fetcher
PER_PAGE_NUMBER = 30
GH_RATE_LIMIT_EXCEEDED_MSG = "Warning: GitHub API rate limit (5000 per hour) exceeded, change log may be " \
"missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."
def initialize(options = {}) def initialize(options = {})
@options = options @options = options
@ -11,7 +15,7 @@ module GitHubChangelogGenerator
@project = @options[:project] @project = @options[:project]
@github_token = fetch_github_token @github_token = fetch_github_token
github_options = { per_page: PER_PAGE_NUMBER } github_options = {per_page: PER_PAGE_NUMBER}
github_options[:oauth_token] = @github_token unless @github_token.nil? github_options[:oauth_token] = @github_token unless @github_token.nil?
github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil? github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil?
github_options[:site] = options[:github_endpoint] unless options[:github_site].nil? github_options[:site] = options[:github_endpoint] unless options[:github_site].nil?
@ -36,5 +40,128 @@ module GitHubChangelogGenerator
env_var env_var
end end
# Fetch all tags from repo
# @return [Array] array of tags
def get_all_tags
if @options[:verbose]
print "Fetching tags...\r"
end
tags = []
begin
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 "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
tags.concat(page)
end
print " \r"
if tags.count == 0
puts "Warning: Can't find any tags in repo. Make sure, that you push tags to remote repo via 'git push --tags'".yellow
elsif @options[:verbose]
puts "Found #{tags.count} tags"
end
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
tags
end
# Return tags after filtering tags in lists provided by option: --between-tags & --exclude-tags
#
# @return [Array]
def get_filtered_tags
all_tags = get_all_tags
filtered_tags = []
if @options[:between_tags]
@options[:between_tags].each do |tag|
unless all_tags.include? tag
puts "Warning: can't find tag #{tag}, specified with --between-tags option.".yellow
end
end
filtered_tags = all_tags.select { |tag| @options[:between_tags].include? tag }
end
filtered_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 and pull requests
def fetch_issues_and_pull_requests
if @options[:verbose]
print "Fetching closed issues...\r"
end
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 "Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
issues.concat(page)
break if @options[:max_issues] && issues.length >= @options[:max_issues]
end
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
print " \r"
if @options[:verbose]
puts "Received issues: #{issues.count}"
end
# remove pull request from issues:
issues.partition { |x|
x[:pull_request].nil?
}
end
# This method fetch missing required attributes for pull requests
# :merged_at - is a date, when issue PR was merged.
# More correct to use this date, not closed date.
def fetch_merged_at_pull_requests
if @options[:verbose]
print "Fetching merged dates...\r"
end
pull_requests = []
begin
response = @github.pull_requests.list @options[:user], @options[:project], state: "closed"
page_i = 0
response.each_page do |page|
page_i += PER_PAGE_NUMBER
count_pages = response.count_pages
print "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
pull_requests.concat(page)
end
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
print " \r"
@pull_requests.each { |pr|
fetched_pr = pull_requests.find { |fpr|
fpr.number == pr.number
}
pr[:merged_at] = fetched_pr[:merged_at]
pull_requests.delete(fetched_pr)
}
if @options[:verbose]
puts "Fetching merged dates: Done!"
end
end
end end
end end