2012-04-08 17:28:58 -04:00
|
|
|
class Milestone < ActiveRecord::Base
|
2015-06-28 16:12:32 -04:00
|
|
|
# Represents a "No Milestone" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no milestone assigned.
|
2015-12-02 08:10:43 -05:00
|
|
|
MilestoneStruct = Struct.new(:title, :name, :id)
|
|
|
|
None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
|
|
|
|
Any = MilestoneStruct.new('Any Milestone', '', -1)
|
2016-03-11 12:46:14 -05:00
|
|
|
Upcoming = MilestoneStruct.new('Upcoming', '#upcoming', -2)
|
2015-06-28 16:12:32 -04:00
|
|
|
|
2013-08-21 05:16:26 -04:00
|
|
|
include InternalId
|
2015-02-05 19:49:41 -05:00
|
|
|
include Sortable
|
2015-12-24 08:43:07 -05:00
|
|
|
include Referable
|
2015-11-26 10:16:50 -05:00
|
|
|
include StripAttribute
|
2016-03-06 23:07:19 -05:00
|
|
|
include Milestoneish
|
2013-08-21 05:16:26 -04:00
|
|
|
|
2012-04-08 17:28:58 -04:00
|
|
|
belongs_to :project
|
|
|
|
has_many :issues
|
2016-02-22 13:17:38 -05:00
|
|
|
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
|
2012-10-26 09:53:45 -04:00
|
|
|
has_many :merge_requests
|
2016-03-06 23:07:19 -05:00
|
|
|
has_many :participants, -> { distinct.reorder('users.name') }, through: :issues, source: :assignee
|
2012-04-08 17:28:58 -04:00
|
|
|
|
2013-02-18 04:10:09 -05:00
|
|
|
scope :active, -> { with_state(:active) }
|
|
|
|
scope :closed, -> { with_state(:closed) }
|
2014-04-24 13:35:06 -04:00
|
|
|
scope :of_projects, ->(ids) { where(project_id: ids) }
|
2012-12-14 00:34:05 -05:00
|
|
|
|
2016-02-02 04:44:30 -05:00
|
|
|
validates :title, presence: true, uniqueness: { scope: :project_id }
|
2012-10-08 20:10:04 -04:00
|
|
|
validates :project, presence: true
|
2013-02-18 04:10:09 -05:00
|
|
|
|
2015-11-26 10:16:50 -05:00
|
|
|
strip_attributes :title
|
|
|
|
|
2013-02-18 08:22:18 -05:00
|
|
|
state_machine :state, initial: :active do
|
2013-02-18 04:10:09 -05:00
|
|
|
event :close do
|
2013-02-18 08:22:18 -05:00
|
|
|
transition active: :closed
|
2013-02-18 04:10:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
event :activate do
|
2013-02-18 08:22:18 -05:00
|
|
|
transition closed: :active
|
2013-02-18 04:10:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
state :closed
|
|
|
|
|
|
|
|
state :active
|
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
|
2015-10-09 00:24:55 -04:00
|
|
|
alias_attribute :name, :title
|
|
|
|
|
2015-08-13 10:48:21 -04:00
|
|
|
class << self
|
2016-03-01 11:05:26 -05:00
|
|
|
# Searches for milestones matching the given query.
|
|
|
|
#
|
|
|
|
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
|
|
|
|
#
|
|
|
|
# query - The search query as a String
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
2015-08-13 10:48:21 -04:00
|
|
|
def search(query)
|
2016-03-01 11:05:26 -05:00
|
|
|
t = arel_table
|
|
|
|
pattern = "%#{query}%"
|
|
|
|
|
|
|
|
where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
|
2015-08-13 10:48:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-24 08:43:07 -05:00
|
|
|
def self.reference_pattern
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.link_reference_pattern
|
2016-03-24 11:41:48 -04:00
|
|
|
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
2016-05-11 12:38:34 -04:00
|
|
|
def self.upcoming_ids_by_projects(projects)
|
|
|
|
rel = unscoped.of_projects(projects).active.where('due_date > ?', Time.now)
|
|
|
|
|
|
|
|
if Gitlab::Database.postgresql?
|
2016-05-16 05:23:21 -04:00
|
|
|
rel.order(:project_id, :due_date).select('DISTINCT ON (project_id) id')
|
2016-05-11 12:38:34 -04:00
|
|
|
else
|
|
|
|
rel.
|
|
|
|
group(:project_id).
|
|
|
|
having('due_date = MIN(due_date)').
|
|
|
|
pluck(:id, :project_id, :due_date).
|
|
|
|
map(&:first)
|
|
|
|
end
|
2016-03-11 12:46:14 -05:00
|
|
|
end
|
|
|
|
|
2015-12-24 08:43:07 -05:00
|
|
|
def to_reference(from_project = nil)
|
2016-01-07 06:26:05 -05:00
|
|
|
escaped_title = self.title.gsub("]", "\\]")
|
|
|
|
|
2016-03-24 12:00:26 -04:00
|
|
|
h = Gitlab::Routing.url_helpers
|
2016-01-07 06:26:05 -05:00
|
|
|
url = h.namespace_project_milestone_url(self.project.namespace, self.project, self)
|
|
|
|
|
|
|
|
"[#{escaped_title}](#{url})"
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reference_link_text(from_project = nil)
|
2016-01-05 10:45:53 -05:00
|
|
|
self.title
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
2012-12-14 00:34:05 -05:00
|
|
|
def expired?
|
|
|
|
if due_date
|
2012-12-23 11:18:27 -05:00
|
|
|
due_date.past?
|
2012-12-14 00:34:05 -05:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|
2015-08-31 00:51:34 -04:00
|
|
|
|
2012-04-08 17:28:58 -04:00
|
|
|
def expires_at
|
2012-12-23 11:18:27 -05:00
|
|
|
if due_date
|
|
|
|
if due_date.past?
|
2016-01-06 17:27:56 -05:00
|
|
|
"expired on #{due_date.to_s(:medium)}"
|
2012-12-23 11:18:27 -05:00
|
|
|
else
|
2016-01-06 17:27:56 -05:00
|
|
|
"expires on #{due_date.to_s(:medium)}"
|
2012-12-23 11:18:27 -05:00
|
|
|
end
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|
2012-12-14 00:34:05 -05:00
|
|
|
|
|
|
|
def can_be_closed?
|
2013-02-18 04:10:09 -05:00
|
|
|
active? && issues.opened.count.zero?
|
2012-12-18 22:14:05 -05:00
|
|
|
end
|
|
|
|
|
2016-03-17 16:59:30 -04:00
|
|
|
def is_empty?(user = nil)
|
|
|
|
total_items_count(user).zero?
|
2012-12-14 00:34:05 -05:00
|
|
|
end
|
|
|
|
|
2012-12-14 15:05:10 -05:00
|
|
|
def author_id
|
2014-03-25 08:01:52 -04:00
|
|
|
nil
|
2012-12-14 15:05:10 -05:00
|
|
|
end
|
2015-10-15 12:10:35 -04:00
|
|
|
|
2016-05-09 10:58:20 -04:00
|
|
|
def title=(value)
|
|
|
|
write_attribute(:title, Sanitize.clean(value.to_s)) if value.present?
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
|
|
|
|
2015-10-15 12:10:35 -04:00
|
|
|
# Sorts the issues for the given IDs.
|
|
|
|
#
|
|
|
|
# This method runs a single SQL query using a CASE statement to update the
|
|
|
|
# position of all issues in the current milestone (scoped to the list of IDs).
|
|
|
|
#
|
|
|
|
# Given the ids [10, 20, 30] this method produces a SQL query something like
|
|
|
|
# the following:
|
|
|
|
#
|
|
|
|
# UPDATE issues
|
|
|
|
# SET position = CASE
|
|
|
|
# WHEN id = 10 THEN 1
|
|
|
|
# WHEN id = 20 THEN 2
|
|
|
|
# WHEN id = 30 THEN 3
|
|
|
|
# ELSE position
|
|
|
|
# END
|
|
|
|
# WHERE id IN (10, 20, 30);
|
|
|
|
#
|
|
|
|
# This method expects that the IDs given in `ids` are already Fixnums.
|
|
|
|
def sort_issues(ids)
|
|
|
|
pairs = []
|
|
|
|
|
|
|
|
ids.each_with_index do |id, index|
|
|
|
|
pairs << id
|
|
|
|
pairs << index + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
conditions = 'WHEN id = ? THEN ? ' * ids.length
|
|
|
|
|
|
|
|
issues.where(id: ids).
|
|
|
|
update_all(["position = CASE #{conditions} ELSE position END", *pairs])
|
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|