Skip to content

Commit

Permalink
Merge pull request openSUSE#15099 from hennevogel/refactoring/project…
Browse files Browse the repository at this point in the history
…_find_package

Simplify Project.find_package
  • Loading branch information
danidoni authored Oct 25, 2023
2 parents 09105fb + 92b8251 commit f19206e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/api/app/models/linked_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class LinkedProject < ApplicationRecord

validate :validate_duplicates

scope :local, -> { where.not(linked_db_project: nil) }

protected

def validate_duplicates
Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ def check_source_access?
true
end

def check_access?
Project.check_access?(project)
end

def check_source_access!
return if check_source_access?
# TODO: Use pundit for authorization instead
Expand Down
40 changes: 25 additions & 15 deletions src/api/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ def update_instance_or_self(namespace = 'OBS', name = 'UpdateProject')
self
end

# Find the project defined in the OBS:UpdateProject attribute
def update_project
attribute = find_attribute('OBS', 'UpdateProject')
return unless attribute
return if attribute.values.first.value.empty?

update_project_name = attribute.values.first.value
project = Project.find_by(name: update_project_name)
return project if project

raise Project::Errors::UnknownObjectError, "Update project configured in #{name} but not found: #{update_project_name}"
end

def cleanup_linking_repos
# replace links to this project repositories with links to the "deleted" repository
find_repos(:linking_repositories) do |linking_repository|
Expand Down Expand Up @@ -814,32 +827,29 @@ def find_package(package_name, check_update_project = nil, processed = {})
end
processed[self] = 1

# package exists in this project
pkg = nil
pkg = update_instance_or_self.packages.find_by_name(package_name) if check_update_project
pkg = packages.find_by_name(package_name) if pkg.nil?
return pkg if pkg && Package.check_access?(pkg)
pkg = find_package_on_update_project(package_name) if check_update_project
pkg ||= packages.find_by(name: package_name)
return pkg if pkg&.check_access?

# search via all linked projects
linking_to.each do |lp|
linking_to.local.each do |lp|
raise CycleError, 'project links against itself, this is not allowed' if self == lp.linked_db_project

pkg = if lp.linked_db_project.nil?
# We can't get a package object from a remote instance ... how shall we handle this ?
nil
else
lp.linked_db_project.find_package(package_name, check_update_project, processed)
end
unless pkg.nil?
return pkg if Package.check_access?(pkg)
end
pkg = lp.linked_db_project.find_package(package_name, check_update_project, processed)
return pkg if pkg&.check_access?
end

# no package found
processed.delete(self)
nil
end

def find_package_on_update_project(package_name)
return unless update_project

update_project.packages.find_by(name: package_name)
end

def expand_all_repositories
repositories.collect(&:expand_all_repositories).flatten.uniq
end
Expand Down

0 comments on commit f19206e

Please sign in to comment.