Class: Event

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Sortable
Defined in:
app/models/event.rb

Overview

Schema Information

Table name: events

id          :integer          not null, primary key
target_type :string
target_id   :integer
title       :string
data        :text
project_id  :integer
created_at  :datetime
updated_at  :datetime
action      :integer
author_id   :integer

Constant Summary collapse

CREATED =
1
UPDATED =
2
CLOSED =
3
REOPENED =
4
PUSHED =
5
COMMENTED =
6
MERGED =
7
JOINED =

User joined project

8
LEFT =

User left project

9
DESTROYED =
10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.contributionsObject


65
66
67
68
69
# File 'app/models/event.rb', line 65

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

.limit_recent(limit = 20, offset = nil) ⇒ Object


71
72
73
# File 'app/models/event.rb', line 71

def limit_recent(limit = 20, offset = nil)
  recent.limit(limit).offset(offset)
end

.reset_event_cache_for(target) ⇒ Object


59
60
61
62
63
# File 'app/models/event.rb', line 59

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

Instance Method Details

#action_nameObject


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
# File 'app/models/event.rb', line 182

def action_name
  if push?
    if new_ref?
      "pushed new"
    elsif rm_ref?
      "deleted"
    else
      "pushed to"
    end
  elsif closed?
    "closed"
  elsif merged?
    "accepted"
  elsif joined?
    'joined'
  elsif left?
    'left'
  elsif destroyed?
    'destroyed'
  elsif commented?
    "commented on"
  elsif created_project?
    if project.external_import?
      "imported"
    else
      "created"
    end
  else
    "opened"
  end
end

#body?Boolean

Returns:

  • (Boolean)

337
338
339
340
341
342
343
344
345
# File 'app/models/event.rb', line 337

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

#branch?Boolean

Returns:

  • (Boolean)

224
225
226
# File 'app/models/event.rb', line 224

def branch?
  Gitlab::Git.branch_ref?(data[:ref])
end

#branch_nameObject


256
257
258
# File 'app/models/event.rb', line 256

def branch_name
  @branch_name ||= Gitlab::Git.ref_name(data[:ref])
end

#closed?Boolean

Returns:

  • (Boolean)

114
115
116
# File 'app/models/event.rb', line 114

def closed?
  action == CLOSED
end

#commented?Boolean

Returns:

  • (Boolean)

134
135
136
# File 'app/models/event.rb', line 134

def commented?
  action == COMMENTED
end

#commit_fromObject


240
241
242
# File 'app/models/event.rb', line 240

def commit_from
  data[:before]
end

#commit_toObject


244
245
246
# File 'app/models/event.rb', line 244

def commit_to
  data[:after]
end

#commitsObject

Max 20 commits from push DESC


265
266
267
# File 'app/models/event.rb', line 265

def commits
  @commits ||= (data[:commits] || []).reverse
end

#commits_countObject


269
270
271
# File 'app/models/event.rb', line 269

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

#created?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'app/models/event.rb', line 102

def created?
  action == CREATED
end

#created_project?Boolean

Returns:

  • (Boolean)

142
143
144
# File 'app/models/event.rb', line 142

def created_project?
  created? && !target && target_type.nil?
end

#created_target?Boolean

Returns:

  • (Boolean)

146
147
148
# File 'app/models/event.rb', line 146

def created_target?
  created? && target
end

#destroyed?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'app/models/event.rb', line 130

def destroyed?
  action == DESTROYED
end

#issueObject


170
171
172
# File 'app/models/event.rb', line 170

def issue
  target if issue?
end

#issue?Boolean

Returns:

  • (Boolean)

158
159
160
# File 'app/models/event.rb', line 158

def issue?
  target_type == "Issue"
end

#issue_note?Boolean

Returns:

  • (Boolean)

301
302
303
# File 'app/models/event.rb', line 301

def issue_note?
  note? && target && target.noteable_type == "Issue"
end

#joined?Boolean

Returns:

  • (Boolean)

122
123
124
# File 'app/models/event.rb', line 122

def joined?
  action == JOINED
end

#last_push_to_non_root?Boolean

Returns:

  • (Boolean)

281
282
283
# File 'app/models/event.rb', line 281

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

#left?Boolean

Returns:

  • (Boolean)

126
127
128
# File 'app/models/event.rb', line 126

def left?
  action == LEFT
end

#md_ref?Boolean

Returns:

  • (Boolean)

236
237
238
# File 'app/models/event.rb', line 236

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

#membership_changed?Boolean

Returns:

  • (Boolean)

138
139
140
# File 'app/models/event.rb', line 138

def membership_changed?
  joined? || left?
end

#merge_requestObject


174
175
176
# File 'app/models/event.rb', line 174

def merge_request
  target if merge_request?
end

#merge_request?Boolean

Returns:

  • (Boolean)

162
163
164
# File 'app/models/event.rb', line 162

def merge_request?
  target_type == "MergeRequest"
end

#merged?Boolean

