Skip to content

Commit

Permalink
Merge branch 'detect-repo-type' into refactor_mirror_action
Browse files Browse the repository at this point in the history
  • Loading branch information
felixsch committed Jan 2, 2024
2 parents 35dbcac + 530de83 commit 76522ac
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
13 changes: 13 additions & 0 deletions app/services/repository_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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 }
Expand Down
13 changes: 3 additions & 10 deletions lib/rmt/scc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/suse/connect/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions spec/lib/rmt/cli/repos_custom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
14 changes: 13 additions & 1 deletion spec/lib/rmt/scc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { [] }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 76522ac

Please sign in to comment.