Skip to content

Commit

Permalink
Service hook for Zendesk
Browse files Browse the repository at this point in the history
With @dadah89 and @morten
  • Loading branch information
Saroj Yadav committed Feb 29, 2012
1 parent f511f1b commit 5e27f7e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
53 changes: 53 additions & 0 deletions services/zendesk.rb
@@ -0,0 +1,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
55 changes: 55 additions & 0 deletions test/zendesk_test.rb
@@ -0,0 +1,55 @@
require File.expand_path('../helper', __FILE__)

class ZendeskTest < Service::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end

def test_subdomain
post

svc = service :event,
{'username' => 'user', 'password' => 'pass', 'subdomain' => 'igor'}, :message => 'My name is zd#12345 what do you say?'
svc.receive_event
end

def test_domain
post

svc = service :event,
{'username' => 'user', 'password' => 'pass', 'subdomain' => 'igor.zendesk.com'}, :message => 'My name is zd#12345 what do you say?'
svc.receive_event
end

def test_unmatched_ticket
post

svc = service :event,
{'username' => 'user', 'password' => 'pass', 'subdomain' => 'igor'}, :message => 'My name is 12345 what do you say?'
svc.receive_event

begin
@stubs.verify_stubbed_calls
rescue RuntimeError
else
assert_true false
end
end

def post
@stubs.post "/api/v2/integrations/github" do |env|
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal 'igor.zendesk.com', env[:url].host
assert_equal '12345', env[:body][:ticket_id]
assert_equal JSON.generate({:message => 'My name is zd#12345 what do you say?'}), env[:body][:payload]
[201, {}, '']
end
end


def service(*args)
super Service::Zendesk, *args
end
end


0 comments on commit 5e27f7e

Please sign in to comment.