Class: Changelog
- Inherits:
-
Object
- Object
- Changelog
- 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
-
#changes ⇒ Object
Returns the value of attribute changes.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#repository ⇒ Object
Returns the value of attribute repository.
Class Method Summary collapse
-
.changes_files_in_paths?(change, paths) ⇒ Boolean
Return the list of all the files in the changeset that also in the given path.
- .changes_with_label(changes, label) ⇒ Object
-
.get_label_code(name) ⇒ Object
Return the label code for a change if the label name matches the expected pattern.
Instance Method Summary collapse
- #add(change) ⇒ Object
-
#add_from_id(id) ⇒ Object
Add a pull request from id.
-
#compute_global_meta ⇒ Object
Go through all changes and compute some aggregated values.
-
#initialize(github_repo, from, to, token: '', prefix: nil) ⇒ Changelog
constructor
github_repo: ‘paritytech/polkadot’ from: some git ref e.g., 7e30258, v1.2.3 to: some git ref e.g., 7e30258, v1.2.3.
- #to_json(*_args) ⇒ Object
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| (c) end end |
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
13 14 15 |
# File 'lib/changelogerator.rb', line 13 def changes @changes end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
14 15 16 |
# File 'lib/changelogerator.rb', line 14 def label @label end |
#meta ⇒ Object
Returns the value of attribute meta.
13 14 15 |
# File 'lib/changelogerator.rb', line 13 def end |
#repository ⇒ Object
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
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) (change) prettify_title(change) changes.prepend(change) = 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_meta ⇒ Object
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 = {} @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 || current = change[:meta][] [] = {} unless [] [][:min] = current[:value] if ![][:min] || current[:value] < [][:min] [][:max] = current[:value] if ![][:max] || current[:value] > [][:max] [][:count] = 0 unless [][:count] [][: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: , repository: @repository.to_h, changes: obj.map(&:to_h) } JSON.fast_generate(commits, opts) end |