Class: Teamistrano::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/teamistrano/notifier.rb

Constant Summary collapse

DEFAULT_COLOR =

blue

"0033CC"
GREEN =
"00CC66"
RED =
"CC0000"

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Notifier

Returns a new instance of Notifier.



13
14
15
16
17
18
# File 'lib/teamistrano/notifier.rb', line 13

def initialize( env )
  @env = env
  @settings = Teamistrano::Settings.new( @env )
  @color = DEFAULT_COLOR
  @msg = ""
end

Instance Method Details

#get_bodyObject

Get the body of the POST message as JSON.



61
62
63
64
65
66
67
# File 'lib/teamistrano/notifier.rb', line 61

def get_body
  return {
    'title' => "Deployment Notice",
    'text' => get_msg,
    'themeColor' => @color
  }.to_json
end

#get_msgObject

Get the full message to be delieved.



54
55
56
57
58
# File 'lib/teamistrano/notifier.rb', line 54

def get_msg
  return "#{@msg} for #{@settings.application}.  (#{@settings.stage})
    \n Branch: #{@settings.branch} 
    \n Deployed by: #{@settings.deployer}"
end

#notify_failedObject

Send notice that deployment failed.



47
48
49
50
51
# File 'lib/teamistrano/notifier.rb', line 47

def notify_failed
  @color = RED
  @msg = "Deployment Failed"
  post
end

#notify_finishingObject

Send notice that deployment finished.



34
35
36
37
38
# File 'lib/teamistrano/notifier.rb', line 34

def notify_finishing
  @color = GREEN
  @msg = "Deployment Finished"
  post
end

#notify_finishing_rollbackObject

Send notice that deployment rollback finished.



41
42
43
44
# File 'lib/teamistrano/notifier.rb', line 41

def notify_finishing_rollback
  @msg = "Deployment Rollback Finished"
  post
end

#notify_revertingObject

Send notice that deployment is reverting.



28
29
30
31
# File 'lib/teamistrano/notifier.rb', line 28

def notify_reverting
  @msg = "Deployment Reverting"
  post
end

#notify_updatingObject

Send notice that deployment started.



22
23
24
25
# File 'lib/teamistrano/notifier.rb', line 22

def notify_updating
  @msg = "Deployment Started"
  post
end

#postObject

Post to Teams.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/teamistrano/notifier.rb', line 70

def post
  uri = URI.parse( @settings.webhook_url )
  request = Net::HTTP::Post.new( uri.path )
  request.content_type = 'application/json'
  request.body = get_body
  n = Net::HTTP.new( uri.host, uri.port )
  n.use_ssl = true

  # Send the payload to the endpoint.
  n.start { |http| http.request( request ) }
end