Class: ChangesSince::ChangelogPrinter

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

Constant Summary collapse

TAGS =
{
  :public   => 'Public',
  :bug      => 'Bugs',
  :internal => 'Internal'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commits, teams, options, repo) ⇒ ChangelogPrinter

Returns a new instance of ChangelogPrinter.



10
11
12
13
14
15
# File 'lib/changes_since/changelog_printer.rb', line 10

def initialize(commits, teams, options, repo)
  @commits = commits
  @teams   = teams
  @options = options
  @repo    = repo
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/changes_since/changelog_printer.rb', line 2

def options
  @options
end

#repoObject (readonly)

Returns the value of attribute repo.



2
3
4
# File 'lib/changes_since/changelog_printer.rb', line 2

def repo
  @repo
end

#teamsObject (readonly)

Returns the value of attribute teams.



2
3
4
# File 'lib/changes_since/changelog_printer.rb', line 2

def teams
  @teams
end

Instance Method Details

#print!Object



17
18
19
20
21
22
23
24
# File 'lib/changes_since/changelog_printer.rb', line 17

def print!
  if teams
    print_team_commits!
  else
    print_commits!(@commits)
  end
  return
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/changes_since/changelog_printer.rb', line 49

def print_commits!(output_commits)
  output_commits.sort! { |a, b| a.author.name <=> b.author.name }

  if options[:tags]
    TAGS.each do |type, title|
      tagged_commits = output_commits.select { |commit| commit.message.include?("##{type}") }
      next if tagged_commits.empty?

      puts "\n#{title}:\n\n"
      tagged_commits.each { |commit| print_message(commit, type) }
      output_commits -= tagged_commits
    end
    return if output_commits.empty?
    puts "\nUnclassified:\n\n"
  end

  output_commits.each { |commit| print_message(commit, nil) }
end


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/changes_since/changelog_printer.rb', line 68

def print_message(commit, type)
  message_lines = commit.message.split("\n\n")
  if message_lines.first =~ /Merge pull request/
    title = message_lines.last
    pr    = message_lines.first.split(" from ").first.split("#").last
  else
    title = message_lines.first
  end
  title.gsub!("##{type}", "") if type
  branch_author = commit.author.name
  sha = commit.sha[0..9] if options[:sha]
  if options[:markdown]
    text = "|#{title}|"
    text << "#{branch_author}|"
    text << "[##{pr}|#{@repo}/pull/#{pr}]|" if @repo
    text << "[#{sha}|#{@repo}/commit/#{sha}]|" if sha
  else
    text = "* #{title} (#{branch_author}) #{options[:sha] ? commit.sha[0..9] : ''}"
  end
  puts text
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/changes_since/changelog_printer.rb', line 26

def print_team_commits!
  teams.each do |team, members|
    author_re = /#{members.join("|")}/i
    team_commits = @commits.select do |commit|
      [commit.author.name, commit.author.email].any? do |str|
        str =~ author_re
      end
    end
    next if team_commits.empty?
    @commits -= team_commits
    if options[:markdown]
      puts "||*#{team}*||Author||PR||#{"Commit" if options[:sha]}"
    else
      puts "\n*#{team}*\n"
    end
    print_commits!(team_commits)
  end

  return if @commits.empty?
  puts "\n*Other*\n\n"
  @commits.each { |commit| print_message(commit, nil) }
end