Merge branch 'milestone-ref' into 'master'

Link to milestone in "Milestone changed" system note

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/4141

See merge request !2203
This commit is contained in:
Robert Speicher 2016-01-07 19:22:43 +00:00
commit 90510f0037
13 changed files with 173 additions and 23 deletions

View file

@ -8,6 +8,7 @@ v 8.4.0 (unreleased)
- Implement search inside emoji picker - Implement search inside emoji picker
- Add API support for looking up a user by username (Stan Hu) - Add API support for looking up a user by username (Stan Hu)
- Add project permissions to all project API endpoints (Stan Hu) - Add project permissions to all project API endpoints (Stan Hu)
- Link to milestone in "Milestone changed" system note
- Only allow group/project members to mention `@all` - Only allow group/project members to mention `@all`
- Expose Git's version in the admin area (Trey Davis) - Expose Git's version in the admin area (Trey Davis)
- Add "Frequently used" category to emoji picker - Add "Frequently used" category to emoji picker

View file

@ -22,6 +22,7 @@ class Milestone < ActiveRecord::Base
include InternalId include InternalId
include Sortable include Sortable
include Referable
include StripAttribute include StripAttribute
belongs_to :project belongs_to :project
@ -61,6 +62,27 @@ class Milestone < ActiveRecord::Base
end end
end end
def self.reference_pattern
nil
end
def self.link_reference_pattern
super("milestones", /(?<milestone>\d+)/)
end
def to_reference(from_project = nil)
escaped_title = self.title.gsub("]", "\\]")
h = Gitlab::Application.routes.url_helpers
url = h.namespace_project_milestone_url(self.project.namespace, self.project, self)
"[#{escaped_title}](#{url})"
end
def reference_link_text(from_project = nil)
self.title
end
def expired? def expired?
if due_date if due_date
due_date.past? due_date.past?

View file

@ -41,7 +41,7 @@ class SystemNoteService
# #
# Returns the created Note object # Returns the created Note object
def self.change_assignee(noteable, project, author, assignee) def self.change_assignee(noteable, project, author, assignee)
body = assignee.nil? ? 'Assignee removed' : "Reassigned to @#{assignee.username}" body = assignee.nil? ? 'Assignee removed' : "Reassigned to #{assignee.to_reference}"
create_note(noteable: noteable, project: project, author: author, note: body) create_note(noteable: noteable, project: project, author: author, note: body)
end end
@ -66,7 +66,7 @@ class SystemNoteService
def self.change_label(noteable, project, author, added_labels, removed_labels) def self.change_label(noteable, project, author, added_labels, removed_labels)
labels_count = added_labels.count + removed_labels.count labels_count = added_labels.count + removed_labels.count
references = ->(label) { "~#{label.id}" } references = ->(label) { label.to_reference(:id) }
added_labels = added_labels.map(&references).join(' ') added_labels = added_labels.map(&references).join(' ')
removed_labels = removed_labels.map(&references).join(' ') removed_labels = removed_labels.map(&references).join(' ')
@ -103,7 +103,7 @@ class SystemNoteService
# Returns the created Note object # Returns the created Note object
def self.change_milestone(noteable, project, author, milestone) def self.change_milestone(noteable, project, author, milestone)
body = 'Milestone ' body = 'Milestone '
body += milestone.nil? ? 'removed' : "changed to #{milestone.title}" body += milestone.nil? ? 'removed' : "changed to #{milestone.to_reference(project)}"
create_note(noteable: noteable, project: project, author: author, note: body) create_note(noteable: noteable, project: project, author: author, note: body)
end end

View file

