Skip to content

Commit

Permalink
[webui] Fix Internal Server Error when adding a new repository, issue…
Browse files Browse the repository at this point in the history
…#1259

This caused by an uncatched exception: Activerecord::RecordNotFound

Related erbit issue:
  https://errbit-opensuse.herokuapp.com/apps/5620ca2bdc71fa00b1000000/problems/562e1782f3fc2f0006000000
  • Loading branch information
bgeuken committed Nov 22, 2015
1 parent a135e89 commit 83a8ff8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/api/app/controllers/webui/project_controller.rb
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
28 changes: 28 additions & 0 deletions src/api/test/functional/webui/project_controller_test.rb
Expand Up @@ -17,6 +17,34 @@ 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
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 83a8ff8

Please sign in to comment.