Skip to content

Commit

Permalink
Move "expand" method to ProjectLinks concern
Browse files Browse the repository at this point in the history
Those methods are exclusively about project links.
  • Loading branch information
hennevogel committed Oct 19, 2023
1 parent 0c9a1b8 commit 672af6f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
71 changes: 71 additions & 0 deletions src/api/app/models/concerns/project_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,77 @@ def expand_linking_to
expand_all_projects(allow_remote_projects: false).map(&:id)
end

def expand_all_projects(project_map: {}, allow_remote_projects: true)
# cycle check
return [] if project_map[self]

project_map[self] = 1

projects = [self]

# add all linked and indirect linked projects
linking_to.each do |lp|
if lp.linked_db_project.nil?
projects << lp.linked_remote_project_name if allow_remote_projects
else
lp.linked_db_project.expand_all_projects(project_map: project_map, allow_remote_projects: allow_remote_projects).each do |p|
projects << p
end
end
end

projects
end

# return array of [:name, :project_id] tuples
def expand_all_packages(packages = [], project_map = {}, package_map = {})
# check for project link cycle
return [] if project_map[self]

project_map[self] = 1

self.packages.joins(:project).pluck(:name, 'projects.name').each do |name, prj_name|
next if package_map[name]

packages << [name, prj_name]
package_map[name] = 1
end

# second path, all packages from indirect linked projects
linking_to.each do |lp|
if lp.linked_db_project.nil?
# FIXME: this is a remote project
else
lp.linked_db_project.expand_all_packages(packages, project_map, package_map)
end
end

packages.sort_by { |package| package.first.downcase }
end

# return array of [:name, :package_id] tuples for all products
# this function is making the products uniq
def expand_all_products
p_map = {}
products = Product.all_products(self).to_a
products.each { |p| p_map[p.cpe] = 1 } # existing packages map
# second path, all packages from indirect linked projects
linking_to.each do |lp|
if lp.linked_db_project.nil?
# FIXME: this is a remote project
else
lp.linked_db_project.expand_all_products.each do |p|
unless p_map[p.cpe]
products << p
p_map[p.cpe] = 1
end
end
end
end

products
end

# replace links to this project with links to the "deleted" project
def cleanup_linking_projects
LinkedProject.transaction do
Expand Down
71 changes: 0 additions & 71 deletions src/api/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -844,77 +844,6 @@ def expand_all_repositories
repositories.collect(&:expand_all_repositories).flatten.uniq
end

def expand_all_projects(project_map: {}, allow_remote_projects: true)
# cycle check
return [] if project_map[self]

project_map[self] = 1

projects = [self]

# add all linked and indirect linked projects
linking_to.each do |lp|
if lp.linked_db_project.nil?
projects << lp.linked_remote_project_name if allow_remote_projects
else
lp.linked_db_project.expand_all_projects(project_map: project_map, allow_remote_projects: allow_remote_projects).each do |p|
projects << p
end
end
end

projects
end

# return array of [:name, :project_id] tuples
def expand_all_packages(packages = [], project_map = {}, package_map = {})
# check for project link cycle
return [] if project_map[self]

project_map[self] = 1

self.packages.joins(:project).pluck(:name, 'projects.name').each do |name, prj_name|
next if package_map[name]

packages << [name, prj_name]
package_map[name] = 1
end

# second path, all packages from indirect linked projects
linking_to.each do |lp|
if lp.linked_db_project.nil?
# FIXME: this is a remote project
else
lp.linked_db_project.expand_all_packages(packages, project_map, package_map)
end
end

packages.sort_by { |package| package.first.downcase }
end

# return array of [:name, :package_id] tuples for all products
# this function is making the products uniq
def expand_all_products
p_map = {}
products = Product.all_products(self).to_a
products.each { |p| p_map[p.cpe] = 1 } # existing packages map
# second path, all packages from indirect linked projects
linking_to.each do |lp|
if lp.linked_db_project.nil?
# FIXME: this is a remote project
else
lp.linked_db_project.expand_all_products.each do |p|
unless p_map[p.cpe]
products << p
p_map[p.cpe] = 1
end
end
end
end

products
end

def add_repository_targets(trepo, source_repo, add_target_repos = [], opts = {})
trepo.clone_repository_from(source_repo)
trepo.rebuild = opts[:rebuild] if opts[:rebuild]
Expand Down

0 comments on commit 672af6f

Please sign in to comment.