diff options
author | Nick Thomas <nick@gitlab.com> | 2017-01-19 14:42:03 +0000 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2017-01-23 13:44:17 +0000 |
commit | e806bdaf6c3c212a711b5f573ca6bc97ffd50a80 (patch) | |
tree | f20904b252d8788c60ecd3a8212cd098a474d44b /db | |
parent | 2de5f511d153ae33a4ed6c21107cb21226ee9582 (diff) | |
download | gitlab-ce-e806bdaf6c3c212a711b5f573ca6bc97ffd50a80.tar.gz |
Avoid repeated dashes in $CI_ENVIRONMENT_SLUG
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20161207231626_add_environment_slug.rb | 18 |
1 files changed, 13 insertions, 5 deletions
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 |