event.rb 5.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# == Schema Information
#
# Table name: events
#
#  id          :integer          not null, primary key
#  target_type :string(255)
#  target_id   :integer
#  title       :string(255)
#  data        :text
#  project_id  :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  action      :integer
#  author_id   :integer
#

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
class Event < ActiveRecord::Base
18 19 20
  attr_accessible :project, :action, :data, :author_id, :project_id,
                  :target_id, :target_type

21
  default_scope { where.not(author_id: nil) }
22

23 24 25 26 27 28 29 30 31
  CREATED   = 1
  UPDATED   = 2
  CLOSED    = 3
  REOPENED  = 4
  PUSHED    = 5
  COMMENTED = 6
  MERGED    = 7
  JOINED    = 8 # User joined project
  LEFT      = 9 # User left project
32

Nihad Abbasov's avatar
Nihad Abbasov committed
33 34 35 36
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
  delegate :title, to: :issue, prefix: true, allow_nil: true
  delegate :title, to: :merge_request, prefix: true, allow_nil: true

randx's avatar
randx committed
37
  belongs_to :author, class_name: "User"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
38
  belongs_to :project
39
  belongs_to :target, polymorphic: true
40

randx's avatar
randx committed
41 42
  # For Hash only
  serialize :data
43

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
44
  # Scopes
Andrew8xx8's avatar
Andrew8xx8 committed
45
  scope :recent, -> { order("created_at DESC") }
46
  scope :code_push, -> { where(action: PUSHED) }
47
  scope :in_projects, ->(project_ids) { where(project_id: project_ids).recent }
48

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
49
  class << self
50
    def create_ref_event(project, user, ref, action = 'add', prefix = 'refs/heads')
51 52
      commit = project.repository.commit(ref.target)

53 54
      if action.to_s == 'add'
        before = '00000000'
55
        after = commit.id
56
      else
57
        before = commit.id
58 59 60
        after = '00000000'
      end

61 62 63 64
      Event.create(
        project: project,
        action: Event::PUSHED,
        data: {
65
          ref: "#{prefix}/#{ref.name}",
66 67
          before: before,
          after: after
68 69 70 71
        },
        author_id: user.id
      )
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
72 73
  end

74 75 76 77 78 79 80 81
  def proper?
    if push?
      true
    elsif membership_changed?
      true
    else
      (issue? || merge_request? || note? || milestone?) && target
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82 83
  end

84 85
  def project_name
    if project
86
      project.name_with_namespace
87
    else
Riyad Preukschas's avatar
Riyad Preukschas committed
88
      "(deleted project)"
89 90 91
    end
  end

92
  def target_title
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
93 94 95
    if target && target.respond_to?(:title)
      target.title
    end
96 97
  end

98
  def push?
99
    action == self.class::PUSHED && valid_push?
100 101
  end

102
  def merged?
103
    action == self.class::MERGED
104 105
  end

106
  def closed?
107
    action == self.class::CLOSED
108 109 110
  end

  def reopened?
111
    action == self.class::REOPENED
112 113
  end

114 115 116 117 118 119 120 121
  def milestone?
    target_type == "Milestone"
  end

  def note?
    target_type == "Note"
  end

122
  def issue?
123
    target_type == "Issue"
124 125
  end

126
  def merge_request?
127
    target_type == "MergeRequest"
128 129
  end

130
  def joined?
131
    action == JOINED
Alex Denisov's avatar
Alex Denisov committed
132 133 134
  end

  def left?
135
    action == LEFT
Alex Denisov's avatar
Alex Denisov committed
136 137 138 139
  end

  def membership_changed?
    joined? || left?
140 141
  end

142
  def issue
143 144 145 146 147 148 149
    target if target_type == "Issue"
  end

  def merge_request
    target if target_type == "MergeRequest"
  end

150 151 152 153
  def action_name
    if closed?
      "closed"
    elsif merged?
154
      "accepted"
155 156
    elsif joined?
      'joined'
Alex Denisov's avatar
Alex Denisov committed
157 158
    elsif left?
      'left'
159
    else
160
      "opened"
161 162
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
163 164

  def valid_push?
165
    data[:ref] && ref_name.present?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  rescue => ex
    false
  end

  def tag?
    data[:ref]["refs/tags"]
  end

  def branch?
    data[:ref]["refs/heads"]
  end

  def new_branch?
    commit_from =~ /^00000/
  end

  def new_ref?
    commit_from =~ /^00000/
  end

  def rm_ref?
    commit_to =~ /^00000/
  end

  def md_ref?
    !(rm_ref? || new_ref?)
  end

  def commit_from
    data[:before]
  end

  def commit_to
    data[:after]
  end

  def ref_name
    if tag?
      tag_name
    else
      branch_name
    end
  end

  def branch_name
    @branch_name ||= data[:ref].gsub("refs/heads/", "")
  end

  def tag_name
    @tag_name ||= data[:ref].gsub("refs/tags/", "")
  end

  # Max 20 commits from push DESC
  def commits
220
    @commits ||= (data[:commits] || []).reverse
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  end

  def commits_count
    data[:total_commits_count] || commits.count || 0
  end

  def ref_type
    tag? ? "tag" : "branch"
  end

  def push_action_name
    if new_ref?
      "pushed new"
    elsif rm_ref?
      "deleted"
    else
      "pushed to"
    end
  end

  def push_with_commits?
242
    md_ref? && commits.any? && commit_from && commit_to
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
243 244 245 246 247 248 249 250 251 252
  end

  def last_push_to_non_root?
    branch? && project.default_branch != branch_name
  end

  def note_commit_id
    target.commit_id
  end

253 254 255 256
  def target_iid
    target.respond_to?(:iid) ? target.iid : target_id
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
257 258 259 260 261 262 263 264
  def note_short_commit_id
    note_commit_id[0..8]
  end

  def note_commit?
    target.noteable_type == "Commit"
  end

Andrew8xx8's avatar
Andrew8xx8 committed
265 266 267 268
  def note_project_snippet?
    target.noteable_type == "Snippet"
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
269 270 271 272 273 274 275 276 277 278 279 280
  def note_target
    target.noteable
  end

  def note_target_id
    if note_commit?
      target.commit_id
    else
      target.noteable_id.to_s
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
281 282 283 284 285 286 287 288
  def note_target_iid
    if note_target.respond_to?(:iid)
      note_target.iid
    else
      note_target_id
    end.to_s
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
289 290 291 292 293 294 295 296 297 298 299
  def wall_note?
    target.noteable_type.blank?
  end

  def note_target_type
    if target.noteable_type.present?
      target.noteable_type.titleize
    else
      "Wall"
    end.downcase
  end
300 301 302 303 304 305 306 307 308 309

  def body?
    if push?
      push_with_commits?
    elsif note?
      true
    else
      target.respond_to? :title
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
310
end