diff --git a/ee/app/models/software_license.rb b/ee/app/models/software_license.rb index a74b54ce8c7a4d43faabded3a87b83d97585fe47..4e0c36fdf99b87df83f58a4bb905b372c3c97922 100644 --- a/ee/app/models/software_license.rb +++ b/ee/app/models/software_license.rb @@ -8,6 +8,7 @@ class SoftwareLicense < ApplicationRecord TransactionInProgressError = Class.new(StandardError) ALL_LICENSE_NAMES_CACHE_KEY = [name, 'all_license_names'].freeze TRANSACTION_MESSAGE = "Sub-transactions are not allowed as there is already an open transaction." + LICENSE_LIMIT = 1_000 validates :name, presence: true, uniqueness: true validates :spdx_identifier, length: { maximum: 255 } @@ -18,13 +19,9 @@ class SoftwareLicense < ApplicationRecord scope :spdx, -> { where.not(spdx_identifier: nil) } scope :unknown, -> { where(spdx_identifier: nil) } scope :grouped_by_name, -> { group(:name) } - scope :unreachable_limit, -> { limit(500) } + scope :unreachable_limit, -> { limit(LICENSE_LIMIT) } class << self - def unclassified_licenses_for(project) - spdx.id_not_in(project.software_licenses).ordered.unreachable_limit - end - def all_license_names Rails.cache.fetch(ALL_LICENSE_NAMES_CACHE_KEY, expires_in: 7.days) do spdx.ordered.unreachable_limit.pluck_names diff --git a/ee/spec/models/software_license_spec.rb b/ee/spec/models/software_license_spec.rb index f5b748837460a0df8c7bb3257aa65217373e15cf..e85eb460b8ccafc8f05a316af73b16862e060515 100644 --- a/ee/spec/models/software_license_spec.rb +++ b/ee/spec/models/software_license_spec.rb @@ -74,29 +74,6 @@ end end - describe '.unclassified_licenses_for' do - subject { described_class.unclassified_licenses_for(project) } - - let_it_be(:mit_license) { create(:software_license, :mit) } - let_it_be(:apache_license) { create(:software_license, :apache_2_0) } - let_it_be(:nonstandard_license) { create(:software_license, :user_entered) } - let_it_be(:project) { create(:project) } - - context 'when a project has not classified licenses' do - it 'returns each license in the SPDX catalogue ordered by name' do - expect(subject.to_a).to eql([apache_license, mit_license]) - end - end - - context 'when some of the licenses are classified' do - let_it_be(:mit_policy) { create(:software_license_policy, project: project, software_license: mit_license) } - - it 'returns each license in the SPDX catalogue that has not been classified' do - expect(subject).to contain_exactly(apache_license) - end - end - end - describe '.all_license_names' do subject { described_class.all_license_names } @@ -113,5 +90,15 @@ subject end + + context 'when the number of spdx licenses exceeds the limit' do + before do + stub_const("#{described_class}::LICENSE_LIMIT", 1) + end + + it 'returns ordered list of license names from the SPDX catalogue within the limit' do + expect(subject.to_a).to eql([apache_license.name]) + end + end end end