BigW Consortium Gitlab

event.rb 6.07 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
  def visible_to_user?(user = nil)
77 78 79 80
    if push?
      true
    elsif membership_changed?
      true
81 82
    elsif created_project?
      true
83 84
    elsif issue? || issue_note?
      Ability.abilities.allowed?(user, :read_issue, note? ? note_target : target)
85
    else
86
      ((merge_request? || note?) && target) || milestone?
87
    end
88 89
  end

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

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

  def created?
    action == CREATED
104 105
  end

106
  def push?
107
    action == PUSHED && valid_push?
108 109
  end

110
  def merged?
111
    action == MERGED
112 113
  end

114
  def closed?
115
    action == CLOSED
116 117 118
  end

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

  def joined?
    action == JOINED
  end

  def left?
    action == LEFT
  end

130 131 132 133
  def destroyed?
    action == DESTROYED
  end

134 135 136 137 138 139
  def commented?
    action == COMMENTED
  end

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

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

  def created_target?
    created? && target
  end

150 151 152 153 154 155 156 157
  def milestone?
    target_type == "Milestone"
  end

  def note?
    target_type == "Note"
  end

158
  def issue?
159
    target_type == "Issue"
160 161
  end

162
  def merge_request?
163
    target_type == "MergeRequest"
164 165
  end

166 167
  def milestone
    target if milestone?
168 169
  end

170
  def issue
171
    target if issue?
172 173 174
  end

  def merge_request
175
    target if merge_request?
176 177
  end

178
  def note
179
    target if note?
180 181
  end

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

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

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

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

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

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

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

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

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

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

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

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

  def note_commit_id
    target.commit_id
  end

289 290 291 292
  def target_iid
    target.respond_to?(:iid) ? target.iid : target_id
  end

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

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

301 302 303 304
  def issue_note?
    note? && target && target.noteable_type == "Issue"
  end

Andrew8xx8 committed
305 306 307 308
  def note_project_snippet?
    target.noteable_type == "Snippet"
  end

Dmitriy Zaporozhets committed
309 310 311 312 313 314 315 316 317 318 319 320
  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
321 322 323 324 325 326 327 328
  def note_target_iid
    if note_target.respond_to?(:iid)
      note_target.iid
    else
      note_target_id
    end.to_s
  end

Dmitriy Zaporozhets committed
329 330 331 332 333 334 335
  def note_target_type
    if target.noteable_type.present?
      target.noteable_type.titleize
    else
      "Wall"
    end.downcase
  end
336 337 338 339 340 341 342 343 344 345

  def body?
    if push?
      push_with_commits?
    elsif note?
      true
    else
      target.respond_to? :title
    end
  end
346 347 348

  def reset_project_activity
    if project
Dmitriy Zaporozhets committed
349
      project.update_column(:last_activity_at, self.created_at)
350 351
    end
  end
352
end