Skip to content

Commit

Permalink
Rename Product#create_service! to Product#find_or_create_service! and…
Browse files Browse the repository at this point in the history
… RepositoryService#create_repository! to RepositoryService#update_or_create_repository for better readability of the code flow.
  • Loading branch information
digitaltom committed Jan 3, 2025
1 parent b2c7d1e commit b6d73c7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/services/repository_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rmt/cli/repos_custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lib/rmt/scc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/products.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

trait :with_service do
after :create do |product, _evaluator|
product.create_service!
product.find_or_create_service!
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -234,15 +234,15 @@
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

context 'when the matching service ID is free' do
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
Expand Down
18 changes: 9 additions & 9 deletions spec/services/repository_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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' }
Expand All @@ -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 }
Expand All @@ -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'
Expand All @@ -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/) }
Expand Down

0 comments on commit b6d73c7

Please sign in to comment.