Skip to content

Commit

Permalink
[webui] Refactoring subproject/ancestors of Project
Browse files Browse the repository at this point in the history
  • Loading branch information
Moisés Déniz Alemán committed Aug 4, 2015
1 parent 511a511 commit 7c6cdbc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 53 deletions.
23 changes: 5 additions & 18 deletions src/api/app/controllers/webui/project_controller.rb
Expand Up @@ -10,11 +10,13 @@ class Webui::ProjectController < Webui::WebuiController

helper 'webui/comment'

before_filter :set_project, only: [:autocomplete_packages, :autocomplete_repositories]
before_filter :set_project, only: [:autocomplete_packages, :autocomplete_repositories,
:subprojects]
# This is the deprecated way of loading WebuiProject
# FIXME: Get rid of "except" clauses, use "only" instead
before_filter :require_project, except: [:autocomplete_projects, :autocomplete_incidents,
:autocomplete_packages, :autocomplete_repositories,
:subprojects,
:clear_failed_comment, :edit_comment_form, :index,
:list, :list_all, :list_simple,
:list_public, :new, :package_buildresult,
Expand Down Expand Up @@ -115,23 +117,8 @@ def users
end

def subprojects
@subprojects = Array.new

projects=Project.arel_table
Project.where(projects[:name].matches("#{@project.name}:%")).each do |sub|
@subprojects << sub
end
@subprojects.sort! { |x,y| x.name <=> y.name } # Sort by hash key for better display
@parentprojects = Hash.new
parent_names = @project.name.split ':'
parent_names.each_with_index do |parent, idx|
parent_name = parent_names.slice(0, idx+1).join(':')
unless [@project.name, 'home'].include?( parent_name )
parent_project = WebuiProject.find( parent_name )
@parentprojects[parent_name] = parent_project unless parent_project.blank?
end
end
@parentprojects = @parentprojects.sort # Sort by hash key for better display
@subprojects = @project.subprojects.order(:name)
@parentprojects = @project.ancestors.order(:name)
end

def new
Expand Down
8 changes: 4 additions & 4 deletions src/api/app/helpers/maintenance_helper.rb
Expand Up @@ -11,7 +11,7 @@ def create_new_maintenance_incident( maintenanceProject, baseProject = nil, requ
mi = nil
tprj = nil
Project.transaction do
mi = MaintenanceIncident.new( :maintenance_db_project => maintenanceProject )
mi = MaintenanceIncident.new( :maintenance_db_project => maintenanceProject )
tprj = Project.create :name => mi.project_name
if baseProject
# copy as much as possible from base project
Expand All @@ -30,7 +30,7 @@ def create_new_maintenance_incident( maintenanceProject, baseProject = nil, requ
tprj.flags.create( :flag => 'access', :status => 'disable')
end
# take over roles from maintenance project
maintenanceProject.relationships.each do |r|
maintenanceProject.relationships.each do |r|
tprj.relationships.create(user: r.user, role: r.role, group: r.group)
end
# set default bugowner if missing
Expand Down Expand Up @@ -259,7 +259,7 @@ def get_updateinfo_id(sourcePackage, targetRepo)

# expand a possible defined update info template in release target of channel
projectFilter = nil
if p = sourcePackage.project.find_parent and p.is_maintenance?
if p = sourcePackage.project.parent and p.is_maintenance?
projectFilter = p.maintained_projects.map{|mp| mp.project}
end
# prefer a channel in the source project to avoid double hits exceptions
Expand Down Expand Up @@ -345,7 +345,7 @@ def instantiate_container(project, opackage, opts={})
arguments="&noservice=1"
arguments << "&requestid=" << opts[:requestid] if opts[:requestid]
arguments << "&comment=" << CGI.escape(opts[:comment]) if opts[:comment]
if opts[:makeoriginolder]
if opts[:makeoriginolder]
# versioned copy
path = pkg.source_path + "?cmd=copy&withvrev=1&oproject=#{CGI.escape(opkg.project.name)}&opackage=#{CGI.escape(opkg.name)}#{arguments}&user=#{CGI.escape(User.current.login)}&comment=initialize+package"
if Package.exists_by_project_and_name(project.name, opkg.name, allow_remote_packages: true)
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/owner.rb
Expand Up @@ -252,7 +252,7 @@ def self.find_maintainers(container, filter)
m.rootproject = ''
if cont.is_a? Package
m.project = cont.project.name
m.package = cont.name
m.package = cont.name
else
m.project = cont.name
end
Expand All @@ -268,7 +268,7 @@ def self.find_maintainers(container, filter)
# add maintainers from parent projects
while not project.nil?
add_owners.call(project)
project = project.find_parent
project = project.parent
end
maintainers
end
Expand Down
35 changes: 24 additions & 11 deletions src/api/app/models/project.rb
Expand Up @@ -134,6 +134,10 @@ def cleanup_before_destroy
end
end

def subprojects
Project.where("name like '#{name}:%'")
end

def revoke_requests
# Find open requests with 'pro' as source or target and decline/revoke them.
# Revoke if source or decline if target went away, pick the first action that matches to decide...
Expand Down Expand Up @@ -778,21 +782,30 @@ def attribute_url
"/source/#{CGI.escape(self.name)}/_project/_attribute"
end

# step down through namespaces until a project is found, returns found project or nil
def self.find_parent_for(project_name)
name_parts = project_name.split(/:/)

while name_parts.length > 1
name_parts.pop
project = Project.find_by_name(name_parts.join(':'))
break if project
# Give me the first ancestor of that project
def parent
project = nil
possible_ancestor_names.find do |name|
project = Project.find_by(name: name)
end
project
end

# convenience method for self.find_parent_for
def find_parent
self.class.find_parent_for self.name
# Give me all the projects that are ancestors of that project
def ancestors
Project.where(name: possible_ancestor_names)
end

# Calculate all possible ancestors names for a project
# Ex: home:foo:bar:purr => ["home:foo:bar", "home:foo", "home"]
def possible_ancestor_names
names = name.split(/:/)
possible_projects = []
while names.length > 1
names.pop
possible_projects << names.join(':')
end
possible_projects
end

def to_axml(opts={})
Expand Down
8 changes: 4 additions & 4 deletions src/api/app/models/user.rb
Expand Up @@ -558,10 +558,10 @@ def can_create_project?(project_name)
return true if /^home:#{self.login}:/.match( project_name ) and ::Configuration.allow_user_to_create_home_project

return true if has_global_permission? 'create_project'
p = Project.find_parent_for(project_name)
return false if p.nil?
parent_project = Project.new(name: project_name).parent
return false if parent_project.nil?
return true if is_admin?
return has_local_permission?( 'create_project', p)
return has_local_permission?( 'create_project', parent_project)
end

def can_modify_attribute_definition?(object)
Expand Down Expand Up @@ -699,7 +699,7 @@ def has_local_permission?( perm_string, object )
when Project
logger.debug "running local permission check: user #{self.login}, project #{object.name}, permission '#{perm_string}'"
#check permission for given project
parent = object.find_parent
parent = object.parent
when nil
return has_global_permission?(perm_string)
else
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/views/webui/project/subprojects.html.haml
Expand Up @@ -12,9 +12,9 @@
%th Parent project
%th Description
%tbody
- @parentprojects.each do |name, project|
- @parentprojects.each do |project|
%tr
%td= link_to name, :action => 'show', :project => name
%td= link_to project.name, :action => 'show', :project => project.name
%td= project.title

%h3= @pagetitle
Expand Down
24 changes: 12 additions & 12 deletions src/api/test/functional/webui/package_branch_test.rb
Expand Up @@ -60,7 +60,7 @@ def test_branch_package_for_home_project
:original_name => 'TestPack',
:original_project => 'home:Iggy')
end

def test_branch_package_double_and_submit_back
use_js

Expand Down Expand Up @@ -97,7 +97,7 @@ def test_branch_package_double_and_submit_back
# cleanup
delete_package("home:Iggy","TestPack_link")
end

def test_branch_package_for_global_project

login_Iggy to: project_show_path(:project => @project)
Expand All @@ -107,8 +107,8 @@ def test_branch_package_for_global_project
:original_name => 'kdelibs',
:original_project => 'kde4')
end


def test_branch_package_twice_duplicate_name

login_Iggy to: project_show_path(:project => @project)
Expand All @@ -119,8 +119,8 @@ def test_branch_package_twice_duplicate_name
:original_name => 'TestPack',
:original_project => 'home:Iggy')
end


def test_branch_package_twice

login_Iggy to: project_show_path(:project => @project)
Expand All @@ -147,7 +147,7 @@ def test_branch_empty_package_name
:original_project => 'home:Iggy',
:expect => :invalid_package_name)
end

def test_branch_empty_project_name

login_Iggy to: project_show_path(:project => @project)
Expand All @@ -172,7 +172,7 @@ def test_branch_package_name_with_spaces


def test_branch_project_name_with_spaces

login_Iggy to: project_show_path(:project => @project)

create_package_branch(
Expand All @@ -181,14 +181,14 @@ def test_branch_project_name_with_spaces
:original_project => 'invalid project name',
:expect => :invalid_project_name)
end

def test_autocomplete_packages
use_js

login_Iggy to: project_show_path(:project => @project)
click_link 'Branch existing package'
results = fill_autocomplete 'linked_project', with: 'home:', select: 'home:dmayr'

results = fill_autocomplete 'linked_project', with: 'home:d', select: 'home:dmayr'
results.must_include 'home:dmayr'
results.wont_include 'Apache'
results = fill_autocomplete 'linked_package', with: 'x11', select: 'x11vnc'
Expand Down

0 comments on commit 7c6cdbc

Please sign in to comment.