event.rb 5.9 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
11 12
#  created_at  :datetime
#  updated_at  :datetime
D
Dmitriy Zaporozhets 已提交
13 14 15 16
#  action      :integer
#  author_id   :integer
#

D
Dmitriy Zaporozhets 已提交
17
class Event < ActiveRecord::Base
18
  include Sortable
19
  default_scope { where.not(author_id: nil) }
20

A
Andrew8xx8 已提交
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

N
Nihad Abbasov 已提交
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
N
Nihad Abbasov 已提交
36

R
randx 已提交
37
  belongs_to :author, class_name: "User"
D
Dmitriy Zaporozhets 已提交
38
  belongs_to :project
39
  belongs_to :target, polymorphic: true
40

R
randx 已提交
41 42
  # For Hash only
  serialize :data
43

44 45 46
  # Callbacks
  after_create :reset_project_activity

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

  scope :in_projects, ->(projects) do
J
Josh Frye 已提交
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

A
Andrey Kumanyaev 已提交
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

Y
Yorick Peterse 已提交
71 72 73
    def limit_recent(limit = 20, offset = nil)
      recent.limit(limit).offset(offset)
    end
D
Dmitriy Zaporozhets 已提交
74 75
  end

76
  def proper?(user = nil)
77 78 79 80
    if push?
      true
    elsif membership_changed?
      true
81 82
    elsif created_project?
      true
83 84
    elsif issue?
      Ability.abilities.allowed?(user, :read_issue, issue)
85
    else
86
      ((merge_request? || note?) && target) || milestone?
87
    end
D
Dmitriy Zaporozhets 已提交
88 89
  end

90 91
  def project_name
    if project
92
      project.name_with_namespace
93
    else
R
Riyad Preukschas 已提交
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?
A
Alex Denisov 已提交
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"
A
Alex Denisov 已提交
195 196
    elsif joined?
      'joined'
A
Alex Denisov 已提交
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
D
Dmitriy Zaporozhets 已提交
213 214

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

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

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

  def new_ref?
229
    Gitlab::Git.blank_ref?(commit_from)
D
Dmitriy Zaporozhets 已提交
230 231 232
  end

  def rm_ref?
233
    Gitlab::Git.blank_ref?(commit_to)
D
Dmitriy Zaporozhets 已提交
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])
D
Dmitriy Zaporozhets 已提交
258 259 260
  end

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

  # Max 20 commits from push DESC
  def commits
266
    @commits ||= (data[:commits] || []).reverse
D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
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

D
Dmitriy Zaporozhets 已提交
293
  def note_short_commit_id
294
    Commit.truncate_sha(note_commit_id)
D
Dmitriy Zaporozhets 已提交
295 296 297 298 299 300
  end

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

A
Andrew8xx8 已提交
301 302 303 304
  def note_project_snippet?
    target.noteable_type == "Snippet"
  end

D
Dmitriy Zaporozhets 已提交
305 306 307 308 309 310 311 312 313 314 315 316
  def note_target
    target.noteable
  end

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

D
Dmitriy Zaporozhets 已提交
317 318 319 320 321 322 323 324
  def note_target_iid
    if note_target.respond_to?(:iid)
      note_target.iid
    else
      note_target_id
    end.to_s
  end

D
Dmitriy Zaporozhets 已提交
325 326 327 328 329 330 331
  def note_target_type
    if target.noteable_type.present?
      target.noteable_type.titleize
    else
      "Wall"
    end.downcase
  end
332 333 334 335 336 337 338 339 340 341

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

  def reset_project_activity
    if project
D
Dmitriy Zaporozhets 已提交
345
      project.update_column(:last_activity_at, self.created_at)
346 347
    end
  end
D
Dmitriy Zaporozhets 已提交
348
end