forked from github/github-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zendesk.rb
53 lines (44 loc) · 1.29 KB
/
zendesk.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
require 'uri'
class Service::Zendesk < Service
default_events :commit_comment, :issues, :issue_comment, :pull_request, :push
string :subdomain, :username
password :password
def invalid_request?
data['username'].to_s.empty? or
data['password'].to_s.empty? or
data['subdomain'].to_s.empty?
end
def service_url(subdomain)
if subdomain =~ /\./
url = "https://#{subdomain}/api/v2/integrations/github"
else
url = "https://#{subdomain}.zendesk.com/api/v2/integrations/github"
end
begin
URI.parse(url)
rescue URI::InvalidURIError
raise_config_error("Invalid subdomain #{subdomain}")
end
url
end
def receive_event
raise_config_error "Missing or bad configuration" if invalid_request?
if payload.inspect =~ /zd#(\d+)/i
ticket_id = $1
else
return
end
http.basic_auth data['username'], data['password']
http.headers['Content-Type'] = 'application/json'
http.headers['Accept'] = 'application/json'
http.headers['X-GitHub-Event'] = event.to_s
url = service_url(data['subdomain'])
res = http_post(url,
:ticket_id => ticket_id,
:payload => JSON.generate(payload)
)
if res.status != 201
raise_config_error("Unexpected response code:#{res.status}")
end
end
end