diff options
author | Tiger <twatson@gitlab.com> | 2019-07-10 13:55:32 +1000 |
---|---|---|
committer | Tiger <twatson@gitlab.com> | 2019-07-19 11:33:07 +1000 |
commit | 41fc4d1e449dedbc417b3ee57ca9e1e8e77bd18c (patch) | |
tree | 48a089eec308c2e3f7fe2c87d0e7bd747565209c /app/models/environment.rb | |
parent | 9578b7e9926db5900b5be07f5699aac04e47c0c6 (diff) | |
download | gitlab-ce-41fc4d1e449dedbc417b3ee57ca9e1e8e77bd18c.tar.gz |
Introduce predictable environment slugs64295-predictable-environment-slugs
If an environment slug is predictable given only the environment
name, we can use the environment slug earlier in the CI variable
evaluation process as we don't have to wait for the environment
record itself to be persisted.
Diffstat (limited to 'app/models/environment.rb')
-rw-r--r-- | app/models/environment.rb | 47 |
1 files changed, 2 insertions, 45 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb index b8ee54c1696..392481ea0cc 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -4,11 +4,6 @@ class Environment < ApplicationRecord include Gitlab::Utils::StrongMemoize include ReactiveCaching - # Used to generate random suffixes for the slug - LETTERS = ('a'..'z').freeze - NUMBERS = ('0'..'9').freeze - SUFFIX_CHARS = LETTERS.to_a + NUMBERS.to_a - belongs_to :project, required: true has_many :deployments, -> { success }, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent @@ -203,40 +198,6 @@ class Environment < ApplicationRecord super.presence || generate_slug end - # An environment name is not necessarily suitable for use in URLs, DNS - # or other third-party contexts, so provide a slugified version. A slug has - # the following properties: - # * contains only lowercase letters (a-z), numbers (0-9), and '-' - # * begins with a letter - # * has a maximum length of 24 bytes (OpenShift limitation) - # * cannot end with `-` - def generate_slug - # Lowercase letters and numbers only - slugified = +name.to_s.downcase.gsub(/[^a-z0-9]/, '-') - - # Must start with a letter - 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 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. - if slugified != name - slugified = slugified[0..16] - slugified << '-' unless slugified.end_with?('-') - slugified << random_suffix - end - - self.slug = slugified - end - def external_url_for(path, commit_sha) return unless self.external_url @@ -274,11 +235,7 @@ class Environment < ApplicationRecord private - # Slugifying a name may remove the uniqueness guarantee afforded by it being - # based on name (which must be unique). To compensate, we add a random - # 6-byte suffix in those circumstances. This is not *guaranteed* uniqueness, - # but the chance of collisions is vanishingly small - def random_suffix - (0..5).map { SUFFIX_CHARS.sample }.join + def generate_slug + self.slug = Gitlab::Slug::Environment.new(name).generate end end |