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() = Parser. if [:token] @github = Github.new oauth_token: [:token] else @github = Github.new end = 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 end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/github_changelog_generator.rb', line 11 def 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 96 |
# File 'lib/github_changelog_generator.rb', line 51 def compund_changelog if [:verbose] puts 'Generating changelog:' end log = "# Changelog\n\n" if [:last] log += self.(self.[0], self.[1]) elsif [:tag1] && [:tag2] tag1 = [:tag1] tag2 = [: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 [:verbose] puts log end log += "\n\n*This file was generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*" 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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/github_changelog_generator.rb', line 171 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 [: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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/github_changelog_generator.rb', line 145 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
98 99 100 101 102 103 104 105 |
# File 'lib/github_changelog_generator.rb', line 98 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 [:user], [:project], :state => 'closed' json = issues.body if [:verbose] puts 'Receive all pull requests' end json end |
#get_all_merged_pull_requests ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/github_changelog_generator.rb', line 111 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 [:verbose] puts "##{req[:number]} #{merged ? 'merged' : 'not merged'}" end !merged } end |
#get_all_tags ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/github_changelog_generator.rb', line 124 def url = "https://api.github.com/repos/#{@options[:user]}/#{@options[:project]}/tags" if [: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 [:verbose] puts "Found #{json_parse.count} tags" end json_parse end |
#get_time_of_tag(prev_tag) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/github_changelog_generator.rb', line 186 def get_time_of_tag(prev_tag) if @tag_times_hash[prev_tag['name']] return @tag_times_hash[prev_tag['name']] end if [:verbose] puts "Get time for tag #{prev_tag['name']}" end github_git_data_commits_get = @github.git_data.commits.get [:user], [: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
107 108 109 |
# File 'lib/github_changelog_generator.rb', line 107 def is_megred(number) @github.pull_requests.merged? [:user], [: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 |