OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
allow project identifier to begin with numbers (#9377)
ulferts9e66604dec77
diff --git a/app/models/project.rb b/app/models/project.rbindex 53c4b999ca9..6534796cbb5 100644--- a/app/models/project.rb+++ b/app/models/project.rb@@ -118,10 +118,6 @@ class Project < ApplicationRecordonly_when_blank: true, # Only generate when identifier not setlimit: IDENTIFIER_MAX_LENGTH,blacklist: RESERVED_IDENTIFIERS,- custom_rule: ->(base_url) {- # remove leading numbers and hyphens as they would clash with the identifier's validations later on.- base_url.sub(/^[-\d]*|-*$/, '')- },adapter: OpenProject::ActsAsUrl::Adapter::OpActiveRecord # use a custom adapter able to handle edge casesvalidates :identifier,@@ -131,12 +127,12 @@ class Project < ApplicationRecordexclusion: RESERVED_IDENTIFIERS,if: ->(p) { p.persisted? || p.identifier.present? }- validates_associated :repository, :wiki- # starts with lower-case letter, a-z, 0-9, dashes and underscores afterwards+ # Contains only a-z, 0-9, dashes and underscores but cannot consist of numbers only as it would clash with the id.validates :identifier,- format: { with: /\A[a-z][a-z0-9\-_]*\z/ },+ format: { with: /\A(?!^\d+\z)[a-z0-9\-_]+\z/ },if: ->(p) { p.identifier_changed? && p.identifier.present? }- # reserved words++ validates_associated :repository, :wikifriendly_id :identifier, use: :findersdiff --git a/lib/open_project/acts_as_url/adapter/op_active_record.rb b/lib/open_project/acts_as_url/adapter/op_active_record.rbindex 01992eca5f6..78e56394b56 100644--- a/lib/open_project/acts_as_url/adapter/op_active_record.rb+++ b/lib/open_project/acts_as_url/adapter/op_active_record.rb@@ -33,8 +33,6 @@## This includes# * the strings '.' and '!' which would lead to an empty string otherwise-# * the ability to add a custom_rule lambda that is able to postprocess the identifier. It will run-# after the default transformation was executed.module OpenProjectmodule ActsAsUrl@@ -60,10 +58,6 @@ module OpenProjectdef modify_base_urlsuper- if !base_url.empty? && settings.respond_to?(:custom_rule)- self.base_url = settings.custom_rule.call(base_url)- end-modify_base_url_custom_rules if base_url.empty?enddiff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rbindex e457b002055..dcf296f4e37 100644--- a/spec/contracts/projects/shared_contract_examples.rb+++ b/spec/contracts/projects/shared_contract_examples.rb@@ -150,6 +150,58 @@ shared_examples_for 'project contract' doendend+ context 'when the identifier consists of only letters' do+ let(:project_identifier) { 'abc' }++ it_behaves_like 'is valid'+ end++ context 'when the identifier consists of letters followed by numbers' do+ let(:project_identifier) { 'abc12' }++ it_behaves_like 'is valid'+ end++ context 'when the identifier consists of letters followed by numbers with a hyphen in between' do+ let(:project_identifier) { 'abc-12' }++ it_behaves_like 'is valid'+ end++ context 'when the identifier consists of letters followed by numbers with an underscore in between' do+ let(:project_identifier) { 'abc_12' }++ it_behaves_like 'is valid'+ end++ context 'when the identifier consists of numbers followed by letters with a hyphen in between' do+ let(:project_identifier) { '12-abc' }++ it_behaves_like 'is valid'+ end++ context 'when the identifier consists of numbers followed by letters with an underscore in between' do+ let(:project_identifier) { '12_abc' }++ it_behaves_like 'is valid'+ end++ context 'when the identifier consists of only numbers' do+ let(:project_identifier) { '12' }++ it 'is invalid' do+ expect_valid(false, identifier: %i(invalid))+ end+ end++ context 'when the identifier consists of a reserved word' do+ let(:project_identifier) { 'new' }++ it 'is invalid' do+ expect_valid(false, identifier: %i(exclusion))+ end+ end+context 'if the user lacks permission' dolet(:permissions) { [] }diff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rbindex 7168c2cc010..ac24fb02208 100644--- a/spec/contracts/projects/update_contract_spec.rb+++ b/spec/contracts/projects/update_contract_spec.rb@@ -33,13 +33,13 @@ describe Projects::UpdateContract doit_behaves_like 'project contract' dolet(:project) doFactoryBot.build_stubbed(:project,- identifier: project_identifier,active: project_active,public: project_public,status: project_status).tap do |p|# in order to actually have something changedp.name = project_namep.parent = project_parent+ p.identifier = project_identifierendendlet(:permissions) { [:edit_project] }diff --git a/spec/services/projects/set_attributes_service_integration_spec.rb b/spec/services/projects/set_attributes_service_integration_spec.rbindex f15674f9d02..faae0ad2fd8 100644--- a/spec/services/projects/set_attributes_service_integration_spec.rb+++ b/spec/services/projects/set_attributes_service_integration_spec.rb@@ -43,9 +43,9 @@ describe Projects::SetAttributesService, 'integration', type: :model dodescribe 'with a project name starting with numbers' dolet(:attributes) { { name: '100 Project A' } }- it 'will create an identifier with the numbers stripped' do+ it 'will create an identifier including the numbers' doexpect(service_result).to be_success- expect(service_result.result.identifier).to eq 'project-a'+ expect(service_result.result.identifier).to eq '100-project-a'endend@@ -73,14 +73,5 @@ describe Projects::SetAttributesService, 'integration', type: :model doexpect(errors).to eq ['Identifier has already been taken.']endend-- context 'with an existing identifier and a project name starting with numbers' do- let(:attributes) { { name: '100 My new project' } }-- it 'will auto correct the identifier with the numbers stripped' do- expect(service_result).to be_success- expect(service_result.result.identifier).to eq 'my-new-project-1'- end- endendenddiff --git a/spec_legacy/unit/project_spec.rb b/spec_legacy/unit/project_spec.rbindex aa4f88f919b..d618d5f9c11 100644--- a/spec_legacy/unit/project_spec.rb+++ b/spec_legacy/unit/project_spec.rb@@ -76,22 +76,6 @@ describe Project, type: :model doassert_equal 'eCook', @ecookbook.nameend- it 'should validate identifier' do- to_test = { 'abc' => true,- 'ab12' => true,- 'ab-12' => true,- 'ab_12' => true,- '12' => false,- 'new' => false }-- to_test.each do |identifier, valid|- p = Project.new- p.identifier = identifier- p.valid?- assert_equal valid, p.errors['identifier'].empty?- end- end-it 'should members should be active users' doProject.all.each do |project|assert_nil project.members.detect { |m| !(m.principal.is_a?(User) && m.principal.active?) }