-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
104 lines (87 loc) · 3 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
require 'bundler'
Bundler.require
require 'logger'
require 'intercom'
INTERCOM_REGEX = /https:\/\/app.intercom.io\/a\/apps\/(?<app_id>\S*)\/inbox\/(\S*\/)?conversation(s)?\/(?<conversation_id>\d*)/
INTERCOM_CLIENT = Intercom::Client.new(token: ENV['INTERCOM_API_KEY'])
JIRA_HOSTNAME = ENV['JIRA_HOSTNAME']
configure :production do
app_logger = Logger.new(STDOUT)
set :logging, Logger::DEBUG
use Rack::CommonLogger, app_logger
set :dump_errors, true
set :raise_errors, true
end
configure :development do
app_logger = Logger.new(STDOUT)
set :logging, Logger::DEBUG
use Rack::CommonLogger, app_logger
end
configure :test do
app_logger = Logger.new('/dev/null')
set :logging, Logger::WARN
use Rack::CommonLogger, app_logger
end
#################
# helper methods
#
def jira_issue_url key
%(https://#{JIRA_HOSTNAME}/browse/#{key})
end
def jira_issue_regex key
/https:\/\/#{JIRA_HOSTNAME}\/browse\/#{key}/
end
#
#################
get '/health' do
content_type :json
{status: 'OK'}.to_json
end
post '/jira_to_intercom' do
content_type :json
request.body.rewind
begin
data = request.body.read
json = JSON.parse(data)
if json.empty?
logger.error('JSON payload is empty')
halt 500
end
rescue JSON::ParserError => ex
logger.error('Unable to parse JSON.')
logger.error(ex)
halt 500
# ensure
# logger.debug(data)
end
if ['jira:issue_created', 'jira:issue_updated'].include?(json['webhookEvent'])
# get issue info
issue_title = json['issue']['fields']['summary']
issue_key = json['issue']['key']
issue_status = json['issue']['fields']['status']['name']
issue_type = json['issue']['fields']['issuetype']['name']
issue_url = jira_issue_url(issue_key)
assignee = json['issue']['fields']['assignee'] ? json['issue']['fields']['assignee']['name'] : "Unassigned"
description = json['issue']['fields']['description']
comment = json['comment'] ? json['comment']['body'] : nil
match_data = description.scan(INTERCOM_REGEX)
# iterate through description and send note to intercom conversation
$i = 0
$num = match_data.length
while $i < $num do
convo_id = match_data[$i][1]
#open conversation and add note
conversation = INTERCOM_CLIENT.conversations.find(:id => convo_id)
status = conversation.to_hash
logger.debug(status)
if status != "open"
logger.info("Opening Intercom conversation #{convo_id}")
INTERCOM_CLIENT.conversations.open(id: convo_id, admin_id: ENV['INTERCOM_ADMIN_ID'])
end
logger.info("Linking Jira issue #{issue_key} to Intercom conversation #{convo_id}")
INTERCOM_CLIENT.conversations.reply(id: convo_id, type: 'admin', admin_id: ENV['INTERCOM_ADMIN_ID'], message_type: 'note', body: "<a href='#{issue_url}' target='_blank'>#{issue_type} [#{issue_key}] #{issue_title} </a><br><b>Status:</b> #{issue_status}<br><b>Assigned to:</b> #{assignee}#{comment ? "<br><b>Comment:</b> #{comment}" : "" }")
#increment loop
$i += 1
end
end
end