BigW Consortium Gitlab

milestone.rb 7.22 KB
Newer Older
1
class Milestone < ActiveRecord::Base
2 3
  # Represents a "No Milestone" state used for filtering Issues and Merge
  # Requests that have no milestone assigned.
4 5 6
  MilestoneStruct = Struct.new(:title, :name, :id)
  None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
  Any = MilestoneStruct.new('Any Milestone', '', -1)
7
  Upcoming = MilestoneStruct.new('Upcoming', '#upcoming', -2)
8
  Started = MilestoneStruct.new('Started', '#started', -3)
9

10
  include CacheMarkdownField
11
  include InternalId
12
  include Sortable
13
  include Referable
14
  include StripAttribute
15
  include Milestoneish
16

17 18 19
  cache_markdown_field :title, pipeline: :single_line
  cache_markdown_field :description

20
  belongs_to :project
Felipe Artur committed
21 22
  belongs_to :group

23
  has_many :issues
24
  has_many :labels, -> { distinct.reorder('labels.title') },  through: :issues
25
  has_many :merge_requests
26
  has_many :events, as: :target, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
27

Felipe Artur committed
28 29
  scope :of_projects, ->(ids) { where(project_id: ids) }
  scope :of_groups, ->(ids) { where(group_id: ids) }
30 31
  scope :active, -> { with_state(:active) }
  scope :closed, -> { with_state(:closed) }
Felipe Artur committed
32 33 34 35 36 37 38 39 40 41 42 43
  scope :for_projects, -> { where(group: nil).includes(:project) }

  scope :for_projects_and_groups, -> (project_ids, group_ids) do
    conditions = []
    conditions << arel_table[:project_id].in(project_ids) if project_ids.compact.any?
    conditions << arel_table[:group_id].in(group_ids) if group_ids.compact.any?

    where(conditions.reduce(:or))
  end

  validates :group, presence: true, unless: :project
  validates :project, presence: true, unless: :group
44

Felipe Artur committed
45 46
  validate :uniqueness_of_title, if: :title_changed?
  validate :milestone_type_check
47
  validate :start_date_should_be_less_than_due_date, if: proc { |m| m.start_date.present? && m.due_date.present? }
48

49 50
  strip_attributes :title

Andrew8xx8 committed
51
  state_machine :state, initial: :active do
52
    event :close do
Andrew8xx8 committed
53
      transition active: :closed
54 55 56
    end

    event :activate do
Andrew8xx8 committed
57
      transition closed: :active
58 59 60 61 62 63
    end

    state :closed

    state :active
  end
64

65 66
  alias_attribute :name, :title

67
  class << self
68 69 70 71 72 73 74
    # 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.
75
    def search(query)
76 77 78 79
      t = arel_table
      pattern = "%#{query}%"

      where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
80
    end
Felipe Artur committed
81 82 83 84 85 86 87 88

    def filter_by_state(milestones, state)
      case state
      when 'closed' then milestones.closed
      when 'all' then milestones
      else milestones.active
      end
    end
89 90
  end

91 92 93 94
  def self.reference_prefix
    '%'
  end

95
  def self.reference_pattern
96 97 98
    # 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.
99
    @reference_pattern ||= %r{
100 101 102
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}
      (?:
103 104 105
        (?<milestone_iid>
          \d+(?!\S\w)\b # Integer-based milestone iid, or
        ) |
106
        (?<milestone_name>
107 108
          [^"\s]+\b |  # String-based single-word milestone title, or
          "[^"]+"      # String-based multi-word milestone surrounded in quotes
109 110 111
        )
      )
    }x
112 113 114
  end

  def self.link_reference_pattern
115
    @link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
116 117
  end

118 119 120 121
  def self.upcoming_ids_by_projects(projects)
    rel = unscoped.of_projects(projects).active.where('due_date > ?', Time.now)

    if Gitlab::Database.postgresql?
122
      rel.order(:project_id, :due_date).select('DISTINCT ON (project_id) id')
