Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove repo after sync #1268

Merged
merged 9 commits into from
Jan 13, 2025
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
21 changes: 19 additions & 2 deletions lib/rmt/scc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,30 @@ 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!
remove_obsolete_repositories(scc_api_client.list_repositories)
digitaltom marked this conversation as resolved.
Show resolved Hide resolved

update_subscriptions(scc_api_client.list_subscriptions)
end

def remove_obsolete_repositories(repos_data)
@logger.info _('Removing obsolete repositories')
scc_repo_ids = repos_data.pluck(:id)


# Find repositories in RMT that no longer exist in SCC
# Only consider repositories that have a non-null scc_id
repos_to_remove = Repository.where.not(scc_id: nil)
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
.where.not(scc_id: scc_repo_ids)
if repos_to_remove.any?
repos_to_remove.delete_all
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
@logger.info("Successfully removed #{repos_to_remove.count} obsolete repositories")
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
end
end

def export(path)
credentials_set? || (raise CredentialsError, 'SCC credentials not set.')

Expand Down Expand Up @@ -185,9 +202,9 @@ def create_product(item, root_product_id = nil, base_product = nil, recommended
end

def create_service(item, product)
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 6 additions & 0 deletions package/obs/rmt-server.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Fri Jan 03 13:06:20 UTC 2025 - Parag Jain <parag.jain@suse.com>

- Version 2.21
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
* Fix: Remove obsolete repositories from rmt during SCC sync (bsc#1232808)

-------------------------------------------------------------------
Mon Dec 23 08:03:56 UTC 2024 - Parag Jain <parag.jain@suse.com>

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
Loading