Skip to content

Commit

Permalink
Merge pull request #2090 from ChrisBr/fix_has_distro
Browse files Browse the repository at this point in the history
[api][webui] Refactors and fixes Project#has_distribution
  • Loading branch information
bgeuken committed Aug 30, 2016
2 parents 35f9e72 + ed66897 commit c047b82
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/api/app/models/project.rb
Expand Up @@ -52,6 +52,8 @@ def autocomplete(search)
has_many :attribs, :dependent => :destroy

has_many :repositories, :dependent => :destroy, foreign_key: :db_project_id
has_many :path_elements, through: :repositories
has_many :linked_repositories, through: :path_elements, source: :link, foreign_key: :repository_id
has_many :repository_architectures, -> { order("position") }, through: :repositories
has_many :architectures, -> { order("position").distinct }, :through => :repository_architectures

Expand Down Expand Up @@ -168,11 +170,7 @@ def maintained_project_names

# Check if the project has a path_element matching project and repository
def has_distribution(project_name, repository)
project = Project.find_by(name: project_name)
return false unless project

link_id = project.repositories.find_by(name: repository).try(:id)
repositories.joins(path_elements: :link).where(links_path_elements: { id: link_id }).exists?
has_local_distribution(repository) || has_remote_distribution(project_name, repository)
end

def number_of_build_problems
Expand Down Expand Up @@ -1857,4 +1855,16 @@ def collect_patchinfo_data(patchinfo)
{}
end
end

def has_remote_distribution(project_name, repository)
linked_repositories.remote.any? do |linked_repository|
project_name.end_with?(linked_repository.remote_project_name) && linked_repository.name == repository
end
end

def has_local_distribution(repository)
linked_repositories.not_remote.any? do |linked_repository|
linked_repository.name == repository
end
end
end
3 changes: 2 additions & 1 deletion src/api/app/models/repository.rb
Expand Up @@ -16,7 +16,8 @@ class Repository < ApplicationRecord
has_many :repository_architectures, -> { order("position") }, :dependent => :destroy, inverse_of: :repository
has_many :architectures, -> { order("position") }, :through => :repository_architectures

scope :not_remote, -> { where(:remote_project_name => nil) }
scope :not_remote, -> { where(remote_project_name: nil) }
scope :remote, -> { where.not(remote_project_name: nil) }

validates :name, length: { in: 1..200 }
# Keep in sync with src/backend/BSVerify.pm
Expand Down
25 changes: 25 additions & 0 deletions src/api/spec/models/project_spec.rb
Expand Up @@ -348,4 +348,29 @@
end
end
end

describe "#has_distribution" do
context "remote distribution" do
let(:remote_project) {create(:remote_project, name: "openSUSE.org")}
let(:remote_distribution) { create(:repository, name: "snapshot", remote_project_name: "openSUSE:Factory", project: remote_project) }
let(:other_remote_distribution) { create(:repository, name: "standard", remote_project_name: "openSUSE:Leap:42.1", project: remote_project) }
let(:repository) { create(:repository, name: "openSUSE_Tumbleweed", project: project) }
let!(:path_element) { create(:path_element, parent_id: repository.id, repository_id: remote_distribution.id, position: 1)}

it { expect(project.has_distribution("openSUSE.org:openSUSE:Factory", "snapshot")).to be(true) }
it { expect(project.has_distribution("openSUSE.org:openSUSE:Leap:42.1", "standard")).to be(false) }
end

context "local distribution" do
let(:distribution) { create(:project, name: "BaseDistro2.0") }
let(:distribution_repository) { create(:repository, name: "BaseDistro2_repo", project: distribution) }
let(:other_distribution) { create(:project, name: "BaseDistro") }
let!(:other_distribution_repository) { create(:repository, name: "BaseDistro_repo", project: distribution) }
let(:repository) { create(:repository, name: "Base_repo", project: project) }
let!(:path_element) { create(:path_element, parent_id: repository.id, repository_id: distribution_repository.id, position: 1)}

it { expect(project.has_distribution("BaseDistro2.0", "BaseDistro2_repo")).to be(true) }
it { expect(project.has_distribution("BaseDistro", "BaseDistro_repo")).to be(false) }
end
end
end

0 comments on commit c047b82

Please sign in to comment.