From 5f93b0e3da9f84f44c5f00af5d3e48c277d68d5b Mon Sep 17 00:00:00 2001
From: Douwe Maan <douwe@gitlab.com>
Date: Tue, 24 Mar 2015 15:21:37 +0100
Subject: [PATCH] Don't allow username to end in period.

---
 CHANGELOG                                     |  2 +
 app/models/namespace.rb                       |  2 +-
 ...047_remove_periods_at_ends_of_usernames.rb | 39 +++++++++++++++++++
 lib/gitlab/markdown.rb                        |  2 +-
 lib/gitlab/regex.rb                           |  6 ++-
 5 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb

diff --git a/CHANGELOG b/CHANGELOG
index 06b7413e616..187de477a81 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -46,6 +46,8 @@ v 7.10.0 (unreleased)
   - Refactor issue filtering
   - AJAX selectbox for issue assignee and author filters
   - Fix issue with missing options in issue filtering dropdown if selected one
+  - Fix "Hello @username." references not working by no longer allowing usernames to end in period.
+
 
 v 7.9.0
   - Send EmailsOnPush email when branch or tag is created or deleted.
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 07d85d05bb4..e2bcd25be21 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -66,7 +66,7 @@ class Namespace < ActiveRecord::Base
       path.gsub!(/@.*\z/,             "")
       path.gsub!(/\.git\z/,           "")
       path.gsub!(/\A-/,               "")
-      path.gsub!(/\z./,               "")
+      path.gsub!(/.\z/,               "")
       path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")
 
       counter = 0
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..36e3faa53be
--- /dev/null
+++ b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb
@@ -0,0 +1,39 @@
+class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration
+  class Namespace < ActiveRecord::Base
+    class << self
+      def by_path(path)
+        where('lower(path) = :value', value: path.downcase).first
+      end
+
+      def clean_path(path)
+        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.by_path(path).present?
+          counter += 1
+          path = "#{base}#{counter}"
+        end
+
+        path
+      end
+    end
+  end
+
+  def up
+    select_all("SELECT id, username FROM users WHERE username LIKE '%.'").each do |user|
+      username = quote_string(Namespace.clean_path(user["username"]))
+      execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}"
+      execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type = NULL AND owner_id = #{user["id"]}"
+    end
+
+    select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group|
+      path = quote_string(Namespace.clean_path(group["path"]))
+      execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{group["id"]}"
+    end
+  end
+end
diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb
index 41bb8d08924..0b29138a729 100644
--- a/lib/gitlab/markdown.rb
+++ b/lib/gitlab/markdown.rb
@@ -152,7 +152,7 @@ module Gitlab
       text
     end
 
-    NAME_STR = '[a-zA-Z0-9_][a-zA-Z0-9_\-\.]*'
+    NAME_STR = Gitlab::Regex::NAMESPACE_REGEX_STR
     PROJ_STR = "(?<project>#{NAME_STR}/#{NAME_STR})"
 
     REFERENCE_PATTERN = %r{
diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb
index 08fcf9c6dc0..0571574aa4f 100644
--- a/lib/gitlab/regex.rb
+++ b/lib/gitlab/regex.rb
@@ -2,13 +2,15 @@ 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
+
     def namespace_regex
-      @namespace_regex ||= /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\.]*(?<!\.git)\z/.freeze
+      @namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze
     end
 
     def namespace_regex_message
       "can contain only letters, digits, '_', '-' and '.'. " \
-      "Cannot start with '-' or end in '.git'" \
+      "Cannot start with '-' or end in '.'." \
     end
 
 
-- 
GitLab