Skip to content

Commit

Permalink
[api] additional route with extra owner ship informations for the web…
Browse files Browse the repository at this point in the history
…ui only.
  • Loading branch information
adrianschroeter committed Jan 15, 2013
1 parent cca842c commit 54c4084
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 66 deletions.
69 changes: 4 additions & 65 deletions src/api/app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

include SearchHelper

class SearchController < ApplicationController

require 'xpath_engine'
Expand Down Expand Up @@ -46,7 +48,7 @@ def attribute
def missing_owner
params[:limit] ||= "0" #unlimited by default

@owners = _owner(params, nil)
@owners = search_owner(params, nil)

end

Expand All @@ -65,70 +67,7 @@ def owner
return
end


@owners = _owner(params, obj)
end

private

def _owner(params, obj)
params[:attribute] ||= "OBS:OwnerRootProject"
at = AttribType.find_by_name(params[:attribute])
unless at
render_error :status => 404, :errorcode => "unknown_attribute_type",
:message => "Attribute Type #{params[:attribute]} does not exist"
return
end

limit = params[:limit] || 1

projects = []
if params[:project]
# default project specified
projects = [Project.get_by_name(params[:project])]
else
# Find all marked projects
projects = Project.find_by_attribute_type(at)
unless projects.length > 0
render_error :status => 400, :errorcode => "attribute_not_set",
:message => "The attribute type #{params[:attribute]} is not set on any projects. No default projects defined."
return
end
end

# search in each marked project
owners = []
projects.each do |project|

attrib = project.attribs.where(attrib_type_id: at.id).first
filter = ["maintainer","bugowner"]
devel = true
if params[:filter]
filter=params[:filter].split(",")
else
if attrib and v=attrib.values.where(value: "BugownerOnly").first
filter=["bugowner"]
end
end
if params[:devel]
devel=false if [ "0", "false" ].include? params[:devel]
else
if attrib and v=attrib.values.where(value: "DisableDevel").first
devel=false
end
end

if obj.nil?
owners = project.find_containers_without_definition(devel, filter)
elsif obj.class == String
owners = project.find_assignees(obj, limit.to_i, devel, filter)
else
owners = project.find_containers(obj, limit.to_i, devel, filter)
end

end

return owners
@owners = search_owner(params, obj)
end

def predicate_from_match_parameter(p)
Expand Down
11 changes: 11 additions & 0 deletions src/api/app/controllers/webui_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'yajl'

include SearchHelper

class WebuiController < ApplicationController

# return all data related that the webui wants to show on /project/show
Expand Down Expand Up @@ -261,4 +263,13 @@ def all_projects
end
render text: Yajl::Encoder.encode(ret), content_type: "application/json"
end

def owner
valid_http_methods :get
required_parameters :binary

Suse::Backend.start_test_backend if Rails.env.test?

@owners = search_owner(params, params[:binary])
end
end
65 changes: 65 additions & 0 deletions src/api/app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

module SearchHelper

def search_owner(params, obj)
params[:attribute] ||= "OBS:OwnerRootProject"
at = AttribType.find_by_name(params[:attribute])
unless at
render_error :status => 404, :errorcode => "unknown_attribute_type",
:message => "Attribute Type #{params[:attribute]} does not exist"
return
end

limit = params[:limit] || 1

projects = []
if params[:project]
# default project specified
projects = [Project.get_by_name(params[:project])]
else
# Find all marked projects
projects = Project.find_by_attribute_type(at)
unless projects.length > 0
render_error :status => 400, :errorcode => "attribute_not_set",
:message => "The attribute type #{params[:attribute]} is not set on any projects. No default projects defined."
return
end
end

# search in each marked project
owners = []
projects.each do |project|

attrib = project.attribs.where(attrib_type_id: at.id).first
filter = ["maintainer","bugowner"]
devel = true
if params[:filter]
filter=params[:filter].split(",")
else
if attrib and v=attrib.values.where(value: "BugownerOnly").first
filter=["bugowner"]
end
end
if params[:devel]
devel=false if [ "0", "false" ].include? params[:devel]
else
if attrib and v=attrib.values.where(value: "DisableDevel").first
devel=false
end
end

if obj.nil?
owners = project.find_containers_without_definition(devel, filter)
elsif obj.class == String
owners = project.find_assignees(obj, limit.to_i, devel, filter)
else
owners = project.find_containers(obj, limit.to_i, devel, filter)
end

end

return owners
end

end

35 changes: 35 additions & 0 deletions src/api/app/views/webui/owner.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
xml.collection do
@owners.each do |o|

