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 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)