Skip to content

Commit

Permalink
Merge pull request #220 from tuexss/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
skywinder committed Mar 27, 2015
2 parents 48c2f26 + c5a01bb commit 8de0b33
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bin/github_changelog_generator
@@ -1,4 +1,4 @@
#! /usr/bin/env ruby

require_relative '../lib/github_changelog_generator'
GitHubChangelogGenerator::ChangelogGenerator.new.compund_changelog
GitHubChangelogGenerator::ChangelogGenerator.new.compound_changelog
58 changes: 22 additions & 36 deletions lib/github_changelog_generator.rb
Expand Up @@ -15,8 +15,8 @@ class ChangelogGenerator
attr_accessor :options, :all_tags, :github

PER_PAGE_NUMBER = 30
GH_RATE_LIMIT_EXCEEDED_MSG = 'Warning: GitHub API rate limit exceed (5000 per hour), change log may not ' \
'contain some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument'
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 = Parser.parse_options
Expand Down Expand Up @@ -95,7 +95,7 @@ def find_closed_date_by_commit(issue)
commit = @github.git_data.commits.get @options[:user], @options[:project], event[:commit_id]
issue[:actual_date] = commit[:author][:date]
rescue
puts "Warning: can't fetch commit #{event[:commit_id]} probably it referenced from another repo.".yellow
puts "Warning: Can't fetch commit #{event[:commit_id]}. It is probably referenced from another repo.".yellow
issue[:actual_date] = issue[:closed_at]
end
end
Expand Down Expand Up @@ -139,7 +139,7 @@ def fetch_merged_at_pull_requests
}

if @options[:verbose]
puts 'Fetching merged dates... Done!'
puts 'Fetching merged dates: Done!'
end
end

Expand All @@ -150,7 +150,7 @@ def get_filtered_pull_requests

unless @options[:include_labels].nil?
filtered_pull_requests = @pull_requests.select { |issue|
# add all labels from @options[:incluse_labels] array
# add all labels from @options[:include_labels] array
(issue.labels.map(&:name) & @options[:include_labels]).any?
}
end
Expand All @@ -176,7 +176,7 @@ def get_filtered_pull_requests
filtered_pull_requests
end

def compund_changelog
def compound_changelog
log = "# Change Log\n\n"

if @options[:unreleased_only]
Expand Down Expand Up @@ -218,13 +218,13 @@ def generate_log_for_all_tags
fetch_tags_dates

if @options[:verbose]
puts 'Sorting tags..'
puts 'Sorting tags...'
end

@all_tags.sort_by! { |x| get_time_of_tag(x) }.reverse!

if @options[:verbose]
puts 'Generating log..'
puts 'Generating log...'
end

log = ''
Expand All @@ -248,7 +248,7 @@ def generate_log_for_all_tags

def fetch_tags_dates
if @options[:verbose]
print "Fetching tags dates..\r"
print "Fetching tag dates...\r"
end

# Async fetching tags:
Expand All @@ -275,12 +275,6 @@ def fetch_tags_dates
end
end

def is_megred(number)
@github.pull_requests.merged? @options[:user], @options[:project], number
rescue
puts GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end

def get_all_tags
if @options[:verbose]
print "Fetching tags...\r"
Expand All @@ -298,12 +292,11 @@ def get_all_tags
tags.concat(page)
end
print " \r"
if @options[:verbose]
puts "Found #{tags.count} tags"
end

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
Expand All @@ -318,7 +311,7 @@ def fetch_github_token

unless env_var
puts 'Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.'.yellow
puts 'This script can make only 50 requests to GitHub API per hour without token!'.yellow
puts 'This script can make only 50 requests per hour to GitHub API without a token!'.yellow
end

@github_token ||= env_var
Expand Down Expand Up @@ -417,23 +410,14 @@ def delete_by_time(array, hash_key, older_tag = nil, newer_tag = nil)
# @param [String] older_tag_name
# @return [String]
def create_log(pull_requests, issues, newer_tag, older_tag_name = nil)
newer_tag_time = newer_tag.nil? ? nil : get_time_of_tag(newer_tag)
newer_tag_name = newer_tag.nil? ? nil : newer_tag['name']
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_link = newer_tag.nil? ? 'HEAD' : newer_tag_name

github_site = options[:github_site] || 'https://github.com'
project_url = "#{github_site}/#{@options[:user]}/#{@options[:project]}"

if newer_tag.nil?
newer_tag_name = @options[:unreleased_label]
newer_tag_link = 'HEAD'
newer_tag_time = Time.new
else
newer_tag_link = newer_tag_name
end

log = ''

log += generate_header(log, newer_tag_name, newer_tag_link, newer_tag_time, older_tag_name, project_url)
log = generate_header(newer_tag_name, newer_tag_link, newer_tag_time, older_tag_name, project_url)

if @options[:issues]
# Generate issues:
Expand Down Expand Up @@ -488,9 +472,11 @@ def generate_log_from_array(issues, prefix)
log
end

def generate_header(log, newer_tag_name, newer_tag_name2, newer_tag_time, older_tag_name, project_url)
def generate_header(newer_tag_name, newer_tag_name2, newer_tag_time, older_tag_name, project_url)
log = ''

