-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OwnerSearch::Base and OwnerSearch::Owned are split now too, leaving only shared functions not to be used from controllers in Base. Creating a module for it
- Loading branch information
Showing
5 changed files
with
144 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'api_exception' | ||
|
||
module OwnerSearch | ||
class Base | ||
class AttributeNotSetError < APIException | ||
setup 'attribute_not_set', 400 | ||
end | ||
|
||
def initialize(params = {}) | ||
self.params = params | ||
self.attribute = AttribType.find_by_name!(params[:attribute] || 'OBS:OwnerRootProject') | ||
|
||
self.limit = (params[:limit] || 1).to_i | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :params, :attribute, :limit | ||
|
||
def object_projects(_obj) | ||
# default project specified | ||
return [Project.get_by_name(params[:project])] if params[:project] | ||
|
||
# Find all marked projects | ||
projects = Project.find_by_attribute_type(attribute) | ||
return projects unless projects.empty? | ||
raise AttributeNotSetError, "The attribute #{attribute.fullname} is not set to define default projects." | ||
end | ||
|
||
def project_attrib(project) | ||
return unless project | ||
project.attribs.find_by(attrib_type: attribute) | ||
end | ||
|
||
def filter(project) | ||
return params[:filter].split(',') if params[:filter] | ||
|
||
attrib = project_attrib(project) | ||
if attrib && attrib.values.where(value: 'BugownerOnly').exists? | ||
['bugowner'] | ||
else | ||
['maintainer', 'bugowner'] | ||
end | ||
end | ||
|
||
def devel_disabled?(project = nil) | ||
return ['0', 'false'].include?(params[:devel]) if params[:devel] | ||
|
||
attrib = project_attrib(project) | ||
attrib && attrib.values.where(value: 'DisableDevel').exists? | ||
end | ||
|
||
def filter_roles(relation, rolefilter) | ||
return relation if rolefilter.empty? | ||
role_ids = rolefilter.map { |r| Role.find_by_title!(r).id } | ||
relation.where(role_id: role_ids) | ||
end | ||
|
||
def filter_users(owner, container, rolefilter, user) | ||
rel = filter_roles(container.relationships.users, rolefilter) | ||
rel = rel.where(user: user) if user | ||
rel = rel.joins(:user).where(relationships: { users: { state: 'confirmed' } }) | ||
rel.each do |p| | ||
owner.users ||= {} | ||
entries = owner.users.fetch(p.role.title, []) << p.user.login | ||
owner.users[p.role.title] = entries | ||
end | ||
end | ||
|
||
def filter_groups(owner, container, rolefilter, group) | ||
rel = filter_roles(container.relationships.groups, rolefilter) | ||
rel = rel.where(group: group) if group | ||
rel.each do |p| | ||
next unless p.group.any_confirmed_users? | ||
owner.groups ||= {} | ||
entries = owner.groups.fetch(p.role.title, []) << p.group.title | ||
owner.groups[p.role.title] = entries | ||
end | ||
end | ||
|
||
def extract_from_container(owner, container, rolefilter, user_or_group = nil) | ||
filter_users(owner, container, rolefilter, user_or_group) unless user_or_group.class == Group | ||
filter_groups(owner, container, rolefilter, user_or_group) unless user_or_group.class == User | ||
owner | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module OwnerSearch | ||
class Owned < Base | ||
|
||
def for(owner) | ||
# search in each marked project | ||
object_projects(owner).map do |project| | ||
find_containers(project, owner) | ||
end.flatten | ||
end | ||
|
||
def find_containers(rootproject, owner, devel) | ||
projects = rootproject.expand_all_projects(allow_remote_projects: false) | ||
|
||
roles = [] | ||
filter(rootproject).each do |f| | ||
roles << Role.find_by_title!(f) | ||
end | ||
|
||
found_packages = Relationship.where(role_id: roles, package_id: Package.where(project_id: projects).pluck(:id)) | ||
found_projects = Relationship.where(role_id: roles, project_id: projects) | ||
# fast find packages with defintions | ||
case owner.class | ||
when User | ||
# user in package object | ||
found_packages = found_packages.where(user_id: owner) | ||
# user in project object | ||
found_projects = found_projects.where(user_id: owner) | ||
when Group | ||
# group in package object | ||
found_packages = found_packages.where(group_id: owner) | ||
# group in project object | ||
found_projects = found_projects.where(group_id: owner) | ||
else | ||
raise ArgumentError, 'illegal object handed to find_containers' | ||
end | ||
unless devel_disabled?(project) | ||
# FIXME: add devel packages, but how do recursive lookup fast in SQL? | ||
end | ||
found_packages = found_packages.pluck(:package_id).uniq | ||
found_projects = found_projects.pluck(:project_id).uniq | ||
|
||
maintainers = [] | ||
|
||
Project.where(id: found_projects).pluck(:name).each do |prj| | ||
maintainers << Owner.new(rootproject: rootproject.name, project: prj) | ||
end | ||
Package.where(id: found_packages).find_each do |pkg| | ||
maintainers << Owner.new(rootproject: rootproject.name, project: pkg.project.name, package: pkg.name) | ||
end | ||
|
||
maintainers | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters