Class: ChangelogGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/github_changelog_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChangelogGenerator

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.parse_options
  if @options[:token]
    @github = Github.new oauth_token: @options[:token]
  else
    @github = Github.new
  end
  @all_tags = self.get_all_tags
  @pull_requests = self.get_all_closed_pull_requests

  @tag_times_hash = {}
end

Instance Attribute Details

#all_tagsObject

Returns the value of attribute all_tags.



11
12
13
# File 'lib/github_changelog_generator.rb', line 11

def all_tags
  @all_tags
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/github_changelog_generator.rb', line 11

def options
  @options
end

Instance Method Details

#compund_changelogObject



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 @options[:verbose]
    puts 'Generating changelog:'
  end

  log = "# Changelog\n\n"

  if @options[:last]
    log += self.generate_log_between_tags(self.all_tags[0], self.all_tags[1])
  elsif @options[:tag1] && @options[:tag2]

    tag1 = @options[:tag1]
    tag2 = @options[:tag2]
    tags_strings = []
    self.all_tags.each { |x| tags_strings.push(x['name'])}

    if tags_strings.include?(tag1)
      if tags_strings.include?(tag2)
        hash = Hash[tags_strings.map.with_index.to_a]
        index1 = hash[tag1]
        index2 = hash[tag2]
        log += self.generate_log_between_tags(self.all_tags[index1], self.all_tags[index2])
      else
        puts "Can't find tag #{tag2} -> exit"
        exit
      end
    else
      puts "Can't find tag #{tag1} -> exit"
      exit
    end
  else
    log += self.generate_log_for_all_tags
  end


  if @options[: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 @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



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 generate_log_between_tags(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_tagsObject



98
99
100
101
102
103
104
105
# File 'lib/github_changelog_generator.rb', line 98

def generate_log_for_all_tags
  log = ''
  for index in 1 ... self.all_tags.size
    log += self.generate_log_between_tags(self.all_tags[index-1], self.all_tags[index])
  end

  log
end

#get_all_closed_pull_requestsObject



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_requestsObject



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 @options[:verbose]
      puts "##{req[:number]} #{merged ? 'merged' : 'not merged'}"
    end
    !merged
  }
end

#get_all_tagsObject



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 get_all_tags

  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



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 @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



107
108
109
# File 'lib/github_changelog_generator.rb', line 107

def is_megred(number)
  @github.pull_requests.merged? @options[:user], @options[:project], number
end


27
28
29
# File 'lib/github_changelog_generator.rb', line 27

def print_json(json)
  puts JSON.pretty_generate(json)
end