2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Todo < ApplicationRecord
|
2016-07-26 17:21:20 -04:00
|
|
|
include Sortable
|
2018-09-11 11:31:34 -04:00
|
|
|
include FromUnion
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2018-12-11 13:15:10 -05:00
|
|
|
# Time to wait for todos being removed when not visible for user anymore.
|
|
|
|
# Prevents TODOs being removed by mistake, for example, removing access from a user
|
|
|
|
# and giving it back again.
|
2020-07-09 05:09:27 -04:00
|
|
|
WAIT_FOR_DELETE = 1.hour
|
2018-12-11 13:15:10 -05:00
|
|
|
|
2020-07-09 05:09:27 -04:00
|
|
|
ASSIGNED = 1
|
|
|
|
MENTIONED = 2
|
|
|
|
BUILD_FAILED = 3
|
|
|
|
MARKED = 4
|
|
|
|
APPROVAL_REQUIRED = 5 # This is an EE-only feature
|
|
|
|
UNMERGEABLE = 6
|
|
|
|
DIRECTLY_ADDRESSED = 7
|
|
|
|
MERGE_TRAIN_REMOVED = 8 # This is an EE-only feature
|
2020-09-09 08:08:22 -04:00
|
|
|
REVIEW_REQUESTED = 9
|
2016-02-12 13:45:44 -05:00
|
|
|
|
2016-06-15 07:20:30 -04:00
|
|
|
ACTION_NAMES = {
|
|
|
|
ASSIGNED => :assigned,
|
2020-09-09 08:08:22 -04:00
|
|
|
REVIEW_REQUESTED => :review_requested,
|
2016-06-15 07:20:30 -04:00
|
|
|
MENTIONED => :mentioned,
|
|
|
|
BUILD_FAILED => :build_failed,
|
2016-07-11 06:02:11 -04:00
|
|
|
MARKED => :marked,
|
2016-12-14 09:37:31 -05:00
|
|
|
APPROVAL_REQUIRED => :approval_required,
|
2016-12-05 07:12:22 -05:00
|
|
|
UNMERGEABLE => :unmergeable,
|
2020-07-09 05:09:27 -04:00
|
|
|
DIRECTLY_ADDRESSED => :directly_addressed,
|
|
|
|
MERGE_TRAIN_REMOVED => :merge_train_removed
|
2017-02-21 18:32:18 -05:00
|
|
|
}.freeze
|
2016-06-15 07:20:30 -04:00
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
belongs_to :author, class_name: "User"
|
2016-02-17 14:45:32 -05:00
|
|
|
belongs_to :note
|
2016-02-12 08:17:42 -05:00
|
|
|
belongs_to :project
|
2018-07-16 09:35:19 -04:00
|
|
|
belongs_to :group
|
2019-03-02 12:31:36 -05:00
|
|
|
belongs_to :target, -> {
|
|
|
|
if self.klass.respond_to?(:with_api_entity_associations)
|
|
|
|
self.with_api_entity_associations
|
|
|
|
else
|
|
|
|
self
|
|
|
|
end
|
|
|
|
}, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations
|
2019-05-21 15:20:27 -04:00
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
belongs_to :user
|
2019-05-21 15:20:27 -04:00
|
|
|
belongs_to :issue, -> { where("target_type = 'Issue'") }, foreign_key: :target_id
|
2016-02-12 08:17:42 -05:00
|
|
|
|
2016-02-12 13:45:44 -05:00
|
|
|
delegate :name, :email, to: :author, prefix: true, allow_nil: true
|
|
|
|
|
2018-07-16 09:35:19 -04:00
|
|
|
validates :action, :target_type, :user, presence: true
|
2018-02-04 14:46:14 -05:00
|
|
|
validates :author, presence: true
|
2016-03-18 09:27:26 -04:00
|
|
|
validates :target_id, presence: true, unless: :for_commit?
|
|
|
|
validates :commit_id, presence: true, if: :for_commit?
|
2018-07-16 09:35:19 -04:00
|
|
|
validates :project, presence: true, unless: :group_id
|
|
|
|
validates :group, presence: true, unless: :project_id
|
2016-02-12 08:17:42 -05:00
|
|
|
|
2020-01-31 22:09:04 -05:00
|
|
|
scope :for_ids, -> (ids) { where(id: ids) }
|
2016-02-12 13:45:44 -05:00
|
|
|
scope :pending, -> { with_state(:pending) }
|
|
|
|
scope :done, -> { with_state(:done) }
|
2018-09-20 09:18:04 -04:00
|
|
|
scope :for_action, -> (action) { where(action: action) }
|
|
|
|
scope :for_author, -> (author) { where(author: author) }
|
2020-01-31 22:09:04 -05:00
|
|
|
scope :for_user, -> (user) { where(user: user) }
|
2019-11-15 10:06:12 -05:00
|
|
|
scope :for_project, -> (projects) { where(project: projects) }
|
|
|
|
scope :for_undeleted_projects, -> { joins(:project).merge(Project.without_deleted) }
|
2018-09-20 09:18:04 -04:00
|
|
|
scope :for_group, -> (group) { where(group: group) }
|
|
|
|
scope :for_type, -> (type) { where(target_type: type) }
|
2018-09-20 11:05:26 -04:00
|
|
|
scope :for_target, -> (id) { where(target_id: id) }
|
|
|
|
scope :for_commit, -> (id) { where(commit_id: id) }
|
2019-06-24 13:48:50 -04:00
|
|
|
scope :with_entity_associations, -> { preload(:target, :author, :note, group: :route, project: [:route, { namespace: :route }]) }
|
2019-05-21 15:20:27 -04:00
|
|
|
scope :joins_issue_and_assignees, -> { left_joins(issue: :assignees) }
|
2016-02-12 13:45:44 -05:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
enum resolved_by_action: { system_done: 0, api_all_done: 1, api_done: 2, mark_all_done: 3, mark_done: 4 }, _prefix: :resolved_by
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
state_machine :state, initial: :pending do
|
2016-02-15 14:49:28 -05:00
|
|
|
event :done do
|
2016-03-16 19:56:23 -04:00
|
|
|
transition [:pending] => :done
|
2016-02-15 14:49:28 -05:00
|
|
|
end
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
state :pending
|
|
|
|
state :done
|
|
|
|
end
|
2016-02-12 13:45:44 -05:00
|
|
|
|
2018-07-16 09:35:19 -04:00
|
|
|
after_save :keep_around_commit, if: :commit_id
|
2016-07-03 19:58:58 -04:00
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
class << self
|
2019-10-17 11:06:17 -04:00
|
|
|
# Returns all todos for the given group ids and their descendants.
|
2018-09-20 09:18:04 -04:00
|
|
|
#
|
2019-10-17 11:06:17 -04:00
|
|
|
# group_ids - Group Ids to retrieve todos for.
|
2018-09-20 09:18:04 -04:00
|
|
|
#
|
|
|
|
# Returns an `ActiveRecord::Relation`.
|
2019-10-17 11:06:17 -04:00
|
|
|
def for_group_ids_and_descendants(group_ids)
|
|
|
|
groups = Group.groups_including_descendants_by(group_ids)
|
2018-09-20 09:18:04 -04:00
|
|
|
|
|
|
|
from_union([
|
|
|
|
for_project(Project.for_group(groups)),
|
|
|
|
for_group(groups)
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
# Returns `true` if the current user has any todos for the given target with the optional given state.
|
2018-09-20 11:05:26 -04:00
|
|
|
#
|
|
|
|
# target - The value of the `target_type` column, such as `Issue`.
|
2019-09-18 10:02:45 -04:00
|
|
|
# state - The value of the `state` column, such as `pending` or `done`.
|
|
|
|
def any_for_target?(target, state = nil)
|
|
|
|
state.nil? ? exists?(target: target) : exists?(target: target, state: state)
|
2018-09-20 11:05:26 -04:00
|
|
|
end
|
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
# Updates attributes of a relation of todos to the new state.
|
2018-09-20 11:05:26 -04:00
|
|
|
#
|
2020-05-28 14:08:37 -04:00
|
|
|
# new_attributes - The new attributes of the todos.
|
2018-09-20 11:05:26 -04:00
|
|
|
#
|
|
|
|
# Returns an `Array` containing the IDs of the updated todos.
|
2020-05-28 14:08:37 -04:00
|
|
|
def batch_update(**new_attributes)
|
|
|
|
# Only update those that have different state
|
|
|
|
base = where.not(state: new_attributes[:state]).except(:order)
|
2018-09-20 11:05:26 -04:00
|
|
|
ids = base.pluck(:id)
|
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
base.update_all(new_attributes.merge(updated_at: Time.current))
|
2018-09-20 11:05:26 -04:00
|
|
|
|
|
|
|
ids
|
|
|
|
end
|
|
|
|
|
2017-03-10 06:10:48 -05:00
|
|
|
# Priority sorting isn't displayed in the dropdown, because we don't show
|
|
|
|
# milestones, but still show something if the user has a URL with that
|
|
|
|
# selected.
|
2018-04-04 05:19:47 -04:00
|
|
|
def sort_by_attribute(method)
|
2018-03-05 11:41:48 -05:00
|
|
|
sorted =
|
|
|
|
case method.to_s
|
|
|
|
when 'priority', 'label_priority' then order_by_labels_priority
|
|
|
|
else order_by(method)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Break ties with the ID column for pagination
|
|
|
|
sorted.order(id: :desc)
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Order by priority depending on which issue/merge request the Todo belongs to
|
|
|
|
# Todos with highest priority first then oldest todos
|
|
|
|
# Need to order by created_at last because of differences on Mysql and Postgres when joining by type "Merge_request/Issue"
|
|
|
|
def order_by_labels_priority
|
2020-12-09 16:09:43 -05:00
|
|
|
highest_priority = highest_label_priority(
|
2016-10-27 20:26:56 -04:00
|
|
|
target_type_column: "todos.target_type",
|
2016-10-17 16:03:06 -04:00
|
|
|
target_column: "todos.target_id",
|
|
|
|
project_column: "todos.project_id"
|
2020-12-09 16:09:43 -05:00
|
|
|
).to_sql
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
select("#{table_name}.*, (#{highest_priority}) AS highest_priority")
|
|
|
|
.order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC'))
|
|
|
|
.order('todos.created_at')
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-17 08:07:33 -04:00
|
|
|
def resource_parent
|
2018-07-16 09:35:19 -04:00
|
|
|
project
|
|
|
|
end
|
|
|
|
|
2016-12-14 09:37:31 -05:00
|
|
|
def unmergeable?
|
|
|
|
action == UNMERGEABLE
|
|
|
|
end
|
|
|
|
|
2016-03-08 13:22:50 -05:00
|
|
|
def build_failed?
|
|
|
|
action == BUILD_FAILED
|
|
|
|
end
|
|
|
|
|
2017-04-10 11:51:15 -04:00
|
|
|
def assigned?
|
|
|
|
action == ASSIGNED
|
|
|
|
end
|
|
|
|
|
2020-09-09 08:08:22 -04:00
|
|
|
def review_requested?
|
|
|
|
action == REVIEW_REQUESTED
|
|
|
|
end
|
|
|
|
|
2020-07-09 05:09:27 -04:00
|
|
|
def merge_train_removed?
|
|
|
|
action == MERGE_TRAIN_REMOVED
|
|
|
|
end
|
|
|
|
|
2019-11-12 10:06:26 -05:00
|
|
|
def done?
|
|
|
|
state == 'done'
|
|
|
|
end
|
|
|
|
|
2016-06-15 07:20:30 -04:00
|
|
|
def action_name
|
|
|
|
ACTION_NAMES[action]
|
|
|
|
end
|
|
|
|
|
2016-02-18 14:16:39 -05:00
|
|
|
def body
|
|
|
|
if note.present?
|
|
|
|
note.note
|
|
|
|
else
|
|
|
|
target.title
|
|
|
|
end
|
2016-02-17 14:45:32 -05:00
|
|
|
end
|
2016-03-16 19:31:30 -04:00
|
|
|
|
|
|
|
def for_commit?
|
|
|
|
target_type == "Commit"
|
|
|
|
end
|
|
|
|
|
2020-05-07 08:09:46 -04:00
|
|
|
def for_design?
|
|
|
|
target_type == DesignManagement::Design.name
|
|
|
|
end
|
|
|
|
|
2020-06-05 14:08:19 -04:00
|
|
|
def for_alert?
|
|
|
|
target_type == AlertManagement::Alert.name
|
|
|
|
end
|
|
|
|
|
2016-03-16 19:31:30 -04:00
|
|
|
# override to return commits, which are not active record
|
|
|
|
def target
|
|
|
|
if for_commit?
|
2016-03-18 12:27:27 -04:00
|
|
|
project.commit(commit_id) rescue nil
|
2016-03-16 19:31:30 -04:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-18 12:24:47 -04:00
|
|
|
def target_reference
|
2016-03-16 19:31:30 -04:00
|
|
|
if for_commit?
|
2019-06-27 01:56:08 -04:00
|
|
|
target.reference_link_text
|
2016-03-16 19:31:30 -04:00
|
|
|
else
|
2019-06-27 01:56:08 -04:00
|
|
|
target.to_reference
|
2016-03-16 19:31:30 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-03 19:58:58 -04:00
|
|
|
|
2017-04-10 11:51:15 -04:00
|
|
|
def self_added?
|
|
|
|
author == user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self_assigned?
|
2020-09-21 08:09:34 -04:00
|
|
|
self_added? && (assigned? || review_requested?)
|
2017-04-10 11:51:15 -04:00
|
|
|
end
|
|
|
|
|
2016-07-03 19:58:58 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def keep_around_commit
|
|
|
|
project.repository.keep_around(self.commit_id)
|
|
|
|
end
|
2016-02-12 08:17:42 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Todo.prepend_if_ee('EE::Todo')
|