@ -70,6 +70,7 @@ module Banzai
end end
def call def call
if object_class.reference_pattern
# `#123` # `#123`
replace_text_nodes_matching(object_class.reference_pattern) do |content| replace_text_nodes_matching(object_class.reference_pattern) do |content|
object_link_filter(content, object_class.reference_pattern) object_link_filter(content, object_class.reference_pattern)
@ -80,7 +81,9 @@ module Banzai
replace_link_nodes_with_href(object_class.reference_pattern) do |link, text| replace_link_nodes_with_href(object_class.reference_pattern) do |link, text|
object_link_filter(link, object_class.reference_pattern, link_text: text) object_link_filter(link, object_class.reference_pattern, link_text: text)
end end
end
if object_class.link_reference_pattern
# `http://gitlab.example.com/namespace/project/issues/123`, which is turned into # `http://gitlab.example.com/namespace/project/issues/123`, which is turned into
# `<a href="http://gitlab.example.com/namespace/project/issues/123">http://gitlab.example.com/namespace/project/issues/123</a>` # `<a href="http://gitlab.example.com/namespace/project/issues/123">http://gitlab.example.com/namespace/project/issues/123</a>`
replace_link_nodes_with_text(object_class.link_reference_pattern) do |text| replace_link_nodes_with_text(object_class.link_reference_pattern) do |text|
@ -93,6 +96,7 @@ module Banzai
object_link_filter(link, object_class.link_reference_pattern, link_text: text) object_link_filter(link, object_class.link_reference_pattern, link_text: text)
end end
end end
end
# Replace references (like `!123` for merge requests) in text with links # Replace references (like `!123` for merge requests) in text with links
# to the referenced object's details page. # to the referenced object's details page.

View file

@ -0,0 +1,22 @@
require 'banzai'
module Banzai
module Filter
# HTML filter that replaces milestone references with links.
class MilestoneReferenceFilter < AbstractReferenceFilter
def self.object_class
Milestone
end
def find_object(project, id)
project.milestones.find_by(iid: id)
end
def url_for_object(issue, project)
h = Gitlab::Application.routes.url_helpers
h.namespace_project_milestone_url(project.namespace, project, milestone,
only_path: context[:only_path])
end
end
end
end

View file

@ -22,6 +22,7 @@ module Banzai
Filter::CommitRangeReferenceFilter, Filter::CommitRangeReferenceFilter,
Filter::CommitReferenceFilter, Filter::CommitReferenceFilter,
Filter::LabelReferenceFilter, Filter::LabelReferenceFilter,
Filter::MilestoneReferenceFilter,
Filter::TaskListFilter Filter::TaskListFilter
] ]

View file

@ -19,7 +19,7 @@ module Gitlab
super(text, context.merge(project: project)) super(text, context.merge(project: project))
end end
%i(user label merge_request snippet commit commit_range).each do |type| %i(user label milestone merge_request snippet commit commit_range).each do |type|
define_method("#{type}s") do define_method("#{type}s") do
@references[type] ||= references(type, reference_context) @references[type] ||= references(type, reference_context)
end end

View file

@ -212,6 +212,7 @@ describe 'GitLab Markdown', feature: true do
expect(doc).to reference_commit_ranges expect(doc).to reference_commit_ranges
expect(doc).to reference_commits expect(doc).to reference_commits
expect(doc).to reference_labels expect(doc).to reference_labels
expect(doc).to reference_milestones
end end
end end

View file

