Commit 045b4c8fcd3898bf66ac74acdf13cc079a104105

Authored by Cyril
1 parent 23a8e599

replace user.name by user.username in GFM

lib/gitlab/markdown.rb
@@ -26,15 +26,19 @@ module Gitlab @@ -26,15 +26,19 @@ module Gitlab
26 # => "<img alt=\":trollface:\" class=\"emoji\" src=\"/images/trollface.png" title=\":trollface:\" /> 26 # => "<img alt=\":trollface:\" class=\"emoji\" src=\"/images/trollface.png" title=\":trollface:\" />
27 module Markdown 27 module Markdown
28 REFERENCE_PATTERN = %r{ 28 REFERENCE_PATTERN = %r{
29 - (\W)? # Prefix (1)  
30 - ( # Reference (2)  
31 - @([\w\._]+) # User name (3)  
32 - |[#!$](\d+) # Issue/MR/Snippet ID (4)  
33 - |([\h]{6,40}) # Commit ID (5) 29 + (?<prefix>\W)? # Prefix
  30 + ( # Reference
  31 + @(?<user>[a-zA-Z][a-zA-Z0-9_\-\.]*) # User name
  32 + |\#(?<issue>\d+) # Issue ID
  33 + |!(?<merge_request>\d+) # MR ID
  34 + |\$(?<snippet>\d+) # Snippet ID
  35 + |(?<commit>[\h]{6,40}) # Commit ID
34 ) 36 )
35 - (\W)? # Suffix (6) 37 + (?<suffix>\W)? # Suffix
36 }x.freeze 38 }x.freeze
37 39
  40 + TYPES = [:user, :issue, :merge_request, :snippet, :commit].freeze
  41 +
38 EMOJI_PATTERN = %r{(:(\S+):)}.freeze 42 EMOJI_PATTERN = %r{(:(\S+):)}.freeze
39 43
40 attr_reader :html_options 44 attr_reader :html_options
@@ -95,16 +99,16 @@ module Gitlab @@ -95,16 +99,16 @@ module Gitlab
95 def parse_references(text) 99 def parse_references(text)
96 # parse reference links 100 # parse reference links
97 text.gsub!(REFERENCE_PATTERN) do |match| 101 text.gsub!(REFERENCE_PATTERN) do |match|
98 - prefix = $1 || ''  
99 - reference = $2  
100 - identifier = $3 || $4 || $5  
101 - suffix = $6 || '' 102 + prefix = $~[:prefix]
  103 + suffix = $~[:suffix]
  104 + type = TYPES.select{|t| !$~[t].nil?}.first
  105 + identifier = $~[type]
102 106
103 # Avoid HTML entities 107 # Avoid HTML entities
104 - if prefix.ends_with?('&') || suffix.starts_with?(';') 108 + if prefix && suffix && prefix[0] == '&' && suffix[-1] == ';'
105 match 109 match
106 - elsif ref_link = reference_link(reference, identifier)  
107 - prefix + ref_link + suffix 110 + elsif ref_link = reference_link(type, identifier)
  111 + "#{prefix}#{ref_link}#{suffix}"
108 else 112 else
109 match 113 match
110 end 114 end
@@ -137,19 +141,12 @@ module Gitlab @@ -137,19 +141,12 @@ module Gitlab
137 # identifier - Object identifier (Issue ID, SHA hash, etc.) 141 # identifier - Object identifier (Issue ID, SHA hash, etc.)
138 # 142 #
139 # Returns string rendered by the processing method 143 # Returns string rendered by the processing method
140 - def reference_link(reference, identifier)  
141 - case reference  
142 - when /^@/ then reference_user(identifier)  
143 - when /^#/ then reference_issue(identifier)  
144 - when /^!/ then reference_merge_request(identifier)  
145 - when /^\$/ then reference_snippet(identifier)  
146 - when /^\h/ then reference_commit(identifier)  
147 - end 144 + def reference_link(type, identifier)
  145 + send("reference_#{type}", identifier)
148 end 146 end
149 147
150 def reference_user(identifier) 148 def reference_user(identifier)
151 - if user = @project.users.where(name: identifier).first  
152 - member = @project.users_projects.where(user_id: user).first 149 + if member = @project.users_projects.joins(:user).where(users: { username: identifier }).first
153 link_to("@#{identifier}", project_team_member_path(@project, member), html_options.merge(class: "gfm gfm-team_member #{html_options[:class]}")) if member 150 link_to("@#{identifier}", project_team_member_path(@project, member), html_options.merge(class: "gfm gfm-team_member #{html_options[:class]}")) if member
154 end 151 end
155 end 152 end
spec/helpers/gitlab_markdown_helper_spec.rb
@@ -3,7 +3,7 @@ require &quot;spec_helper&quot; @@ -3,7 +3,7 @@ require &quot;spec_helper&quot;
3 describe GitlabMarkdownHelper do 3 describe GitlabMarkdownHelper do
4 let!(:project) { create(:project) } 4 let!(:project) { create(:project) }
5 5
6 - let(:user) { create(:user, name: 'gfm') } 6 + let(:user) { create(:user, username: 'gfm') }
7 let(:commit) { CommitDecorator.decorate(project.commit) } 7 let(:commit) { CommitDecorator.decorate(project.commit) }
8 let(:issue) { create(:issue, project: project) } 8 let(:issue) { create(:issue, project: project) }
9 let(:merge_request) { create(:merge_request, project: project) } 9 let(:merge_request) { create(:merge_request, project: project) }
@@ -81,7 +81,7 @@ describe GitlabMarkdownHelper do @@ -81,7 +81,7 @@ describe GitlabMarkdownHelper do
81 end 81 end
82 82
83 describe "referencing a team member" do 83 describe "referencing a team member" do
84 - let(:actual) { "@#{user.name} you are right." } 84 + let(:actual) { "@#{user.username} you are right." }
85 let(:expected) { project_team_member_path(project, member) } 85 let(:expected) { project_team_member_path(project, member) }
86 86
87 before do 87 before do
@@ -103,18 +103,18 @@ describe GitlabMarkdownHelper do @@ -103,18 +103,18 @@ describe GitlabMarkdownHelper do
103 end 103 end
104 104
105 it "should link with adjacent text" do 105 it "should link with adjacent text" do
106 - actual = "Mail the admin (@gfm)" 106 + actual = "Mail the admin (@#{user.username})"
107 gfm(actual).should match(expected) 107 gfm(actual).should match(expected)
108 end 108 end
109 109
110 it "should keep whitespace intact" do 110 it "should keep whitespace intact" do
111 - actual = "Yes, @#{user.name} is right."  
112 - expected = /Yes, <a.+>@#{user.name}<\/a> is right/ 111 + actual = "Yes, @#{user.username} is right."
  112 + expected = /Yes, <a.+>@#{user.username}<\/a> is right/
113 gfm(actual).should match(expected) 113 gfm(actual).should match(expected)
114 end 114 end
115 115
116 it "should not link with an invalid id" do 116 it "should not link with an invalid id" do
117 - actual = expected = "@#{user.name.reverse} you are right." 117 + actual = expected = "@#{user.username.reverse} you are right."
118 gfm(actual).should == expected 118 gfm(actual).should == expected
119 end 119 end
120 120
@@ -316,10 +316,10 @@ describe GitlabMarkdownHelper do @@ -316,10 +316,10 @@ describe GitlabMarkdownHelper do
316 it "should handle references in lists" do 316 it "should handle references in lists" do
317 project.users << user 317 project.users << user
318 318
319 - actual = "\n* dark: ##{issue.id}\n* light by @#{member.user_name}" 319 + actual = "\n* dark: ##{issue.id}\n* light by @#{member.user.username}"
320 320
321 markdown(actual).should match(%r{<li>dark: <a.+>##{issue.id}</a></li>}) 321 markdown(actual).should match(%r{<li>dark: <a.+>##{issue.id}</a></li>})
322 - markdown(actual).should match(%r{<li>light by <a.+>@#{member.user_name}</a></li>}) 322 + markdown(actual).should match(%r{<li>light by <a.+>@#{member.user.username}</a></li>})
323 end 323 end
324 324
325 it "should handle references in <em>" do 325 it "should handle references in <em>" do
spec/requests/gitlab_flavored_markdown_spec.rb
@@ -19,7 +19,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do @@ -19,7 +19,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do
19 @test_file = "gfm_test_file" 19 @test_file = "gfm_test_file"
20 i.add(@test_file, "foo\nbar\n") 20 i.add(@test_file, "foo\nbar\n")
21 # add commit with gfm 21 # add commit with gfm
22 - i.commit("fix ##{issue.id}\n\nask @#{fred.name} for details", head: @branch_name) 22 + i.commit("fix ##{issue.id}\n\nask @#{fred.username} for details", head: @branch_name)
23 23
24 # add test tag 24 # add test tag
25 @tag_name = "gfm-test-tag" 25 @tag_name = "gfm-test-tag"
@@ -56,7 +56,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do @@ -56,7 +56,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do
56 it "should render description in commits#show" do 56 it "should render description in commits#show" do
57 visit project_commit_path(project, commit) 57 visit project_commit_path(project, commit)
58 58
59 - page.should have_link("@#{fred.name}") 59 + page.should have_link("@#{fred.username}")
60 end 60 end
61 61
62 it "should render title in refs#tree", js: true do 62 it "should render title in refs#tree", js: true do
@@ -93,7 +93,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do @@ -93,7 +93,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do
93 assignee: @user, 93 assignee: @user,
94 project: project, 94 project: project,
95 title: "fix ##{@other_issue.id}", 95 title: "fix ##{@other_issue.id}",
96 - description: "ask @#{fred.name} for details") 96 + description: "ask @#{fred.username} for details")
97 end 97 end
98 98
99 it "should render subject in issues#index" do 99 it "should render subject in issues#index" do
@@ -111,7 +111,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do @@ -111,7 +111,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do
111 it "should render details in issues#show" do 111 it "should render details in issues#show" do
112 visit project_issue_path(project, @issue) 112 visit project_issue_path(project, @issue)
113 113
114 - page.should have_link("@#{fred.name}") 114 + page.should have_link("@#{fred.username}")
115 end 115 end
116 end 116 end
117 117
@@ -142,7 +142,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do @@ -142,7 +142,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do
142 @milestone = create(:milestone, 142 @milestone = create(:milestone,
143 project: project, 143 project: project,
144 title: "fix ##{issue.id}", 144 title: "fix ##{issue.id}",
145 - description: "ask @#{fred.name} for details") 145 + description: "ask @#{fred.username} for details")
146 end 146 end
147 147
148 it "should render title in milestones#index" do 148 it "should render title in milestones#index" do
@@ -160,7 +160,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do @@ -160,7 +160,7 @@ describe &quot;Gitlab Flavored Markdown&quot; do
160 it "should render description in milestones#show" do 160 it "should render description in milestones#show" do
161 visit project_milestone_path(project, @milestone) 161 visit project_milestone_path(project, @milestone)
162 162
163 - page.should have_link("@#{fred.name}") 163 + page.should have_link("@#{fred.username}")
164 end 164 end
165 end 165 end
166 166