Commit a4006a6b authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'better-commit-mentions' into 'master'

Clean up code around commit mentions.

Builds upon !527.

See merge request !528
parents d9f74990 630e8790
...@@ -10,11 +10,11 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -10,11 +10,11 @@ class Projects::CommitController < Projects::ApplicationController
def show def show
return git_not_found! unless @commit return git_not_found! unless @commit
@line_notes = @project.notes.for_commit_id(commit.id).inline @line_notes = commit.notes(@project).inline
@diffs = @commit.diffs @diffs = @commit.diffs
@note = @project.build_commit_note(commit) @note = @project.build_commit_note(commit)
@notes_count = @project.notes.for_commit_id(commit.id).count @notes_count = commit.notes(@project).count
@notes = @project.notes.for_commit_id(@commit.id).not_inline.fresh @notes = commit.notes(@project).not_inline.fresh
@noteable = @commit @noteable = @commit
@comments_allowed = @reply_allowed = true @comments_allowed = @reply_allowed = true
@comments_target = { @comments_target = {
......
...@@ -134,6 +134,25 @@ class Commit ...@@ -134,6 +134,25 @@ class Commit
User.find_for_commit(committer_email, committer_name) User.find_for_commit(committer_email, committer_name)
end end
def participants(project, current_user = nil)
users = []
users << author
users << committer
users.push *self.mentioned_users(current_user, project)
notes(project).each do |note|
users << note.author
users.push *note.mentioned_users(current_user, project)
end
users.uniq
end
def notes(project)
project.notes.for_commit_id(self.id)
end
def method_missing(m, *args, &block) def method_missing(m, *args, &block)
@raw.send(m, *args, &block) @raw.send(m, *args, &block)
end end
......
...@@ -122,15 +122,15 @@ module Issuable ...@@ -122,15 +122,15 @@ module Issuable
users = [] users = []
users << author users << author
users << assignee if is_assigned? users << assignee if is_assigned?
mentions = []
mentions << self.mentioned_users(current_user) users.push *self.mentioned_users(current_user)
notes.each do |note| notes.each do |note|
users << note.author users << note.author
mentions << note.mentioned_users(current_user) users.push *note.mentioned_users(current_user)
end end
users.concat(mentions.reduce([], :|)).uniq users.uniq
end end
def subscribed?(user) def subscribed?(user)
......
...@@ -42,10 +42,10 @@ module Mentionable ...@@ -42,10 +42,10 @@ module Mentionable
Note.cross_reference_exists?(target, local_reference) Note.cross_reference_exists?(target, local_reference)
end end
def mentioned_users(current_user = nil) def mentioned_users(current_user = nil, p = project)
return [] if mentionable_text.blank? return [] if mentionable_text.blank?
ext = Gitlab::ReferenceExtractor.new(self.project, current_user) ext = Gitlab::ReferenceExtractor.new(p, current_user)
ext.analyze(mentionable_text) ext.analyze(mentionable_text)
ext.users.uniq ext.users.uniq
end end
......
...@@ -327,14 +327,6 @@ class Note < ActiveRecord::Base ...@@ -327,14 +327,6 @@ class Note < ActiveRecord::Base
current_application_settings.max_attachment_size.megabytes.to_i current_application_settings.max_attachment_size.megabytes.to_i
end end
def commit_author
@commit_author ||=
project.team.users.find_by(email: noteable.author_email) ||
project.team.users.find_by(name: noteable.author_name)
rescue
nil
end
def cross_reference? def cross_reference?
note.start_with?(self.class.cross_reference_note_prefix) note.start_with?(self.class.cross_reference_note_prefix)
end end
......
...@@ -87,6 +87,18 @@ class Snippet < ActiveRecord::Base ...@@ -87,6 +87,18 @@ class Snippet < ActiveRecord::Base
visibility_level visibility_level
end end
def participants(current_user = self.author)
users = []
users << author
notes.each do |note|
users << note.author
users.push *note.mentioned_users(current_user)
end
users.uniq
end
class << self class << self
def search(query) def search(query)
where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%") where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%")
......
...@@ -127,17 +127,12 @@ class NotificationService ...@@ -127,17 +127,12 @@ class NotificationService
recipients = [] recipients = []
if note.commit_id.present?
recipients << note.commit_author
end
# Add all users participating in the thread (author, assignee, comment authors) # Add all users participating in the thread (author, assignee, comment authors)
participants = participants =
if target.respond_to?(:participants) if target.is_a?(Commit)
target.participants target.participants(note.project, note.author)
elsif target.is_a?(Commit) elsif target.respond_to?(:participants)
author_ids = Note.for_commit_id(target.id).pluck(:author_id).uniq target.participants(note.author)
User.where(id: author_ids)
else else
note.mentioned_users note.mentioned_users
end end
......
...@@ -13,19 +13,21 @@ module Projects ...@@ -13,19 +13,21 @@ module Projects
end end
def participants_in(type, id) def participants_in(type, id)
users = case type users =
when "Issue" case type
issue = project.issues.find_by_iid(id) when "Issue"
issue ? issue.participants(current_user) : [] issue = project.issues.find_by_iid(id)
when "MergeRequest" issue.participants(current_user) if issue
merge_request = project.merge_requests.find_by_iid(id) when "MergeRequest"
merge_request ? merge_request.participants(current_user) : [] merge_request = project.merge_requests.find_by_iid(id)
when "Commit" merge_request.participants(current_user) if merge_request
author_ids = Note.for_commit_id(id).pluck(:author_id).uniq when "Commit"
User.where(id: author_ids) commit = project.repository.commit(id)
else commit.participants(project, current_user) if commit
[] end
end
return [] unless users
sorted(users) sorted(users)
end end
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
- if @note_counts - if @note_counts
- note_count = @note_counts.fetch(commit.id, 0) - note_count = @note_counts.fetch(commit.id, 0)
- else - else
- notes = project.notes.for_commit_id(commit.id) - notes = commit.notes(project)
- note_count = notes.user.count - note_count = notes.user.count
- if note_count > 0 - if note_count > 0
......
...@@ -149,7 +149,7 @@ describe NotificationService do ...@@ -149,7 +149,7 @@ describe NotificationService do
before do before do
build_team(note.project) build_team(note.project)
note.stub(:commit_author => @u_committer) allow_any_instance_of(Commit).to receive(:author).and_return(@u_committer)
end end
describe :new_note do describe :new_note do
......
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