Class: Release::Notes::Commits

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/release/notes/commits.rb

Constant Summary collapse

REGEX_DELIMETER =
/(?=-)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, value:, writer:, tagger:) ⇒ Commits

Release::Notes::Commits initializer method

Parameters:

  • title (String)
    • a label (Implemented enhancements.)
  • value (String)
    • all commits and commit subjects (sha - message\n)
  • writer (Release::Notes::Writer)
    • Writer obeject containing the message header (v2.0.0)


19
20
21
22
23
24
25
# File 'lib/release/notes/commits.rb', line 19

def initialize(title:, value:, writer:, tagger:)
  @title = title
  @value = value
  @writer = writer

  @tagger = tagger
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/release/notes/commits.rb', line 9

def title
  @title
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/release/notes/commits.rb', line 9

def value
  @value
end

Instance Method Details

#choose_messages_by_hashArray (private)

Determine what messages will be added to final output

Returns:

  • (Array)

    non-duplicated log messages



50
51
52
53
54
# File 'lib/release/notes/commits.rb', line 50

def choose_messages_by_hash
  split_commits.tap do |obj|
    remove_duplicate_hashes(obj)
  end
end

#create_commits_hash(arr, hsh) ⇒ String (private)

Create the commits hash

Parameters:

  • arr (Array)
    • Array where [0] is a commit hash and [1] is the first formatted commit message for...
  • hsh (Hash)
    • Hash of formatted commit messages to be injected per sha

Returns:

  • (String)

    Commit message



64
65
66
# File 'lib/release/notes/commits.rb', line 64

def create_commits_hash(arr, hsh)
  hsh[arr[0].strip] = arr[1..-1].join
end

#duplicate_commit?(key) ⇒ Boolean (private)

Determines whether key has already been added to the release_notes

Parameters:

  • key (String)
    • commit sha

Returns:

  • (Boolean)

    Is this a duplicate commit message?



75
76
77
# File 'lib/release/notes/commits.rb', line 75

def duplicate_commit?(key)
  config_single_label && @tagger._hashes.include?(key)
end

#join_messages(arr) ⇒ String (private)

Transforms an array of messages into a single

Parameters:

  • arr (Array)
    • array formatted log messages

Returns:

  • (String)

    formatted log messages



86
87
88
# File 'lib/release/notes/commits.rb', line 86

def join_messages(arr)
  "#{arr.join(NEWLINE)}#{NEWLINE}"
end

#output_unique_messages(messages) ⇒ String (private)

Log messages without duplicates

Parameters:

  • messages (Array)
    • Array of git commit subjects

Returns:

  • (String)
    • formatted messages


97
98
99
# File 'lib/release/notes/commits.rb', line 97

def output_unique_messages(messages)
  writer_digest_title(title: title, log_message: join_messages(messages))
end

#performFile

Perform method

Returns:

  • (File)

    File with array of git sha's



32
33
34
35
36
37
38
39
40
41
# File 'lib/release/notes/commits.rb', line 32

def perform
  choose_messages_by_hash.tap do |obj|
    next if obj.empty?

    @tagger._hashes += obj.keys
    output_unique_messages(obj.values)
  end

  self
end

#remove_duplicate_hashes(obj) ⇒ Array (private)

Remove duplicate log messages

Parameters:

  • obj (Hash)
    • Commit sha's and messages

Returns:

  • (Array)

    Array of unique git sha's



108
109
110
# File 'lib/release/notes/commits.rb', line 108

def remove_duplicate_hashes(obj)
  obj.keys.each { |key| obj.delete(key) if duplicate_commit?(key) }
end

#split_commitsHash (private)

Split commits by sha

Returns:

  • (Hash)

    Commit and message



117
118
119
120
121
122
123
# File 'lib/release/notes/commits.rb', line 117

def split_commits
  split_values do |arr, obj|
    arr.split(REGEX_DELIMETER).tap do |split_arr|
      create_commits_hash(split_arr, obj) # "- Add our own release notes"
    end
  end
end

#split_values(&_block) ⇒ Hash (private)

Split messages by sha

NEWLINE = "\n"

Parameters:

  • &_block (Proc)
    • optional block

Returns:

  • (Hash)

    commits and commit sha's



134
135
136
137
138
# File 'lib/release/notes/commits.rb', line 134

def split_values(&_block)
  value.split(NEWLINE).each_with_object({}) do |arr, obj|
    yield arr, obj
  end
end