Commit 42d088fc authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix/cross-reference-notes-forks' into 'master'

Fix cross reference notes  on forks

Updates `cross_reference_exists?` to match on commit only.

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

See merge request !2731
parents 8db62f25 11913a76
...@@ -49,6 +49,7 @@ v 8.5.0 (unreleased) ...@@ -49,6 +49,7 @@ v 8.5.0 (unreleased)
- Fixed logo animation on Safari (Roman Rott) - Fixed logo animation on Safari (Roman Rott)
- Hide remove source branch button when the MR is merged but new commits are pushed (Zeger-Jan van de Weg) - Hide remove source branch button when the MR is merged but new commits are pushed (Zeger-Jan van de Weg)
- In seach autocomplete show only groups and projects you are member of - In seach autocomplete show only groups and projects you are member of
- Don't process cross-reference notes from forks
- Fix: init.d script not working on OS X - Fix: init.d script not working on OS X
- Faster snippet search - Faster snippet search
- Title for milestones should be unique (Zeger-Jan van de Weg) - Title for milestones should be unique (Zeger-Jan van de Weg)
......
...@@ -274,12 +274,15 @@ class SystemNoteService ...@@ -274,12 +274,15 @@ class SystemNoteService
# Check if a cross reference to a noteable from a mentioner already exists # 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 # This method is used to prevent multiple notes being created for a mention
# when a issue is updated, for example. # when a issue is updated, for example. The method also calls notes_for_mentioner
# to check if the mentioner is a commit, and return matches only on commit hash
# instead of project + commit, to avoid repeated mentions from forks.
# #
# noteable - Noteable object being referenced # noteable - Noteable object being referenced
# mentioner - Mentionable object # mentioner - Mentionable object
# #
# Returns Boolean # 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)
...@@ -291,14 +294,20 @@ class SystemNoteService ...@@ -291,14 +294,20 @@ class SystemNoteService
notes = notes.where(noteable_id: noteable.id) notes = notes.where(noteable_id: noteable.id)
end end
gfm_reference = mentioner.gfm_reference(noteable.project) notes_for_mentioner(mentioner, noteable, notes).count > 0
notes = notes.where(note: cross_reference_note_content(gfm_reference))
notes.count > 0
end end
private private
def self.notes_for_mentioner(mentioner, noteable, notes)
if mentioner.is_a?(Commit)
notes.where('note LIKE ?', "#{cross_reference_note_prefix}%#{mentioner.to_reference(nil)}")
else
gfm_reference = mentioner.gfm_reference(noteable.project)
notes.where(note: cross_reference_note_content(gfm_reference))
end
end
def self.create_note(args = {}) def self.create_note(args = {})
Note.create(args.merge(system: true)) Note.create(args.merge(system: true))
end end
......
...@@ -424,6 +424,21 @@ describe SystemNoteService, services: true do ...@@ -424,6 +424,21 @@ describe SystemNoteService, services: true do
to be_falsey to be_falsey
end end
end end
context 'commit with cross-reference from fork' do
let(:author2) { create(:user) }
let(:forked_project) { Projects::ForkService.new(project, author2).execute }
let(:commit2) { forked_project.commit }
before do
described_class.cross_reference(noteable, commit0, author2)
end
it 'is true when a fork mentions an external issue' do
expect(described_class.cross_reference_exists?(noteable, commit2)).
to be true
end
end
end end
include JiraServiceHelper include JiraServiceHelper
......
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