attribs={}
attribs[:rootproject] = o[:rootproject]
attribs[:project] = o[:project]
attribs[:package] = o[:package] if o[:package]
xml.owner(attribs) do

roles = []
roles += o[:users].keys if o[:users]
roles += o[:groups].keys if o[:groups]

roles.each do |role|
if o[:users] and o[:users][role]
o[:users][role].each do |user|
u = User.find_by_login user
xml.person do
xml.login user
xml.email u.email
xml.realname u.realname
end
end
end
if o[:groups] and o[:groups][role]
o[:groups][role].each do |group|
xml.group
xml.name group
end
end
end
end
end
end

5 changes: 5 additions & 0 deletions src/api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@

match 'public/status/:action' => 'status#index'

#
# NOTE: webui routes are NOT stable and change together with the webui.
# DO NOT USE THEM IN YOUR TOOLS!
#
controller :webui do
match 'webui/project_infos' => :project_infos
match 'webui/project_requests' => :project_requests
Expand All @@ -306,6 +310,7 @@
match 'webui/request_list' => :request_list
match 'webui/change_role' => :change_role, via: :post
match 'webui/all_projects' => :all_projects
match 'webui/owner' => :owner
end

match "/404" => "main#notfound"
Expand Down
76 changes: 75 additions & 1 deletion src/api/test/functional/webui_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
require File.expand_path(File.dirname(__FILE__) + "/..") + "/test_helper"

class WebuiControllerTest < ActionController::IntegrationTest


fixtures :all

def setup
super
wait_for_scheduler_start
end

def test_project_infos
get "/webui/project_infos?project=home:Iggy"
assert_response 401
Expand All @@ -11,4 +18,71 @@ def test_project_infos
assert_response :success

end

def test_search_owner
prepare_request_with_user "king", "sunflower"

get "/webui/owner"
assert_response 400
assert_xml_tag :tag => 'status', :attributes => { :code => "missing_parameter" }

# must be after first search controller call or backend might not be started on single test case runs
wait_for_publisher()

get "/webui/owner?binary='package'"
assert_response 400
assert_xml_tag :tag => 'status', :attributes => { :code => "attribute_not_set" }

get "/webui/owner?binary='package'&attribute='OBS:does_not_exist'"
assert_response 404
assert_xml_tag :tag => 'status', :attributes => { :code => "unknown_attribute_type" }

post "/source/home:Iggy/_attribute", "<attributes><attribute namespace='OBS' name='OwnerRootProject' /></attributes>"
assert_response :success

get "/webui/owner?binary=DOES_NOT_EXIST"
assert_response :success
assert_xml_tag :tag => 'collection', :children => { :count => 0 }

get "/webui/owner?binary=package"
assert_response :success
assert_xml_tag :tag => 'owner', :attributes => { :rootproject => "home:Iggy", :project => "home:Iggy", :package => "TestPack" }
assert_xml_tag :tag => 'login', :content => "fred"
assert_xml_tag :tag => 'email', :content => "fred@feuerstein.de"
assert_xml_tag :tag => 'realname', :content => "Frederic Feuerstone"

get "/webui/owner?binary=package"
assert_response :success
assert_xml_tag :tag => 'owner', :attributes => { :rootproject => "home:Iggy", :project => "home:Iggy", :package => "TestPack" }
assert_xml_tag :tag => 'login', :content => "fred"
assert_xml_tag :tag => 'email', :content => "fred@feuerstein.de"
assert_xml_tag :tag => 'realname', :content => "Frederic Feuerstone"

# set devel package (this one has another devel package in home:coolo:test)
pkg = Package.find_by_project_and_name "home:Iggy", "TestPack"
pkg.develpackage = Package.find_by_project_and_name "kde4", "kdelibs"
pkg.save

# include devel package
get "/webui/owner?binary=package"
assert_response :success
assert_xml_tag :tag => 'owner', :attributes => { :project => "home:coolo:test" }

# search again, but ignore devel package
get "/webui/owner?binary=package&devel=false"
assert_response :success
assert_xml_tag :tag => 'owner', :attributes => { :rootproject => "home:Iggy", :project => "home:Iggy", :package => "TestPack" }

get "/webui/owner?binary=package&limit=-1"
assert_response :success
assert_xml_tag :tag => 'login', :content => "tom"

# reset devel package setting again
pkg.develpackage = nil
pkg.save
# cleanup
delete "/source/home:Iggy/_attribute/OBS:OwnerRootProject"
assert_response :success
end

end

0 comments on commit 54c4084

Please sign in to comment.