2012-04-08 21:28:58 +00:00
|
|
|
class Milestone < ActiveRecord::Base
|
2015-06-28 20:12:32 +00:00
|
|
|
# Represents a "No Milestone" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no milestone assigned.
|
2015-12-02 13:10:43 +00:00
|
|
|
MilestoneStruct = Struct.new(:title, :name, :id)
|
|
|
|
None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
|
|
|
|
Any = MilestoneStruct.new('Any Milestone', '', -1)
|
2016-03-11 17:46:14 +00:00
|
|
|
Upcoming = MilestoneStruct.new('Upcoming', '#upcoming', -2)
|
2015-06-28 20:12:32 +00:00
|
|
|
|
2016-10-06 21:17:11 +00:00
|
|
|
include CacheMarkdownField
|
2013-08-21 09:16:26 +00:00
|
|
|
include InternalId
|
2015-02-06 00:49:41 +00:00
|
|
|
include Sortable
|
2015-12-24 13:43:07 +00:00
|
|
|
include Referable
|
2015-11-26 15:16:50 +00:00
|
|
|
include StripAttribute
|
2016-03-07 04:07:19 +00:00
|
|
|
include Milestoneish
|
2013-08-21 09:16:26 +00:00
|
|
|
|
2016-10-06 21:17:11 +00:00
|
|
|
cache_markdown_field :title, pipeline: :single_line
|
|
|
|
cache_markdown_field :description
|
|
|
|
|
2012-04-08 21:28:58 +00:00
|
|
|
belongs_to :project
|
|
|
|
has_many :issues
|
2016-02-22 18:17:38 +00:00
|
|
|
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
|
2012-10-26 13:53:45 +00:00
|
|
|
has_many :merge_requests
|
2016-03-07 04:07:19 +00:00
|
|
|
has_many :participants, -> { distinct.reorder('users.name') }, through: :issues, source: :assignee
|
2016-07-01 13:34:10 +00:00
|
|
|
has_many :events, as: :target, dependent: :destroy
|
2012-04-08 21:28:58 +00:00
|
|
|
|
2013-02-18 09:10:09 +00:00
|
|
|
scope :active, -> { with_state(:active) }
|
|
|
|
scope :closed, -> { with_state(:closed) }
|
2014-04-24 17:35:06 +00:00
|
|
|
scope :of_projects, ->(ids) { where(project_id: ids) }
|
2012-12-14 05:34:05 +00:00
|
|
|
|
2016-02-02 09:44:30 +00:00
|
|
|
validates :title, presence: true, uniqueness: { scope: :project_id }
|
2012-10-09 00:10:04 +00:00
|
|
|
validates :project, presence: true
|
2016-11-15 17:48:30 +00:00
|
|
|
validate :start_date_should_be_less_than_due_date, if: Proc.new { |m| m.start_date.present? && m.due_date.present? }
|
2013-02-18 09:10:09 +00:00
|
|
|
|
2015-11-26 15:16:50 +00:00
|
|
|
strip_attributes :title
|
|
|
|
|
2013-02-18 13:22:18 +00:00
|
|
|
state_machine :state, initial: :active do
|
2013-02-18 09:10:09 +00:00
|
|
|
event :close do
|
2013-02-18 13:22:18 +00:00
|
|
|
transition active: :closed
|
2013-02-18 09:10:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :activate do
|
2013-02-18 13:22:18 +00:00
|
|
|
transition closed: :active
|
2013-02-18 09:10:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
state :closed
|
|
|
|
|
|
|
|
state :active
|
|
|
|
end
|
2012-04-08 21:28:58 +00:00
|
|
|
|
2015-10-09 04:24:55 +00:00
|
|
|
alias_attribute :name, :title
|
|
|
|
|
2015-08-13 14:48:21 +00:00
|
|
|
class << self
|
2016-03-01 16:05:26 +00: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 14:48:21 +00:00
|
|
|
def search(query)
|
2016-03-01 16:05:26 +00:00
|
|
|
t = arel_table
|
|
|
|
pattern = "%#{query}%"
|
|
|
|
|
|
|
|
where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
|
2015-08-13 14:48:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-31 02:12:34 +00:00
|
|
|
def self.reference_prefix
|
|
|
|
'%'
|
|
|
|
end
|
|
|
|
|
2015-12-24 13:43:07 +00:00
|
|
|
def self.reference_pattern
|
2016-04-06 00:35:43 +00:00
|
|
|
# NOTE: The iid pattern only matches when all characters on the expression
|
|
|
|
# are digits, so it will match %2 but not %2.1 because that's probably a
|
|
|
|
# milestone name and we want it to be matched as such.
|
2016-05-19 04:45:25 +00:00
|
|
|
@reference_pattern ||= %r{
|
2016-03-31 02:12:34 +00:00
|
|
|
(#{Project.reference_pattern})?
|
|
|
|
#{Regexp.escape(reference_prefix)}
|
|
|
|
(?:
|
2016-04-06 00:35:43 +00:00
|
|
|
(?<milestone_iid>
|
|
|
|
\d+(?!\S\w)\b # Integer-based milestone iid, or
|
|
|
|
) |
|
2016-03-31 02:12:34 +00:00
|
|
|
(?<milestone_name>
|
2016-04-06 00:35:43 +00:00
|
|
|
[^"\s]+\b | # String-based single-word milestone title, or
|
|
|
|
"[^"]+" # String-based multi-word milestone surrounded in quotes
|
2016-03-31 02:12:34 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}x
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.link_reference_pattern
|
2016-03-24 15:41:48 +00:00
|
|
|
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
|
|
|
|
2016-05-11 16:38:34 +00: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 09:23:21 +00:00
|
|
|
rel.order(:project_id, :due_date).select('DISTINCT ON (project_id) id')
|
2016-05-11 16:38:34 +00:00
|
|
|
else
|
|
|
|
rel.
|
|
|
|
group(:project_id).
|
|
|
|
having('due_date = MIN(due_date)').
|
|
|
|
pluck(:id, :project_id, :due_date).
|
|
|
|
map(&:first)
|
|
|
|
end
|
2016-03-11 17:46:14 +00:00
|
|
|
end
|
|
|
|
|
2016-04-01 00:54:00 +00:00
|
|
|
##
|
|
|
|
# Returns the String necessary to reference this Milestone in Markdown
|
|
|
|
#
|
|
|
|
# format - Symbol format to use (default: :iid, optional: :name)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2016-11-02 23:49:13 +00:00
|
|
|
# Milestone.first.to_reference # => "%1"
|
|
|
|
# Milestone.first.to_reference(format: :name) # => "%\"goal\""
|
|
|
|
# Milestone.first.to_reference(cross_namespace_project) # => "gitlab-org/gitlab-ce%1"
|
|
|
|
# Milestone.first.to_reference(same_namespace_project) # => "gitlab-ce%1"
|
2016-04-01 00:54:00 +00:00
|
|
|
#
|
|
|
|
def to_reference(from_project = nil, format: :iid)
|
2016-03-31 02:12:34 +00:00
|
|
|
format_reference = milestone_format_reference(format)
|
|
|
|
reference = "#{self.class.reference_prefix}#{format_reference}"
|
2016-01-07 11:26:05 +00:00
|
|
|
|
2016-11-02 23:49:13 +00:00
|
|
|
"#{project.to_reference(from_project)}#{reference}"
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reference_link_text(from_project = nil)
|
2016-01-05 15:45:53 +00:00
|
|
|
self.title
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
|
|
|
|
2012-12-14 05:34:05 +00:00
|
|
|
def can_be_closed?
|
2013-02-18 09:10:09 +00:00
|
|
|
active? && issues.opened.count.zero?
|
2012-12-19 03:14:05 +00:00
|
|
|
end
|
|
|
|
|
2016-03-17 20:59:30 +00:00
|
|
|
def is_empty?(user = nil)
|
|
|
|
total_items_count(user).zero?
|
2012-12-14 05:34:05 +00:00
|
|
|
end
|
|
|
|
|
2012-12-14 20:05:10 +00:00
|
|
|
def author_id
|
2014-03-25 12:01:52 +00:00
|
|
|
nil
|
2012-12-14 20:05:10 +00:00
|
|
|
end
|
2015-10-15 16:10:35 +00:00
|
|
|
|
2016-05-09 14:58:20 +00:00
|
|
|
def title=(value)
|
2016-09-26 23:47:34 +00:00
|
|
|
write_attribute(:title, sanitize_title(value)) if value.present?
|
2016-05-04 21:21:57 +00:00
|
|
|
end
|
|
|
|
|
2015-10-15 16:10:35 +00: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
|
2016-03-31 02:12:34 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-04-01 00:54:00 +00:00
|
|
|
def milestone_format_reference(format = :iid)
|
2016-05-19 04:45:25 +00:00
|
|
|
raise ArgumentError, 'Unknown format' unless [:iid, :name].include?(format)
|
2016-03-31 02:12:34 +00:00
|
|
|
|
|
|
|
if format == :name && !name.include?('"')
|
|
|
|
%("#{name}")
|
|
|
|
else
|
2016-04-01 00:54:00 +00:00
|
|
|
iid
|
2016-03-31 02:12:34 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-26 23:47:34 +00:00
|
|
|
|
|
|
|
def sanitize_title(value)
|
|
|
|
CGI.unescape_html(Sanitize.clean(value.to_s))
|
|
|
|
end
|
2016-11-15 17:48:30 +00:00
|
|
|
|
|
|
|
def start_date_should_be_less_than_due_date
|
|
|
|
if due_date <= start_date
|
|
|
|
errors.add(:start_date, "Can't be greater than due date")
|
|
|
|
end
|
|
|
|
end
|
2012-04-08 21:28:58 +00:00
|
|
|
end
|