Class: Codebot::Formatters::Gitlab::JobHook

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

Overview

Triggers on a Job or Build event

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

#author(commit) ⇒ Object



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

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

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

#branchObject



58
59
60
61
62
63
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 58

def branch
  pieces = extract(:ref).split('/')
  return pieces[0] if pieces.length == 1

  pieces[2..-1].join('/')
end

#formatObject



44
45
46
47
48
49
50
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 44

def format
  if extract(:build_status) == 'created'
    [job_description] + [format_commit(extract(:commit))]
  else
    [job_description]
  end
end

#format_commit(commit) ⇒ Object



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

def format_commit(commit)
  '%<repository>s/%<branch>s %<hash>s %<author>s: %<title>s' % {
    repository: format_repository(repo_name),
    branch: format_branch(branch),
    hash: format_hash(commit['id']),
    author: format_user(author(commit)),
    title: prettify(commit['message'])
  }
end

#format_job_statusObject

rubocop:disable Metrics/MethodLength



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 8

def format_job_status # rubocop:disable Metrics/MethodLength
  case extract(:build_status)
  when 'created'
    'was created from commit:'
  when 'success'
    'succeeded'
  when 'skipped'
    'was skipped'
  when /^fail/, /^err/
    "failed: #{extract(:build_failure_reason)}"
  else
    "did something: #{extract(:build_status)}"
  end
end

#job_descriptionObject



35
36
37
38
39
40
41
42
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 35

def job_description
  '[%<repository>s] job \'%<build>s\' (%<url>s) %<status>s' % {
    repository: format_repository(repo_name),
    build: extract(:build_name),
    url: job_url,
    status: format_job_status
  }
end

#job_urlObject



31
32
33
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 31

def job_url
  shorten_url "#{repo_url}/-/jobs/#{extract(:build_id)}"
end

#repo_nameObject



27
28
29
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 27

def repo_name
  extract(:repository, :name)
end

#repo_urlObject



23
24
25
# File 'lib/codebot/formatters/gitlab_job_hook.rb', line 23

def repo_url
  extract(:repository, :homepage)
end