Class: SemVerComponents::Outputs::SemanticReleaseGenerateNotes

Inherits:
SemVerComponents::Output show all
Defined in:
lib/sem_ver_components/outputs/semantic_release_generate_notes.rb

Instance Method Summary collapse

Methods inherited from SemVerComponents::Output

#initialize

Constructor Details

This class inherits a constructor from SemVerComponents::Output

Instance Method Details

#process(commits_info) ⇒ Object

Process commits info

Parameters
  • commits_info (Array< Hash<Symbol, Object> >): List of commits info:

    • components_bump_levels (Hash<String or nil, Integer>): Set of bump levels (0: patch, 1: minor, 2: major) per component name (nil for global)

    • commit (Git::Object::Commit): Corresponding git commit



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
91
92
93
94
95
96
97
98
# File 'lib/sem_ver_components/outputs/semantic_release_generate_notes.rb', line 15

def process(commits_info)
  # Compute new version
  new_version =
    if @local_git.git_from.nil?
      '0.0.1'
    elsif @local_git.git_from =~ /^v(\d+)\.(\d+)\.(\d+)$/
      major = Integer($1)
      minor = Integer($2)
      patch = Integer($3)
      bump_level = commits_info.map { |commit_info| commit_info[:components_bump_levels].values }.flatten(1).max
      case bump_level
      when 0
        patch += 1
      when 1
        minor += 1
        patch = 0
      when 2
        major += 1
        minor = 0
        patch = 0
      else
        raise "Invalid bump level: #{bump_level}"
      end
      "#{major}.#{minor}.#{patch}"
    else
      raise "Can't generate release notes from a git ref that is not a semantic release (#{@local_git.git_from})"
    end
  git_url = @local_git.git.remote('origin').url
  git_url = git_url[0..-5] if git_url.end_with?('.git')
  # Group commits per bump level, per component
  # Hash< String or nil, Hash< Integer, Array<Git::Object::Commit> >
  commits_per_component = {}
  # Also reference commits to be ignored: commits that are part of a merge commit
  merged_commits = []
  commits_info.each do |commit_info|
    git_commit = commit_info[:commit]
    commit_info[:components_bump_levels].each do |component, bump_level|
      commits_per_component[component] = {} unless commits_per_component.key?(component)
      commits_per_component[component][bump_level] = [] unless commits_per_component[component].key?(bump_level)
      commits_per_component[component][bump_level] << git_commit
    end
    # In the case of a merge commit, reference all commits that are part of this merge commit, directly from the graph
    merged_commits.concat(@local_git.git_log.between(@local_git.git.merge_base(*git_commit.parents.map(&:sha)).first.sha, git_commit.sha)[1..-1].map(&:sha)) if git_commit.parents.size > 1
  end
  puts "# [v#{new_version}](#{git_url}/compare/#{@local_git.git_from}...v#{new_version}) (#{Time.now.utc.strftime('%F %T')})"
  puts
  commits_per_component.sort_by { |component, _component_info| component || '' }.each do |(component, component_info)|
    puts "## #{component.nil? ? 'Global changes' : "Changes for #{component}"}\n" if commits_per_component.size > 1 || !component.nil?
    component_info.each do |bump_level, commits|
      puts "### #{
        case bump_level
        when 0
          'Patches'
        when 1
          'Features'
        when 2
          'Breaking changes'
        else
          raise "Invalid bump level: #{bump_level}"
        end
      }"
      puts
      # Gather an ordered set of commit lines (with the corresponding commit sha) in order to not duplicate the info when there are merge commits
      # Hash< String, String >
      commit_lines = {}
      commits.each do |commit|
        # Don't put merged commits as we consider the changelog should contain the merge commit comment.
        next if merged_commits.include?(commit.sha)
        message_lines = commit.message.split("\n")
        commit_line = message_lines.first
        if commit_line =~ /^Merge pull request .+$/
          # Consider the next line as commit line
          next_line = message_lines[1..-1].join("\n").strip.split("\n").first
          commit_line = next_line unless next_line.nil?
        end
        commit_lines[commit_line] = commit.sha
      end
      commit_lines.each do |commit_line, commit_sha|
        puts "* [#{commit_line}](#{git_url}/commit/#{commit_sha})"
      end
      puts
    end
  end
end