Class: SlackService

Inherits:
Service
  • Object
show all
Defined in:
app/models/project_services/slack_service.rb,
app/models/project_services/slack_service/push_message.rb,
app/models/project_services/slack_service/base_message.rb,
app/models/project_services/slack_service/note_message.rb,
app/models/project_services/slack_service/merge_message.rb,
app/models/project_services/slack_service/issue_message.rb,
app/models/project_services/slack_service/build_message.rb,
app/models/project_services/slack_service/wiki_page_message.rb

Overview

Schema Information

Table name: services

id                    :integer          not null, primary key
type                  :string
title                 :string
project_id            :integer
created_at            :datetime         not null
updated_at            :datetime         not null
active                :boolean          not null
properties            :text
template              :boolean          default(FALSE)
push_events           :boolean          default(TRUE)
issues_events         :boolean          default(TRUE)
merge_requests_events :boolean          default(TRUE)
tag_push_events       :boolean          default(TRUE)
note_events           :boolean          default(TRUE), not null
build_events          :boolean          default(FALSE), not null
category              :string           default("common"), not null
default               :boolean          default(FALSE)
wiki_page_events      :boolean          default(TRUE)

Defined Under Namespace

Classes: BaseMessage, BuildMessage, IssueMessage, MergeMessage, NoteMessage, PushMessage, WikiPageMessage

Instance Method Summary collapse

Methods inherited from Service

#activated?, #async_execute, available_services_names, boolean_accessor, #can_test?, #category, create_from_template, #issue_tracker?, prop_accessor, #reset_updated_properties, #template?, #test, #updated_properties

Instance Method Details

#descriptionObject


41
42
43
# File 'app/models/project_services/slack_service.rb', line 41

def description
  'A team communication tool for the 21st century'
end

#execute(data) ⇒ Object


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
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/project_services/slack_service.rb', line 69

def execute(data)
  return unless supported_events.include?(data[:object_kind])
  return unless webhook.present?

  object_kind = data[:object_kind]

  data = data.merge(
    project_url: project_url,
    project_name: project_name
  )

  # WebHook events often have an 'update' event that follows a 'open' or
  # 'close' action. Ignore update events for now to prevent duplicate
  # messages from arriving.

  message = \
    case object_kind
    when "push", "tag_push"
      PushMessage.new(data)
    when "issue"
      IssueMessage.new(data) unless is_update?(data)
    when "merge_request"
      MergeMessage.new(data) unless is_update?(data)
    when "note"
      NoteMessage.new(data)
    when "build"
      BuildMessage.new(data) if should_build_be_notified?(data)
    when "wiki_page"
      WikiPageMessage.new(data)
    end

  opt = {}
  opt[:channel] = channel if channel
  opt[:username] = username if username

  if message
    notifier = Slack::Notifier.new(webhook, opt)
    notifier.ping(message.pretext, attachments: message.attachments, fallback: message.fallback)
  end
end

#fieldsObject


55
56
57
58
59
60
61
62
63
# File 'app/models/project_services/slack_service.rb', line 55

def fields
  [
    { type: 'text', name: 'webhook',
      placeholder: 'https://hooks.slack.com/services/...' },
    { type: 'text', name: 'username', placeholder: 'username' },
    { type: 'text', name: 'channel', placeholder: '#channel' },
    { type: 'checkbox', name: 'notify_only_broken_builds' },
  ]
end

#helpObject


49
50
51
52
53
# File 'app/models/project_services/slack_service.rb', line 49

def help
  'This service sends notifications to your Slack channel.<br/>
  To setup this Service you need to create a new <b>"Incoming webhook"</b> in your Slack integration panel,
  and enter the Webhook URL below.'
end

#initialize_propertiesObject


30
31
32
33
34
35
# File 'app/models/project_services/slack_service.rb', line 30

def initialize_properties
  if properties.nil?
    self.properties = {}
    self.notify_only_broken_builds = true
  end
end

#supported_eventsObject


65
66
67
# File 'app/models/project_services/slack_service.rb', line 65

def supported_events
  %w(push issue merge_request note tag_push build wiki_page)
end

#titleObject


37
38
39
# File 'app/models/project_services/slack_service.rb', line 37

def title
  'Slack'
end

#to_paramObject


45
46
47
# File 'app/models/project_services/slack_service.rb', line 45

def to_param
  'slack'
end