Skip to content

Add support for issues in CHANGELOG #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 93 additions & 38 deletions lib/github_changelog_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ChangelogGenerator

attr_accessor :options, :all_tags
attr_accessor :options, :all_tags, :github

def initialize()

Expand All @@ -20,6 +20,7 @@ def initialize()
end
@all_tags = self.get_all_tags
@pull_requests = self.get_all_closed_pull_requests
@issues = self.get_all_issues

@tag_times_hash = {}
end
Expand All @@ -35,17 +36,14 @@ def exec_command(cmd)


def get_all_closed_pull_requests


issues = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
json = issues.body
request = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
pull_requests = request.body

if @options[:verbose]
puts 'Receive all pull requests'
end

json

pull_requests
end

def compund_changelog
Expand Down Expand Up @@ -87,7 +85,7 @@ def compund_changelog
puts log
end

log += "\n\n*This file was generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*"
log += "\n\n**This file was generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*"

output_filename = "#{@options[:output]}"
File.open(output_filename, 'w') { |file| file.write(log) }
Expand All @@ -111,19 +109,6 @@ def is_megred(number)
@github.pull_requests.merged? @options[:user], @options[:project], number
end

def get_all_merged_pull_requests
json = self.get_all_closed_pull_requests
puts 'Check if the requests is merged... (it can take a while)'

json.delete_if { |req|
merged = self.is_megred(req[:number])
if @options[:verbose]
puts "##{req[:number]} #{merged ? 'merged' : 'not merged'}"
end
!merged
}
end

def get_all_tags

url = "https://api.github.com/repos/#{@options[:user]}/#{@options[:project]}/tags"
Expand All @@ -133,7 +118,7 @@ def get_all_tags
end

response = HTTParty.get(url,
:headers => {'Authorization' => 'token 8587bb22f6bf125454768a4a19dbcc774ea68d48',
:headers => {'Authorization' => "token #{@options[:token]}",
'User-Agent' => 'Changelog-Generator'})

json_parse = JSON.parse(response.body)
Expand All @@ -160,15 +145,37 @@ def generate_log_between_tags(since_tag, till_tag)
pull_requests = Array.new(@pull_requests)

pull_requests.delete_if { |req|
t = Time.parse(req[:merged_at]).utc
tag_is_later_since = t > since_tag_time
tag_is_before_till = t <= till_tag_time
if req[:merged_at]
t = Time.parse(req[:merged_at]).utc
tag_is_later_since = t > since_tag_time
tag_is_before_till = t <= till_tag_time

in_range = (tag_is_later_since) && (tag_is_before_till)
!in_range
else
true
end

in_range = (tag_is_later_since) && (tag_is_before_till)
!in_range
}

self.create_log(pull_requests, till_tag_name, till_tag_time)
issues = Array.new(@issues)

issues.delete_if{ |issue|
if issue[:closed_at]
t = Time.parse(issue[:closed_at]).utc
tag_is_later_since = t > since_tag_time
tag_is_before_till = t <= till_tag_time

in_range = (tag_is_later_since) && (tag_is_before_till)
!in_range
else
true
end

}

self.create_log(pull_requests, issues, till_tag_name, till_tag_time)

end

def generate_log_before_tag(tag)
Expand All @@ -178,26 +185,61 @@ def generate_log_before_tag(tag)
pull_requests = Array.new(@pull_requests)

pull_requests.delete_if { |req|
t = Time.parse(req[:closed_at]).utc
t > tag_time
if req[:merged_at]
t = Time.parse(req[:merged_at]).utc
t > tag_time
else
true
end

}

self.create_log(pull_requests, tag_name, tag_time)
issues = Array.new(@issues)

issues.delete_if{ |issue|
if issue[:closed_at]
t = Time.parse(issue[:closed_at]).utc
t > tag_time
else
true
end
}

self.create_log(pull_requests, issues, tag_name, tag_time)

end

def create_log(pull_requests, tag_name, tag_time)
def create_log(pull_requests, issues, tag_name, tag_time)

# Generate tag name and link
trimmed_tag = tag_name.tr('v', '')
log = "## [#{trimmed_tag}] (https://github.com/#{@options[:user]}/#{@options[:project]}/tree/#{tag_name})\n"

#Generate date string:
time_string = tag_time.strftime @options[:format]
log += "#### #{time_string}\n"

pull_requests.each { |dict|
merge = "#{dict[:title]} [\\##{dict[:number]}](https://github.com/#{@options[:user]}/#{@options[:project]}/pull/#{dict[:number]})\n\n"
log += "- #{merge}"
}
if @options[:pulls]
# Generate pull requests:
if pull_requests
pull_requests.each { |dict|
merge = "#{dict[:title]} [\\##{dict[:number]}](https://github.com/#{@options[:user]}/#{@options[:project]}/pull/#{dict[:number]})\n\n"
log += "- #{merge}"
}
end
end

if @options[:issues]
# Generate issues:
if issues
issues.each { |dict|
merge = "*Fixed issue:* #{dict[:title]} [\\##{dict[:number]}](https://github.com/#{@options[:user]}/#{@options[:project]}/issues/#{dict[:number]})\n\n"
log += "- #{merge}"
}
end

end

log
end

Expand All @@ -217,10 +259,23 @@ def get_time_of_tag(prev_tag)
@tag_times_hash[prev_tag['name']] = Time.parse(time_string)
end

def get_all_issues
all_issues = []
@options[:labels].each { |label|
issues = @github.issues.list user: @options[:user], repo: @options[:project], state: 'closed', filter: 'all', labels: label
all_issues = all_issues.concat(issues.body)
}
if @options[:verbose]
puts "Receive all closed issues with labels #{@options[:labels]}: #{all_issues.count} issues"
end

all_issues

end

end

if __FILE__ == $0
changelog_generator = ChangelogGenerator.new
# changelog_generator.compund_changelog

changelog_generator.compund_changelog
end
13 changes: 11 additions & 2 deletions lib/github_changelog_generator/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class Parser
def self.parse_options
options = {:tag1 => nil, :tag2 => nil, :format => '%d/%m/%y', :output => 'CHANGELOG.md'}
options = {:tag1 => nil, :tag2 => nil, :format => '%d/%m/%y', :output => 'CHANGELOG.md', :labels => %w(bug enhancement), :pulls => true, :issues => true }

parser = OptionParser.new { |opts|
opts.banner = 'Usage: changelog_generator --user username --project project_name [options]'
Expand All @@ -23,7 +23,13 @@ def self.parse_options
opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
options[:verbose] = v
end
opts.on('-l', '--last-changes', 'Generate log between only last 2 tags') do |last|
opts.on('--[no-]issues', 'Include closed issues to changelog. Default is true') do |v|
options[:issues] = v
end
opts.on('--[no-]pull-requests', 'Include pull-requests to changelog. Default is true') do |v|
options[:pulls] = v
end
opts.on('-l', '--last-changes', 'Generate log between last 2 tags only') do |last|
options[:last] = last
end
opts.on('-f', '--date-format [FORMAT]', 'Date format. Default is %d/%m/%y') do |last|
Expand All @@ -32,6 +38,9 @@ def self.parse_options
opts.on('-o', '--output [NAME]', 'Output file. Default is CHANGELOG.md') do |last|
options[:output] = last
end
opts.on('--labels x,y,z', Array, 'List of labels. Issues with that labels will be included to changelog. Default is \'bug,enhancement\'') do |list|
options[:labels] = list
end
}

parser.parse!
Expand Down