diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2017-01-31 11:41:51 +0000 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2017-01-31 11:41:51 +0000 |
commit | 810979f02b72f68b805b2e0c4a067b36e77334b6 (patch) | |
tree | 74a1a5cc7d6887851d06ef5a44aa5afdbdf9d565 | |
parent | e4bcdddfa79d855e7f320a8d91e79e17d2e1bf5f (diff) | |
parent | e806bdaf6c3c212a711b5f573ca6bc97ffd50a80 (diff) | |
download | gitlab-ce-810979f02b72f68b805b2e0c4a067b36e77334b6.tar.gz |
Merge branch '26852-fix-slug-for-openshift' into 'master'
Avoid repeated dashes in $CI_ENVIRONMENT_SLUG
Closes #26852
See merge request !8638
-rw-r--r-- | app/models/environment.rb | 18 | ||||
-rw-r--r-- | changelogs/unreleased/26852-fix-slug-for-openshift.yml | 4 | ||||
-rw-r--r-- | db/migrate/20161207231626_add_environment_slug.rb | 18 | ||||
-rw-r--r-- | spec/models/environment_spec.rb | 5 |
4 files changed, 35 insertions, 10 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb index 652abf18a8a..577367f1eed 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -1,7 +1,8 @@ class Environment < ActiveRecord::Base # Used to generate random suffixes for the slug + LETTERS = 'a'..'z' NUMBERS = '0'..'9' - SUFFIX_CHARS = ('a'..'z').to_a + NUMBERS.to_a + SUFFIX_CHARS = LETTERS.to_a + NUMBERS.to_a belongs_to :project, required: true, validate: true @@ -148,17 +149,24 @@ class Environment < ActiveRecord::Base slugified = name.to_s.downcase.gsub(/[^a-z0-9]/, '-') # Must start with a letter - slugified = "env-" + slugified if NUMBERS.cover?(slugified[0]) + slugified = 'env-' + slugified unless LETTERS.cover?(slugified[0]) + + # Repeated dashes are invalid (OpenShift limitation) + slugified.gsub!(/\-+/, '-') # Maximum length: 24 characters (OpenShift limitation) slugified = slugified[0..23] - # Cannot end with a "-" character (Kubernetes label limitation) - slugified = slugified[0..-2] if slugified[-1] == "-" + # Cannot end with a dash (Kubernetes label limitation) + slugified.chop! if slugified.end_with?('-') # Add a random suffix, shortening the current string if necessary, if it # has been slugified. This ensures uniqueness. - slugified = slugified[0..16] + "-" + random_suffix if slugified != name + if slugified != name + slugified = slugified[0..16] + slugified << '-' unless slugified.end_with?('-') + slugified << random_suffix + end self.slug = slugified end diff --git a/changelogs/unreleased/26852-fix-slug-for-openshift.yml b/changelogs/unreleased/26852-fix-slug-for-openshift.yml new file mode 100644 index 00000000000..fb65b068b23 --- /dev/null +++ b/changelogs/unreleased/26852-fix-slug-for-openshift.yml @@ -0,0 +1,4 @@ +--- +title: Avoid repeated dashes in $CI_ENVIRONMENT_SLUG +merge_request: 8638 +author: diff --git a/db/migrate/20161207231626_add_environment_slug.rb b/db/migrate/20161207231626_add_environment_slug.rb index 7153e6a32b1..8e98ee5b9ba 100644 --- a/db/migrate/20161207231626_add_environment_slug.rb +++ b/db/migrate/20161207231626_add_environment_slug.rb @@ -8,8 +8,9 @@ class AddEnvironmentSlug < ActiveRecord::Migration DOWNTIME_REASON = 'Adding NOT NULL column environments.slug with dependent data' # Used to generate random suffixes for the slug + LETTERS = 'a'..'z' NUMBERS = '0'..'9' - SUFFIX_CHARS = ('a'..'z').to_a + NUMBERS.to_a + SUFFIX_CHARS = LETTERS.to_a + NUMBERS.to_a def up environments = Arel::Table.new(:environments) @@ -39,17 +40,24 @@ class AddEnvironmentSlug < ActiveRecord::Migration slugified = name.to_s.downcase.gsub(/[^a-z0-9]/, '-') # Must start with a letter - slugified = "env-" + slugified if NUMBERS.cover?(slugified[0]) + slugified = 'env-' + slugified unless LETTERS.cover?(slugified[0]) + + # Repeated dashes are invalid (OpenShift limitation) + slugified.gsub!(/\-+/, '-') # Maximum length: 24 characters (OpenShift limitation) slugified = slugified[0..23] - # Cannot end with a "-" character (Kubernetes label limitation) - slugified = slugified[0..-2] if slugified[-1] == "-" + # Cannot end with a dash (Kubernetes label limitation) + slugified.chop! if slugified.end_with?('-') # Add a random suffix, shortening the current string if necessary, if it # has been slugified. This ensures uniqueness. - slugified = slugified[0..16] + "-" + random_suffix if slugified != name + if slugified != name + slugified = slugified[0..16] + slugified << '-' unless slugified.end_with?('-') + slugified << random_suffix + end slugified end diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index b2e06541e66..eba392044bf 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -288,6 +288,11 @@ describe Environment, models: true do "1-foo" => "env-1-foo" + SUFFIX, "1/foo" => "env-1-foo" + SUFFIX, "foo-" => "foo" + SUFFIX, + "foo--bar" => "foo-bar" + SUFFIX, + "foo**bar" => "foo-bar" + SUFFIX, + "*-foo" => "env-foo" + SUFFIX, + "staging-12345678-" => "staging-12345678" + SUFFIX, + "staging-12345678-01234567" => "staging-12345678" + SUFFIX, }.each do |name, matcher| it "returns a slug matching #{matcher}, given #{name}" do slug = described_class.new(name: name).generate_slug |