Returns:

  • (Boolean)

110
111
112
# File 'app/models/event.rb', line 110

def merged?
  action == MERGED
end

#milestoneObject


166
167
168
# File 'app/models/event.rb', line 166

def milestone
  target if milestone?
end

#milestone?Boolean

Returns:

  • (Boolean)

150
151
152
# File 'app/models/event.rb', line 150

def milestone?
  target_type == "Milestone"
end

#new_ref?Boolean

Returns:

  • (Boolean)

228
229
230
# File 'app/models/event.rb', line 228

def new_ref?
  Gitlab::Git.blank_ref?(commit_from)
end

#noteObject


178
179
180
# File 'app/models/event.rb', line 178

def note
  target if note?
end

#note?Boolean

Returns:

  • (Boolean)

154
155
156
# File 'app/models/event.rb', line 154

def note?
  target_type == "Note"
end

#note_commit?Boolean

Returns:

  • (Boolean)

297
298
299
# File 'app/models/event.rb', line 297

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

#note_commit_idObject


285
286
287
# File 'app/models/event.rb', line 285

def note_commit_id
  target.commit_id
end

#note_project_snippet?Boolean

Returns:

  • (Boolean)

305
306
307
# File 'app/models/event.rb', line 305

def note_project_snippet?
  target.noteable_type == "Snippet"
end

#note_short_commit_idObject


293
294
295
# File 'app/models/event.rb', line 293

def note_short_commit_id
  Commit.truncate_sha(note_commit_id)
end

#note_targetObject


309
310
311
# File 'app/models/event.rb', line 309

def note_target
  target.noteable
end

#note_target_idObject


313
314
315
316
317
318
319
# File 'app/models/event.rb', line 313

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

#note_target_iidObject


321
322
323
324
325
326
327
# File 'app/models/event.rb', line 321

def note_target_iid
  if note_target.respond_to?(:iid)
    note_target.iid
  else
    note_target_id
  end.to_s
end

#note_target_typeObject


329
330
331
332
333
334
335
# File 'app/models/event.rb', line 329

def note_target_type
  if target.noteable_type.present?
    target.noteable_type.titleize
  else
    "Wall"
  end.downcase
end

#project_nameObject


90
91
92
93
94
95
96
# File 'app/models/event.rb', line 90

def project_name
  if project
    project.name_with_namespace
  else
    "(deleted project)"
  end
end

#push?Boolean

Returns:

  • (Boolean)

106
107
108
# File 'app/models/event.rb', line 106

def push?
  action == PUSHED && valid_push?
end

#push_with_commits?Boolean

Returns:

  • (Boolean)

277
278
279
# File 'app/models/event.rb', line 277

def push_with_commits?
  !commits.empty? && commit_from && commit_to
end

#ref_nameObject


248
249
250
251
252
253
254
# File 'app/models/event.rb', line 248

def ref_name
  if tag?
    tag_name
  else
    branch_name
  end
end

#ref_typeObject


273
274
275
# File 'app/models/event.rb', line 273

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

#reopened?Boolean

Returns:

  • (Boolean)

118
119
120
# File 'app/models/event.rb', line 118

def reopened?
  action == REOPENED
end

#reset_project_activityObject


347
348
349
350
351
# File 'app/models/event.rb', line 347

def reset_project_activity
  if project && Gitlab::ExclusiveLease.new("project:update_last_activity_at:#{project.id}", timeout: 60).try_obtain
    project.update_column(:last_activity_at, self.created_at)
  end
end

#rm_ref?Boolean

Returns:

  • (Boolean)

232
233
234
# File 'app/models/event.rb', line 232

def rm_ref?
  Gitlab::Git.blank_ref?(commit_to)
end

#tag?Boolean

Returns:

  • (Boolean)

220
221
222
# File 'app/models/event.rb', line 220

def tag?
  Gitlab::Git.tag_ref?(data[:ref])
end

#tag_nameObject


260
261
262
# File 'app/models/event.rb', line 260

def tag_name
  @tag_name ||= Gitlab::Git.ref_name(data[:ref])
end

#target_iidObject


289
290
291
# File 'app/models/event.rb', line 289

def target_iid
  target.respond_to?(:iid) ? target.iid : target_id
end

#target_titleObject


98
99
100
# File 'app/models/event.rb', line 98

def target_title
  target.title if target && target.respond_to?(:title)
end

#valid_push?Boolean

Returns:

  • (Boolean)

214
215
216
217
218
# File 'app/models/event.rb', line 214

def valid_push?
  data[:ref] && ref_name.present?
rescue
  false
end

#visible_to_user?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)

76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/event.rb', line 76

def visible_to_user?(user = nil)
  if push?
    true
  elsif membership_changed?
    true
  elsif created_project?
    true
  elsif issue? || issue_note?
    Ability.abilities.allowed?(user, :read_issue, note? ? note_target : target)
  else
    ((merge_request? || note?) && target) || milestone?
  end
end