# Generate date string:
time_string = newer_tag_time.strftime @options[:format]
time_string = newer_tag_time.strftime @options[:dateformat]

# Generate tag name and link
if newer_tag_name.equal? @options[:unreleased_label]
Expand Down Expand Up @@ -532,7 +518,7 @@ def get_filtered_issues

unless @options[:include_labels].nil?
filtered_issues = issues.select { |issue|
# add all labels from @options[:incluse_labels] array
# add all labels from @options[:include_labels] array
(issue.labels.map(&:name) & @options[:include_labels]).any?
}
end
Expand Down Expand Up @@ -635,6 +621,6 @@ def fetch_events_async(issues)
end

if __FILE__ == $PROGRAM_NAME
GitHubChangelogGenerator::ChangelogGenerator.new.compund_changelog
GitHubChangelogGenerator::ChangelogGenerator.new.compound_changelog
end
end
41 changes: 22 additions & 19 deletions lib/github_changelog_generator/parser.rb
Expand Up @@ -9,24 +9,27 @@ def self.parse_options
options = {
tag1: nil,
tag2: nil,
format: '%Y-%m-%d',
dateformat: '%Y-%m-%d',
output: 'CHANGELOG.md',
exclude_labels: %w(duplicate question invalid wontfix),
pulls: true,
issues: true,
verbose: true,
add_issues_wo_labels: true,
add_pr_wo_labels: true,
pulls: true,
filter_issues_by_milestone: true,
author: true,
unreleased: true,
unreleased_label: 'Unreleased',
compare_link: true,
include_labels: %w(bug enhancement),
exclude_labels: %w(duplicate question invalid wontfix),
max_issues: nil,
simple_list: false,
verbose: true,

merge_prefix: '**Merged pull requests:**',
issue_prefix: '**Closed issues:**',
bug_prefix: '**Fixed bugs:**',
enhancement_prefix: '**Implemented enhancements:**',
author: true,
filter_issues_by_milestone: true,
max_issues: nil,
compare_link: true,
unreleased: true,
unreleased_label: 'Unreleased',
branch: 'origin'
}

Expand All @@ -38,25 +41,25 @@ def self.parse_options
opts.on('-p', '--project [PROJECT]', 'Name of project on GitHub') do |last|
options[:project] = last
end
opts.on('-t', '--token [TOKEN]', 'To make more than 50 requests per hour your GitHub token required. You can generate it here: https://github.com/settings/tokens/new') do |last|
opts.on('-t', '--token [TOKEN]', 'To make more than 50 requests per hour your GitHub token is required. You can generate it at: https://github.com/settings/tokens/new') do |last|
options[:token] = last
end
opts.on('-f', '--date-format [FORMAT]', 'Date format. Default is %d/%m/%y') do |last|
options[:format] = last
opts.on('-f', '--date-format [FORMAT]', 'Date format. Default is %Y-%m-%d') do |last|
options[:dateformat] = last
end
opts.on('-o', '--output [NAME]', 'Output file. Default is CHANGELOG.md') do |last|
options[:output] = last
end
opts.on('--[no-]issues', 'Include closed issues to changelog. Default is true') do |v|
opts.on('--[no-]issues', 'Include closed issues in changelog. Default is true') do |v|
options[:issues] = v
end
opts.on('--[no-]issues-wo-labels', 'Include closed issues without labels to changelog. Default is true') do |v|
opts.on('--[no-]issues-wo-labels', 'Include closed issues without labels in changelog. Default is true') do |v|
options[:add_issues_wo_labels] = v
end
opts.on('--[no-]pr-wo-labels', 'Include pull requests without labels to changelog. Default is true') do |v|
opts.on('--[no-]pr-wo-labels', 'Include pull requests without labels in changelog. Default is true') do |v|
options[:add_pr_wo_labels] = v
end
opts.on('--[no-]pull-requests', 'Include pull-requests to changelog. Default is true') do |v|
opts.on('--[no-]pull-requests', 'Include pull-requests in changelog. Default is true') do |v|
options[:pulls] = v
end
opts.on('--[no-]filter-by-milestone', 'Use milestone to detect when issue was resolved. Default is true') do |last|
Expand All @@ -77,10 +80,10 @@ def self.parse_options
opts.on('--[no-]compare-link', 'Include compare link (Full Changelog) between older version and newer version. Default is true') do |v|
options[:compare_link] = v
end
opts.on('--include-labels x,y,z', Array, 'Issues only with that labels will be included to changelog. Default is \'bug,enhancement\'') do |list|
opts.on('--include-labels x,y,z', Array, 'Only issues with the specified labels will be included in the changelog. Default is \'bug,enhancement\'') do |list|
options[:include_labels] = list
end
opts.on('--exclude-labels x,y,z', Array, 'Issues with that labels will be always excluded from changelog. Default is \'duplicate,question,invalid,wontfix\'') do |list|
opts.on('--exclude-labels x,y,z', Array, 'Issues with the specified labels will be always excluded from changelog. Default is \'duplicate,question,invalid,wontfix\'') do |list|
options[:exclude_labels] = list
end
opts.on('--max-issues [NUMBER]', Integer, 'Max number of issues to fetch from GitHub. Default is unlimited') do |max|
Expand Down

0 comments on commit 8de0b33

Please sign in to comment.