Skip to content
Snippets Groups Projects

Use a separate table for storing push events

Merged Yorick Peterse requested to merge split-events-into-push-events into master
Compare and Show latest version
12 files
+ 639
96
Compare changes
  • Side-by-side
  • Inline
Files
12
+ 64
19
@@ -44,12 +44,10 @@ class Event < ActiveRecord::Base
delegate :title, to: :issue, prefix: true, allow_nil: true
delegate :title, to: :merge_request, prefix: true, allow_nil: true
delegate :title, to: :note, prefix: true, allow_nil: true
delegate :ref_type, to: :push_event_payload, allow_nil: true
belongs_to :author, class_name: "User"
belongs_to :project
belongs_to :target, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
has_one :push_event_payload
# For Hash only
serialize :data # rubocop:disable Cop/ActiveRecordSerialize
@@ -73,7 +71,31 @@ class Event < ActiveRecord::Base
scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) }
self.inheritance_column = 'action'
class << self
def find_sti_class(action)
if action.to_i == PUSHED
PushEvent
else
Event
end
end
def subclass_from_attributes(attrs)
# Without this Rails will keep calling this method on the returned class,
# resulting in an infinite loop.
return unless self == Event
action = attrs.with_indifferent_access[inheritance_column].to_i
if action == PUSHED
PushEvent
else
nil
end
end
# Update Gitlab::ContributionsCalendar#activity_dates if this changes
def contributions
where("action = ? OR (target_type IN (?) AND action IN (?)) OR (target_type = ? AND action = ?)",
@@ -242,23 +264,25 @@ def action_name
end
def valid_push?
push_event_payload ? push_event_payload.ref.present? : false
data[:ref] && ref_name.present?
rescue
false
end
def tag?
push_event_payload ? push_event_payload.tag? : false
Gitlab::Git.tag_ref?(data[:ref])
end
def branch?
push_event_payload ? push_event_payload.branch? : false
Gitlab::Git.branch_ref?(data[:ref])
end
def new_ref?
push_event_payload.created?
Gitlab::Git.blank_ref?(commit_from)
end
def rm_ref?
push_event_payload.removed?
Gitlab::Git.blank_ref?(commit_to)
end
def md_ref?
@@ -266,37 +290,58 @@ def md_ref?
end
def commit_from
push_event_payload.first_commit
data[:before]
end
def commit_to
push_event_payload.last_commit
data[:after]
end
def ref_name
push_event_payload.ref
if tag?
tag_name
else
branch_name
end
end
def branch_name
@branch_name ||= Gitlab::Git.ref_name(data[:ref])
end
def tag_name
@tag_name ||= Gitlab::Git.ref_name(data[:ref])
end
# Max 20 commits from push DESC
def commits
[
{
id: push_event_payload.commit_after,
message: push_event_payload.commit_message
}
]
@commits ||= (data[:commits] || []).reverse
end
def commit_message
commit = commits.last
commit[:message] if commit
end
def commit_id
commit_to || commit_from
end
def commits_count
push_event_payload.commit_count
data[:total_commits_count] || commits.count || 0
end
def ref_type
tag? ? "tag" : "branch"
end
def push_with_commits?
commit_from && commit_to
!commits.empty? && commit_from && commit_to
end
def last_push_to_non_root?
branch? && project.default_branch != ref_name
branch? && project.default_branch != branch_name
end
def target_iid
Loading