summaryrefslogtreecommitdiffstats
path: root/app/models/merge_request_status.rb
blob: 7272d219e59371c68245bf9e417ca0b1daec3ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

class MergeRequestStatus < ActiveRecord::Base
  belongs_to :project

  validates_presence_of :project, :state, :name
  validates_format_of :color, :with => /#[0-9a-f]{3,6}/i,
    :message => "should be hex encoded (eg '#cccccc', like in CSS)", :allow_blank => true

  before_save :synchronize_merge_request_statuses

  def self.default
    find(:first, :conditions => {:default => true})
  end

  def self.create_defaults_for_project(project)
    project.merge_request_statuses.create!({
        :name => "Open",
        :state => MergeRequest::STATUS_OPEN,
        :color => "#408000",
        :default => true
      })
    project.merge_request_statuses.create!({
        :name => "Closed",
        :state => MergeRequest::STATUS_CLOSED,
        :color => "#AA0000"
      })    
  end

  def self.default_states
    {
      "Open" => MergeRequest::STATUS_OPEN,
      "Closed" => MergeRequest::STATUS_CLOSED
    }
  end

  def open?
    state == MergeRequest::STATUS_OPEN
  end

  def closed?
    state == MergeRequest::STATUS_CLOSED
  end

  # Updates the status of all the merge requests for the mainlines in
  # the project who has the same status_tag as this MergeRequestStatus
  def synchronize_merge_request_statuses
    if state_changed? || name_changed?
      # FIXME: doing it like this is a bit inefficient...
      merge_requests = self.project.repositories.mainlines.map(&:merge_requests).flatten
      old_name = (name_changed? ? name_change.first : name)
      merge_requests.select{|mr| mr.status_tag.to_s == old_name }.each do |mr|
        mr.status = self.state if state_changed?
        mr.status_tag = self.name if name_changed?
        mr.save!
      end
    end
  end
end