Module: Codebot::Formatters

Defined in:
lib/codebot/formatters.rb,
lib/codebot/formatters/fork.rb,
lib/codebot/formatters/ping.rb,
lib/codebot/formatters/push.rb,
lib/codebot/formatters/watch.rb,
lib/codebot/formatters/gollum.rb,
lib/codebot/formatters/issues.rb,
lib/codebot/formatters/public.rb,
lib/codebot/formatters/pull_request.rb,
lib/codebot/formatters/issue_comment.rb,
lib/codebot/formatters/commit_comment.rb,
lib/codebot/formatters/gitlab_job_hook.rb,
lib/codebot/formatters/gitlab_note_hook.rb,
lib/codebot/formatters/gitlab_push_hook.rb,
lib/codebot/formatters/gitlab_issue_hook.rb,
lib/codebot/formatters/gitlab_pipeline_hook.rb,
lib/codebot/formatters/gitlab_wiki_page_hook.rb,
lib/codebot/formatters/gitlab_merge_request_hook.rb,
lib/codebot/formatters/pull_request_review_comment.rb

Overview

This module provides methods for formatting outgoing IRC messages.

Defined Under Namespace

Modules: Gitlab Classes: CommitComment, Fork, Gollum, IssueComment, Issues, Ping, Public, PullRequest, PullRequestReviewComment, Push, Watch

Class Method Summary collapse

Class Method Details

.create_formatter(event, payload, integration) ⇒ Object

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



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

def self.create_formatter(event, payload, integration) # rubocop:disable Metrics/LineLength, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
  case event
  when :commit_comment
    Formatters::CommitComment.new(payload, Shortener::Github.new)
  when :fork
    Formatters::Fork.new(payload, Shortener::Github.new)
  when :gollum
    Formatters::Gollum.new(payload, Shortener::Github.new)
  when :issue_comment
    Formatters::IssueComment.new(payload, Shortener::Github.new)
  when :issues
    Formatters::Issues.new(payload, Shortener::Github.new)
  when :ping
    Formatters::Ping.new(payload, Shortener::Github.new)
  when :public
    Formatters::Public.new(payload, Shortener::Github.new)
  when :pull_request
    Formatters::PullRequest.new(payload, Shortener::Github.new)
  when :pull_request_review_comment
    Formatters::PullRequestReviewComment.new(payload, Shortener::Github.new)
  when :push
    Formatters::Push.new(payload, Shortener::Github.new)
  when :watch
    Formatters::Watch.new(payload, Shortener::Github.new)
  when :gitlab_push_hook
    Formatters::Gitlab::PushHook.new(payload, shortener(integration))
  when :gitlab_tag_push_hook
    Formatters::Gitlab::PushHook.new(payload, shortener(integration))
  when :gitlab_job_hook
    Formatters::Gitlab::JobHook.new(payload, shortener(integration))
  when :gitlab_build_hook
    Formatters::Gitlab::JobHook.new(payload, shortener(integration))
  when :gitlab_pipeline_hook
    Formatters::Gitlab::PipelineHook.new(payload, shortener(integration))
  when :gitlab_issue_hook
    Formatters::Gitlab::IssueHook.new(payload, shortener(integration))
  when :gitlab_note_hook
    Formatters::Gitlab::NoteHook.new(payload, shortener(integration))
  when :gitlab_merge_request_hook
    Formatters::Gitlab::MergeRequestHook.new(payload,
                                             shortener(integration))
  when :gitlab_wiki_page_hook
    Formatters::Gitlab::WikiPageHook.new(payload,
                                         shortener(integration))
  else "Error: missing formatter for #{event.inspect}"
  end
end

.format(event, payload, integration, color = true) ⇒ Array<String>

Formats IRC messages for an event.

Parameters:

  • event (Symbol)

    the webhook event

  • payload (Object)

    the JSON payload object

  • color (Boolean) (defaults to: true)

    whether to use formatting codes

Returns:

  • (Array<String>)

    the formatted messages



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/codebot/formatters.rb', line 34

def self.format(event, payload, integration, color = true)
  messages = format_color(event, payload, integration).to_a
  messages.map! { |msg| "\x0F" + msg }
  messages.map! { |msg| ::Cinch::Formatting.unformat(msg) } unless color
  messages
rescue StandardError => e
  STDERR.puts e.message
  STDERR.puts e.backtrace
  url = ::Cinch::Formatting.format(:blue, :underline, FORMATTER_ISSUE_URL)
  ['An error occurred while formatting this message. More information ' \
   "has been printed to STDERR. Please report this issue to #{url}."]
end

.format_color(event, payload, integration) ⇒ Array<String>

Formats colored IRC messages. This method should not be called directly from outside this module.

Parameters:

  • event (Symbol)

    the webhook event

  • payload (Object)

    the JSON payload object

Returns:

  • (Array<String>)

    the formatted messages



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

def self.format_color(event, payload, integration) # rubocop:disable Metrics/LineLength
  create_formatter(event, payload, integration).format
end

.shortener(inte) ⇒ Object



47
48
49
# File 'lib/codebot/formatters.rb', line 47

def self.shortener(inte)
  Shortener::Custom.new(inte.shortener_url, inte.shortener_secret)
end