Class: Codebot::Formatters::Push

Inherits:
Codebot::Formatter show all
Defined in:
lib/codebot/formatters/push.rb

Overview

This class formats push events.

Instance Attribute Summary

Attributes inherited from Codebot::Formatter

#payload

Instance Method Summary collapse

Methods inherited from Codebot::Formatter

#abbreviate, #action, #ary_to_sentence, #closed?, #extract, #format_branch, #format_dangerous, #format_event, #format_hash, #format_number, #format_repository, #format_url, #format_user, #gitlab_action, #gitlab_closed?, #gitlab_opened?, #gitlab_repository_url, #gitlab_url, #initialize, #opened?, #prettify, #repository_name, #repository_url, #sanitize, #sender_name, #shorten_url, #url

Constructor Details

This class inherits a constructor from Codebot::Formatter

Instance Method Details

#after_shaObject



157
158
159
# File 'lib/codebot/formatters/push.rb', line 157

def after_sha
  extract(:after).to_s
end

#after_sha_urlObject



161
162
163
# File 'lib/codebot/formatters/push.rb', line 161

def after_sha_url
  "#{repository_url}/commits/#{after_sha}"
end

#base_refObject



133
134
135
# File 'lib/codebot/formatters/push.rb', line 133

def base_ref
  extract(:base_ref)
end

#base_ref_nameObject



137
138
139
# File 'lib/codebot/formatters/push.rb', line 137

def base_ref_name
  base_ref.sub(%r{\Arefs/(heads|tags)/}, '')
end

#before_shaObject



149
150
151
# File 'lib/codebot/formatters/push.rb', line 149

def before_sha
  extract(:before).to_s
end

#before_sha_urlObject



153
154
155
# File 'lib/codebot/formatters/push.rb', line 153

def before_sha_url
  "#{repository_url}/commits/#{before_sha}"
end

#branch_urlObject



141
142
143
# File 'lib/codebot/formatters/push.rb', line 141

def branch_url
  "#{repository_url}/commits/#{branch_name}"
end

#commitsObject



114
115
116
# File 'lib/codebot/formatters/push.rb', line 114

def commits
  extract(:commits)
end

#compare_urlObject



145
146
147
# File 'lib/codebot/formatters/push.rb', line 145

def compare_url
  extract(:compare).to_s
end

#created?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/codebot/formatters/push.rb', line 98

def created?
  /\A0{40}\z/ =~ extract(:before)
end

#default_formatObject



79
80
81
# File 'lib/codebot/formatters/push.rb', line 79

def default_format
  '%<repository>s/%<branch>s %<hash>s %<author>s: %<title>s'
end

#deleted?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/codebot/formatters/push.rb', line 102

def deleted?
  /\A0{40}\z/ =~ extract(:after)
end

#distinct_commitsObject



165
166
167
168
169
# File 'lib/codebot/formatters/push.rb', line 165

def distinct_commits
  extract(:distinct_commits) || commits.select do |commit|
    commit['distinct'] && !commit['message'].to_s.strip.empty?
  end
end

#forced?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/codebot/formatters/push.rb', line 106

def forced?
  extract(:forced)
end

#formatArray<String>

Formats IRC messages for a push event.

Returns:

  • (Array<String>)

    the formatted messages



13
14
15
16
17
# File 'lib/codebot/formatters/push.rb', line 13

def format
  ["#{summary}: #{format_url url}"] + distinct_commits.map do |commit|
    format_commit_message(commit)
  end
end

#format_commit_message(commit) ⇒ Object

rubocop:disable Metrics/AbcSize



68
69
70
71
72
73
74
75
76
77
# File 'lib/codebot/formatters/push.rb', line 68

def format_commit_message(commit) # rubocop:disable Metrics/AbcSize
  author = commit['author']['name'] if commit['author'].is_a? Hash
  default_format % {
    repository: format_repository(repository_name),
    branch: format_branch(branch_name),
    hash: format_hash(commit['id']),
    author: format_user(author),
    title: prettify(full_commit_message(commit))
  }
end

#full_commit_message(commit) ⇒ Object



83
84
85
# File 'lib/codebot/formatters/push.rb', line 83

def full_commit_message(commit)
  (commit.is_a?(Hash) ? commit['message'] : nil).to_s
end

#pusher_nameObject



118
119
120
# File 'lib/codebot/formatters/push.rb', line 118

def pusher_name
  extract(:pusher, :name) || 'somebody'
end

#refObject



122
123
124
# File 'lib/codebot/formatters/push.rb', line 122

def ref
  extract(:ref).to_s
end

#ref_nameObject Also known as: tag_name, branch_name



126
127
128
# File 'lib/codebot/formatters/push.rb', line 126

def ref_name
  ref.sub(%r{\Arefs/(heads|tags)/}, '')
end

#summaryObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/LineLength



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
# File 'lib/codebot/formatters/push.rb', line 19

def summary # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/LineLength
  msg = "[#{format_repository repository_name}]"
  msg << " #{format_user(pusher_name)}"

  if created?
    if tag?
      msg << " tagged #{format_branch tag_name} at "
      msg << if base_ref
               format_branch(base_ref_name)
             else
               format_hash(after_sha)
             end
    else
      msg << " created #{format_branch branch_name}"
      msg << if base_ref
               " from #{format_branch base_ref_name}"
             else
               " at #{format_hash after_sha}"
             end

      len = distinct_commits.length
      msg << " (+#{format_number len, 'new commit', 'new commits'})"
    end
  elsif deleted?
    msg << " #{format_dangerous 'deleted'}"
    msg << " #{format_branch branch_name}"
    msg << " at #{format_hash before_sha}"
  elsif forced?
    msg << " #{format_dangerous 'force-pushed'}"
    msg << " #{format_branch branch_name}"
    msg << " from #{format_hash before_sha}"
    msg << " to #{format_hash after_sha}"
  elsif !commits.empty? && distinct_commits.empty?
    if base_ref
      msg << " merged #{format_branch base_ref_name}"
      msg << " into #{format_branch branch_name}"
    else
      msg << " fast-forwarded #{format_branch branch_name}"
      msg << " from #{format_hash before_sha}"
      msg << " to #{format_hash after_sha}"
    end
  else
    len = distinct_commits.length
    msg << " pushed #{format_number len, 'new commit', 'new commits'}"
    msg << " to #{format_branch branch_name}"
  end
  msg
end

#summary_urlObject

rubocop:disable Metrics/AbcSize



87
88
89
90
91
92
93
94
95
96
# File 'lib/codebot/formatters/push.rb', line 87

def summary_url # rubocop:disable Metrics/AbcSize
  if created?    then distinct_commits.empty? ? branch_url : compare_url
  elsif deleted? then before_sha_url
  elsif forced?  then branch_url
  elsif distinct_commits.length == 1
    distinct_commits.first['url'].to_s
  else
    compare_url
  end
end

#tag?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/codebot/formatters/push.rb', line 110

def tag?
  %r{\Arefs/tags/} =~ ref
end