forked from github/github-services
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Saroj Yadav
committed
Feb 29, 2012
1 parent
f511f1b
commit 5e27f7e
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|