From b6d73c78d751a18144817875c4d30a2283201b96 Mon Sep 17 00:00:00 2001 From: Thomas Schmidt Date: Fri, 3 Jan 2025 15:02:44 +0100 Subject: [PATCH] Rename Product#create_service! to Product#find_or_create_service! and RepositoryService#create_repository! to RepositoryService#update_or_create_repository for better readability of the code flow. --- app/models/product.rb | 2 +- app/services/repository_service.rb | 2 +- lib/rmt/cli/repos_custom.rb | 2 +- lib/rmt/scc.rb | 5 +++-- spec/factories/products.rb | 2 +- spec/models/product_spec.rb | 8 ++++---- spec/services/repository_service_spec.rb | 18 +++++++++--------- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/app/models/product.rb b/app/models/product.rb index 8f65f7f60..91bf4d483 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -153,7 +153,7 @@ def self.recommended_extensions(root_product_ids) joins(:product_extensions_associations).where(products_extensions: { recommended: true, root_product_id: root_product_ids }) end - def create_service! + def find_or_create_service! service = Service.find_by(product_id: id) return service if service diff --git a/app/services/repository_service.rb b/app/services/repository_service.rb index d967feb7b..7f5ebe7ca 100644 --- a/app/services/repository_service.rb +++ b/app/services/repository_service.rb @@ -3,7 +3,7 @@ class RepositoryService class RepositoryNotFound < RuntimeError end - def create_repository!(product, url, attributes, custom: false) + def update_or_create_repository!(product, url, attributes, custom: false) repository = if custom Repository.find_or_initialize_by(external_url: url) else diff --git a/lib/rmt/cli/repos_custom.rb b/lib/rmt/cli/repos_custom.rb index cccf333ea..482626fe1 100644 --- a/lib/rmt/cli/repos_custom.rb +++ b/lib/rmt/cli/repos_custom.rb @@ -35,7 +35,7 @@ def add(url, name) raise RMT::CLI::Error.new(_("Couldn't add custom repository.")) end - repository_service.create_repository!(nil, url, { + repository_service.update_or_create_repository!(nil, url, { name: name.strip, mirroring_enabled: true, autorefresh: true, diff --git a/lib/rmt/scc.rb b/lib/rmt/scc.rb index 0faccfe65..6660b988b 100644 --- a/lib/rmt/scc.rb +++ b/lib/rmt/scc.rb @@ -23,6 +23,7 @@ def sync data.each { |item| create_product(item) } data.each { |item| migration_paths(item) } + # Update repositories with details (eg. access token) from API update_repositories(scc_api_client.list_repositories) Repository.remove_suse_repos_without_tokens! @@ -201,9 +202,9 @@ def create_product(item, root_product_id = nil, base_product = nil, recommended end def create_service(item, product) - product.create_service! + product.find_or_create_service! item[:repositories].each do |repo_item| - repository_service.create_repository!(product, repo_item[:url], repo_item) + repository_service.update_or_create_repository!(product, repo_item[:url], repo_item) end end diff --git a/spec/factories/products.rb b/spec/factories/products.rb index 4612dfb00..8e2120740 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -117,7 +117,7 @@ trait :with_service do after :create do |product, _evaluator| - product.create_service! + product.find_or_create_service! end end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 5dc3771b3..56dea3964 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -218,13 +218,13 @@ end end - describe '#create_service!' do + describe '#find_or_create_service!' do context 'when service already exists' do let!(:product) { create :product } let!(:service) { create :service, product_id: product.id } it 'returns the existing service' do - expect(product.create_service!).to eq(service) + expect(product.find_or_create_service!).to eq(service) end end @@ -234,7 +234,7 @@ let!(:other_service) { create :service, id: product.id, product_id: other_product.id } it 'creates a service with a random ID' do - expect(product.create_service!.id).not_to eq(other_service.id) + expect(product.find_or_create_service!.id).not_to eq(other_service.id) end end @@ -242,7 +242,7 @@ let!(:product) { create :product } it 'creates a service with a matching ID' do - expect(product.create_service!.id).to eq(product.id) + expect(product.find_or_create_service!.id).to eq(product.id) end end end diff --git a/spec/services/repository_service_spec.rb b/spec/services/repository_service_spec.rb index 0035395b5..d028aa837 100644 --- a/spec/services/repository_service_spec.rb +++ b/spec/services/repository_service_spec.rb @@ -6,7 +6,7 @@ let(:product) { create :product, :with_service } describe '#create_repository' do - subject(:repository) { service.create_repository!(product, url, attributes, custom: custom).reload } + subject(:repository) { service.update_or_create_repository!(product, url, attributes, custom: custom).reload } let(:attributes) do { @@ -38,10 +38,10 @@ context 'URLs of SCC repositories changes' do subject(:repository) do - service.create_repository!(product, old_url, attributes, custom: custom) + service.update_or_create_repository!(product, old_url, attributes, custom: custom) expect(Repository.find_by(external_url: old_url)).not_to eq(nil) - service.create_repository!(product, url, attributes, custom: custom).reload + service.update_or_create_repository!(product, url, attributes, custom: custom).reload end let(:old_url) { 'https://foo.bar.com/bar/foo' } @@ -53,10 +53,10 @@ context 'self heals SCC repos' do subject(:repository) do - service.create_repository!(product, url, attributes, custom: custom).update(scc_id: old_scc_id) + service.update_or_create_repository!(product, url, attributes, custom: custom).update(scc_id: old_scc_id) expect(Repository.find_by(scc_id: old_scc_id)).not_to eq(nil) - service.create_repository!(product, url, attributes, custom: custom).reload + service.update_or_create_repository!(product, url, attributes, custom: custom).reload end let(:old_scc_id) { 666 } @@ -68,8 +68,8 @@ context 'custom repo with same url' do subject(:repository) do - service.create_repository!(product, url, attributes, custom: custom).update(scc_id: nil) - service.create_repository!(product, url, attributes, custom: custom).reload + service.update_or_create_repository!(product, url, attributes, custom: custom).update(scc_id: nil) + service.update_or_create_repository!(product, url, attributes, custom: custom).reload end it_behaves_like 'scc repositories' @@ -90,9 +90,9 @@ context 'already existing repositories with changing URL', :skip_sqlite do subject(:repository) do - service.create_repository!(product, url, attributes, custom: custom).reload + service.update_or_create_repository!(product, url, attributes, custom: custom).reload url = 'https://foo.bar.com/bar/foo' - service.create_repository!(product, url, attributes, custom: custom).reload + service.update_or_create_repository!(product, url, attributes, custom: custom).reload end it('raises error when the id is the same') { expect { repository }.to raise_error(/Duplicate entry/) }