BigW Consortium Gitlab

event.rb 5.87 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
  DESTROYED = 10
31

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

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

41 42
  # For Hash only
  serialize :data
43

44 45 46
  # Callbacks
  after_create :reset_project_activity

Andrey Kumanyaev committed
47
  # Scopes
48
  scope :recent, -> { reorder(id: :desc) }
49
  scope :code_push, -> { where(action: PUSHED) }
50 51

  scope :in_projects, ->(projects) do
Josh Frye committed
52
    where(project_id: projects.map(&:id)).recent
53 54
  end

55
  scope :with_associations, -> { includes(project: :namespace) }
56
  scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) }
57

Andrey Kumanyaev committed
58
  class << self
59 60 61 62 63
    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
64 65 66 67 68 69

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

71 72 73
    def limit_recent(limit = 20, offset = nil)
      recent.limit(limit).offset(offset)
    end
Dmitriy Zaporozhets committed
74 75
  end

76 77 78 79 80
  def proper?
    if push?
      true
    elsif membership_changed?
      true
81 82
    elsif created_project?
      true
83
    else
84
      ((issue? || merge_request? || note?) && target) || milestone?
85
    end
86 87
  end

88 89
  def project_name
    if project
90
      project.name_with_namespace
91
    else
92
      "(deleted project)"
93 94 95
    end
  end

96
  def target_title
97 98 99 100 101
    target.title if target && target.respond_to?(:title)
  end

  def created?
    action == CREATED
102 103
  end

104
  def push?
105
    action == PUSHED && valid_push?
106 107
  end

108
  def merged?
109
    action == MERGED
110 111
  end

112
  def closed?
113
    action == CLOSED
114 115 116
  end

  def reopened?
117 118 119 120 121 122 123 124 125 126 127
    action == REOPENED
  end

  def joined?
    action == JOINED
  end

  def left?
    action == LEFT
  end

128 129 130 131
  def destroyed?
    action == DESTROYED
  end

132 133 134 135 136 137
  def commented?
    action == COMMENTED
  end

  def membership_changed?
    joined? || left?
138 139
  end

140
  def created_project?
141
    created? && !target && target_type.nil?
142 143 144 145 146 147
  end

  def created_target?
    created? && target
  end

148 149 150 151 152 153 154 155
  def milestone?
    target_type == "Milestone"
  end

  def note?
    target_type == "Note"
  end

156
  def issue?
157
    target_type == "Issue"
158 159
  end

160
  def merge_request?
161
    target_type == "MergeRequest"
162 163
  end

164 165
  def milestone
    target if milestone?
166 167
  end

168
  def issue
169
    target if issue?
170 171 172
  end

  def merge_request
173
    target if merge_request?
174 175
  end

176
  def note
177
    target if note?
178 179
  end

180
  def action_name
181 182 183 184 185 186 187 188 189
    if push?
      if new_ref?
        "pushed new"
      elsif rm_ref?
        "deleted"
      else
        "pushed to"
      end
    elsif closed?
190 191
      "closed"
    elsif merged?
192
      "accepted"
193 194
    elsif joined?
      'joined'
195 196
    elsif left?
      'left'
197 198
    elsif destroyed?
      'destroyed'
199 200
    elsif commented?
      "commented on"
201
    elsif created_project?
202
      if project.external_import?
203 204 205 206
        "imported"
      else
        "created"
      end
207
    else
208
      "opened"
209 210
    end
  end
Dmitriy Zaporozhets committed
211 212

  def valid_push?
213
    data[:ref] && ref_name.present?
214
  rescue
Dmitriy Zaporozhets committed
215 216 217 218
    false
  end

  def tag?
219
    Gitlab::Git.tag_ref?(data[:ref])
Dmitriy Zaporozhets committed
220 221 222
  end

  def branch?
223
    Gitlab::Git.branch_ref?(data[:ref])
Dmitriy Zaporozhets committed
224 225 226
  end

  def new_ref?
227
    Gitlab::Git.blank_ref?(commit_from)
Dmitriy Zaporozhets committed
228 229 230
  end

  def rm_ref?
231
    Gitlab::Git.blank_ref?(commit_to)
Dmitriy Zaporozhets committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  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
255
    @branch_name ||= Gitlab::Git.ref_name(data[:ref])
Dmitriy Zaporozhets committed
256 257 258
  end

  def tag_name
259
    @tag_name ||= Gitlab::Git.ref_name(data[:ref])
Dmitriy Zaporozhets committed
260 261 262 263
  end

  # Max 20 commits from push DESC
  def commits
264
    @commits ||= (data[:commits] || []).reverse
Dmitriy Zaporozhets committed
265 266 267 268 269 270 271 272 273 274 275
  end

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

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

  def push_with_commits?
276
    !commits.empty? && commit_from && commit_to
Dmitriy Zaporozhets committed
277 278 279 280 281 282 283 284 285 286
  end

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

  def note_commit_id
    target.commit_id
  end

287 288 289 290
  def target_iid
    target.respond_to?(:iid) ? target.iid : target_id
  end

Dmitriy Zaporozhets committed
291
  def note_short_commit_id
292
    Commit.truncate_sha(note_commit_id)
Dmitriy Zaporozhets committed
293 294 295 296 297 298
  end

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

Andrew8xx8 committed
299 300 301 302
  def note_project_snippet?
    target.noteable_type == "Snippet"
  end

Dmitriy Zaporozhets committed
303 304 305 306 307 308 309 310 311 312 313 314
  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
315 316 317 318 319 320 321 322
  def note_target_iid
    if note_target.respond_to?(:iid)
      note_target.iid
    else
      note_target_id
    end.to_s
  end

Dmitriy Zaporozhets committed
323 324 325 326 327 328 329
  def note_target_type
    if target.noteable_type.present?
      target.noteable_type.titleize
    else
      "Wall"
    end.downcase
  end
330 331 332 333 334 335 336 337 338 339

  def body?
    if push?
      push_with_commits?
    elsif note?
      true
    else
      target.respond_to? :title
    end
  end
340 341 342

  def reset_project_activity
    if project
Dmitriy Zaporozhets committed
343
      project.update_column(:last_activity_at, self.created_at)
344 345
    end
  end
346
end