From afa1a2d5df09c944c67aa924e002ae6be94e1867 Mon Sep 17 00:00:00 2001 From: Nikita Frolenkov Date: Mon, 30 Jul 2012 17:51:55 +0600 Subject: [PATCH 1/5] Added SmarterBase service + test. --- services/smarterbase.rb | 44 ++++++++++++++++++++++++++++++++++++++++ test/smarterbase_test.rb | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 services/smarterbase.rb create mode 100644 test/smarterbase_test.rb diff --git a/services/smarterbase.rb b/services/smarterbase.rb new file mode 100644 index 000000000..c4887950d --- /dev/null +++ b/services/smarterbase.rb @@ -0,0 +1,44 @@ +class Service::Smarterbase < Service + default_events :commit_comment, :issues, :issue_comment + string :subdomain, :username + password :password + white_list :subdomain, :username + + 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 = "http://#{subdomain}/external/github" + else + url = "http://#{subdomain}.smarterbase.com/external/github" + end + + begin + Addressable::URI.parse(url) + rescue Addressable::URI::InvalidURIError + raise_config_error("Invalid subdomain #{subdomain}") + end + + url + end + + def receive_event + raise_config_error "Bad configuration" if invalid_request? + puts event.to_s + http.headers['X-GitHub-Event'] = event.to_s + + url = service_url(data['subdomain']) + res = http_post(url, { :payload => payload, + :subdomain => data['subdomain'], + :username => data['username'], + :password => data['password'] }.to_json) + + unless res.status.to_s[/2\d+/] + raise_config_error("Unexpected response code:#{res.status}") + end + end +end diff --git a/test/smarterbase_test.rb b/test/smarterbase_test.rb new file mode 100644 index 000000000..1e3c76d5d --- /dev/null +++ b/test/smarterbase_test.rb @@ -0,0 +1,37 @@ +require File.expand_path("../helper", __FILE__) + +class SmarterbaseTest < Service::TestCase + def setup + @stubs = Faraday::Adapter::Test::Stubs.new + @data = { "username" => "user", "password" => "pass", "subdomain" => "test_subdomain" } + @payload = { :message => "Some message" } + end + + def test_subdomain + post(@data) + svc = service :event, @data, @payload + svc.receive_event + end + + def test_domain + @data.merge("subdomain" => "test_subdomain.smarterbase.com") + + post(@data) + + svc = service :event, @data, @payload + svc.receive_event + end + + + def post(data) + @stubs.post "/external/github" do |env| + assert_equal "test_subdomain.smarterbase.com", env[:url].host + assert_equal ({ :payload => @payload }.merge(@data).to_json), env[:body] + [ 201, {}, "" ] + end + end + + def service(*args) + super Service::Smarterbase, *args + end +end From abaabd621e37dd05bd107497d78fc76bbe1a9b43 Mon Sep 17 00:00:00 2001 From: Nikita Frolenkov Date: Mon, 30 Jul 2012 18:09:07 +0600 Subject: [PATCH 2/5] Added smarterbase doc. --- docs/smarterbase | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 docs/smarterbase diff --git a/docs/smarterbase b/docs/smarterbase new file mode 100644 index 000000000..b156fb981 --- /dev/null +++ b/docs/smarterbase @@ -0,0 +1,15 @@ +Smarterbase +======= + +Use this service hook to selectively update tickets with comments based on GitHub commit messages and issue updates. + +In order to update a Smarterbase ticket, all you need to do is to configure and enable this hook, and then subsequently reference the ticket you want updated with the GitHub status. The format of the ticket reference is SB#123 where 123 is the id of the ticket. Only commits/comments containing a valid SB ticket reference are sent to Smarterbase. + +Install Notes +------------- + + 1. The subdomain is your Smarterbase subdomain, i.e. "subdomain" if you visit http://subdomain.smarterbase.com + + 2. Username is the email address of the user you wish to authenticate as + + 3. Password is the password of the user From 9c92d2654129993c31eeaff731802d3b647577b8 Mon Sep 17 00:00:00 2001 From: Nikita Frolenkov Date: Tue, 31 Jul 2012 18:08:52 +0600 Subject: [PATCH 3/5] Update smarterbase docs with the info about non-GAE deployment --- docs/smarterbase | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/smarterbase b/docs/smarterbase index b156fb981..03c402f0f 100644 --- a/docs/smarterbase +++ b/docs/smarterbase @@ -8,7 +8,8 @@ In order to update a Smarterbase ticket, all you need to do is to configure and Install Notes ------------- - 1. The subdomain is your Smarterbase subdomain, i.e. "subdomain" if you visit http://subdomain.smarterbase.com + 1. The subdomain is your Smarterbase subdomain, i.e. "subdomain" if you visit http://subdomain.smarterbase.com or + in the case of non-GAE deployment on http:///www.customer.com it will be, i.e. "customer.com" 2. Username is the email address of the user you wish to authenticate as From c4b113721f0bc499812c0d3391b873c979639ecb Mon Sep 17 00:00:00 2001 From: Nikita Frolenkov Date: Thu, 2 Aug 2012 13:05:33 +0600 Subject: [PATCH 4/5] Updated smarterbase doc. --- docs/smarterbase | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/smarterbase b/docs/smarterbase index 03c402f0f..b5cfb1a1e 100644 --- a/docs/smarterbase +++ b/docs/smarterbase @@ -9,8 +9,8 @@ Install Notes ------------- 1. The subdomain is your Smarterbase subdomain, i.e. "subdomain" if you visit http://subdomain.smarterbase.com or - in the case of non-GAE deployment on http:///www.customer.com it will be, i.e. "customer.com" + in the case of non-GAE deployment on http://www.customer.com it will be "customer.com" - 2. Username is the email address of the user you wish to authenticate as + 2. Username is username of the user you wish to authenticate as 3. Password is the password of the user From ad34044e6043fed920f4b4016bf76057ae33ab7c Mon Sep 17 00:00:00 2001 From: Nikita Frolenkov Date: Fri, 10 Aug 2012 17:46:54 +0600 Subject: [PATCH 5/5] Added oauth2 for smarterbase. --- services/smarterbase.rb | 44 ++++++++++++++++++++++++---------------- test/smarterbase_test.rb | 8 +++++--- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/services/smarterbase.rb b/services/smarterbase.rb index c4887950d..643a7e20a 100644 --- a/services/smarterbase.rb +++ b/services/smarterbase.rb @@ -1,20 +1,22 @@ +require 'oauth' + class Service::Smarterbase < Service default_events :commit_comment, :issues, :issue_comment - string :subdomain, :username - password :password - white_list :subdomain, :username + string :subdomain, :consumer_key, :consumer_secret + white_list :subdomain, :consumer_key, :consumer_secret def invalid_request? - data['username'].to_s.empty? or - data['password'].to_s.empty? or - data['subdomain'].to_s.empty? + puts data + data['subdomain'].to_s.empty? or + data['consumer_key'].to_s.empty? or + data['consumer_secret'].to_s.empty? end - def service_url(subdomain) + def full_url(subdomain, path='') if subdomain =~ /\./ - url = "http://#{subdomain}/external/github" + url = "http://#{subdomain}/#{path}" else - url = "http://#{subdomain}.smarterbase.com/external/github" + url = "http://#{subdomain}.smarterbase.com/#{path}" end begin @@ -24,21 +26,29 @@ def service_url(subdomain) end url + + end + + def service_url(subdomain) + full_url(subdomain, 'external/github') end def receive_event - raise_config_error "Bad configuration" if invalid_request? - puts event.to_s - http.headers['X-GitHub-Event'] = event.to_s + + consumer = OAuth::Consumer.new(data['consumer_key'], data['consumer_secret'], + :site => full_url(data['subdomain']), :http_method => :get, :scheme => :query_string) + access_token = OAuth::AccessToken.new(consumer) + + raise_config_error "Bad configuration" if invalid_request? + url = service_url(data['subdomain']) - res = http_post(url, { :payload => payload, + res = access_token.post(url, { :payload => payload.to_json, :subdomain => data['subdomain'], - :username => data['username'], - :password => data['password'] }.to_json) + :event => event.to_s }) - unless res.status.to_s[/2\d+/] - raise_config_error("Unexpected response code:#{res.status}") + unless res.code.to_s[/2\d+/] + raise_config_error("Unexpected response code:#{res.code}") end end end diff --git a/test/smarterbase_test.rb b/test/smarterbase_test.rb index 1e3c76d5d..cd522b209 100644 --- a/test/smarterbase_test.rb +++ b/test/smarterbase_test.rb @@ -3,7 +3,9 @@ class SmarterbaseTest < Service::TestCase def setup @stubs = Faraday::Adapter::Test::Stubs.new - @data = { "username" => "user", "password" => "pass", "subdomain" => "test_subdomain" } + @data = { "subdomain" => "test", + "consumer_key" => "1d5ff56af429bcd3da32cc23f4982c3ccde7cf9ae3a8a68f0da507ecc8d47f7", + "consumer_secret" => "dc9ad345843e6c7f2d116f1cad8531382be386b6f6d389ba3e6f7bd1521be02"} @payload = { :message => "Some message" } end @@ -14,7 +16,7 @@ def test_subdomain end def test_domain - @data.merge("subdomain" => "test_subdomain.smarterbase.com") + @data.merge("subdomain" => "test.smarterbase.com") post(@data) @@ -25,7 +27,7 @@ def test_domain def post(data) @stubs.post "/external/github" do |env| - assert_equal "test_subdomain.smarterbase.com", env[:url].host + assert_equal "test.smarterbase.com", env[:url].host assert_equal ({ :payload => @payload }.merge(@data).to_json), env[:body] [ 201, {}, "" ] end