From dcb228a22fab8d9889d76244892136f5bd445ee0 Mon Sep 17 00:00:00 2001 From: Arun Persaud Date: Mon, 17 Feb 2014 22:51:35 -0800 Subject: [PATCH 1/2] allow multiple tokens for different OBS builds Use a comma separated list of tokens and send several http post requests. Signed-off-by: Arun Persaud --- docs/obs | 2 ++ lib/services/obs.rb | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/docs/obs b/docs/obs index 31205aad4..696b81f8d 100644 --- a/docs/obs +++ b/docs/obs @@ -20,6 +20,8 @@ Install Notes That means the token can only be used for this package. It also means you do not have to specify it in github.com. Just using the token is enough. + You can also use a comma separated list of tokens to trigger several obs builds. + 2. Enter your credentials at github.com - The token which got created (use "osc token" if you lost it) - optional: Modify the api url, if you do not use the openSUSE Build Service instance. diff --git a/lib/services/obs.rb b/lib/services/obs.rb index 20b613d58..f4b37dfd8 100644 --- a/lib/services/obs.rb +++ b/lib/services/obs.rb @@ -15,7 +15,7 @@ class Service::Obs < Service::HttpPost def receive_push # required - token = required_config_value('token') + token = required_config_value('token').to_s url = config_value('url') url = "https://api.opensuse.org:443" if url.blank? @@ -23,18 +23,21 @@ def receive_push project = config_value('project') package = config_value('package') - if token.match(/^[A-Za-z0-9+\/=]+$/) == nil + # multiple tokens? handle each one individually + token.split(",").each do |t| # token is not base64 - raise_config_error "Invalid token" + if t.strip.match(/^[A-Za-z0-9+\/=]+$/) == nil + raise_config_error "Invalid token" + end + + http.ssl[:verify] = false + http.headers['Authorization'] = "Token #{t.strip}" + + url = "#{url}/trigger/runservice" + unless project.blank? or package.blank? + url << "?project=#{CGI.escape(project)}&package=#{CGI.escape(package)}" + end + deliver url end - - http.ssl[:verify] = false - http.headers['Authorization'] = "Token #{token}" - - url = "#{url}/trigger/runservice" - unless project.blank? or package.blank? - url << "?project=#{CGI.escape(project)}&package=#{CGI.escape(package)}" - end - deliver url end end From 4f56566029ef0fd571222893395d04a6f7407a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Schr=C3=B6ter?= Date: Tue, 18 Feb 2014 10:15:01 +0100 Subject: [PATCH 2/2] add test case for multi token OBS notification --- test/obs_test.rb | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/test/obs_test.rb b/test/obs_test.rb index 13767e399..48250945b 100644 --- a/test/obs_test.rb +++ b/test/obs_test.rb @@ -5,7 +5,7 @@ def setup @stubs = Faraday::Adapter::Test::Stubs.new end - def data + def single_token_data { # service works with all current OBS 2.5 instances "url" => "http://api.opensuse.org:443", @@ -16,7 +16,18 @@ def data } end - def test_push + def multi_token_data + { + # service works with all current OBS 2.5 instances + "url" => "http://api.opensuse.org:443", + "token" => "github/test/token/one, github/test/token/two", + # optional + "project" => "home:adrianSuSE", + "package" => "4github", + } + end + + def test_push_single_token apicall = "/trigger/runservice" @stubs.post apicall do |env| assert_equal 'api.opensuse.org', env[:url].host @@ -27,8 +38,26 @@ def test_push [200, {}, ''] end - svc = service :push, data, payload + svc = service :push, single_token_data, payload + svc.receive + end + + def test_push_multi_token + apicall = "/trigger/runservice" + match = 0 + @stubs.post apicall do |env| + assert_equal 'api.opensuse.org', env[:url].host + params = Faraday::Utils.parse_query env[:body] + match=match+1 if ['Token github/test/token/one', 'Token github/test/token/two'].include? env[:request_headers]["Authorization"] + assert_equal '/trigger/runservice', env[:url].path + assert_equal 'project=home%3AadrianSuSE&package=4github', env[:url].query + [200, {}, ''] + end + + svc = service :push, multi_token_data, payload svc.receive + # both tokens received + assert_equal match, 2 end def service(*args)