Class: ChangelogGenerator
- Inherits:
-
Object
- Object
- ChangelogGenerator
- Defined in:
- lib/github_changelog_generator.rb
Instance Attribute Summary collapse
-
#all_tags ⇒ Object
Returns the value of attribute all_tags.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #compund_changelog ⇒ Object
- #create_log(pull_requests, tag_name, tag_time) ⇒ Object
- #exec_command(cmd) ⇒ Object
- #generate_log_between_tags(since_tag, till_tag) ⇒ Object
- #generate_log_for_all_tags ⇒ Object
- #get_all_closed_pull_requests ⇒ Object
- #get_all_merged_pull_requests ⇒ Object
- #get_all_tags ⇒ Object
- #get_time_of_tag(prev_tag) ⇒ Object
-
#initialize ⇒ ChangelogGenerator
constructor
A new instance of ChangelogGenerator.
- #is_megred(number) ⇒ Object
- #print_json(json) ⇒ Object
Constructor Details
#initialize ⇒ ChangelogGenerator
Returns a new instance of ChangelogGenerator.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/github_changelog_generator.rb', line 13 def initialize() @options = Parser. if @options[:token] @github = Github.new oauth_token: @options[:token] else @github = Github.new end @all_tags = self. @pull_requests = self.get_all_closed_pull_requests @tag_times_hash = {} end |
Instance Attribute Details
#all_tags ⇒ Object
Returns the value of attribute all_tags.
11 12 13 |
# File 'lib/github_changelog_generator.rb', line 11 def @all_tags end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/github_changelog_generator.rb', line 11 def @options end |
Instance Method Details
#compund_changelog ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/github_changelog_generator.rb', line 51 def compund_changelog if @options[:verbose] puts 'Generating changelog:' end log = "# Changelog\n\n" if @options[:last] log += self.(self.[0], self.[1]) elsif @options[:tag1] && @options[:tag2] tag1 = @options[:tag1] tag2 = @options[:tag2] = [] self..each { |x| .push(x['name'])} if .include?(tag1) if .include?(tag2) hash = Hash[.map.with_index.to_a] index1 = hash[tag1] index2 = hash[tag2] log += self.(self.[index1], self.[index2]) else puts "Can't find tag #{tag2} -> exit" exit end else puts "Can't find tag #{tag1} -> exit" exit end else log += self. end if @options[:verbose] puts log end output_filename = "#{@options[:project]}_changelog.md" File.open(output_filename, 'w') { |file| file.write(log) } puts "Done! Generated log placed in #{output_filename}" end |
#create_log(pull_requests, tag_name, tag_time) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/github_changelog_generator.rb', line 170 def create_log(pull_requests, tag_name, tag_time) 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 @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}" } log end |
#exec_command(cmd) ⇒ Object
31 32 33 34 |
# File 'lib/github_changelog_generator.rb', line 31 def exec_command(cmd) exec_cmd = "cd #{$project_path} && #{cmd}" %x[#{exec_cmd}] end |
#generate_log_between_tags(since_tag, till_tag) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/github_changelog_generator.rb', line 144 def (since_tag, till_tag) since_tag_time = self.get_time_of_tag(since_tag) till_tag_time = self.get_time_of_tag(till_tag) # if we mix up tags order - lits fix it! if since_tag_time > till_tag_time since_tag, till_tag = till_tag, since_tag since_tag_time, till_tag_time = till_tag_time, since_tag_time end till_tag_name = till_tag['name'] pull_requests = Array.new(@pull_requests) pull_requests.delete_if { |req| t = Time.parse(req[:closed_at]).utc true_classor_false_class = t > since_tag_time classor_false_class = t < till_tag_time in_range = (true_classor_false_class) && (classor_false_class) !in_range } self.create_log(pull_requests, till_tag_name, till_tag_time) end |
#generate_log_for_all_tags ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/github_changelog_generator.rb', line 97 def log = '' for index in 1 ... self..size log += self.(self.[index-1], self.[index]) end log end |
#get_all_closed_pull_requests ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/github_changelog_generator.rb', line 37 def get_all_closed_pull_requests issues = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed' json = issues.body if @options[:verbose] puts 'Receive all pull requests' end json end |
#get_all_merged_pull_requests ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/github_changelog_generator.rb', line 110 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 |
#get_all_tags ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/github_changelog_generator.rb', line 123 def url = "https://api.github.com/repos/#{@options[:user]}/#{@options[:project]}/tags" if @options[:verbose] puts "Receive tags for repo #{url}" end response = HTTParty.get(url, :headers => {'Authorization' => 'token 8587bb22f6bf125454768a4a19dbcc774ea68d48', 'User-Agent' => 'Changelog-Generator'}) json_parse = JSON.parse(response.body) if @options[:verbose] puts "Found #{json_parse.count} tags" end json_parse end |
#get_time_of_tag(prev_tag) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/github_changelog_generator.rb', line 185 def get_time_of_tag(prev_tag) if @tag_times_hash[prev_tag['name']] return @tag_times_hash[prev_tag['name']] end if @options[:verbose] puts "Get time for tag #{prev_tag['name']}" end 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.parse(time_string) @tag_times_hash[prev_tag['name']] = Time.parse(time_string) end |
#is_megred(number) ⇒ Object
106 107 108 |
# File 'lib/github_changelog_generator.rb', line 106 def is_megred(number) @github.pull_requests.merged? @options[:user], @options[:project], number end |
#print_json(json) ⇒ Object
27 28 29 |
# File 'lib/github_changelog_generator.rb', line 27 def print_json(json) puts JSON.pretty_generate(json) end |