Commit 3cb6a338 authored by Robert Speicher's avatar Robert Speicher

More SystemNoteService cleanup

parent 2c1bf717
...@@ -24,11 +24,11 @@ module Mentionable ...@@ -24,11 +24,11 @@ module Mentionable
# #
# By default this will be the class name and the result of calling # By default this will be the class name and the result of calling
# `to_reference` on the object. # `to_reference` on the object.
def gfm_reference def gfm_reference(from_project = nil)
# "MergeRequest" > "merge_request" > "Merge request" > "merge request" # "MergeRequest" > "merge_request" > "Merge request" > "merge request"
friendly_name = self.class.to_s.underscore.humanize.downcase friendly_name = self.class.to_s.underscore.humanize.downcase
"#{friendly_name} #{to_reference}" "#{friendly_name} #{to_reference(from_project)}"
end end
# Construct a String that contains possible GFM references. # Construct a String that contains possible GFM references.
......
...@@ -326,8 +326,8 @@ class Note < ActiveRecord::Base ...@@ -326,8 +326,8 @@ class Note < ActiveRecord::Base
end end
# Mentionable override. # Mentionable override.
def gfm_reference def gfm_reference(from_project = nil)
noteable.gfm_reference noteable.gfm_reference(from_project)
end end
# Mentionable override. # Mentionable override.
......
...@@ -10,7 +10,7 @@ class SystemNoteService ...@@ -10,7 +10,7 @@ class SystemNoteService
# author - User performing the change # author - User performing the change
# new_commits - Array of Commits added since last push # new_commits - Array of Commits added since last push
# existing_commits - Array of Commits added in a previous push # existing_commits - Array of Commits added in a previous push
# oldrev - TODO (rspeicher): I have no idea what this actually does # oldrev - Optional String SHA of a previous Commit
# #
# See new_commit_summary and existing_commit_summary. # See new_commit_summary and existing_commit_summary.
# #
...@@ -138,11 +138,11 @@ class SystemNoteService ...@@ -138,11 +138,11 @@ class SystemNoteService
# #
# Example Note text: # Example Note text:
# #
# "Mentioned in #1" # "mentioned in #1"
# #
# "Mentioned in !2" # "mentioned in !2"
# #
# "Mentioned in 54f7727c" # "mentioned in 54f7727c"
# #
# See cross_reference_note_content. # See cross_reference_note_content.
# #
...@@ -150,7 +150,7 @@ class SystemNoteService ...@@ -150,7 +150,7 @@ class SystemNoteService
def self.cross_reference(noteable, mentioner, author) def self.cross_reference(noteable, mentioner, author)
return if cross_reference_disallowed?(noteable, mentioner) return if cross_reference_disallowed?(noteable, mentioner)
gfm_reference = mentioner_gfm_ref(noteable, mentioner) gfm_reference = mentioner.gfm_reference(noteable.project)
note_options = { note_options = {
project: noteable.project, project: noteable.project,
...@@ -181,12 +181,21 @@ class SystemNoteService ...@@ -181,12 +181,21 @@ class SystemNoteService
# #
# Returns Boolean # Returns Boolean
def self.cross_reference_disallowed?(noteable, mentioner) def self.cross_reference_disallowed?(noteable, mentioner)
return false unless MergeRequest === mentioner return false unless mentioner.is_a?(MergeRequest)
return false unless Commit === noteable return false unless noteable.is_a?(Commit)
mentioner.commits.include?(noteable) mentioner.commits.include?(noteable)
end end
# Check if a cross reference to a noteable from a mentioner already exists
#
# This method is used to prevent multiple notes being created for a mention
# when a issue is updated, for example.
#
# noteable - Noteable object being referenced
# mentioner - Mentionable object
#
# Returns Boolean
def self.cross_reference_exists?(noteable, mentioner) def self.cross_reference_exists?(noteable, mentioner)
# Initial scope should be system notes of this noteable type # Initial scope should be system notes of this noteable type
notes = Note.system.where(noteable_type: noteable.class) notes = Note.system.where(noteable_type: noteable.class)
...@@ -198,7 +207,7 @@ class SystemNoteService ...@@ -198,7 +207,7 @@ class SystemNoteService
notes = notes.where(noteable_id: noteable.id) notes = notes.where(noteable_id: noteable.id)
end end
gfm_reference = mentioner_gfm_ref(noteable, mentioner, true) gfm_reference = mentioner.gfm_reference(noteable.project)
notes = notes.where(note: cross_reference_note_content(gfm_reference)) notes = notes.where(note: cross_reference_note_content(gfm_reference))
notes.count > 0 notes.count > 0
...@@ -210,39 +219,6 @@ class SystemNoteService ...@@ -210,39 +219,6 @@ class SystemNoteService
Note.create(args.merge(system: true)) Note.create(args.merge(system: true))
end end
# Prepend the mentioner's namespaced project path to the GFM reference for
# cross-project references. For same-project references, return the
# unmodified GFM reference.
def self.mentioner_gfm_ref(noteable, mentioner, cross_reference = false)
# FIXME (rspeicher): This was breaking things.
# if mentioner.is_a?(Commit) && cross_reference
# return mentioner.gfm_reference.sub('commit ', 'commit %')
# end
full_gfm_reference(mentioner.project, noteable.project, mentioner)
end
# Return the +mentioner+ GFM reference. If the mentioner and noteable
# projects are not the same, add the mentioning project's path to the
# returned value.
def self.full_gfm_reference(mentioning_project, noteable_project, mentioner)
if mentioning_project == noteable_project
mentioner.gfm_reference
else
if mentioner.is_a?(Commit)
mentioner.gfm_reference.sub(
/(commit )/,
"\\1#{mentioning_project.path_with_namespace}@"
)
else
mentioner.gfm_reference.sub(
/(issue |merge request )/,
"\\1#{mentioning_project.path_with_namespace}"
)
end
end
end
def self.cross_reference_note_prefix def self.cross_reference_note_prefix
'mentioned in ' 'mentioned in '
end end
...@@ -267,7 +243,7 @@ class SystemNoteService ...@@ -267,7 +243,7 @@ class SystemNoteService
# #
# noteable - MergeRequest object # noteable - MergeRequest object
# existing_commits - Array of existing Commit objects # existing_commits - Array of existing Commit objects
# oldrev - Optional String SHA of ... TODO (rspeicher): I have no idea what this actually does. # oldrev - Optional String SHA of a previous Commit
# #
# Examples: # Examples:
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment