add support oprions to put username project name. token. date format

This commit is contained in:
Petr Korolev 2014-11-06 14:52:47 +02:00
parent 723d09df10
commit 178b846e17
2 changed files with 38 additions and 17 deletions

View File

@ -1,6 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require_relative 'constants'
require_relative 'parser' require_relative 'parser'
require 'github_api' require 'github_api'
require 'json' require 'json'
@ -13,8 +12,8 @@ class ChangelogGenerator
def initialize() def initialize()
@options = Parser.new.options @options = Parser.new.options
if $oauth_token if @options[:token]
@github = Github.new oauth_token: $oauth_token @github = Github.new oauth_token: @options[:token]
else else
@github = Github.new @github = Github.new
end end
@ -37,7 +36,7 @@ class ChangelogGenerator
def get_all_closed_pull_requests def get_all_closed_pull_requests
issues = @github.pull_requests.list $github_user, $github_repo_name, :state => 'closed' issues = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
json = issues.body json = issues.body
if @options[:verbose] if @options[:verbose]
@ -53,7 +52,7 @@ class ChangelogGenerator
puts 'Generating changelog:' puts 'Generating changelog:'
end end
log = "# Changelog\n\n----\n\n" log = "# Changelog\n\n"
if @options[:last] if @options[:last]
log += self.generate_log_between_tags(self.all_tags[0], self.all_tags[1]) log += self.generate_log_between_tags(self.all_tags[0], self.all_tags[1])
@ -87,7 +86,7 @@ class ChangelogGenerator
puts log puts log
end end
output_filename = "#{$github_repo_name}_changelog.md" output_filename = "#{@options[:project]}_changelog.md"
File.open(output_filename, 'w') { |file| file.write(log) } File.open(output_filename, 'w') { |file| file.write(log) }
puts "Done! Generated log placed in #{output_filename}" puts "Done! Generated log placed in #{output_filename}"
@ -104,7 +103,7 @@ class ChangelogGenerator
end end
def is_megred(number) def is_megred(number)
@github.pull_requests.merged? $github_user, $github_repo_name, number @github.pull_requests.merged? @options[:user], @options[:project], number
end end
def get_all_merged_pull_requests def get_all_merged_pull_requests
@ -123,15 +122,21 @@ class ChangelogGenerator
def get_all_tags def get_all_tags
if @options[:verbose] if @options[:verbose]
puts "Receive tags for repo #{$github_repo_name}" puts "Receive tags for repo #{@options[:project]}"
end end
url = "https://api.github.com/repos/#{$github_user}/#{$github_repo_name}/tags" url = "https://api.github.com/repos/#{@options[:user]}/#{@options[:project]}/tags"
response = HTTParty.get(url, response = HTTParty.get(url,
:headers => {'Authorization' => 'token 8587bb22f6bf125454768a4a19dbcc774ea68d48', :headers => {'Authorization' => 'token 8587bb22f6bf125454768a4a19dbcc774ea68d48',
'User-Agent' => 'Changelog-Generator'}) 'User-Agent' => 'Changelog-Generator'})
json_parse = JSON.parse(response.body) json_parse = JSON.parse(response.body)
if @options[:verbose]
puts "Found #{json_parse.count} tags"
end
json_parse
end end
def generate_log_between_tags(since_tag, till_tag) def generate_log_between_tags(since_tag, till_tag)
@ -162,13 +167,14 @@ class ChangelogGenerator
def create_log(pull_requests, tag_name, tag_time) def create_log(pull_requests, tag_name, tag_time)
log = "## [#{tag_name}] (https://github.com/#{$github_user}/#{$github_repo_name}/tree/#{tag_name})\n" trimmed_tag = tag_name.tr('v', '')
log = "## [#{trimmed_tag}] (https://github.com/#{@options[:user]}/#{@options[:project]}/tree/#{tag_name})\n"
time_string = tag_time.strftime '%d/%m/%y' time_string = tag_time.strftime @options[:format]
log += "#### #{time_string}\n" log += "#### #{time_string}\n"
pull_requests.each { |dict| pull_requests.each { |dict|
merge = "#{dict[:title]} ([\\##{dict[:number]}](https://github.com/#{$github_user}/#{$github_repo_name}/pull/#{dict[:number]}))\n\n" merge = "#{dict[:title]} [\\##{dict[:number]}](https://github.com/#{@options[:user]}/#{@options[:project]}/pull/#{dict[:number]})\n\n"
log += "- #{merge}" log += "- #{merge}"
} }
log log
@ -184,7 +190,7 @@ class ChangelogGenerator
puts "Get time for tag #{prev_tag['name']}" puts "Get time for tag #{prev_tag['name']}"
end end
github_git_data_commits_get = @github.git_data.commits.get $github_user, $github_repo_name, prev_tag['commit']['sha'] github_git_data_commits_get = @github.git_data.commits.get @options[:user], @options[:project], prev_tag['commit']['sha']
time_string = github_git_data_commits_get['committer']['date'] time_string = github_git_data_commits_get['committer']['date']
Time.parse(time_string) Time.parse(time_string)
@tag_times_hash[prev_tag['name']] = Time.parse(time_string) @tag_times_hash[prev_tag['name']] = Time.parse(time_string)

View File

@ -5,23 +5,33 @@ class Parser
attr_reader :options attr_reader :options
def initialize def initialize
@options = {:tag1 => nil, :tag2 => nil} @options = {:tag1 => nil, :tag2 => nil, :format => '%d/%m/%y'}
parser = OptionParser.new { |opts| parser = OptionParser.new { |opts|
opts.banner = 'Usage: changelog_generator.rb [tag1 tag2] [options]' opts.banner = 'Usage: changelog_generator.rb [tag1 tag2] [-u user_name -p project_name] [options]'
opts.on('-h', '--help', 'Displays Help') do opts.on('-h', '--help', 'Displays Help') do
puts opts puts opts
exit exit
end end
opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
@options[:verbose] = v @options[:verbose] = v
end end
opts.on('-l', '--last-changes', 'generate log between last 2 tags') do |last| opts.on('-l', '--last-changes', 'generate log between last 2 tags') do |last|
@options[:last] = last @options[:last] = last
end end
opts.on('-u', '--user [USER]', 'your username on GitHub') do |last|
@options[:user] = last
end
opts.on('-p', '--project [PROJECT]', 'name of project on GitHub') do |last|
@options[:project] = last
end
opts.on('-t', '--token [TOKEN]', 'your OAuth token GitHub') do |last|
@options[:token] = last
end
opts.on('-f', '--date-format [FORMAT]', 'date format. default is %d/%m/%y') do |last|
@options[:format] = last
end
} }
parser.parse! parser.parse!
@ -32,6 +42,11 @@ class Parser
exit exit
end end
if !@options[:user] || !@options[:project]
puts parser.banner
exit
end
if ARGV[1] if ARGV[1]
@options[:tag1] = ARGV[0] @options[:tag1] = ARGV[0]
@options[:tag2] = ARGV[1] @options[:tag2] = ARGV[1]