@ -214,6 +214,13 @@ References should be parseable even inside _<%= merge_request.to_reference %>_ e
- Ignored in links: [Link to <%= simple_label.to_reference %>](#label-link) - Ignored in links: [Link to <%= simple_label.to_reference %>](#label-link)
- Link to label by reference: [Label](<%= label.to_reference %>) - Link to label by reference: [Label](<%= label.to_reference %>)
#### MilestoneReferenceFilter
- Milestone: <%= milestone.to_reference %>
- Milestone in another project: <%= xmilestone.to_reference(project) %>
- Ignored in code: `<%= milestone.to_reference %>`
- Link to milestone by URL: [Milestone](<%= urls.namespace_project_milestone_url(milestone.project.namespace, milestone.project, milestone) %>)
### Task Lists ### Task Lists
- [ ] Incomplete task 1 - [ ] Incomplete task 1

View file

@ -0,0 +1,75 @@
require 'spec_helper'
describe Banzai::Filter::MilestoneReferenceFilter, lib: true do
include FilterSpecHelper
let(:project) { create(:project, :public) }
let(:milestone) { create(:milestone, project: project) }
it 'requires project context' do
expect { described_class.call('') }.to raise_error(ArgumentError, /:project/)
end
%w(pre code a style).each do |elem|
it "ignores valid references contained inside '#{elem}' element" do
exp = act = "<#{elem}>milestone #{milestone.to_reference}</#{elem}>"
expect(reference_filter(act).to_html).to eq exp
end
end
context 'internal reference' do
# Convert the Markdown link to only the URL, since these tests aren't run through the regular Markdown pipeline.
# Milestone reference behavior in the full Markdown pipeline is tested elsewhere.
let(:reference) { milestone.to_reference.gsub(/\[([^\]]+)\]\(([^)]+)\)/, '\2') }
it 'links to a valid reference' do
doc = reference_filter("See #{reference}")
expect(doc.css('a').first.attr('href')).to eq urls.
namespace_project_milestone_url(project.namespace, project, milestone)
end
it 'links with adjacent text' do
doc = reference_filter("milestone (#{reference}.)")
expect(doc.to_html).to match(/\(<a.+>#{Regexp.escape(milestone.title)}<\/a>\.\)/)
end
it 'includes a title attribute' do
doc = reference_filter("milestone #{reference}")
expect(doc.css('a').first.attr('title')).to eq "Milestone: #{milestone.title}"
end
it 'escapes the title attribute' do
milestone.update_attribute(:title, %{"></a>whatever<a title="})
doc = reference_filter("milestone #{reference}")
expect(doc.text).to eq "milestone #{milestone.title}"
end
it 'includes default classes' do
doc = reference_filter("milestone #{reference}")
expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-milestone'
end
it 'includes a data-project attribute' do
doc = reference_filter("milestone #{reference}")
link = doc.css('a').first
expect(link).to have_attribute('data-project')
expect(link.attr('data-project')).to eq project.id.to_s
end
it 'includes a data-milestone attribute' do
doc = reference_filter("See #{reference}")
link = doc.css('a').first
expect(link).to have_attribute('data-milestone')
expect(link.attr('data-milestone')).to eq milestone.id.to_s
end
it 'adds to the results hash' do
result = reference_pipeline_result("milestone #{reference}")
expect(result[:references][:milestone]).to eq [milestone]
end
end
end

View file

@ -171,7 +171,7 @@ describe SystemNoteService, services: true do
context 'when milestone added' do context 'when milestone added' do
it 'sets the note text' do it 'sets the note text' do
expect(subject.note).to eq "Milestone changed to #{milestone.title}" expect(subject.note).to eq "Milestone changed to #{milestone.to_reference}"
end end
end end

View file

@ -59,6 +59,10 @@ class MarkdownFeature
@label ||= create(:label, name: 'awaiting feedback', project: project) @label ||= create(:label, name: 'awaiting feedback', project: project)
end end
def milestone
@milestone ||= create(:milestone, project: project)
end
# Cross-references ----------------------------------------------------------- # Cross-references -----------------------------------------------------------
def xproject def xproject
@ -93,6 +97,10 @@ class MarkdownFeature
end end
end end
def xmilestone
@xmilestone ||= create(:milestone, project: xproject)
end
def urls def urls
Gitlab::Application.routes.url_helpers Gitlab::Application.routes.url_helpers
end end

View file

@ -130,6 +130,15 @@ module MarkdownMatchers
end end
end end
# MilestoneReferenceFilter
matcher :reference_milestones do
set_default_markdown_messages
match do |actual|
expect(actual).to have_selector('a.gfm.gfm-milestone', count: 3)
end
end
# TaskListFilter # TaskListFilter
matcher :parse_task_lists do matcher :parse_task_lists do
set_default_markdown_messages set_default_markdown_messages