BigW Consortium Gitlab

event.rb 5.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# == 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
Dmitriy Zaporozhets committed
11 12
#  created_at  :datetime
#  updated_at  :datetime
13 14 15 16
#  action      :integer
#  author_id   :integer
#

17
class Event < ActiveRecord::Base
18
  include Sortable
19
  default_scope { where.not(author_id: nil) }
20

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

31 32 33
  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
34
  delegate :title, to: :note, prefix: true, allow_nil: true
35

randx committed
36
  belongs_to :author, class_name: "User"
37
  belongs_to :project
38
  belongs_to :target, polymorphic: true
39

40 41
  # For Hash only
  serialize :data
42

43 44 45
  # Callbacks
  after_create :reset_project_activity

Andrey Kumanyaev committed
46
  # Scopes
47
  scope :recent, -> { order("created_at DESC") }
48
  scope :code_push, -> { where(action: PUSHED) }
49
  scope :in_projects, ->(project_ids) { where(project_id: project_ids).recent }
50
  scope :with_associations, -> { includes(project: :namespace) }
51

Andrey Kumanyaev committed
52
  class << self
53 54 55 56 57
    def reset_event_cache_for(target)
      Event.where(target_id: target.id, target_type: target.class.to_s).
        order('id DESC').limit(100).
        update_all(updated_at: Time.now)
    end
58 59 60 61 62 63

    def contributions
      where("action = ? OR (target_type in (?) AND action in (?))",
            Event::PUSHED, ["MergeRequest", "Issue"],
            [Event::CREATED, Event::CLOSED, Event::MERGED])
    end
Dmitriy Zaporozhets committed
64 65
  end

66 67 68 69 70
  def proper?
    if push?
      true
    elsif membership_changed?
      true
71 72
    elsif created_project?
      true
73 74 75
    else
      (issue? || merge_request? || note? || milestone?) && target
    end
76 77
  end

78 79
  def project_name
    if project
80
      project.name_with_namespace
81
    else
82
      "(deleted project)"
83 84 85
    end
  end

86
  def target_title
87 88 89 90 91
    target.title if target && target.respond_to?(:title)
  end

  def created?
    action == CREATED
92 93
  end

94
  def push?
95
    action == PUSHED && valid_push?
96 97
  end

98
  def merged?
99
    action == MERGED
100 101
  end

102
  def closed?
103
    action == CLOSED
104 105 106
  end

  def reopened?
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    action == REOPENED
  end

  def joined?
    action == JOINED
  end

  def left?
    action == LEFT
  end

  def commented?
    action == COMMENTED
  end

  def membership_changed?
    joined? || left?
124 125
  end

126 127 128 129 130 131 132 133
  def created_project?
    created? && !target
  end

  def created_target?
    created? && target
  end

134 135 136 137 138 139 140 141
  def milestone?
    target_type == "Milestone"
  end

  def note?
    target_type == "Note"
  end

142
  def issue?
143
    target_type == "Issue"
144 145
  end

146
  def merge_request?
147
    target_type == "MergeRequest"
148 149
  end

150 151
  def milestone
    target if milestone?
152 153
  end

154
  def issue
155
    target if issue?
156 157 158
  end

  def merge_request
159
    target if merge_request?
160 161
  end

162
  def note
163
    target if note?
164 165
  end

166
  def action_name
167 168 169 170 171 172 173 174 175
    if push?
      if new_ref?
        "pushed new"
      elsif rm_ref?
        "deleted"
      else
        "pushed to"
      end
    elsif closed?
176 177
      "closed"
    elsif merged?
178
      "accepted"
179 180
    elsif joined?
      'joined'
181 182
    elsif left?
      'left'
183 184
    elsif commented?
      "commented on"
185
    elsif created_project?
186 187 188 189 190
      if project.import?
        "imported"
      else
        "created"
      end
191
    else
192
      "opened"
193 194
    end
  end
Dmitriy Zaporozhets committed
195 196

  def valid_push?
197
    data[:ref] && ref_name.present?
198
  rescue
Dmitriy Zaporozhets committed
199 200 201 202
    false
  end

  def tag?
203
    Gitlab::Git.tag_ref?(data[:ref])
Dmitriy Zaporozhets committed
204 205 206
  end

  def branch?
207
    Gitlab::Git.branch_ref?(data[:ref])
Dmitriy Zaporozhets committed
208 209 210
  end

  def new_ref?
211
    Gitlab::Git.blank_ref?(commit_from)
Dmitriy Zaporozhets committed
212 213 214
  end

  def rm_ref?
215
    Gitlab::Git.blank_ref?(commit_to)
Dmitriy Zaporozhets committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  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
239
    @branch_name ||= Gitlab::Git.ref_name(data[:ref])
Dmitriy Zaporozhets committed
240 241 242
  end

  def tag_name
243
    @tag_name ||= Gitlab::Git.ref_name(data[:ref])
Dmitriy Zaporozhets committed
244 245 246 247
  end

  # Max 20 commits from push DESC
  def commits
248
    @commits ||= (data[:commits] || []).reverse
Dmitriy Zaporozhets committed
249 250 251 252 253 254 255 256 257 258 259
  end

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

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

  def push_with_commits?
260
    !commits.empty? && commit_from && commit_to
Dmitriy Zaporozhets committed
261 262 263 264 265 266 267 268 269 270
  end

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

  def note_commit_id
    target.commit_id
  end

271 272 273 274
  def target_iid
    target.respond_to?(:iid) ? target.iid : target_id
  end

Dmitriy Zaporozhets committed
275
  def note_short_commit_id
276
    Commit.truncate_sha(note_commit_id)
Dmitriy Zaporozhets committed
277 278 279 280 281 282
  end

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

Andrew8xx8 committed
283 284 285 286
  def note_project_snippet?
    target.noteable_type == "Snippet"
  end

Dmitriy Zaporozhets committed
287 288 289 290 291 292 293 294 295 296 297 298
  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 committed
299 300 301 302 303 304 305 306
  def note_target_iid
    if note_target.respond_to?(:iid)
      note_target.iid
    else
      note_target_id
    end.to_s
  end

Dmitriy Zaporozhets committed
307 308 309 310 311 312 313
  def note_target_type
    if target.noteable_type.present?
      target.noteable_type.titleize
    else
      "Wall"
    end.downcase
  end
314 315 316 317 318 319 320 321 322 323

  def body?
    if push?
      push_with_commits?
    elsif note?
      true
    else
      target.respond_to? :title
    end
  end
324 325 326

  def reset_project_activity
    if project
Dmitriy Zaporozhets committed
327
      project.update_column(:last_activity_at, self.created_at)
328 329
    end
  end
330
end