From 1826d3e2c6afdbb76fae356e9b67eb5d4c481aa4 Mon Sep 17 00:00:00 2001 From: Thomas Schmidt Date: Thu, 21 Dec 2023 14:54:28 +0100 Subject: [PATCH 1/3] Include id of existing repository in error message --- lib/rmt/cli/repos_custom.rb | 2 +- spec/lib/rmt/cli/repos_custom_spec.rb | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/rmt/cli/repos_custom.rb b/lib/rmt/cli/repos_custom.rb index cbc7848ba..cccf333ea 100644 --- a/lib/rmt/cli/repos_custom.rb +++ b/lib/rmt/cli/repos_custom.rb @@ -21,7 +21,7 @@ def add(url, name) error = nil if Repository.find_by(external_url: url) - error = _('A repository by the URL %{url} already exists.') % { url: url } + error = _('A repository by the URL %{url} already exists (ID %{id}).') % { url: url, id: Repository.find_by(external_url: url).friendly_id } elsif Repository.find_by(friendly_id: options.id.to_s) # When given an ID by a user, don't append to it to make a unique ID. error = _('A repository by the ID %{id} already exists.') % { id: friendly_id } diff --git a/spec/lib/rmt/cli/repos_custom_spec.rb b/spec/lib/rmt/cli/repos_custom_spec.rb index b241f0c2b..165bc3a4f 100644 --- a/spec/lib/rmt/cli/repos_custom_spec.rb +++ b/spec/lib/rmt/cli/repos_custom_spec.rb @@ -104,10 +104,10 @@ it 'does not update previous repository if non-custom' do expect(described_class).to receive(:exit) + existing_repo = create :repository, external_url: external_url, name: 'foobar' expect do - create :repository, external_url: external_url, name: 'foobar' described_class.start(argv) - end.to output("\e[31mA repository by the URL #{external_url} already exists.\e[0m\nCouldn't add custom repository.\n") + end.to output("\e[31mA repository by the URL #{external_url} already exists (ID #{existing_repo.friendly_id}).\e[0m\nCouldn't add custom repository.\n") .to_stderr .and output('').to_stdout expect(Repository.find_by(external_url: external_url).name).to eq('foobar') @@ -119,20 +119,21 @@ expect do described_class.start(%w[add http://example.com/repo/ foo]) end.to output("Successfully added custom repository.\n").to_stdout.and output('').to_stderr - + existing_repo = Repository.find_by(external_url: 'http://example.com/repo/') expect do described_class.start(%w[add http://example.com/repo foo]) - end.to output("\e[31mA repository by the URL http://example.com/repo/ already exists.\e[0m\nCouldn't add custom repository.\n") + end.to output("\e[31mA repository by the URL http://example.com/repo/ already exists (ID #{existing_repo.friendly_id})." \ + "\e[0m\nCouldn't add custom repository.\n") .to_stderr .and output('').to_stdout end it 'does not update previous repository if custom' do expect(described_class).to receive(:exit) + existing_repo = create :repository, :custom, external_url: external_url, name: 'foobar' expect do - create :repository, :custom, external_url: external_url, name: 'foobar' described_class.start(argv) - end.to output("\e[31mA repository by the URL #{external_url} already exists.\e[0m\nCouldn't add custom repository.\n") + end.to output("\e[31mA repository by the URL #{external_url} already exists (ID #{existing_repo.friendly_id}).\e[0m\nCouldn't add custom repository.\n") .to_stderr .and output('').to_stdout expect(Repository.find_by(external_url: external_url).name).to eq('foobar') From c890d5ff86659e4c4860bc7d791f785e23afbbfe Mon Sep 17 00:00:00 2001 From: Thomas Schmidt Date: Thu, 21 Dec 2023 14:55:23 +0100 Subject: [PATCH 2/3] add logs when loading data from SCC --- lib/suse/connect/api.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/suse/connect/api.rb b/lib/suse/connect/api.rb index a399e6c5c..f45f65501 100644 --- a/lib/suse/connect/api.rb +++ b/lib/suse/connect/api.rb @@ -45,18 +45,22 @@ def list_orders end def list_products + @logger.info(_('Loading product data from SCC')) make_paginated_request(:get, "#{connect_api}/organizations/products") end def list_products_unscoped + @logger.info(_('Loading product data from SCC')) make_paginated_request(:get, "#{connect_api}/organizations/products/unscoped") end def list_repositories + @logger.info(_('Loading repository data from SCC')) make_paginated_request(:get, "#{connect_api}/organizations/repositories") end def list_subscriptions + @logger.info(_('Loading subscription data from SCC')) make_paginated_request(:get, "#{connect_api}/organizations/subscriptions") end From 1356c658e16445045e1b6ac05b24d519cedd5711 Mon Sep 17 00:00:00 2001 From: Thomas Schmidt Date: Thu, 21 Dec 2023 15:19:05 +0100 Subject: [PATCH 3/3] update repo url from scc data on sync (bsc#1218243) --- app/services/repository_service.rb | 13 +++++++++++++ lib/rmt/scc.rb | 13 +++---------- spec/lib/rmt/scc_spec.rb | 14 +++++++++++++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/services/repository_service.rb b/app/services/repository_service.rb index 8e4dfdfe2..d967feb7b 100644 --- a/app/services/repository_service.rb +++ b/app/services/repository_service.rb @@ -37,6 +37,19 @@ def create_repository!(product, url, attributes, custom: false) repository end + def update_repository!(repo_data) + uri = URI(repo_data[:url]) + auth_token = uri.query + + Repository.find_by!(scc_id: repo_data[:id]).update!( + auth_token: auth_token, + enabled: repo_data[:enabled], + autorefresh: repo_data[:autorefresh], + external_url: "#{uri.scheme}://#{uri.host}#{uri.path}", + local_path: Repository.make_local_path(uri) + ) + end + def attach_product!(product, repository) RepositoriesServicesAssociation.find_or_create_by!( service_id: product.service.id, diff --git a/lib/rmt/scc.rb b/lib/rmt/scc.rb index 05a5595c1..1ba9158ff 100644 --- a/lib/rmt/scc.rb +++ b/lib/rmt/scc.rb @@ -18,8 +18,8 @@ def sync @logger.info(_('Downloading data from SCC')) scc_api_client = SUSE::Connect::Api.new(Settings.scc.username, Settings.scc.password) - @logger.info(_('Updating products')) data = scc_api_client.list_products + @logger.info(_('Updating products')) data.each { |item| create_product(item) } data.each { |item| migration_paths(item) } @@ -132,8 +132,8 @@ def credentials_set? def update_repositories(repos) @logger.info _('Updating repositories') - repos.each do |item| - update_auth_token_enabled_attr(item) + repos.each do |repo| + repository_service.update_repository!(repo) end end @@ -191,13 +191,6 @@ def create_service(item, product) end end - def update_auth_token_enabled_attr(item) - uri = URI(item[:url]) - auth_token = uri.query - - Repository.find_by!(scc_id: item[:id]).update! auth_token: auth_token, enabled: item[:enabled] - end - def migration_paths(item) product = get_product(item[:id]) ProductPredecessorAssociation.where(product_id: product.id).destroy_all diff --git a/spec/lib/rmt/scc_spec.rb b/spec/lib/rmt/scc_spec.rb index 04d062d7b..a4732b0cd 100644 --- a/spec/lib/rmt/scc_spec.rb +++ b/spec/lib/rmt/scc_spec.rb @@ -113,6 +113,17 @@ include_examples 'saves in database' end + context 'with changed repo url in SCC' do + before do + allow(Settings).to receive(:scc).and_return OpenStruct.new(username: 'foo', password: 'bar') + described_class.new.sync + Repository.first.update(external_url: 'https://outdated.com/') + described_class.new.sync + end + + include_examples 'saves in database' + end + context 'with SLES15 product tree' do let(:products) { JSON.parse(file_fixture('products/sle15_tree.json').read, symbolize_names: true) } let(:subscriptions) { [] } @@ -155,7 +166,8 @@ id: 999999, url: 'http://example.com/extension-without-base', name: 'Repo of an extension without base', - enabled: true + enabled: true, + autorefresh: true } end let(:extra_product) do