github-changelog-generator/log_generator.rb

128 lines
3.0 KiB
Ruby
Raw Normal View History

2014-11-04 16:42:13 +00:00
require_relative 'constants'
require 'github_api'
require 'json'
2014-11-05 13:22:31 +00:00
require 'httparty'
2014-11-04 16:42:13 +00:00
2014-11-04 16:27:33 +00:00
class LogGenerator
2014-11-05 11:27:28 +00:00
attr_accessor :options
def initialize(options = {})
@options = options
2014-11-05 11:50:31 +00:00
if $oauth_token
@github = Github.new oauth_token: $oauth_token
else
@github = Github.new
end
2014-11-05 11:27:28 +00:00
end
2014-11-04 16:42:13 +00:00
def print_json(json)
puts JSON.pretty_generate(json)
end
def exec_command(cmd)
exec_cmd = "cd #{$project_path} && #{cmd}"
%x[#{exec_cmd}]
end
2014-11-05 11:27:28 +00:00
def find_prev_tag_date
2014-11-04 16:42:13 +00:00
value1 = exec_command "git log --tags --simplify-by-decoration --pretty=\"format:%ci %d\" | grep tag"
unless value1
puts 'not found this tag'
exit
end
scan_results = value1.scan(/.*tag.*/)
prev_tag = scan_results[1]
unless scan_results.any?
puts 'Not found any versions -> exit'
exit
end
2014-11-05 11:27:28 +00:00
if @options[:verbose]
puts "Prev tag is #{prev_tag}"
end
2014-11-04 16:42:13 +00:00
time = Time.parse(prev_tag)
end
2014-11-05 11:27:28 +00:00
def get_all_closed_pull_requests
2014-11-04 16:42:13 +00:00
2014-11-05 11:50:31 +00:00
issues = @github.pull_requests.list $github_user, $github_repo_name, :state => 'closed'
2014-11-04 16:42:13 +00:00
json = issues.body
2014-11-05 11:55:02 +00:00
if @options[:verbose]
puts 'All pull requests:'
json.each { |dict|
p "##{dict[:number]} - #{dict[:title]} (#{dict[:closed_at]})"
}
end
2014-11-04 16:42:13 +00:00
json
end
def compund_changelog(tag_time, pull_requests)
2014-11-05 13:22:31 +00:00
if @options[:verbose]
puts 'Generating changelog:'
end
2014-11-04 16:42:13 +00:00
log = ''
last_tag = exec_command('git describe --abbrev=0 --tags').strip
log += "## [#{last_tag}] (https://github.com/#{$github_user}/#{$github_repo_name}/tree/#{last_tag})\n"
time_string = tag_time.strftime "%Y/%m/%d"
log += "#### #{time_string}\n"
pull_requests.each { |dict|
merge = "#{dict[:title]} ([\\##{dict[:number]}](https://github.com/#{$github_user}/#{$github_repo_name}/pull/#{dict[:number]}))\n"
log += "- #{merge}"
}
puts log
File.open('output.txt', 'w') { |file| file.write(log) }
end
2014-11-05 11:50:31 +00:00
def is_megred(number)
@github.pull_requests.merged? $github_user, $github_repo_name, number
end
def get_all_merged_pull_requests
json = self.get_all_closed_pull_requests
2014-11-05 11:55:02 +00:00
puts 'Check if the requests is merged... (it can take a while)'
2014-11-05 11:50:31 +00:00
json.delete_if { |req|
merged = self.is_megred(req[:number])
if @options[:verbose]
2014-11-05 11:55:02 +00:00
puts "##{req[:number]} #{merged ? 'merged' : 'not merged'}"
2014-11-05 11:50:31 +00:00
end
!merged
}
end
2014-11-05 13:22:31 +00:00
def get_all_tags
url = "https://api.github.com/repos/skywinder/ActionSheetPicker-3.0/tags"
so_url = 'https://api.stackexchange.com/2.2/questions?site=stackoverflow'
response = HTTParty.get(url,
:headers => { "Authorization" => "token 8587bb22f6bf125454768a4a19dbcc774ea68d48",
"User-Agent" => "APPLICATION_NAME"})
json_parse = JSON.parse(response.body)
json_parse.each { |obj| p obj['name'] }
end
2014-11-05 11:50:31 +00:00
end
if __FILE__ == $0
log_generator = LogGenerator.new({:verbose => true})
2014-11-05 13:22:31 +00:00
tags = log_generator.get_all_tags
p tags
2014-11-05 11:50:31 +00:00
2014-11-04 16:27:33 +00:00
end