Skip to content

Commit

Permalink
Merge pull request #1388 from bgeuken/fix_issue_1377
Browse files Browse the repository at this point in the history
Fix issue 1259
  • Loading branch information
adrianschroeter committed Nov 24, 2015
2 parents b26564a + d9999a5 commit 3c32ffe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
32 changes: 20 additions & 12 deletions src/api/app/controllers/webui/project_controller.rb
Expand Up @@ -210,8 +210,8 @@ def linking_projects
render_dialog
end

# NOTE: This is a GET action, no data will be written
def add_repository_from_default_list
authorize @project, :update?
@distributions = {}
Distribution.all_including_remotes.each do |dis|
@distributions[dis['vendor']] ||= []
Expand Down Expand Up @@ -456,20 +456,28 @@ def save_distributions
params[:distributions] ||= []

distributions = Distribution.all_including_remotes.select{ |distribution| params[:distributions].include?(distribution['reponame']) }
ActiveRecord::Base.transaction do
distributions.each do |distribution|
repository = @project.repositories.new(name: distribution['reponame'])
target_repository = Repository.find_by_project_and_name(distribution['project'], distribution['repository'])
unless target_repository
raise Activerecord::RecordNotFound
end
repository.path_elements.new(link: target_repository, position: 1 )
distribution['architectures'].each_with_index do |architecture, index|
repository.repository_architectures.new(architecture: Architecture.archcache[architecture], position: index + 1)

begin
# FIXME: This should happen in the model
ActiveRecord::Base.transaction do
distributions.each do |distribution|
repository = @project.repositories.new(name: distribution['reponame'])
target_repository = Repository.find_by_project_and_name(distribution['project'], distribution['repository'])
unless target_repository
raise Activerecord::RecordNotFound
end
repository.path_elements.new(link: target_repository, position: 1 )
distribution['architectures'].each_with_index do |architecture, index|
repository.repository_architectures.new(architecture: Architecture.archcache[architecture], position: index + 1)
end
repository.save!
end
repository.save!
end
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, error: "Can't add repositories: #{e.message}"
return
end

# FIXME:
if params['images']
repository = @project.repositories.create!(name: 'images')
Expand Down
4 changes: 3 additions & 1 deletion src/api/app/models/project.rb
Expand Up @@ -162,7 +162,9 @@ def maintained_project_names
def has_distribution(project_name, repository)
project = Project.find_by(name: project_name)
return false unless project
repositories.joins(path_elements: :link).where(links_path_elements: {name: repository, db_project_id: project.id }).exists?

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

def number_of_build_problems
Expand Down
Expand Up @@ -16,7 +16,7 @@
<h3><%= sprite_tag("distributions-#{vendor.downcase}") %> <%= vendor -%> distributions</h3>
<p>
<% list.each do |distribution| %>
<% selected = @project.has_distribution(distribution['project'], distribution['repo']) %>
<% selected = @project.has_distribution(distribution['project'], distribution['repository']) %>
<span class="nowrap">
<%= check_box_tag 'distributions[]', distribution['reponame'], selected, :disabled => selected, :id => 'repo_' + distribution['reponame'], :class => 'repocheckbox' -%>
<label for="<%= "repo_#{distribution['reponame']}" %>"><%= distribution['name'] %></label>
Expand Down
33 changes: 33 additions & 0 deletions src/api/test/functional/webui/project_controller_test.rb
Expand Up @@ -17,6 +17,39 @@ class Webui::ProjectControllerTest < Webui::IntegrationTest
Ignore: package:bash
Ignore: package:cups'

def test_save_distributions
login_tom
visit "/project/add_repository_from_default_list/home:tom"
check("OBS Base 2.0")
find("#submitrepos").click
page.must_have_text "Successfully added repositories"
assert_equal "/project/repositories/home:tom", page.current_path

# Test that repository checkbox get's disabled
visit "/project/add_repository_from_default_list/home:tom"
assert find("input#repo_Base_repo").disabled?,
"Checkbox for 'OBS Base 2.0' should be disabled"
end

def test_save_distributions_with_existing_repository
login_tom

visit "/project/add_repository_from_default_list/home:tom"
check("OBS Base 2.0")

# Fake that the project got added meanwhile, eg. when a user has a second screen open
project = Project.find_by(name: "home:tom")
repository = Repository.create(db_project_id: project.id, name: "Base_repo")
repository.path_elements.create(link: repository, position: 1)

check("OBS Base 2.0")
find("#submitrepos").click
page.must_have_text "Can't add repositories: Validation failed: Project already has repository with name Base_repo"
assert_equal "/project/add_repository_from_default_list/home:tom", page.current_path
ensure
repository.destroy if defined?(:repository)
end

def test_project_show
use_js
visit project_show_path(project: 'Apache')
Expand Down

0 comments on commit 3c32ffe

Please sign in to comment.