Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/obs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 15 additions & 12 deletions lib/services/obs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ 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?

# optional. The token may set the package container already.
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
35 changes: 32 additions & 3 deletions test/obs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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)
Expand Down