Unverified Commit 3a23639b authored by Ershad Kunnakkadan's avatar Ershad Kunnakkadan
Browse files

Create directly_addressed Todos when mentioned in beginning of a line

parent 11d33873
Loading
Loading
Loading
Loading
+3 −1
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@ def todo_action_name(todo)
    when Todo::MARKED then 'added a todo for'
    when Todo::MARKED then 'added a todo for'
    when Todo::APPROVAL_REQUIRED then 'set you as an approver for'
    when Todo::APPROVAL_REQUIRED then 'set you as an approver for'
    when Todo::UNMERGEABLE then 'Could not merge'
    when Todo::UNMERGEABLE then 'Could not merge'
    when Todo::DIRECTLY_ADDRESSED then 'directly addressed you on'
    end
    end
  end
  end


@@ -88,7 +89,8 @@ def todo_actions_options
      { id: Todo::ASSIGNED, text: 'Assigned' },
      { id: Todo::ASSIGNED, text: 'Assigned' },
      { id: Todo::MENTIONED, text: 'Mentioned' },
      { id: Todo::MENTIONED, text: 'Mentioned' },
      { id: Todo::MARKED, text: 'Added' },
      { id: Todo::MARKED, text: 'Added' },
      { id: Todo::BUILD_FAILED, text: 'Pipelines' }
      { id: Todo::BUILD_FAILED, text: 'Pipelines' },
      { id: Todo::DIRECTLY_ADDRESSED, text: 'Directly addressed' }
    ]
    ]
  end
  end


+15 −4
Original line number Original line Diff line number Diff line
@@ -44,9 +44,16 @@ def local_reference
  end
  end


  def all_references(current_user = nil, extractor: nil)
  def all_references(current_user = nil, extractor: nil)
    extractor ||= Gitlab::ReferenceExtractor.
    # Use custom extractor if it's passed in the function parameters.
    if extractor
      @extractor = extractor
    else
      @extractor ||= Gitlab::ReferenceExtractor.
        new(project, current_user)
        new(project, current_user)


      @extractor.reset_memoized_values
    end

    self.class.mentionable_attrs.each do |attr, options|
    self.class.mentionable_attrs.each do |attr, options|
      text    = __send__(attr)
      text    = __send__(attr)
      options = options.merge(
      options = options.merge(
@@ -55,16 +62,20 @@ def all_references(current_user = nil, extractor: nil)
        skip_project_check: skip_project_check?
        skip_project_check: skip_project_check?
      )
      )


      extractor.analyze(text, options)
      @extractor.analyze(text, options)
    end
    end


    extractor
    @extractor
  end
  end


  def mentioned_users(current_user = nil)
  def mentioned_users(current_user = nil)
    all_references(current_user).users
    all_references(current_user).users
  end
  end


  def directly_addressed_users(current_user = nil)
    all_references(current_user).directly_addressed_users
  end

  # Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
  # Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
  def referenced_mentionables(current_user = self.author)
  def referenced_mentionables(current_user = self.author)
    refs = all_references(current_user)
    refs = all_references(current_user)
+7 −0
Original line number Original line Diff line number Diff line
class DirectlyAddressedUser
  class << self
    def reference_pattern
      User.reference_pattern
    end
  end
end
+9 −7
Original line number Original line Diff line number Diff line
@@ -7,6 +7,7 @@ class Todo < ActiveRecord::Base
  MARKED             = 4
  MARKED             = 4
  APPROVAL_REQUIRED  = 5 # This is an EE-only feature
  APPROVAL_REQUIRED  = 5 # This is an EE-only feature
  UNMERGEABLE        = 6
  UNMERGEABLE        = 6
  DIRECTLY_ADDRESSED = 7


  ACTION_NAMES = {
  ACTION_NAMES = {
    ASSIGNED => :assigned,
    ASSIGNED => :assigned,
@@ -14,7 +15,8 @@ class Todo < ActiveRecord::Base
    BUILD_FAILED => :build_failed,
    BUILD_FAILED => :build_failed,
    MARKED => :marked,
    MARKED => :marked,
    APPROVAL_REQUIRED => :approval_required,
    APPROVAL_REQUIRED => :approval_required,
    UNMERGEABLE => :unmergeable
    UNMERGEABLE => :unmergeable,
    DIRECTLY_ADDRESSED => :directly_addressed
  }
  }


  belongs_to :author, class_name: "User"
  belongs_to :author, class_name: "User"
+16 −2
Original line number Original line Diff line number Diff line
@@ -243,6 +243,12 @@ def create_assignment_todo(issuable, author)
  end
  end


  def create_mention_todos(project, target, author, note = nil)
  def create_mention_todos(project, target, author, note = nil)
    # Create Todos for directly addressed users
    directly_addressed_users = filter_directly_addressed_users(project, note || target, author)
    attributes = attributes_for_todo(project, target, author, Todo::DIRECTLY_ADDRESSED, note)
    create_todos(directly_addressed_users, attributes)

    # Create Todos for mentioned users
    mentioned_users = filter_mentioned_users(project, note || target, author)
    mentioned_users = filter_mentioned_users(project, note || target, author)
    attributes = attributes_for_todo(project, target, author, Todo::MENTIONED, note)
    attributes = attributes_for_todo(project, target, author, Todo::MENTIONED, note)
    create_todos(mentioned_users, attributes)
    create_todos(mentioned_users, attributes)
@@ -282,10 +288,18 @@ def attributes_for_todo(project, target, author, action, note = nil)
    )
    )
  end
  end


  def filter_todo_users(users, project, target)
    reject_users_without_access(users, project, target).uniq
  end

  def filter_mentioned_users(project, target, author)
  def filter_mentioned_users(project, target, author)
    mentioned_users = target.mentioned_users(author)
    mentioned_users = target.mentioned_users(author)
    mentioned_users = reject_users_without_access(mentioned_users, project, target)
    filter_todo_users(mentioned_users, project, target)
    mentioned_users.uniq
  end

  def filter_directly_addressed_users(project, target, author)
    directly_addressed_users = target.directly_addressed_users(author)
    filter_todo_users(directly_addressed_users, project, target)
  end
  end


  def reject_users_without_access(users, project, target)
  def reject_users_without_access(users, project, target)
Loading