Class: Codebot::Formatters::Gitlab::PushHook

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

Overview

This class formats Push events from Gitlab

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, #sanitize, #sender_name, #shorten_url, #url

Constructor Details

This class inherits a constructor from Codebot::Formatter

Instance Method Details

#after_commitObject



12
13
14
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 12

def after_commit
  extract(:after)
end

#before_commitObject



8
9
10
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 8

def before_commit
  extract(:before)
end

#branchObject



49
50
51
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 49

def branch
  extract(:ref).split('/')[2..-1].join('/')
end

#commit_author(commit) ⇒ Object



33
34
35
36
37
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 33

def commit_author(commit)
  return nil unless commit['author'].is_a? Hash

  commit['author']['name']
end

#commit_default_formatObject



29
30
31
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 29

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

#commit_summary(commit) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 39

def commit_summary(commit)
  commit_default_format % {
    repository: format_repository(repository_name),
    branch: format_branch(branch),
    hash: format_hash(commit['id']),
    author: format_user(commit_author(commit)),
    title: prettify(commit['message'])
  }
end

#compare_urlObject



24
25
26
27
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 24

def compare_url
  commits = "#{before_commit}...#{before_commit}"
  shorten_url "#{repository_url}/compare/#{commits}"
end

#default_summaryObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 63

def default_summary
  user_name = extract(:user_name)

  summary_format % {
    repository: format_repository(repository_name),
    user: format_user(user_name),
    num_commits: num_commits,
    branch: format_branch(branch),
    url: compare_url
  }
end

#formatObject



75
76
77
78
79
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 75

def format
  [default_summary] + extract(:commits).map do |commit|
    commit_summary(commit)
  end
end

#num_commitsObject



53
54
55
56
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 53

def num_commits
  format_number(extract(:total_commits_count).to_i,
                'new commit', 'new commits')
end

#repository_nameObject



16
17
18
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 16

def repository_name
  extract(:repository, :name)
end

#repository_urlObject



20
21
22
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 20

def repository_url
  extract(:repository, :homepage)
end

#summary_formatObject



58
59
60
61
# File 'lib/codebot/formatters/gitlab_push_hook.rb', line 58

def summary_format
  '[%<repository>s] %<user>s pushed %<num_commits>s ' \
    'to %<branch>s: %<url>s'
end