From 1d2a5ee1887bd767ede8c84babb1868954c38e6f Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 11:52:54 +0200 Subject: Revert "Revert disallowing usernames to end in period." This reverts commit c75c6b840ba9ed82bb01eca52e968c2b0ec985e6. --- CHANGELOG | 1 + ...24133047_remove_periods_at_ends_of_usernames.rb | 76 ++++++++++++++++++++++ lib/gitlab/regex.rb | 4 +- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb diff --git a/CHANGELOG b/CHANGELOG index 0d6b0f2d942..47eb152ad73 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -97,6 +97,7 @@ v 7.10.0 (unreleased) - Fix admin user projects lists. - Don't leak private group existence by redirecting from namespace controller to group controller. - Ability to skip some items from backup (database, respositories or uploads) + - Fix "Hello @username." references not working by no longer allowing usernames to end in period. - Archive repositories in background worker. - Import GitHub, Bitbucket or GitLab.com projects owned by authenticated user into current namespace. - Project labels are now available over the API under the "tag_list" field (Cristian Medina) diff --git a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb new file mode 100644 index 00000000000..dc38b0eceb7 --- /dev/null +++ b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb @@ -0,0 +1,76 @@ +class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration + include Gitlab::ShellAdapter + + class Namespace < ActiveRecord::Base + class << self + def find_by_path_or_name(path) + find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) + end + + def clean_path(path) + path = path.dup + path.gsub!(/@.*\z/, "") + path.gsub!(/\.git\z/, "") + path.gsub!(/\A-+/, "") + path.gsub!(/\.+\z/, "") + path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") + + counter = 0 + base = path + while Namespace.find_by_path_or_name(path) + counter += 1 + path = "#{base}#{counter}" + end + + path + end + end + end + + def up + changed_paths = {} + + select_all("SELECT id, username FROM users WHERE username LIKE '%.'").each do |user| + username_was = user["username"] + username = Namespace.clean_path(username_was) + changed_paths[username_was] = username + + username = quote_string(username) + execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}" + execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type IS NULL AND owner_id = #{user["id"]}" + end + + select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group| + path_was = group["path"] + path = Namespace.clean_path(path_was) + changed_paths[path_was] = path + + path = quote_string(path) + execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{group["id"]}" + end + + changed_paths.each do |path_was, path| + if gitlab_shell.mv_namespace(path_was, path) + # If repositories moved successfully we need to remove old satellites + # and send update instructions to users. + # However we cannot allow rollback since we moved namespace dir + # So we basically we mute exceptions in next actions + begin + gitlab_shell.rm_satellites(path_was) + # We cannot send update instructions since models and mailers + # can't safely be used from migrations as they may be written for + # later versions of the database. + # send_update_instructions + rescue + # Returning false does not rollback after_* transaction but gives + # us information about failing some of tasks + false + end + else + # if we cannot move namespace directory we should rollback + # db changes in order to prevent out of sync between db and fs + raise Exception.new('namespace directory cannot be moved') + end + end + end +end diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index 9aeed5e6939..0571574aa4f 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -2,7 +2,7 @@ module Gitlab module Regex extend self - NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*)'.freeze + NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze def namespace_regex @namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze @@ -10,7 +10,7 @@ module Gitlab def namespace_regex_message "can contain only letters, digits, '_', '-' and '.'. " \ - "Cannot start with '-'." \ + "Cannot start with '-' or end in '.'." \ end -- cgit v1.2.1 From 164a29df86f70910ac0bc6bd29f0fe6f4037b56e Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 11:53:32 +0200 Subject: Move changelog item tot 7.11. --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 47eb152ad73..e5c06776ecf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ v 7.11.0 (unreleased) - Ignore invalid lines in .gitmodules - Fix "Cannot move project" error message from popping up after a successful transfer (Stan Hu) - Redirect to sign in page after signing out. + - Fix "Hello @username." references not working by no longer allowing usernames to end in period. - - Add "Reply quoting selected text" shortcut key (`r`) - Fix bug causing `@whatever` inside an issue's first code block to be picked up as a user mention. @@ -97,7 +98,6 @@ v 7.10.0 (unreleased) - Fix admin user projects lists. - Don't leak private group existence by redirecting from namespace controller to group controller. - Ability to skip some items from backup (database, respositories or uploads) - - Fix "Hello @username." references not working by no longer allowing usernames to end in period. - Archive repositories in background worker. - Import GitHub, Bitbucket or GitLab.com projects owned by authenticated user into current namespace. - Project labels are now available over the API under the "tag_list" field (Cristian Medina) -- cgit v1.2.1 From c0116926c743818b2593474946abb40b56d8fefa Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 11:58:45 +0200 Subject: Rename namespace_regex to namespace_path_regex. --- app/models/namespace.rb | 4 ++-- app/models/user.rb | 4 ++-- lib/gitlab/markdown/cross_project_reference.rb | 2 +- lib/gitlab/markdown/user_reference_filter.rb | 2 +- lib/gitlab/reference_extractor.rb | 2 +- lib/gitlab/regex.rb | 8 ++++---- spec/requests/api/users_spec.rb | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/models/namespace.rb b/app/models/namespace.rb index e1de114375e..24363a853d8 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -33,8 +33,8 @@ class Namespace < ActiveRecord::Base presence: true, length: { within: 1..255 }, exclusion: { in: Gitlab::Blacklist.path }, - format: { with: Gitlab::Regex.namespace_regex, - message: Gitlab::Regex.namespace_regex_message } + format: { with: Gitlab::Regex.namespace_path_regex, + message: Gitlab::Regex.namespace_path_regex_message } delegate :name, to: :owner, allow_nil: true, prefix: true diff --git a/app/models/user.rb b/app/models/user.rb index d6b93afe739..7d7faa64f72 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -131,8 +131,8 @@ class User < ActiveRecord::Base presence: true, uniqueness: { case_sensitive: false }, exclusion: { in: Gitlab::Blacklist.path }, - format: { with: Gitlab::Regex.namespace_regex, - message: Gitlab::Regex.namespace_regex_message } + format: { with: Gitlab::Regex.namespace_path_regex, + message: Gitlab::Regex.namespace_path_regex_message } validates :notification_level, inclusion: { in: Notification.notification_levels }, presence: true validate :namespace_uniq, if: ->(user) { user.username_changed? } diff --git a/lib/gitlab/markdown/cross_project_reference.rb b/lib/gitlab/markdown/cross_project_reference.rb index c436fabd658..261fc0feb0e 100644 --- a/lib/gitlab/markdown/cross_project_reference.rb +++ b/lib/gitlab/markdown/cross_project_reference.rb @@ -3,7 +3,7 @@ module Gitlab # Common methods for ReferenceFilters that support an optional cross-project # reference. module CrossProjectReference - NAMING_PATTERN = Gitlab::Regex::NAMESPACE_REGEX_STR + NAMING_PATTERN = Gitlab::Regex::NAMESPACE_PATH_REGEX_STR PROJECT_PATTERN = "(?#{NAMING_PATTERN}/#{NAMING_PATTERN})" # Given a cross-project reference string, get the Project record diff --git a/lib/gitlab/markdown/user_reference_filter.rb b/lib/gitlab/markdown/user_reference_filter.rb index 5fc8ed55fe2..d7885fdeefd 100644 --- a/lib/gitlab/markdown/user_reference_filter.rb +++ b/lib/gitlab/markdown/user_reference_filter.rb @@ -22,7 +22,7 @@ module Gitlab end # Pattern used to extract `@user` user references from text - USER_PATTERN = /@(?#{Gitlab::Regex::NAMESPACE_REGEX_STR})/ + USER_PATTERN = /@(?#{Gitlab::Regex::NAMESPACE_PATH_REGEX_STR})/ def call replace_text_nodes_matching(USER_PATTERN) do |content| diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb index 949dd5d26b1..1d6df9a29f1 100644 --- a/lib/gitlab/reference_extractor.rb +++ b/lib/gitlab/reference_extractor.rb @@ -85,7 +85,7 @@ module Gitlab private - NAME_STR = Gitlab::Regex::NAMESPACE_REGEX_STR + NAME_STR = Gitlab::Regex::NAMESPACE_PATH_REGEX_STR PROJ_STR = "(?#{NAME_STR}/#{NAME_STR})" REFERENCE_PATTERN = %r{ diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index 0571574aa4f..e5f5482a223 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -2,13 +2,13 @@ module Gitlab module Regex extend self - NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze + NAMESPACE_PATH_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze - def namespace_regex - @namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze + def namespace_path_regex + @namespace_path_regex ||= /\A#{NAMESPACE_PATH_REGEX_STR}\z/.freeze end - def namespace_regex_message + def namespace_path_regex_message "can contain only letters, digits, '_', '-' and '.'. " \ "Cannot start with '-' or end in '.'." \ end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 327f3e6d23c..cb41ea44918 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -145,7 +145,7 @@ describe API::API, api: true do expect(json_response['message']['projects_limit']). to eq(['must be greater than or equal to 0']) expect(json_response['message']['username']). - to eq([Gitlab::Regex.send(:namespace_regex_message)]) + to eq([Gitlab::Regex.send(:namespace_path_regex_message)]) end it "shouldn't available for non admin users" do @@ -271,7 +271,7 @@ describe API::API, api: true do expect(json_response['message']['projects_limit']). to eq(['must be greater than or equal to 0']) expect(json_response['message']['username']). - to eq([Gitlab::Regex.send(:namespace_regex_message)]) + to eq([Gitlab::Regex.send(:namespace_path_regex_message)]) end context "with existing user" do -- cgit v1.2.1 From fac1f83fd51d4d6cc6e8a0c571507bca4460b514 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 12:00:08 +0200 Subject: Properly migrate users with usernames like "." or "..". --- app/models/namespace.rb | 6 +++++- db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 24363a853d8..4c760ac6176 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -66,9 +66,13 @@ class Namespace < ActiveRecord::Base path.gsub!(/\.+\z/, "") path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") + # Users with the great usernames of "." or ".." would end up with a blank username. + # Work around that by setting their username to "blank", followed by a counter. + path = "blank" if path.blank? + counter = 0 base = path - while Namespace.by_path(path).present? + while Namespace.find_by_path_or_name(path) counter += 1 path = "#{base}#{counter}" end diff --git a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb index dc38b0eceb7..e2f231ba709 100644 --- a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb +++ b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb @@ -15,6 +15,10 @@ class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration path.gsub!(/\.+\z/, "") path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") + # Users with the great usernames of "." or ".." would end up with a blank username. + # Work around that by setting their username to "blank", followed by a counter. + path = "blank" if path.blank? + counter = 0 base = path while Namespace.find_by_path_or_name(path) -- cgit v1.2.1 From 4934bc031205c668d4405c2a6faaee32367234d6 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 12:00:32 +0200 Subject: Change migration timestamp. --- ...24133047_remove_periods_at_ends_of_usernames.rb | 80 ---------------------- ...21120000_remove_periods_at_ends_of_usernames.rb | 80 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 80 deletions(-) delete mode 100644 db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb create mode 100644 db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb diff --git a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb deleted file mode 100644 index e2f231ba709..00000000000 --- a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb +++ /dev/null @@ -1,80 +0,0 @@ -class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration - include Gitlab::ShellAdapter - - class Namespace < ActiveRecord::Base - class << self - def find_by_path_or_name(path) - find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) - end - - def clean_path(path) - path = path.dup - path.gsub!(/@.*\z/, "") - path.gsub!(/\.git\z/, "") - path.gsub!(/\A-+/, "") - path.gsub!(/\.+\z/, "") - path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") - - # Users with the great usernames of "." or ".." would end up with a blank username. - # Work around that by setting their username to "blank", followed by a counter. - path = "blank" if path.blank? - - counter = 0 - base = path - while Namespace.find_by_path_or_name(path) - counter += 1 - path = "#{base}#{counter}" - end - - path - end - end - end - - def up - changed_paths = {} - - select_all("SELECT id, username FROM users WHERE username LIKE '%.'").each do |user| - username_was = user["username"] - username = Namespace.clean_path(username_was) - changed_paths[username_was] = username - - username = quote_string(username) - execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}" - execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type IS NULL AND owner_id = #{user["id"]}" - end - - select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group| - path_was = group["path"] - path = Namespace.clean_path(path_was) - changed_paths[path_was] = path - - path = quote_string(path) - execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{group["id"]}" - end - - changed_paths.each do |path_was, path| - if gitlab_shell.mv_namespace(path_was, path) - # If repositories moved successfully we need to remove old satellites - # and send update instructions to users. - # However we cannot allow rollback since we moved namespace dir - # So we basically we mute exceptions in next actions - begin - gitlab_shell.rm_satellites(path_was) - # We cannot send update instructions since models and mailers - # can't safely be used from migrations as they may be written for - # later versions of the database. - # send_update_instructions - rescue - # Returning false does not rollback after_* transaction but gives - # us information about failing some of tasks - false - end - else - # if we cannot move namespace directory we should rollback - # db changes in order to prevent out of sync between db and fs - raise Exception.new('namespace directory cannot be moved') - end - end - end -end diff --git a/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb new file mode 100644 index 00000000000..e2f231ba709 --- /dev/null +++ b/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb @@ -0,0 +1,80 @@ +class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration + include Gitlab::ShellAdapter + + class Namespace < ActiveRecord::Base + class << self + def find_by_path_or_name(path) + find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) + end + + def clean_path(path) + path = path.dup + path.gsub!(/@.*\z/, "") + path.gsub!(/\.git\z/, "") + path.gsub!(/\A-+/, "") + path.gsub!(/\.+\z/, "") + path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") + + # Users with the great usernames of "." or ".." would end up with a blank username. + # Work around that by setting their username to "blank", followed by a counter. + path = "blank" if path.blank? + + counter = 0 + base = path + while Namespace.find_by_path_or_name(path) + counter += 1 + path = "#{base}#{counter}" + end + + path + end + end + end + + def up + changed_paths = {} + + select_all("SELECT id, username FROM users WHERE username LIKE '%.'").each do |user| + username_was = user["username"] + username = Namespace.clean_path(username_was) + changed_paths[username_was] = username + + username = quote_string(username) + execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}" + execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type IS NULL AND owner_id = #{user["id"]}" + end + + select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group| + path_was = group["path"] + path = Namespace.clean_path(path_was) + changed_paths[path_was] = path + + path = quote_string(path) + execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{group["id"]}" + end + + changed_paths.each do |path_was, path| + if gitlab_shell.mv_namespace(path_was, path) + # If repositories moved successfully we need to remove old satellites + # and send update instructions to users. + # However we cannot allow rollback since we moved namespace dir + # So we basically we mute exceptions in next actions + begin + gitlab_shell.rm_satellites(path_was) + # We cannot send update instructions since models and mailers + # can't safely be used from migrations as they may be written for + # later versions of the database. + # send_update_instructions + rescue + # Returning false does not rollback after_* transaction but gives + # us information about failing some of tasks + false + end + else + # if we cannot move namespace directory we should rollback + # db changes in order to prevent out of sync between db and fs + raise Exception.new('namespace directory cannot be moved') + end + end + end +end -- cgit v1.2.1 From 2fd47a0e95da2ef2dada1012f09d6c0d1b7431a9 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 15:01:48 +0200 Subject: Don't attempt to move if original path only contains periods. --- db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb index e2f231ba709..01ea199d618 100644 --- a/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb +++ b/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb @@ -54,6 +54,9 @@ class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration end changed_paths.each do |path_was, path| + # Don't attempt to move if original path only contains periods. + next if path_was =~ /\A\.+\z/ + if gitlab_shell.mv_namespace(path_was, path) # If repositories moved successfully we need to remove old satellites # and send update instructions to users. -- cgit v1.2.1 From 456e3a7000ecbc45e6a7d3ebb185686a18e4bfc1 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 15:01:52 +0200 Subject: Update schema. --- db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 2b7e27e3a31..6db7e386c86 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150417122318) do +ActiveRecord::Schema.define(version: 20150421120000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" -- cgit v1.2.1 From 70f9893ed697873e4c95de4f3ddb3ff8c4fb82f6 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 21 Apr 2015 15:49:16 +0200 Subject: Explain namespace clearn regex. --- app/models/namespace.rb | 5 +++++ db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 4c760ac6176..f51a14a9514 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -60,10 +60,15 @@ class Namespace < ActiveRecord::Base def clean_path(path) path = path.dup + # Get the email username by removing everything after an `@` sign. path.gsub!(/@.*\z/, "") + # Usernames can't end in .git, so remove it. path.gsub!(/\.git\z/, "") + # Remove dashes at the start of the username. path.gsub!(/\A-+/, "") + # Remove periods at the end of the username. path.gsub!(/\.+\z/, "") + # Remove everything that's not in the list of allowed characters. path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") # Users with the great usernames of "." or ".." would end up with a blank username. diff --git a/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb index 01ea199d618..3057ea3c68c 100644 --- a/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb +++ b/db/migrate/20150421120000_remove_periods_at_ends_of_usernames.rb @@ -9,10 +9,15 @@ class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration def clean_path(path) path = path.dup + # Get the email username by removing everything after an `@` sign. path.gsub!(/@.*\z/, "") + # Usernames can't end in .git, so remove it. path.gsub!(/\.git\z/, "") + # Remove dashes at the start of the username. path.gsub!(/\A-+/, "") + # Remove periods at the end of the username. path.gsub!(/\.+\z/, "") + # Remove everything that's not in the list of allowed characters. path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") # Users with the great usernames of "." or ".." would end up with a blank username. -- cgit v1.2.1 From 5f839770e720dc2f176c51d4635dceb6c34ff97a Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 24 Apr 2015 15:16:38 +0200 Subject: Revert "Rename namespace_regex to namespace_path_regex." This reverts commit c0116926c743818b2593474946abb40b56d8fefa. --- app/models/namespace.rb | 4 ++-- app/models/user.rb | 4 ++-- lib/gitlab/markdown/cross_project_reference.rb | 2 +- lib/gitlab/markdown/user_reference_filter.rb | 2 +- lib/gitlab/reference_extractor.rb | 2 +- lib/gitlab/regex.rb | 8 ++++---- spec/requests/api/users_spec.rb | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/models/namespace.rb b/app/models/namespace.rb index f51a14a9514..211dfa76b81 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -33,8 +33,8 @@ class Namespace < ActiveRecord::Base presence: true, length: { within: 1..255 }, exclusion: { in: Gitlab::Blacklist.path }, - format: { with: Gitlab::Regex.namespace_path_regex, - message: Gitlab::Regex.namespace_path_regex_message } + format: { with: Gitlab::Regex.namespace_regex, + message: Gitlab::Regex.namespace_regex_message } delegate :name, to: :owner, allow_nil: true, prefix: true diff --git a/app/models/user.rb b/app/models/user.rb index 7d7faa64f72..d6b93afe739 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -131,8 +131,8 @@ class User < ActiveRecord::Base presence: true, uniqueness: { case_sensitive: false }, exclusion: { in: Gitlab::Blacklist.path }, - format: { with: Gitlab::Regex.namespace_path_regex, - message: Gitlab::Regex.namespace_path_regex_message } + format: { with: Gitlab::Regex.namespace_regex, + message: Gitlab::Regex.namespace_regex_message } validates :notification_level, inclusion: { in: Notification.notification_levels }, presence: true validate :namespace_uniq, if: ->(user) { user.username_changed? } diff --git a/lib/gitlab/markdown/cross_project_reference.rb b/lib/gitlab/markdown/cross_project_reference.rb index 261fc0feb0e..c436fabd658 100644 --- a/lib/gitlab/markdown/cross_project_reference.rb +++ b/lib/gitlab/markdown/cross_project_reference.rb @@ -3,7 +3,7 @@ module Gitlab # Common methods for ReferenceFilters that support an optional cross-project # reference. module CrossProjectReference - NAMING_PATTERN = Gitlab::Regex::NAMESPACE_PATH_REGEX_STR + NAMING_PATTERN = Gitlab::Regex::NAMESPACE_REGEX_STR PROJECT_PATTERN = "(?#{NAMING_PATTERN}/#{NAMING_PATTERN})" # Given a cross-project reference string, get the Project record diff --git a/lib/gitlab/markdown/user_reference_filter.rb b/lib/gitlab/markdown/user_reference_filter.rb index d7885fdeefd..5fc8ed55fe2 100644 --- a/lib/gitlab/markdown/user_reference_filter.rb +++ b/lib/gitlab/markdown/user_reference_filter.rb @@ -22,7 +22,7 @@ module Gitlab end # Pattern used to extract `@user` user references from text - USER_PATTERN = /@(?#{Gitlab::Regex::NAMESPACE_PATH_REGEX_STR})/ + USER_PATTERN = /@(?#{Gitlab::Regex::NAMESPACE_REGEX_STR})/ def call replace_text_nodes_matching(USER_PATTERN) do |content| diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb index 1d6df9a29f1..949dd5d26b1 100644 --- a/lib/gitlab/reference_extractor.rb +++ b/lib/gitlab/reference_extractor.rb @@ -85,7 +85,7 @@ module Gitlab private - NAME_STR = Gitlab::Regex::NAMESPACE_PATH_REGEX_STR + NAME_STR = Gitlab::Regex::NAMESPACE_REGEX_STR PROJ_STR = "(?#{NAME_STR}/#{NAME_STR})" REFERENCE_PATTERN = %r{ diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index e5f5482a223..0571574aa4f 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -2,13 +2,13 @@ module Gitlab module Regex extend self - NAMESPACE_PATH_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze + NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze - def namespace_path_regex - @namespace_path_regex ||= /\A#{NAMESPACE_PATH_REGEX_STR}\z/.freeze + def namespace_regex + @namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze end - def namespace_path_regex_message + def namespace_regex_message "can contain only letters, digits, '_', '-' and '.'. " \ "Cannot start with '-' or end in '.'." \ end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index cb41ea44918..327f3e6d23c 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -145,7 +145,7 @@ describe API::API, api: true do expect(json_response['message']['projects_limit']). to eq(['must be greater than or equal to 0']) expect(json_response['message']['username']). - to eq([Gitlab::Regex.send(:namespace_path_regex_message)]) + to eq([Gitlab::Regex.send(:namespace_regex_message)]) end it "shouldn't available for non admin users" do @@ -271,7 +271,7 @@ describe API::API, api: true do expect(json_response['message']['projects_limit']). to eq(['must be greater than or equal to 0']) expect(json_response['message']['username']). - to eq([Gitlab::Regex.send(:namespace_path_regex_message)]) + to eq([Gitlab::Regex.send(:namespace_regex_message)]) end context "with existing user" do -- cgit v1.2.1