samson_hipchat 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/decorators/stage_decorator.rb +8 -6
- data/app/models/hipchat_notification.rb +37 -12
- data/app/models/hipchat_room.rb +29 -0
- data/app/views/samson_hipchat/_fields.html.erb +35 -1
- data/app/views/samson_hipchat/notification.text.erb +1 -1
- data/db/migrate/20150706182832_add_notify_on_to_hipchat_room.rb +8 -0
- data/lib/samson_hipchat/samson_plugin.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4b61d9b6e5b8d1cbf71e7c8caec7c7100fd7f39
|
4
|
+
data.tar.gz: eea4c0d26ac5f3722f9eed70fb37ee7be2aa1e71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 083f463cd20974ffde51d5a912702f0d804edc1157ee933d36f18b1e6056c8712fee995059f72a7f55e3eb4e2b2e1b7723fb630f64f852326cf92dbe45710b95
|
7
|
+
data.tar.gz: 03592f994dc71930e4e14fa68fe81311cbeb89e84caf2ea6f45f3d26f7e5ef225c40a4ee55a6e5c9d3f9205ce864551bde54034f1aa1c5ecb81283a139e63425
|
@@ -20,12 +20,14 @@ Stage.class_eval do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def update_room_id
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
self.hipchat_rooms.each do |room|
|
24
|
+
if room_for(room_name)
|
25
|
+
# we attempt to save room id
|
26
|
+
room.room_id = room_for(room_name)['id']
|
27
|
+
else
|
28
|
+
# if we fail, this maybe a notification token, just set it to 0
|
29
|
+
room.room_id = 0 if room_name
|
30
|
+
end
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
@@ -6,15 +6,30 @@ class HipchatNotification
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def deliver
|
9
|
-
|
9
|
+
return if @stage.hipchat_rooms.empty?
|
10
|
+
|
11
|
+
@stage.hipchat_rooms.each do |room|
|
12
|
+
try_delivery(@deploy, room)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def try_delivery(deploy, room)
|
18
|
+
if !room.accept_notify?(deploy)
|
19
|
+
Rails.logger.debug("No need to notify #{room.name}")
|
20
|
+
return
|
21
|
+
end
|
22
|
+
|
23
|
+
Rails.logger.debug("Begin to notify #{room.name}")
|
24
|
+
message = Message.new(deploy, room.multi_message?)
|
10
25
|
begin
|
11
|
-
hipchat[
|
26
|
+
hipchat(room.token)[room.name].send message.from, message.to_s, **message.style
|
12
27
|
rescue HipChat::UnknownRoom => e
|
13
28
|
Rails.logger.error("Room did not existed")
|
14
29
|
rescue HipChat::Unauthorized => e
|
15
30
|
Rails.logger.error("Invalid token to post to room")
|
16
31
|
rescue HipChat::UnknownResponseCode => e
|
17
|
-
Rails.logger.error("Could not deliver hipchat message: #{e.message}")
|
32
|
+
Rails.logger.error("Could not deliver hipchat message: #{e.message} to room #{room.name}")
|
18
33
|
end
|
19
34
|
end
|
20
35
|
|
@@ -22,12 +37,13 @@ class HipchatNotification
|
|
22
37
|
delegate :project, :stage, :user, to: :@deploy
|
23
38
|
delegate :project_deploy_url, to: 'AppRoutes.url_helpers'
|
24
39
|
|
25
|
-
def initialize(deploy)
|
40
|
+
def initialize(deploy, is_multi_message)
|
26
41
|
@deploy = deploy
|
27
42
|
@stage = deploy.stage
|
28
43
|
@project = @stage.project
|
29
44
|
@user = @deploy.user
|
30
45
|
@changeset = @deploy.changeset
|
46
|
+
@is_multi_message = is_multi_message
|
31
47
|
end
|
32
48
|
|
33
49
|
def style
|
@@ -39,13 +55,17 @@ class HipchatNotification
|
|
39
55
|
end
|
40
56
|
|
41
57
|
def subject
|
42
|
-
|
58
|
+
if @is_multi_message
|
59
|
+
subject = "#{@user.name} is about to <a href='#{deploy_url}'>deploy</a> <strong>#{@project.name}</strong> on <strong>#{@stage.name}</strong><br>"
|
43
60
|
|
44
|
-
|
61
|
+
subject = "#{@user.name} successfully deploy <strong>#{@project.name}</strong> @<a href='#{diff_url}'>#{@deploy.commit}...#{@changeset.try(:previous_commit)}</a> on <strong>#{@stage.name}</strong><br>" if @deploy.job.succeeded?
|
45
62
|
|
46
|
-
|
63
|
+
subject = "#{@user.name} failed to <a href='#{deploy_url}'>deploy</a> <strong>#{@project.name}</strong> on <strong>#{@stage.name}</strong><br>" if @deploy.job.failed? || @deploy.job.errored?
|
47
64
|
|
48
|
-
|
65
|
+
subject
|
66
|
+
else
|
67
|
+
subject = "#{@user.name} successfully deploy <strong>#{@project.name}</strong> @<a href='#{diff_url}'>#{@deploy.commit}...#{@changeset.try(:previous_commit)}</a> on <strong>#{@stage.name}</strong><br>" if @deploy.job.succeeded?
|
68
|
+
end
|
49
69
|
end
|
50
70
|
|
51
71
|
def to_s
|
@@ -53,8 +73,12 @@ class HipchatNotification
|
|
53
73
|
end
|
54
74
|
|
55
75
|
def content
|
56
|
-
|
57
|
-
|
76
|
+
if @is_multi_message
|
77
|
+
return subject if @deploy.job.succeeded? || @deploy.job.failed? || @deploy.job.errored?
|
78
|
+
@content ||= HipchatNotificationRenderer.render(@deploy, subject)
|
79
|
+
else
|
80
|
+
@content ||= HipchatNotificationRenderer.render(@deploy, subject)
|
81
|
+
end
|
58
82
|
end
|
59
83
|
|
60
84
|
private
|
@@ -76,7 +100,8 @@ class HipchatNotification
|
|
76
100
|
|
77
101
|
private
|
78
102
|
|
79
|
-
def hipchat
|
80
|
-
HipChat::Client.new @stage.hipchat_rooms.first.token, api_version: 'v2'
|
103
|
+
def hipchat(token = nil)
|
104
|
+
HipChat::Client.new @stage.hipchat_rooms.first.token, api_version: 'v2' if token.nil?
|
105
|
+
HipChat::Client.new token, api_version: 'v2'
|
81
106
|
end
|
82
107
|
end
|
data/app/models/hipchat_room.rb
CHANGED
@@ -1,3 +1,32 @@
|
|
1
1
|
class HipchatRoom < ActiveRecord::Base
|
2
2
|
belongs_to :stage
|
3
|
+
enum notify_on: { always: 0, succeeded: 1, failed: 2 }
|
4
|
+
|
5
|
+
def always_notify?
|
6
|
+
return notify_on == "always"
|
7
|
+
end
|
8
|
+
|
9
|
+
def only_notify_suceeded?
|
10
|
+
return notify_on == "succeeded"
|
11
|
+
end
|
12
|
+
|
13
|
+
def accept_notify?(deploy)
|
14
|
+
if deploy.job.failed? || deploy.job.errored?
|
15
|
+
return always_notify?
|
16
|
+
end
|
17
|
+
|
18
|
+
if deploy.job.succeeded?
|
19
|
+
return always_notify? || only_notify_suceeded?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Other status, we will only notifiy if we enable always notify
|
23
|
+
return always_notify?
|
24
|
+
end
|
25
|
+
|
26
|
+
# Check if we can send multi message to this room
|
27
|
+
# Our logic: notify_on = suceeded only has to be single message becase we
|
28
|
+
# will only notify on succeeded
|
29
|
+
def multi_message?
|
30
|
+
return notify_on == "always"
|
31
|
+
end
|
3
32
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
<fieldset>
|
1
|
+
<fieldset id="hipchat-room-containers">
|
2
2
|
<legend>Hipchat</legend>
|
3
3
|
<p class="col-lg-offset-2">Hipchat room and token.</p>
|
4
4
|
|
@@ -7,6 +7,7 @@
|
|
7
7
|
|
8
8
|
<%= form.fields_for :hipchat_rooms do |hipchat_fields| %>
|
9
9
|
<div class="form-group">
|
10
|
+
|
10
11
|
<div class="col-lg-2 col-lg-offset-2">
|
11
12
|
<%= hipchat_fields.text_field :name, class: "form-control", placeholder: "Room name" %>
|
12
13
|
</div>
|
@@ -14,6 +15,11 @@
|
|
14
15
|
<%= hipchat_fields.text_field :token, class: "form-control", placeholder: "Token" %>
|
15
16
|
</div>
|
16
17
|
|
18
|
+
<div class="col-lg-2 col-lg-offset-2">
|
19
|
+
<%= hipchat_fields.select :notify_on, [['Always', :always], ['Succeeed', :succeeded]] %>
|
20
|
+
<%= hipchat_fields.label :notify_on, "Notify?" %>
|
21
|
+
</div>
|
22
|
+
|
17
23
|
<% if hipchat_fields.object.persisted? %>
|
18
24
|
<div class="col-lg-1 checkbox">
|
19
25
|
<%= hipchat_fields.check_box :_destroy if hipchat_fields.object.persisted? %>
|
@@ -22,4 +28,32 @@
|
|
22
28
|
<% end %>
|
23
29
|
</div>
|
24
30
|
<% end %>
|
31
|
+
<button class="btn btn-default" id="hipchat-room-create">Create room</button>
|
25
32
|
</fieldset>
|
33
|
+
|
34
|
+
<script>
|
35
|
+
//@TODO put them into asset
|
36
|
+
$(document).ready(function () {
|
37
|
+
function addHipChatForm(e) {
|
38
|
+
var formCount =$('#hipchat-room-containers')
|
39
|
+
.find(".form-group")
|
40
|
+
.length
|
41
|
+
|
42
|
+
e.preventDefault()
|
43
|
+
var form = $('#hipchat-room-containers')
|
44
|
+
.find(".form-group").last().clone()
|
45
|
+
.insertBefore('#hipchat-room-containers #hipchat-room-create')
|
46
|
+
form.find('input').each(function () {
|
47
|
+
var element = $(this)
|
48
|
+
if (element.prop('name') != undefined) {
|
49
|
+
element.prop('name', element.prop('name').replace(/\[[0-9]+\]/g, '[' + formCount + ']'));
|
50
|
+
}
|
51
|
+
if (element.attr('id') != undefined) {
|
52
|
+
element.attr('id', element.attr('id').replace(/_[0-9]+_/g, '_' + formCount + '_'));
|
53
|
+
}
|
54
|
+
})
|
55
|
+
}
|
56
|
+
|
57
|
+
$('#hipchat-room-create').click(addHipChatForm)
|
58
|
+
})
|
59
|
+
</script>
|
@@ -9,7 +9,7 @@ Samson::Hooks.callback :stage_clone do |old_stage, new_stage|
|
|
9
9
|
end
|
10
10
|
|
11
11
|
Samson::Hooks.callback :stage_permitted_params do
|
12
|
-
{ hipchat_rooms_attributes: [:id, :name, :token, :_destroy] }
|
12
|
+
{ hipchat_rooms_attributes: [:id, :name, :token, :notify_on, :_destroy] }
|
13
13
|
end
|
14
14
|
|
15
15
|
notify = -> (deploy, _buddy) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: samson_hipchat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vinh Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hipchat
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- app/views/samson_hipchat/notification.text.erb
|
39
39
|
- config/initializers/hipchat.rb
|
40
40
|
- db/migrate/20150519105221_create_hipchat_rooms.rb
|
41
|
+
- db/migrate/20150706182832_add_notify_on_to_hipchat_room.rb
|
41
42
|
- lib/samson_hipchat/samson_plugin.rb
|
42
43
|
homepage:
|
43
44
|
licenses: []
|