diff options
author | Rémy Coutable <remy@rymai.me> | 2017-08-16 10:10:24 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-08-16 10:10:24 +0000 |
commit | 1b46a8360e0adffba79fad5730e94e333350f063 (patch) | |
tree | 73db4cf8ef38d8b99c345264901c44744e8a9806 | |
parent | 765b1831687aac0a70819793890ac0e1ca5ae63f (diff) | |
parent | e99444bb2d3a249461825550fc6271f4e0ee8874 (diff) | |
download | gitlab-ce-1b46a8360e0adffba79fad5730e94e333350f063.tar.gz |
Merge branch '34643-fix-project-path-slugify' into 'master'
Fix CI_PROJECT_PATH_SLUG slugify
Closes #34643
See merge request !13350
-rw-r--r-- | app/models/ci/build.rb | 5 | ||||
-rw-r--r-- | app/models/project.rb | 6 | ||||
-rw-r--r-- | changelogs/unreleased/34643-fix-project-path-slugify.yml | 4 | ||||
-rw-r--r-- | lib/gitlab/utils.rb | 13 | ||||
-rw-r--r-- | spec/lib/gitlab/utils_spec.rb | 16 | ||||
-rw-r--r-- | spec/models/ci/build_spec.rb | 2 |
6 files changed, 39 insertions, 7 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 8be2dee6479..4692fb5644a 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -194,10 +194,7 @@ module Ci # * Maximum length is 63 bytes # * First/Last Character is not a hyphen def ref_slug - ref.to_s - .downcase - .gsub(/[^a-z0-9]/, '-')[0..62] - .gsub(/(\A-+|-+\z)/, '') + Gitlab::Utils.slugify(ref.to_s) end # Variables whose value does not depend on environment diff --git a/app/models/project.rb b/app/models/project.rb index 0de7da0ddaa..0d15d644413 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1282,12 +1282,16 @@ class Project < ActiveRecord::Base status.zero? end + def full_path_slug + Gitlab::Utils.slugify(full_path.to_s) + end + def predefined_variables [ { key: 'CI_PROJECT_ID', value: id.to_s, public: true }, { key: 'CI_PROJECT_NAME', value: path, public: true }, { key: 'CI_PROJECT_PATH', value: full_path, public: true }, - { key: 'CI_PROJECT_PATH_SLUG', value: full_path.parameterize, public: true }, + { key: 'CI_PROJECT_PATH_SLUG', value: full_path_slug, public: true }, { key: 'CI_PROJECT_NAMESPACE', value: namespace.full_path, public: true }, { key: 'CI_PROJECT_URL', value: web_url, public: true } ] diff --git a/changelogs/unreleased/34643-fix-project-path-slugify.yml b/changelogs/unreleased/34643-fix-project-path-slugify.yml new file mode 100644 index 00000000000..f7018a1aca5 --- /dev/null +++ b/changelogs/unreleased/34643-fix-project-path-slugify.yml @@ -0,0 +1,4 @@ +--- +title: Fix CI_PROJECT_PATH_SLUG slugify +merge_request: 13350 +author: Ivan Chernov diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb index fa182c4deda..9670c93759e 100644 --- a/lib/gitlab/utils.rb +++ b/lib/gitlab/utils.rb @@ -14,6 +14,19 @@ module Gitlab str.force_encoding(Encoding::UTF_8) end + # A slugified version of the string, suitable for inclusion in URLs and + # domain names. Rules: + # + # * Lowercased + # * Anything not matching [a-z0-9-] is replaced with a - + # * Maximum length is 63 bytes + # * First/Last Character is not a hyphen + def slugify(str) + return str.downcase + .gsub(/[^a-z0-9]/, '-')[0..62] + .gsub(/(\A-+|-+\z)/, '') + end + def to_boolean(value) return value if [true, false].include?(value) return true if value =~ /^(true|t|yes|y|1|on)$/i diff --git a/spec/lib/gitlab/utils_spec.rb b/spec/lib/gitlab/utils_spec.rb index 111c873f79c..92787bb262e 100644 --- a/spec/lib/gitlab/utils_spec.rb +++ b/spec/lib/gitlab/utils_spec.rb @@ -1,7 +1,21 @@ require 'spec_helper' describe Gitlab::Utils do - delegate :to_boolean, :boolean_to_yes_no, to: :described_class + delegate :to_boolean, :boolean_to_yes_no, :slugify, to: :described_class + + describe '.slugify' do + { + 'TEST' => 'test', + 'project_with_underscores' => 'project-with-underscores', + 'namespace/project' => 'namespace-project', + 'a' * 70 => 'a' * 63, + 'test_trailing_' => 'test-trailing' + }.each do |original, expected| + it "slugifies #{original} to #{expected}" do + expect(slugify(original)).to eq(expected) + end + end + end describe '.to_boolean' do it 'accepts booleans' do diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 86afa856ea7..767f0ad9e65 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1220,7 +1220,7 @@ describe Ci::Build do { key: 'CI_PROJECT_ID', value: project.id.to_s, public: true }, { key: 'CI_PROJECT_NAME', value: project.path, public: true }, { key: 'CI_PROJECT_PATH', value: project.full_path, public: true }, - { key: 'CI_PROJECT_PATH_SLUG', value: project.full_path.parameterize, public: true }, + { key: 'CI_PROJECT_PATH_SLUG', value: project.full_path_slug, public: true }, { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.full_path, public: true }, { key: 'CI_PROJECT_URL', value: project.web_url, public: true }, { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }, |