Class: ChangelogGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ChangelogGenerator

Returns a new instance of ChangelogGenerator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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
  @issues = self.get_all_issues

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

#github ⇒ Object

Returns the value of attribute github.



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

def github
  @github
end

#options ⇒ Object

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_changelog ⇒ Object



49
50
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
# File 'lib/github_changelog_generator.rb', line 49

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

  log += "\n\n\\* *This changelog was generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*"

  output_filename = "#{@options[:output]}"
  File.open(output_filename, 'w') { |file| file.write(log) }

  puts "Done! Generated log placed in #{output_filename}"

end

#create_log(pull_requests, issues, tag_name, tag_time) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/github_changelog_generator.rb', line 212

def create_log(pull_requests, issues, tag_name, tag_time)

  # Generate tag name and link
  trimmed_tag = tag_name.tr('v', '')
  log = "## [#{trimmed_tag}] (https://github.com/#{@options[:user]}/#{@options[:project]}/tree/#{tag_name})\n"

  #Generate date string:
  time_string = tag_time.strftime @options[:format]
  log += "#### #{time_string}\n"

  if @options[:pulls]
    # Generate pull requests:
    if pull_requests
      pull_requests.each { |dict|
        merge = "#{@options[:merge_prefix]}#{dict[:title]} [\\##{dict[:number]}](#{dict.html_url})\n\n"
        log += "- #{merge}"
      }
    end
  end

  if @options[:issues]
    # Generate issues:
    if issues
      issues.sort! { |x, y|
        if x.labels.any? && y.labels.any?
          x.labels[0].name <=> y.labels[0].name
        else
          if x.labels.any?
            1
          else
            if y.labels.any?
              -1
            else
              0
            end
          end
        end
      }.reverse!
    end
    issues.each { |dict|
      is_bug = false
      is_enhancement = false
      dict.labels.each { |label|
        if label.name == 'bug'
          is_bug = true
        end
        if label.name == 'enhancement'
          is_enhancement = true
        end
      }

      intro = 'Closed issue'
      if is_bug
        intro = 'Fixed bug'
      end

      if is_enhancement
        intro = 'Implemented enhancement'
      end

      merge = "*#{intro}:* #{dict[:title]} [\\##{dict[:number]}](#{dict.html_url})\n\n"
      log += "- #{merge}"
    }
  end
  log
end

#exec_command(cmd) ⇒ Object



32
33
34
35
# File 'lib/github_changelog_generator.rb', line 32

def exec_command(cmd)
  exec_cmd = "cd #{$project_path} && #{cmd}"
  %x[#{exec_cmd}]
end

#generate_log_before_tag(tag) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/github_changelog_generator.rb', line 181

def generate_log_before_tag(tag)
  tag_time = self.get_time_of_tag(tag)
  tag_name = tag['name']

  pull_requests = Array.new(@pull_requests)

  pull_requests.delete_if { |req|
    if req[:merged_at]
      t = Time.parse(req[:merged_at]).utc
      t > tag_time
    else
      true
    end

  }

  issues = Array.new(@issues)

  issues.delete_if { |issue|
    if issue[:closed_at]
      t = Time.parse(issue[:closed_at]).utc
      t > tag_time
    else
      true
    end
  }

  self.create_log(pull_requests, issues, tag_name, tag_time)

end

#generate_log_between_tags(since_tag, till_tag) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
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
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/github_changelog_generator.rb', line 133

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|
    if req[:merged_at]
      t = Time.parse(req[:merged_at]).utc
      tag_is_later_since = t > since_tag_time
      tag_is_before_till = t <= till_tag_time

      in_range = (tag_is_later_since) && (tag_is_before_till)
      !in_range
    else
      true
    end

  }

  issues = Array.new(@issues)

  issues.delete_if { |issue|
    if issue[:closed_at]
      t = Time.parse(issue[:closed_at]).utc
      tag_is_later_since = t > since_tag_time
      tag_is_before_till = t <= till_tag_time

      in_range = (tag_is_later_since) && (tag_is_before_till)
      !in_range
    else
      true
    end

  }

  self.create_log(pull_requests, issues, till_tag_name, till_tag_time)

end

#generate_log_for_all_tags ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/github_changelog_generator.rb', line 92

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 += self.generate_log_before_tag(self.all_tags.last)

  log
end

#get_all_closed_pull_requests ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/github_changelog_generator.rb', line 38

def get_all_closed_pull_requests
  request = @github.pull_requests.list @options[:user], @options[:project], :state => 'closed'
  pull_requests = request.body

  if @options[:verbose]
    puts 'Receive all pull requests'
  end

  pull_requests
end

#get_all_issues ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/github_changelog_generator.rb', line 295

def get_all_issues
  all_issues = []

  issues_req = @github.issues.list user: @options[:user], repo: @options[:project], state: 'closed', filter: 'all', labels: nil

  filtered_issues = issues_req.body.select { |issues|
    #compare is there any labels from @options[:labels] array
    (issues.labels.map { |issue| issue.name } & @options[:labels]).any?
  }

  if @options[:add_issues_wo_labels]
    issues_wo_labels = issues_req.body.select {
        |issues| !issues.labels.map { |issue| issue.name }.any?
    }
    filtered_issues.concat(issues_wo_labels)
  end

# remove pull request from issues:
  filtered_issues.select! { |x|
    x.pull_request == nil
  }

  if @options[:verbose]
    puts "Receive all closed issues with labels #{@options[:labels]}: #{all_issues.count} issues"
  end

  filtered_issues

end

#get_all_tags ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/github_changelog_generator.rb', line 107

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

  if @options[:token]
    response = HTTParty.get(url,
                            :headers => {'Authorization' => "token #{@options[:token]}",
                                         'User-Agent' => 'Changelog-Generator'})
  else
    response = HTTParty.get(url,
                            :headers => {'User-Agent' => 'Changelog-Generator'})
  end

  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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/github_changelog_generator.rb', line 279

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



103
104
105
# File 'lib/github_changelog_generator.rb', line 103

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


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

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