Class: Vidar::SlackNotification

Inherits:
Object
  • Object
show all
Defined in:
lib/vidar/slack_notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github:, revision:, revision_name:, deploy_config:, build_url: nil, connection: Faraday.new) ⇒ SlackNotification

Returns a new instance of SlackNotification.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vidar/slack_notification.rb', line 13

def initialize(github:, revision:, revision_name:, deploy_config:, build_url: nil, connection: Faraday.new)
  @github = github
  @revision = revision
  @revision_name = revision_name
  @build_url = build_url
  @deploy_name = deploy_config.name
  @deploy_url = deploy_config.url
  @default_color = deploy_config.default_color
  @success_color = deploy_config.success_color
  @failure_color = deploy_config.failure_color
  @webhook_url = deploy_config.slack_webhook_url
  @connection = connection
end

Class Method Details

.getObject



3
4
5
6
7
8
9
10
11
# File 'lib/vidar/slack_notification.rb', line 3

def self.get
  new(
    github:        Config.get!(:github),
    revision:      Config.get!(:revision),
    revision_name: Config.get!(:revision_name),
    build_url:     Config.build_url,
    deploy_config: Config.deploy_config
  )
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/vidar/slack_notification.rb', line 27

def configured?
  !webhook_url.to_s.empty?
end

#deliver(message:, color: default_color) ⇒ Object



47
48
49
# File 'lib/vidar/slack_notification.rb', line 47

def deliver(message:, color: default_color)
  perform_with data(message:, color:)
end

#failureObject



31
32
33
34
35
36
37
# File 'lib/vidar/slack_notification.rb', line 31

def failure
  message = [
    "<!channel> Failed deploy of #{github_link} to #{deploy_link}.",
    build_link,
  ]
  perform_with data(message:, color: failure_color)
end

#perform_with(data) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/vidar/slack_notification.rb', line 51

def perform_with(data)
  connection.post do |req|
    req.url webhook_url
    req.headers['Content-Type'] = 'application/json'
    req.body = data.to_json
  end
end

#successObject



39
40
41
42
43
44
45
# File 'lib/vidar/slack_notification.rb', line 39

def success
  message = [
    "Successful deploy of #{github_link} to #{deploy_link}.",
    build_link,
  ]
  perform_with data(message:, color: success_color)
end