factor-connector-hipchat 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/factor/connector/hipchat.rb +202 -0
- metadata +78 -0
@@ -0,0 +1,202 @@
|
|
1
|
+
require 'factor-connector-api'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
Factor::Connector.service 'hipchat' do
|
5
|
+
%w(message notification exit enter topic_change).each do |listener_name|
|
6
|
+
listener listener_name do
|
7
|
+
start do |params|
|
8
|
+
room_id = params['room_id'] || params['room']
|
9
|
+
api_key = params['api_key']
|
10
|
+
filter = params['filter']
|
11
|
+
listener_id = "room_#{listener_name}"
|
12
|
+
|
13
|
+
fail 'API Key is required' unless api_key
|
14
|
+
fail 'Room ID is required' unless room_id
|
15
|
+
fail 'Filter is required' if !filter && listener_name == 'message'
|
16
|
+
|
17
|
+
hook_url = get_web_hook(listener_id)
|
18
|
+
|
19
|
+
base = 'https://api.hipchat.com/v2/'
|
20
|
+
path = ['room', room_id, 'webhook'].join('/')
|
21
|
+
auth_query = "?auth_token=#{api_key}"
|
22
|
+
uri = base + path + auth_query
|
23
|
+
|
24
|
+
headers = {
|
25
|
+
'Content-Type' => 'application/json',
|
26
|
+
'Accept' => 'application/json'
|
27
|
+
}
|
28
|
+
|
29
|
+
info 'Getting hooks'
|
30
|
+
begin
|
31
|
+
http_response = HTTParty.get(uri, body: {}.to_json, headers: headers)
|
32
|
+
rescue
|
33
|
+
fail 'Could not get list of hooks. Check your creds.'
|
34
|
+
end
|
35
|
+
|
36
|
+
if http_response.body
|
37
|
+
response = JSON.parse(http_response.body)
|
38
|
+
if response['items']
|
39
|
+
hook = response['items'].find { |h| h['url'] == hook_url }
|
40
|
+
elsif response['error']
|
41
|
+
fail "Couldn't create web hook: #{response['error']['message']}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
if hook
|
46
|
+
info 'Looks like this web hook is already registered'
|
47
|
+
hook_id = hook['id']
|
48
|
+
else
|
49
|
+
info "Creating hook in `#{room_id}` room"
|
50
|
+
begin
|
51
|
+
body = {
|
52
|
+
url: hook_url,
|
53
|
+
event: listener_id,
|
54
|
+
name: 'workflow'
|
55
|
+
}
|
56
|
+
body[:pattern] = filter if filter
|
57
|
+
post_params = {
|
58
|
+
body: body.to_json,
|
59
|
+
headers: headers
|
60
|
+
}
|
61
|
+
http_response = HTTParty.post(uri, post_params)
|
62
|
+
rescue
|
63
|
+
fail 'Could not connect to Hipchat'
|
64
|
+
end
|
65
|
+
|
66
|
+
if http_response.body
|
67
|
+
response = JSON.parse(http_response.body)
|
68
|
+
if response['error']
|
69
|
+
fail "Couldn't create web hook: #{response['error']['message']}"
|
70
|
+
elsif response['id']
|
71
|
+
hook_id = response['id']
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
hook_url = web_hook id: listener_id do
|
77
|
+
start do |listener_start_params, hook_data, _req, _res|
|
78
|
+
info 'Triggering workflow...'
|
79
|
+
begin
|
80
|
+
filter = listener_start_params['filter']
|
81
|
+
|
82
|
+
if hook_data['item']['message'] && hook_data['item']['message']['message']
|
83
|
+
original_message = hook_data['item']['message']['message']
|
84
|
+
hook_data['message'] = original_message
|
85
|
+
|
86
|
+
if filter
|
87
|
+
regexp = Regexp.new(filter)
|
88
|
+
matches = regexp.match(original_message).captures
|
89
|
+
hook_data['matches'] = matches
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
hook_data['hook_id'] = hook_id
|
94
|
+
rescue => ex
|
95
|
+
fail "Couldn't parse message from Hipchat", exception: ex
|
96
|
+
end
|
97
|
+
|
98
|
+
begin
|
99
|
+
start_workflow hook_data
|
100
|
+
rescue => ex
|
101
|
+
fail 'Internal error: failed to send message for next step', exception: ex
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
stop do |params|
|
108
|
+
room_id = params['room_id'] || params['room']
|
109
|
+
api_key = params['api_key']
|
110
|
+
|
111
|
+
fail 'API Key is required' unless api_key
|
112
|
+
fail 'Room ID is required' unless room_id
|
113
|
+
|
114
|
+
info "Deleting hook #{listener_name}"
|
115
|
+
hook_url = get_web_hook(listener_id)
|
116
|
+
|
117
|
+
base = 'https://api.hipchat.com/v2/'
|
118
|
+
path = ['room', room_id, 'webhook'].join('/')
|
119
|
+
auth_query = "?auth_token=#{api_key}"
|
120
|
+
uri = base + path + auth_query
|
121
|
+
|
122
|
+
headers = {
|
123
|
+
'Content-Type' => 'application/json',
|
124
|
+
'Accept' => 'application/json'
|
125
|
+
}
|
126
|
+
|
127
|
+
info 'Getting hooks'
|
128
|
+
begin
|
129
|
+
http_response = HTTParty.get(uri, body: {}.to_json, headers: headers)
|
130
|
+
rescue => ex
|
131
|
+
fail "Couldn't get list of hooks. Consider refreshing token.", exception: ex
|
132
|
+
end
|
133
|
+
|
134
|
+
if http_response.body
|
135
|
+
response = JSON.parse(http_response.body)
|
136
|
+
if response['items']
|
137
|
+
hooks = response['items'].select { |h| h['url'] == hook_url }
|
138
|
+
|
139
|
+
hooks.each do |hook|
|
140
|
+
begin
|
141
|
+
base = 'https://api.hipchat.com/v2/'
|
142
|
+
path = ['room', room_id, 'webhook', hook['id']].join('/')
|
143
|
+
auth_query = "?auth_token=#{api_key}"
|
144
|
+
delete_uri = base + path + auth_query
|
145
|
+
|
146
|
+
http_response = HTTParty.delete(delete_uri, headers: headers)
|
147
|
+
rescue => ex
|
148
|
+
fail "Couldn't delete hook with id #{hook['id']}", exception: ex
|
149
|
+
end
|
150
|
+
end
|
151
|
+
elsif response['error']
|
152
|
+
fail "Couldn't delete web hook: #{response['error']['message']}"
|
153
|
+
end
|
154
|
+
else
|
155
|
+
fail "Couldn't get list of hooks from Hipchat"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
action 'send' do |data|
|
162
|
+
room_id = data['room_id'] || data['room']
|
163
|
+
color = data['color'] || 'gray'
|
164
|
+
format = data['format'] || 'text'
|
165
|
+
api_key = data['api_key']
|
166
|
+
message = data['message']
|
167
|
+
|
168
|
+
fail 'API Key is required' unless api_key
|
169
|
+
fail 'Message is required' unless message
|
170
|
+
fail 'Room ID is required' unless room_id
|
171
|
+
|
172
|
+
base = 'https://api.hipchat.com/v2/'
|
173
|
+
path = ['room', room_id, 'notification'].join('/')
|
174
|
+
auth_query = "?auth_token=#{api_key}"
|
175
|
+
uri = base + path + auth_query
|
176
|
+
|
177
|
+
body = {
|
178
|
+
message: message,
|
179
|
+
message_format: format,
|
180
|
+
color: color,
|
181
|
+
format: 'json'
|
182
|
+
}
|
183
|
+
|
184
|
+
headers = {
|
185
|
+
'Content-Type' => 'application/json',
|
186
|
+
'Accept' => 'application/json'
|
187
|
+
}
|
188
|
+
|
189
|
+
info "Posting message to `#{room_id}` room"
|
190
|
+
begin
|
191
|
+
http_response = HTTParty.post(uri, body: body.to_json, headers: headers)
|
192
|
+
rescue
|
193
|
+
fail "Couldn't post message"
|
194
|
+
end
|
195
|
+
|
196
|
+
if http_response.response.code == '204'
|
197
|
+
action_callback status: 'sent'
|
198
|
+
else
|
199
|
+
action_callback status: 'failed'
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: factor-connector-hipchat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maciej Skierkowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.12.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.12.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: factor-connector-api
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.0.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.0.1
|
46
|
+
description:
|
47
|
+
email:
|
48
|
+
- maciej@factor.io
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/factor/connector/hipchat.rb
|
54
|
+
homepage: https://factor.io
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.25
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Hipchat Factor.io Connector
|
78
|
+
test_files: []
|