123
    else
124 125 126 127 128
      rel
        .group(:project_id)
        .having('due_date = MIN(due_date)')
        .pluck(:id, :project_id, :due_date)
        .map(&:first)
129
    end
130 131
  end

132
  def participants
133
    User.joins(assigned_issues: :milestone).where("milestones.id = ?", id).uniq
134 135
  end

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  def self.sort(method)
    case method.to_s
    when 'due_date_asc'
      reorder(Gitlab::Database.nulls_last_order('due_date', 'ASC'))
    when 'due_date_desc'
      reorder(Gitlab::Database.nulls_last_order('due_date', 'DESC'))
    when 'start_date_asc'
      reorder(Gitlab::Database.nulls_last_order('start_date', 'ASC'))
    when 'start_date_desc'
      reorder(Gitlab::Database.nulls_last_order('start_date', 'DESC'))
    else
      order_by(method)
    end
  end

151
  ##
152 153 154
  # Returns the String necessary to reference this Milestone in Markdown. Group
  # milestones only support name references, and do not support cross-project
  # references.
155 156 157 158 159
  #
  # format - Symbol format to use (default: :iid, optional: :name)
  #
  # Examples:
  #
160 161 162 163
  #   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"
164
  #
165
  def to_reference(from = nil, format: :name, full: false)
166 167
    format_reference = milestone_format_reference(format)
    reference = "#{self.class.reference_prefix}#{format_reference}"
168

169
    if project
170
      "#{project.to_reference(from, full: full)}#{reference}"
171 172 173
    else
      reference
    end
174 175
  end

176
  def reference_link_text(from = nil)
177
    self.title
178 179
  end

180 181 182 183
  def milestoneish_ids
    id
  end

Felipe Artur committed
184 185 186 187
  def for_display
    self
  end

188
  def can_be_closed?
189
    active? && issues.opened.count.zero?
190 191
  end

192
  def author_id
193
    nil
194
  end
195

196
  def title=(value)
197
    write_attribute(:title, sanitize_title(value)) if value.present?
198 199
  end

Felipe Artur committed
200 201 202 203 204 205 206 207
  def safe_title
    title.to_slug.normalize.to_s
  end

  def parent
    group || project
  end

208
  def group_milestone?
Felipe Artur committed
209 210 211
    group_id.present?
  end

212
  def project_milestone?
Felipe Artur committed
213 214 215
    project_id.present?
  end

216 217
  private

Felipe Artur committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  # Milestone titles must be unique across project milestones and group milestones
  def uniqueness_of_title
    if project
      relation = Milestone.for_projects_and_groups([project_id], [project.group&.id])
    elsif group
      project_ids = group.projects.map(&:id)
      relation = Milestone.for_projects_and_groups(project_ids, [group.id])
    end

    title_exists = relation.find_by_title(title)
    errors.add(:title, "already being used for another group or project milestone.") if title_exists
  end

  # Milestone should be either a project milestone or a group milestone
  def milestone_type_check
    if group_id && project_id
      field = project_id_changed? ? :project_id : :group_id
      errors.add(field, "milestone should belong either to a project or a group.")
    end
  end

239
  def milestone_format_reference(format = :iid)
240
    raise ArgumentError, 'Unknown format' unless [:iid, :name].include?(format)
241

242 243 244 245
    if group_milestone? && format == :iid
      raise ArgumentError, 'Cannot refer to a group milestone by an internal id!'
    end

246 247 248
    if format == :name && !name.include?('"')
      %("#{name}")
    else
249
      iid
250 251
    end
  end
252 253 254 255

  def sanitize_title(value)
    CGI.unescape_html(Sanitize.clean(value.to_s))
  end
256 257 258

  def start_date_should_be_less_than_due_date
    if due_date <= start_date
259
      errors.add(:due_date, "must be greater than start date")
260 261
    end
  end
262 263

  def issues_finder_params
264
    { project_id: project_id }
265
  end
266
end