Class: Changelog

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

Overview

A small wrapper class for more easily generating and manipulating Github/Git changelogs. Given two different git objects (sha, tag, whatever), it will find all PRs that made up that diff and store them as a list. Also allows for filtering by label, and the importance of that change (labels), based on how we classify the importance of PRs in the paritytech/polkadot project.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_repo, from, to, token: '', prefix: nil) ⇒ Changelog

github_repo: ‘paritytech/polkadot’ from: some git ref e.g., 7e30258, v1.2.3 to: some git ref e.g., 7e30258, v1.2.3

Optional named parameters: token: a Github personal access token prefix: whether or not to prefix PR numbers with their repo in the changelog



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/changelogerator.rb', line 78

def initialize(github_repo, from, to, token: '', prefix: nil)
  @repo = github_repo
  @gh = Octokit::Client.new(
    access_token: token
  )
  @repository = @gh.repository(@repo)
  @prefix = prefix
  ids = pr_ids_from_git_diff(from, to)
  @changes = prs_from_ids(ids)
  @changes.map do |c|
    compute_change_meta(c)
  end

  compute_global_meta
end

Instance Attribute Details

#changesObject

Returns the value of attribute changes.



13
14
15
# File 'lib/changelogerator.rb', line 13

def changes
  @changes
end

#labelObject (readonly)

Returns the value of attribute label.



14
15
16
# File 'lib/changelogerator.rb', line 14

def label
  @label
end

#metaObject

Returns the value of attribute meta.



13
14
15
# File 'lib/changelogerator.rb', line 13

def meta
  @meta
end

#repositoryObject

Returns the value of attribute repository.



13
14
15
# File 'lib/changelogerator.rb', line 13

def repository
  @repository
end

Class Method Details

.changes_files_in_paths?(change, paths) ⇒ Boolean

Return the list of all the files in the changeset that also in the given path

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/changelogerator.rb', line 48

def self.changes_files_in_paths?(change, paths)
  changed_files = GitDiffParser.parse(Octokit.get(change.diff_url)).files
  paths = [paths] unless paths.is_a? Array
  paths.each do |path|
    return true if changed_files.find { |l| l.match path }
  end
  nil
end

.changes_with_label(changes, label) ⇒ Object



16
17
18
19
20
# File 'lib/changelogerator.rb', line 16

def self.changes_with_label(changes, label)
  changes.select do |change|
    change[:labels].any? { |c| c[:name] == label } == true
  end
end

.get_label_code(name) ⇒ Object

Return the label code for a change if the label name matches the expected pattern. nil otherwise.



60
61
62
63
64
65
66
67
# File 'lib/changelogerator.rb', line 60

def self.get_label_code(name)
  m = match = name.match(/^([a-z])(\d+)-(.*)$/i)
  if m
    letter, number, text = match.captures
    return [letter, number, text]
  end
  nil
end

Instance Method Details

#add(change) ⇒ Object



94
95
96
97
98
99
# File 'lib/changelogerator.rb', line 94

def add(change)
  compute_change_meta(change)
  prettify_title(change)
  changes.prepend(change)
  @meta = compute_global_meta
end

#add_from_id(id) ⇒ Object

Add a pull request from id



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

def add_from_id(id)
  pull = @gh.pull_request(@repo, id)
  add pull
end

#compute_global_metaObject

Go through all changes and compute some aggregated values



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/changelogerator.rb', line 24

def compute_global_meta
  @meta = {}

  @changes.each do |change|
    # here we remove some of the fields to reduce (considerably) the size
    # of the json output
    change.head = nil
    change.base = nil
    change._links = nil

    change[:meta].each_key do |meta_key|
      current = change[:meta][meta_key]

      meta[meta_key] = {} unless meta[meta_key]
      meta[meta_key][:min] = current[:value] if !meta[meta_key][:min] || current[:value] < meta[meta_key][:min]
      meta[meta_key][:max] = current[:value] if !meta[meta_key][:max] || current[:value] > meta[meta_key][:max]
      meta[meta_key][:count] = 0 unless meta[meta_key][:count]
      meta[meta_key][:count] += 1
    end
  end
end

#to_json(*_args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/changelogerator.rb', line 107

def to_json(*_args)
  opts = {
    array_nl: "\n",
    object_nl: "\n",
    indent: '  ',
    space_before: ' ',
    space: ' '
  }
  obj = @changes

  commits = {
    meta: @meta,
    repository: @repository.to_h,
    changes: obj.map(&:to_h)
  }

  JSON.fast_generate(commits, opts)
end