Commit 3e59872d authored by Markus Koller's avatar Markus Koller

Merge branch '215154-refactor-events-to-use-enums' into 'master'

Refactor events to use enums

See merge request gitlab-org/gitlab!32117
parents 910d2550 19b81779
...@@ -72,9 +72,10 @@ class EventsFinder ...@@ -72,9 +72,10 @@ class EventsFinder
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def by_action(events) def by_action(events)
return events unless Event::ACTIONS[params[:action]] safe_action = Event.actions[params[:action]]
return events unless safe_action
events.where(action: Event::ACTIONS[params[:action]]) events.where(action: safe_action)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -10,35 +10,24 @@ class Event < ApplicationRecord ...@@ -10,35 +10,24 @@ class Event < ApplicationRecord
default_scope { reorder(nil) } default_scope { reorder(nil) }
CREATED = 1
UPDATED = 2
CLOSED = 3
REOPENED = 4
PUSHED = 5
COMMENTED = 6
MERGED = 7
JOINED = 8 # User joined project
LEFT = 9 # User left project
DESTROYED = 10
EXPIRED = 11 # User left project due to expiry
APPROVED = 12
ACTIONS = HashWithIndifferentAccess.new( ACTIONS = HashWithIndifferentAccess.new(
created: CREATED, created: 1,
updated: UPDATED, updated: 2,
closed: CLOSED, closed: 3,
reopened: REOPENED, reopened: 4,
pushed: PUSHED, pushed: 5,
commented: COMMENTED, commented: 6,
merged: MERGED, merged: 7,
joined: JOINED, joined: 8, # User joined project
left: LEFT, left: 9, # User left project
destroyed: DESTROYED, destroyed: 10,
expired: EXPIRED, expired: 11, # User left project due to expiry
approved: APPROVED approved: 12
).freeze ).freeze
WIKI_ACTIONS = [CREATED, UPDATED, DESTROYED].freeze private_constant :ACTIONS
WIKI_ACTIONS = [:created, :updated, :destroyed].freeze
TARGET_TYPES = HashWithIndifferentAccess.new( TARGET_TYPES = HashWithIndifferentAccess.new(
issue: Issue, issue: Issue,
...@@ -54,6 +43,8 @@ class Event < ApplicationRecord ...@@ -54,6 +43,8 @@ class Event < ApplicationRecord
RESET_PROJECT_ACTIVITY_INTERVAL = 1.hour RESET_PROJECT_ACTIVITY_INTERVAL = 1.hour
REPOSITORY_UPDATED_AT_INTERVAL = 5.minutes REPOSITORY_UPDATED_AT_INTERVAL = 5.minutes
enum action: ACTIONS, _suffix: true
delegate :name, :email, :public_email, :username, to: :author, prefix: true, allow_nil: true delegate :name, :email, :public_email, :username, to: :author, prefix: true, allow_nil: true
delegate :title, to: :issue, prefix: true, allow_nil: true delegate :title, to: :issue, prefix: true, allow_nil: true
delegate :title, to: :merge_request, prefix: true, allow_nil: true delegate :title, to: :merge_request, prefix: true, allow_nil: true
...@@ -83,8 +74,6 @@ class Event < ApplicationRecord ...@@ -83,8 +74,6 @@ class Event < ApplicationRecord
# Scopes # Scopes
scope :recent, -> { reorder(id: :desc) } scope :recent, -> { reorder(id: :desc) }
scope :code_push, -> { where(action: PUSHED) }
scope :merged, -> { where(action: MERGED) }
scope :for_wiki_page, -> { where(target_type: 'WikiPage::Meta') } scope :for_wiki_page, -> { where(target_type: 'WikiPage::Meta') }
# Needed to implement feature flag: can be removed when feature flag is removed # Needed to implement feature flag: can be removed when feature flag is removed
...@@ -115,7 +104,7 @@ class Event < ApplicationRecord ...@@ -115,7 +104,7 @@ class Event < ApplicationRecord
end end
def find_sti_class(action) def find_sti_class(action)
if action.to_i == PUSHED if actions.fetch(action, action) == actions[:pushed] # action can be integer or symbol
PushEvent PushEvent
else else
Event Event
...@@ -125,19 +114,15 @@ class Event < ApplicationRecord ...@@ -125,19 +114,15 @@ class Event < ApplicationRecord
# Update Gitlab::ContributionsCalendar#activity_dates if this changes # Update Gitlab::ContributionsCalendar#activity_dates if this changes
def contributions def contributions
where("action = ? OR (target_type IN (?) AND action IN (?)) OR (target_type = ? AND action = ?)", where("action = ? OR (target_type IN (?) AND action IN (?)) OR (target_type = ? AND action = ?)",
Event::PUSHED, actions[:pushed],
%w(MergeRequest Issue), [Event::CREATED, Event::CLOSED, Event::MERGED], %w(MergeRequest Issue), [actions[:created], actions[:closed], actions[:merged]],
"Note", Event::COMMENTED) "Note", actions[:commented])
end end
def limit_recent(limit = 20, offset = nil) def limit_recent(limit = 20, offset = nil)
recent.limit(limit).offset(offset) recent.limit(limit).offset(offset)
end end
def actions
ACTIONS.keys
end
def target_types def target_types
TARGET_TYPES.keys TARGET_TYPES.keys
end end
...@@ -161,46 +146,10 @@ class Event < ApplicationRecord ...@@ -161,46 +146,10 @@ class Event < ApplicationRecord
target.try(:title) target.try(:title)
end end
def created_action?
action == CREATED
end
def push_action? def push_action?
false false
end end
def merged_action?
action == MERGED
end
def closed_action?
action == CLOSED
end
def reopened_action?
action == REOPENED
end
def joined_action?
action == JOINED
end
def left_action?
action == LEFT
end
def expired_action?
action == EXPIRED
end
def destroyed_action?
action == DESTROYED
end
def commented_action?
action == COMMENTED
end
def membership_changed? def membership_changed?
joined_action? || left_action? || expired_action? joined_action? || left_action? || expired_action?
end end
...@@ -210,11 +159,11 @@ class Event < ApplicationRecord ...@@ -210,11 +159,11 @@ class Event < ApplicationRecord
end end
def created_wiki_page? def created_wiki_page?
wiki_page? && action == CREATED wiki_page? && created_action?
end end
def updated_wiki_page? def updated_wiki_page?
wiki_page? && action == UPDATED wiki_page? && updated_action?
end end
def created_target? def created_target?
......
...@@ -896,11 +896,11 @@ class MergeRequest < ApplicationRecord ...@@ -896,11 +896,11 @@ class MergeRequest < ApplicationRecord
end end
def merge_event def merge_event
@merge_event ||= target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::MERGED).last @merge_event ||= target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: :merged).last
end end
def closed_event def closed_event
@closed_event ||= target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::CLOSED).last @closed_event ||= target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: :closed).last
end end
def work_in_progress? def work_in_progress?
......
...@@ -445,7 +445,7 @@ class Project < ApplicationRecord ...@@ -445,7 +445,7 @@ class Project < ApplicationRecord
scope :archived, -> { where(archived: true) } scope :archived, -> { where(archived: true) }
scope :non_archived, -> { where(archived: false) } scope :non_archived, -> { where(archived: false) }
scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids).distinct } scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids).distinct }
scope :with_push, -> { joins(:events).where('events.action = ?', Event::PUSHED) } scope :with_push, -> { joins(:events).merge(Event.pushed_action) }
scope :with_project_feature, -> { joins('LEFT JOIN project_features ON projects.id = project_features.project_id') } scope :with_project_feature, -> { joins('LEFT JOIN project_features ON projects.id = project_features.project_id') }
scope :inc_routes, -> { includes(:route, namespace: :route) } scope :inc_routes, -> { includes(:route, namespace: :route) }
scope :with_statistics, -> { includes(:statistics) } scope :with_statistics, -> { includes(:statistics) }
......
...@@ -68,7 +68,7 @@ class PushEvent < Event ...@@ -68,7 +68,7 @@ class PushEvent < Event
end end
def self.sti_name def self.sti_name
PUSHED actions[:pushed]
end end
def push_action? def push_action?
...@@ -111,7 +111,7 @@ class PushEvent < Event ...@@ -111,7 +111,7 @@ class PushEvent < Event
end end
def validate_push_action def validate_push_action
return if action == PUSHED return if pushed_action?
errors.add(:action, "the action #{action.inspect} is not valid") errors.add(:action, "the action #{action.inspect} is not valid")
end end
......
...@@ -13,79 +13,79 @@ class EventCreateService ...@@ -13,79 +13,79 @@ class EventCreateService
def open_issue(issue, current_user) def open_issue(issue, current_user)
create_resource_event(issue, current_user, :opened) create_resource_event(issue, current_user, :opened)
create_record_event(issue, current_user, Event::CREATED) create_record_event(issue, current_user, :created)
end end
def close_issue(issue, current_user) def close_issue(issue, current_user)
create_resource_event(issue, current_user, :closed) create_resource_event(issue, current_user, :closed)
create_record_event(issue, current_user, Event::CLOSED) create_record_event(issue, current_user, :closed)
end end
def reopen_issue(issue, current_user) def reopen_issue(issue, current_user)
create_resource_event(issue, current_user, :reopened) create_resource_event(issue, current_user, :reopened)
create_record_event(issue, current_user, Event::REOPENED) create_record_event(issue, current_user, :reopened)
end end
def open_mr(merge_request, current_user) def open_mr(merge_request, current_user)
create_resource_event(merge_request, current_user, :opened) create_resource_event(merge_request, current_user, :opened)
create_record_event(merge_request, current_user, Event::CREATED) create_record_event(merge_request, current_user, :created)
end end
def close_mr(merge_request, current_user) def close_mr(merge_request, current_user)
create_resource_event(merge_request, current_user, :closed) create_resource_event(merge_request, current_user, :closed)
create_record_event(merge_request, current_user, Event::CLOSED) create_record_event(merge_request, current_user, :closed)
end end
def reopen_mr(merge_request, current_user) def reopen_mr(merge_request, current_user)
create_resource_event(merge_request, current_user, :reopened) create_resource_event(merge_request, current_user, :reopened)
create_record_event(merge_request, current_user, Event::REOPENED) create_record_event(merge_request, current_user, :reopened)
end end
def merge_mr(merge_request, current_user) def merge_mr(merge_request, current_user)
create_resource_event(merge_request, current_user, :merged) create_resource_event(merge_request, current_user, :merged)
create_record_event(merge_request, current_user, Event::MERGED) create_record_event(merge_request, current_user, :merged)
end end
def open_milestone(milestone, current_user) def open_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::CREATED) create_record_event(milestone, current_user, :created)
end end
def close_milestone(milestone, current_user) def close_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::CLOSED) create_record_event(milestone, current_user, :closed)
end end
def reopen_milestone(milestone, current_user) def reopen_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::REOPENED) create_record_event(milestone, current_user, :reopened)
end end
def destroy_milestone(milestone, current_user) def destroy_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::DESTROYED) create_record_event(milestone, current_user, :destroyed)
end end
def leave_note(note, current_user) def leave_note(note, current_user)
create_record_event(note, current_user, Event::COMMENTED) create_record_event(note, current_user, :commented)
end end
def join_project(project, current_user) def join_project(project, current_user)
create_event(project, current_user, Event::JOINED) create_event(project, current_user, :joined)
end end
def leave_project(project, current_user) def leave_project(project, current_user)
create_event(project, current_user, Event::LEFT) create_event(project, current_user, :left)
end end
def expired_leave_project(project, current_user) def expired_leave_project(project, current_user)
create_event(project, current_user, Event::EXPIRED) create_event(project, current_user, :expired)
end end
def create_project(project, current_user) def create_project(project, current_user)
create_event(project, current_user, Event::CREATED) create_event(project, current_user, :created)
end end
def push(project, current_user, push_data) def push(project, current_user, push_data)
...@@ -100,7 +100,7 @@ class EventCreateService ...@@ -100,7 +100,7 @@ class EventCreateService
# #
# @param [WikiPage::Meta] wiki_page_meta The event target # @param [WikiPage::Meta] wiki_page_meta The event target
# @param [User] author The event author # @param [User] author The event author
# @param [Integer] action One of the Event::WIKI_ACTIONS # @param [Symbol] action One of the Event::WIKI_ACTIONS
# #
# @return a tuple of event and either :found or :created # @return a tuple of event and either :found or :created
def wiki_event(wiki_page_meta, author, action) def wiki_event(wiki_page_meta, author, action)
...@@ -114,7 +114,7 @@ class EventCreateService ...@@ -114,7 +114,7 @@ class EventCreateService
event = create_record_event(wiki_page_meta, author, action) event = create_record_event(wiki_page_meta, author, action)
# Ensure that the event is linked in time to the metadata, for non-deletes # Ensure that the event is linked in time to the metadata, for non-deletes
unless action == Event::DESTROYED unless event.destroyed_action?
time_stamp = wiki_page_meta.updated_at time_stamp = wiki_page_meta.updated_at
event.update_columns(updated_at: time_stamp, created_at: time_stamp) event.update_columns(updated_at: time_stamp, created_at: time_stamp)
end end
...@@ -125,9 +125,9 @@ class EventCreateService ...@@ -125,9 +125,9 @@ class EventCreateService
private private
def existing_wiki_event(wiki_page_meta, action) def existing_wiki_event(wiki_page_meta, action)
if action == Event::DESTROYED if Event.actions.fetch(action) == Event.actions[:destroyed]
most_recent = Event.for_wiki_meta(wiki_page_meta).recent.first most_recent = Event.for_wiki_meta(wiki_page_meta).recent.first
return most_recent if most_recent.present? && most_recent.action == action return most_recent if most_recent.present? && Event.actions[most_recent.action] == Event.actions[action]
else else
Event.for_wiki_meta(wiki_page_meta).created_at(wiki_page_meta.updated_at).first Event.for_wiki_meta(wiki_page_meta).created_at(wiki_page_meta.updated_at).first
end end
...@@ -142,7 +142,7 @@ class EventCreateService ...@@ -142,7 +142,7 @@ class EventCreateService
# when creating push payload data will result in the event creation being # when creating push payload data will result in the event creation being
# rolled back as well. # rolled back as well.
event = Event.transaction do event = Event.transaction do
new_event = create_event(project, current_user, Event::PUSHED) new_event = create_event(project, current_user, :pushed)
service_class.new(new_event, push_data).execute service_class.new(new_event, push_data).execute
......
...@@ -21,11 +21,11 @@ module Git ...@@ -21,11 +21,11 @@ module Git
def event_action def event_action
case raw_change.operation case raw_change.operation
when :added when :added
Event::CREATED :created
when :deleted when :deleted
Event::DESTROYED :destroyed
else else
Event::UPDATED :updated
end end
end end
......
...@@ -22,7 +22,7 @@ module WikiPages ...@@ -22,7 +22,7 @@ module WikiPages
end end
def event_action def event_action
Event::CREATED :created
end end
end end
end end
...@@ -19,7 +19,7 @@ module WikiPages ...@@ -19,7 +19,7 @@ module WikiPages
end end
def event_action def event_action
Event::DESTROYED :destroyed
end end
end end
end end
...@@ -22,7 +22,7 @@ module WikiPages ...@@ -22,7 +22,7 @@ module WikiPages
end end
def event_action def event_action
Event::UPDATED :updated
end end
def slug_for_page(page) def slug_for_page(page)
......
...@@ -302,7 +302,7 @@ you will see two pushes with the same "from" SHA: ...@@ -302,7 +302,7 @@ you will see two pushes with the same "from" SHA:
```ruby ```ruby
p = Project.find_with_namespace('u/p') p = Project.find_with_namespace('u/p')
p.events.code_push.last(100).each do |e| p.events.pushed_action.last(100).each do |e|
printf "%-20.20s %8s...%8s (%s)\n", e.data[:ref], e.data[:before], e.data[:after], e.author.try(:username) printf "%-20.20s %8s...%8s (%s)\n", e.data[:ref], e.data[:before], e.data[:after], e.author.try(:username)
end end
``` ```
...@@ -311,7 +311,7 @@ GitLab 9.5 and above: ...@@ -311,7 +311,7 @@ GitLab 9.5 and above:
```ruby ```ruby
p = Project.find_by_full_path('u/p') p = Project.find_by_full_path('u/p')
p.events.code_push.last(100).each do |e| p.events.pushed_action.last(100).each do |e|
printf "%-20.20s %8s...%8s (%s)\n", e.push_event_payload[:ref], e.push_event_payload[:commit_from], e.push_event_payload[:commit_to], e.author.try(:username) printf "%-20.20s %8s...%8s (%s)\n", e.push_event_payload[:ref], e.push_event_payload[:commit_from], e.push_event_payload[:commit_to], e.author.try(:username)
end end
``` ```
......
...@@ -14,7 +14,7 @@ class MergeRequestsComplianceFinder < MergeRequestsFinder ...@@ -14,7 +14,7 @@ class MergeRequestsComplianceFinder < MergeRequestsFinder
lateral = Event lateral = Event
.select(:created_at, :target_id) .select(:created_at, :target_id)
.where('projects.id = project_id') .where('projects.id = project_id')
.merged .merged_action
.recent .recent
.limit(1) .limit(1)
.to_sql .to_sql
......
...@@ -53,7 +53,7 @@ class Burndown ...@@ -53,7 +53,7 @@ class Burndown
def closed_issues_events_count def closed_issues_events_count
strong_memoize(:closed_issues_events_count) do strong_memoize(:closed_issues_events_count) do
Event.closed.where(target: closed_issues).count Event.closed_action.where(target: closed_issues).count
end end
end end
...@@ -76,7 +76,7 @@ class Burndown ...@@ -76,7 +76,7 @@ class Burndown
strong_memoize(:milestone_events_per_issue) do strong_memoize(:milestone_events_per_issue) do
Event Event
.where(target: issues, action: [Event::CLOSED, Event::REOPENED]) .where(target: issues, action: [:closed, :reopened])
.where('created_at <= ?', end_date.end_of_day) .where('created_at <= ?', end_date.end_of_day)
.order(:created_at) .order(:created_at)
.group_by(&:target_id) .group_by(&:target_id)
...@@ -85,7 +85,7 @@ class Burndown ...@@ -85,7 +85,7 @@ class Burndown
# Use issue creation date as the source of truth for created events # Use issue creation date as the source of truth for created events
def transformed_create_event_for(issue) def transformed_create_event_for(issue)
build_burndown_event(issue.created_at, issue.weight, 'created') build_burndown_event(issue.created_at, issue.weight, :created)
end end
# Use issue events as the source of truth for events other than 'created' # Use issue events as the source of truth for events other than 'created'
...@@ -101,10 +101,10 @@ class Burndown ...@@ -101,10 +101,10 @@ class Burndown
# created for both of them. We can ignore these "duplicit" events because # created for both of them. We can ignore these "duplicit" events because
# if an event is already closed, another close action doesn't change its # if an event is already closed, another close action doesn't change its
# state. # state.
next if event.action == previous_action next if event.action.to_s == previous_action.to_s
previous_action = event.action previous_action = event.action
build_burndown_event(event.created_at, issue.weight, Event::ACTIONS.key(event.action).to_s) build_burndown_event(event.created_at, issue.weight, event.action)
end.compact end.compact
end end
...@@ -113,11 +113,11 @@ class Burndown ...@@ -113,11 +113,11 @@ class Burndown
return [] unless issue.closed? return [] unless issue.closed?
return [] if milestone_events_per_issue[issue.id]&.any?(&:closed_action?) return [] if milestone_events_per_issue[issue.id]&.any?(&:closed_action?)
build_burndown_event(start_date.beginning_of_day, issue.weight, 'closed') build_burndown_event(start_date.beginning_of_day, issue.weight, :closed)
end end
def build_burndown_event(created_at, issue_weight, action) def build_burndown_event(created_at, issue_weight, action)
{ created_at: created_at, weight: issue_weight, action: action } { created_at: created_at, weight: issue_weight, action: action.to_s }
end end
def filter_issues_created_before(date, issues) def filter_issues_created_before(date, issues)
......
...@@ -10,10 +10,6 @@ module EE ...@@ -10,10 +10,6 @@ module EE
scope :issues, -> { where(target_type: 'Issue') } scope :issues, -> { where(target_type: 'Issue') }
scope :merge_requests, -> { where(target_type: 'MergeRequest') } scope :merge_requests, -> { where(target_type: 'MergeRequest') }
scope :created, -> { where(action: ::Event::CREATED) }
scope :closed, -> { where(action: ::Event::CLOSED) }
scope :merged, -> { where(action: ::Event::MERGED) }
scope :approved, -> { where(action: ::Event::APPROVED) }
scope :totals_by_author, -> { group(:author_id).count } scope :totals_by_author, -> { group(:author_id).count }
scope :totals_by_author_target_type_action, -> { group(:author_id, :target_type, :action).count } scope :totals_by_author_target_type_action, -> { group(:author_id, :target_type, :action).count }
scope :epics, -> { where(target_type: 'Epic') } scope :epics, -> { where(target_type: 'Epic') }
...@@ -46,9 +42,5 @@ module EE ...@@ -46,9 +42,5 @@ module EE
def epic? def epic?
target_type == 'Epic' target_type == 'Epic'
end end
def approved_action?
action == ::Event::APPROVED
end
end end
end end
...@@ -3,19 +3,19 @@ ...@@ -3,19 +3,19 @@
module EE module EE
module EventCreateService module EventCreateService
def open_epic(epic, current_user) def open_epic(epic, current_user)
create_record_event(epic, current_user, ::Event::CREATED) create_record_event(epic, current_user, :created)
end end
def close_epic(epic, current_user) def close_epic(epic, current_user)
create_record_event(epic, current_user, ::Event::CLOSED) create_record_event(epic, current_user, :closed)
end end
def reopen_epic(epic, current_user) def reopen_epic(epic, current_user)
create_record_event(epic, current_user, ::Event::REOPENED) create_record_event(epic, current_user, :reopened)
end end
def approve_mr(merge_request, current_user) def approve_mr(merge_request, current_user)
create_record_event(merge_request, current_user, ::Event::APPROVED) create_record_event(merge_request, current_user, :approved)
end end
end end
end end
...@@ -15,68 +15,68 @@ module Gitlab ...@@ -15,68 +15,68 @@ module Gitlab
end end
def push_by_author_count def push_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash| all_counts.each_with_object({}) do |(event, count), hash|
hash[author_id] = count if target_type.nil? && action.eql?(Event::PUSHED) hash[event.author_id] = count if event.target_type.nil? && event.pushed_action?
end end
end end
def issues_created_by_author_count def issues_created_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash| all_counts.each_with_object({}) do |(event, count), hash|
hash[author_id] = count if target_type.eql?(Issue.name) && action.eql?(Event::CREATED) hash[event.author_id] = count if event.issue? && event.created_action?
end end
end end
def issues_closed_by_author_count def issues_closed_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash| all_counts.each_with_object({}) do |(event, count), hash|
hash[author_id] = count if target_type.eql?(Issue.name) && action.eql?(Event::CLOSED) hash[event.author_id] = count if event.issue? && event.closed_action?
end end
end end
def merge_requests_created_by_author_count def merge_requests_created_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash| all_counts.each_with_object({}) do |(event, count), hash|
hash[author_id] = count if target_type.eql?(MergeRequest.name) && action.eql?(Event::CREATED) hash[event.author_id] = count if event.merge_request? && event.created_action?
end end
end end
def merge_requests_merged_by_author_count def merge_requests_merged_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash| all_counts.each_with_object({}) do |(event, count), hash|
hash[author_id] = count if target_type.eql?(MergeRequest.name) && action.eql?(Event::MERGED) hash[event.author_id] = count if event.merge_request? && event.merged_action?
end end
end end
def total_events_by_author_count def total_events_by_author_count
all_counts.each_with_object({}) do |((author_id, _target_type, _action), count), hash| all_counts.each_with_object({}) do |(event, count), hash|
hash[author_id] ||= 0 hash[event.author_id] ||= 0
hash[author_id] += count hash[event.author_id] += count
end end
end end
def total_push_author_count def total_push_author_count
all_counts.count { |(_, _, action), _| action.eql?(Event::PUSHED) } all_counts.count { |event, _| event.pushed_action? }
end end
def total_push_count def total_push_count
all_counts.sum { |(_, _, action), count| action.eql?(Event::PUSHED) ? count : 0 } all_counts.sum { |event, count| event.pushed_action? ? count : 0 }
end end
def total_commit_count def total_commit_count
PushEventPayload.commit_count_for(base_query.code_push) PushEventPayload.commit_count_for(base_query.pushed_action)
end end
def total_merge_requests_created_count def total_merge_requests_created_count
all_counts.sum { |(_, target_type, action), count| target_type.eql?(MergeRequest.name) && action.eql?(Event::CREATED) ? count : 0 } all_counts.sum { |event, count| event.merge_request? && event.created_action? ? count : 0 }
end end
def total_merge_requests_merged_count def total_merge_requests_merged_count
all_counts.sum { |(_, target_type, action), count| target_type.eql?(MergeRequest.name) && action.eql?(Event::MERGED) ? count : 0 } all_counts.sum { |event, count| event.merge_request? && event.merged_action? ? count : 0 }
end end
def total_issues_created_count def total_issues_created_count
all_counts.sum { |(_, target_type, action), count| target_type.eql?(Issue.name) && action.eql?(Event::CREATED) ? count : 0 } all_counts.sum { |event, count| event.issue? && event.created_action? ? count : 0 }
end end
def total_issues_closed_count def total_issues_closed_count
all_counts.sum { |(_, target_type, action), count| target_type.eql?(Issue.name) && action.eql?(Event::CLOSED) ? count : 0 } all_counts.sum { |event, count| event.issue? && event.closed_action? ? count : 0 }
end end
def users def users
...@@ -112,8 +112,8 @@ module Gitlab ...@@ -112,8 +112,8 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def base_query def base_query
Event Event
.where(action: ::Event::PUSHED).or( .where(action: :pushed).or(
Event.where(target_type: [::MergeRequest.name, ::Issue.name], action: [::Event::CREATED, ::Event::CLOSED, ::Event::MERGED]) Event.where(target_type: [::MergeRequest.name, ::Issue.name], action: [:created, :closed, :merged])
) )
.where(Event.arel_table[:created_at].gteq(from)) .where(Event.arel_table[:created_at].gteq(from))
.joins(:project) .joins(:project)
...@@ -121,13 +121,21 @@ module Gitlab ...@@ -121,13 +121,21 @@ module Gitlab
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def all_counts
@all_counts ||= raw_counts.transform_keys do |author_id, target_type, action|
Event.new(author_id: author_id, target_type: target_type, action: action).tap do |event|
event.readonly!
end
end
end
# Format: # Format:
# { # {
# [user1_id, target_type, action] => count, # [user1_id, target_type, action] => count,
# [user2_id, target_type, action] => count # [user2_id, target_type, action] => count
# } # }
def all_counts def raw_counts
@all_counts ||= Rails.cache.fetch(cache_key, expires_in: 1.minute) do Rails.cache.fetch(cache_key, expires_in: 1.minute) do
base_query.totals_by_author_target_type_action base_query.totals_by_author_target_type_action
end end
end end
......
...@@ -23,7 +23,7 @@ describe Groups::ContributionAnalyticsController do ...@@ -23,7 +23,7 @@ describe Groups::ContributionAnalyticsController do
end end
def create_push_event(author, project) def create_push_event(author, project)
event = create_event(author, project, nil, Event::PUSHED) event = create_event(author, project, nil, :pushed)
PushEventPayloadService.new(event, push_data).execute PushEventPayloadService.new(event, push_data).execute
end end
...@@ -124,10 +124,10 @@ describe Groups::ContributionAnalyticsController do ...@@ -124,10 +124,10 @@ describe Groups::ContributionAnalyticsController do
before do before do
sign_in(user) sign_in(user)
create_event(user, project, issue, Event::CLOSED) create_event(user, project, issue, :closed)
create_event(user2, project, issue, Event::CLOSED) create_event(user2, project, issue, :closed)
create_event(user2, project, merge_request, Event::CREATED) create_event(user2, project, merge_request, :created)
create_event(user3, project, merge_request, Event::CREATED) create_event(user3, project, merge_request, :created)
create_push_event(user, project) create_push_event(user, project)
create_push_event(user3, project) create_push_event(user3, project)
end end
...@@ -168,7 +168,7 @@ describe Groups::ContributionAnalyticsController do ...@@ -168,7 +168,7 @@ describe Groups::ContributionAnalyticsController do
subgroup = create(:group, parent: group) subgroup = create(:group, parent: group)
subproject = create(:project, :repository, group: subgroup) subproject = create(:project, :repository, group: subgroup)
create_event(user, subproject, issue, Event::CLOSED) create_event(user, subproject, issue, :closed)
create_push_event(user, subproject) create_push_event(user, subproject)
get :show, params: { group_id: group.path }, format: :json get :show, params: { group_id: group.path }, format: :json
...@@ -184,7 +184,7 @@ describe Groups::ContributionAnalyticsController do ...@@ -184,7 +184,7 @@ describe Groups::ContributionAnalyticsController do
empty_group.add_reporter(user) empty_group.add_reporter(user)
create_event(user, other_project, issue, Event::CLOSED) create_event(user, other_project, issue, :closed)
create_push_event(user, other_project) create_push_event(user, other_project)
get :show, params: { group_id: empty_group.path }, format: :json get :show, params: { group_id: empty_group.path }, format: :json
......
...@@ -6,9 +6,10 @@ FactoryBot.modify do ...@@ -6,9 +6,10 @@ FactoryBot.modify do
group group
author(factory: :user) author(factory: :user)
target(factory: :epic) target(factory: :epic)
action { Event::CREATED } action { :created }
project { nil } project { nil }
end end
trait(:approved) { action { Event::APPROVED } }
trait(:approved) { action { :approved } }
end end
end end
...@@ -28,26 +28,26 @@ describe Gitlab::ContributionAnalytics::DataCollector do ...@@ -28,26 +28,26 @@ describe Gitlab::ContributionAnalytics::DataCollector do
end end
end end
context 'deriving various counts from #all_counts' do context 'deriving various counts from #raw_counts' do
let(:all_counts) do let(:raw_counts) do
{ {
[1, nil, Event::PUSHED] => 2, [1, nil, Event.actions[:pushed]] => 2,
[2, nil, Event::PUSHED] => 2, [2, nil, Event.actions[:pushed]] => 2,
[1, MergeRequest.name, Event::MERGED] => 2, [1, MergeRequest.name, Event.actions[:merged]] => 2,
[4, MergeRequest.name, Event::MERGED] => 2, [4, MergeRequest.name, Event.actions[:merged]] => 2,
[5, MergeRequest.name, Event::CREATED] => 0, [5, MergeRequest.name, Event.actions[:created]] => 0,
[6, MergeRequest.name, Event::CREATED] => 1, [6, MergeRequest.name, Event.actions[:created]] => 1,
[10, Issue.name, Event::CLOSED] => 10, [10, Issue.name, Event.actions[:closed]] => 10,
[11, Issue.name, Event::CLOSED] => 11 [11, Issue.name, Event.actions[:closed]] => 11
} }
end end
let(:data_collector) { described_class.new(group: Group.new) } let(:data_collector) { described_class.new(group: Group.new) }
before do before do
allow(data_collector).to receive(:all_counts).and_return(all_counts) allow(data_collector).to receive(:raw_counts).and_return(raw_counts)
end end
describe 'extracts correct counts from all_counts' do describe 'extracts correct counts from raw_counts' do
it 'for #push_by_author_count' do it 'for #push_by_author_count' do
expect(data_collector.push_by_author_count).to eq({ 1 => 2, 2 => 2 }) expect(data_collector.push_by_author_count).to eq({ 1 => 2, 2 => 2 })
end end
...@@ -73,7 +73,7 @@ describe Gitlab::ContributionAnalytics::DataCollector do ...@@ -73,7 +73,7 @@ describe Gitlab::ContributionAnalytics::DataCollector do
end end
it 'handles empty result' do it 'handles empty result' do
allow(data_collector).to receive(:all_counts).and_return({}) allow(data_collector).to receive(:raw_counts).and_return({})
expect(data_collector.push_by_author_count).to eq({}) expect(data_collector.push_by_author_count).to eq({})
expect(data_collector.total_push_author_count).to eq(0) expect(data_collector.total_push_author_count).to eq(0)
......
...@@ -102,7 +102,7 @@ describe Burndown do ...@@ -102,7 +102,7 @@ describe Burndown do
end end
it "ignores follow-up events with the same action" do it "ignores follow-up events with the same action" do
create(:event, target: milestone.issues.first, created_at: milestone.start_date + 1.minute, action: Event::REOPENED) create(:event, target: milestone.issues.first, created_at: milestone.start_date + 1.minute, action: :reopened)
event1 = create(:closed_issue_event, target: milestone.issues.first, created_at: milestone.start_date + 2.minutes) event1 = create(:closed_issue_event, target: milestone.issues.first, created_at: milestone.start_date + 2.minutes)
event2 = create(:closed_issue_event, target: milestone.issues.first, created_at: milestone.start_date + 3.minutes) event2 = create(:closed_issue_event, target: milestone.issues.first, created_at: milestone.start_date + 3.minutes)
...@@ -112,7 +112,7 @@ describe Burndown do ...@@ -112,7 +112,7 @@ describe Burndown do
context "when all closed issues do not have closed events" do context "when all closed issues do not have closed events" do
before do before do
Event.where(target: milestone.issues, action: Event::CLOSED).destroy_all # rubocop: disable DestroyAll Event.where(target: milestone.issues, action: :closed).destroy_all # rubocop: disable DestroyAll
end end
it "considers closed_at as milestone start date" do it "considers closed_at as milestone start date" do
...@@ -137,7 +137,7 @@ describe Burndown do ...@@ -137,7 +137,7 @@ describe Burndown do
context "when one but not all closed issues does not have a closed event" do context "when one but not all closed issues does not have a closed event" do
it "sets attribute accurate to false" do it "sets attribute accurate to false" do
Event.where(target: milestone.issues.closed.first, action: Event::CLOSED).destroy_all # rubocop: disable DestroyAll Event.where(target: milestone.issues.closed.first, action: :closed).destroy_all # rubocop: disable DestroyAll
burndown = described_class.new(milestone.issues_visible_to_user(user), milestone.start_date, milestone.due_date) burndown = described_class.new(milestone.issues_visible_to_user(user), milestone.start_date, milestone.due_date)
aggregate_failures do aggregate_failures do
......
...@@ -12,7 +12,7 @@ describe EventCreateService do ...@@ -12,7 +12,7 @@ describe EventCreateService do
it "creates new event" do it "creates new event" do
event = service.open_epic(epic, epic.author) event = service.open_epic(epic, epic.author)
expect_event(event, Event::CREATED) expect_event(event, 'created')
end end
end end
...@@ -20,7 +20,7 @@ describe EventCreateService do ...@@ -20,7 +20,7 @@ describe EventCreateService do
it "creates new event" do it "creates new event" do
event = service.close_epic(epic, epic.author) event = service.close_epic(epic, epic.author)
expect_event(event, Event::CLOSED) expect_event(event, 'closed')
end end
end end
...@@ -28,7 +28,7 @@ describe EventCreateService do ...@@ -28,7 +28,7 @@ describe EventCreateService do
it "creates new event" do it "creates new event" do
event = service.reopen_epic(epic, epic.author) event = service.reopen_epic(epic, epic.author)
expect_event(event, Event::REOPENED) expect_event(event, 'reopened')
end end
end end
...@@ -38,7 +38,7 @@ describe EventCreateService do ...@@ -38,7 +38,7 @@ describe EventCreateService do
event = service.leave_note(note, epic.author) event = service.leave_note(note, epic.author)
expect_event(event, Event::COMMENTED) expect_event(event, 'commented')
end end
end end
...@@ -60,7 +60,7 @@ describe EventCreateService do ...@@ -60,7 +60,7 @@ describe EventCreateService do
it 'creates new event' do it 'creates new event' do
service.approve_mr(merge_request, user) service.approve_mr(merge_request, user)
change { Event.approved.where(target: merge_request).count }.by(1) change { Event.approved_action.where(target: merge_request).count }.by(1)
end end
end end
end end
......
...@@ -27,15 +27,15 @@ class EventFilter ...@@ -27,15 +27,15 @@ class EventFilter
case filter case filter
when PUSH when PUSH
events.where(action: Event::PUSHED) events.pushed_action
when MERGED when MERGED
events.where(action: Event::MERGED) events.merged_action
when COMMENTS when COMMENTS
events.where(action: Event::COMMENTED) events.commented_action
when TEAM when TEAM
events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED]) events.where(action: [:joined, :left, :expired])
when ISSUE when ISSUE
events.where(action: [Event::CREATED, Event::UPDATED, Event::CLOSED, Event::REOPENED], target_type: 'Issue') events.where(action: [:created, :updated, :closed, :reopened], target_type: 'Issue')
when WIKI when WIKI
wiki_events(events) wiki_events(events)
else else
......
...@@ -24,13 +24,13 @@ module Gitlab ...@@ -24,13 +24,13 @@ module Gitlab
# project_features for the (currently) 3 different contribution types # project_features for the (currently) 3 different contribution types
date_from = 1.year.ago date_from = 1.year.ago
repo_events = event_counts(date_from, :repository) repo_events = event_counts(date_from, :repository)
.having(action: Event::PUSHED) .having(action: :pushed)
issue_events = event_counts(date_from, :issues) issue_events = event_counts(date_from, :issues)
.having(action: [Event::CREATED, Event::CLOSED], target_type: "Issue") .having(action: [:created, :closed], target_type: "Issue")
mr_events = event_counts(date_from, :merge_requests) mr_events = event_counts(date_from, :merge_requests)
.having(action: [Event::MERGED, Event::CREATED, Event::CLOSED], target_type: "MergeRequest") .having(action: [:merged, :created, :closed], target_type: "MergeRequest")
note_events = event_counts(date_from, :merge_requests) note_events = event_counts(date_from, :merge_requests)
.having(action: [Event::COMMENTED]) .having(action: :commented)
events = Event events = Event
.from_union([repo_events, issue_events, mr_events, note_events]) .from_union([repo_events, issue_events, mr_events, note_events])
......
...@@ -145,7 +145,7 @@ describe Projects::MilestonesController do ...@@ -145,7 +145,7 @@ describe Projects::MilestonesController do
delete :destroy, params: { namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid }, format: :js delete :destroy, params: { namespace_id: project.namespace.id, project_id: project.id, id: milestone.iid }, format: :js
expect(response).to be_successful expect(response).to be_successful
expect(Event.recent.first.action).to eq(Event::DESTROYED) expect(Event.recent.first).to be_destroyed_action
expect { Milestone.find(milestone.id) }.to raise_exception(ActiveRecord::RecordNotFound) expect { Milestone.find(milestone.id) }.to raise_exception(ActiveRecord::RecordNotFound)
issue.reload issue.reload
......
...@@ -4,27 +4,27 @@ FactoryBot.define do ...@@ -4,27 +4,27 @@ FactoryBot.define do
factory :event do factory :event do
project project
author(factory: :user) { project.creator } author(factory: :user) { project.creator }
action { Event::JOINED } action { :joined }
trait(:created) { action { Event::CREATED } } trait(:created) { action { :created } }
trait(:updated) { action { Event::UPDATED } } trait(:updated) { action { :updated } }
trait(:closed) { action { Event::CLOSED } } trait(:closed) { action { :closed } }
trait(:reopened) { action { Event::REOPENED } } trait(:reopened) { action { :reopened } }
trait(:pushed) { action { Event::PUSHED } } trait(:pushed) { action { :pushed } }
trait(:commented) { action { Event::COMMENTED } } trait(:commented) { action { :commented } }
trait(:merged) { action { Event::MERGED } } trait(:merged) { action { :merged } }
trait(:joined) { action { Event::JOINED } } trait(:joined) { action { :joined } }
trait(:left) { action { Event::LEFT } } trait(:left) { action { :left } }
trait(:destroyed) { action { Event::DESTROYED } } trait(:destroyed) { action { :destroyed } }
trait(:expired) { action { Event::EXPIRED } } trait(:expired) { action { :expired } }
factory :closed_issue_event do factory :closed_issue_event do
action { Event::CLOSED } action { :closed }
target factory: :closed_issue target factory: :closed_issue
end end
factory :wiki_page_event do factory :wiki_page_event do
action { Event::CREATED } action { :created }
project { @overrides[:wiki_page]&.container || create(:project, :wiki_repo) } project { @overrides[:wiki_page]&.container || create(:project, :wiki_repo) }
target { create(:wiki_page_meta, :for_wiki_page, wiki_page: wiki_page) } target { create(:wiki_page_meta, :for_wiki_page, wiki_page: wiki_page) }
...@@ -39,7 +39,7 @@ FactoryBot.define do ...@@ -39,7 +39,7 @@ FactoryBot.define do
note { create(:note, author: author, project: project, noteable: design) } note { create(:note, author: author, project: project, noteable: design) }
end end
action { Event::COMMENTED } action { :commented }
target { note } target { note }
end end
end end
...@@ -47,7 +47,7 @@ FactoryBot.define do ...@@ -47,7 +47,7 @@ FactoryBot.define do
factory :push_event, class: 'PushEvent' do factory :push_event, class: 'PushEvent' do
project factory: :project_empty_repo project factory: :project_empty_repo
author(factory: :user) { project.creator } author(factory: :user) { project.creator }
action { Event::PUSHED } action { :pushed }
end end
factory :push_event_payload do factory :push_event_payload do
......
...@@ -59,7 +59,7 @@ describe 'Contributions Calendar', :js do ...@@ -59,7 +59,7 @@ describe 'Contributions Calendar', :js do
def note_comment_contribution def note_comment_contribution
note_comment_params = { note_comment_params = {
project: contributed_project, project: contributed_project,
action: Event::COMMENTED, action: :commented,
target: issue_note, target: issue_note,
author_id: user.id author_id: user.id
} }
......
...@@ -12,7 +12,7 @@ describe 'Tooltips on .timeago dates', :js do ...@@ -12,7 +12,7 @@ describe 'Tooltips on .timeago dates', :js do
before do before do
project.add_maintainer(user) project.add_maintainer(user)
Event.create( project: project, author_id: user.id, action: Event::JOINED, Event.create( project: project, author_id: user.id, action: :joined,
updated_at: created_date, created_at: created_date) updated_at: created_date, created_at: created_date)
sign_in user sign_in user
......
...@@ -18,7 +18,7 @@ describe 'Project member activity', :js do ...@@ -18,7 +18,7 @@ describe 'Project member activity', :js do
context 'when a user joins the project' do context 'when a user joins the project' do
before do before do
visit_activities_and_wait_with_event(Event::JOINED) visit_activities_and_wait_with_event(:joined)
end end
it "presents the correct message" do it "presents the correct message" do
...@@ -29,7 +29,7 @@ describe 'Project member activity', :js do ...@@ -29,7 +29,7 @@ describe 'Project member activity', :js do
context 'when a user leaves the project' do context 'when a user leaves the project' do
before do before do
visit_activities_and_wait_with_event(Event::LEFT) visit_activities_and_wait_with_event(:left)
end end
it "presents the correct message" do it "presents the correct message" do
...@@ -40,7 +40,7 @@ describe 'Project member activity', :js do ...@@ -40,7 +40,7 @@ describe 'Project member activity', :js do
context 'when a users membership expires for the project' do context 'when a users membership expires for the project' do
before do before do
visit_activities_and_wait_with_event(Event::EXPIRED) visit_activities_and_wait_with_event(:expired)
end end
it "presents the correct message" do it "presents the correct message" do
......
...@@ -11,18 +11,18 @@ describe EventsFinder do ...@@ -11,18 +11,18 @@ describe EventsFinder do
let(:closed_issue) { create(:closed_issue, project: project1, author: user) } let(:closed_issue) { create(:closed_issue, project: project1, author: user) }
let(:opened_merge_request) { create(:merge_request, source_project: project2, author: user) } let(:opened_merge_request) { create(:merge_request, source_project: project2, author: user) }
let!(:closed_issue_event) { create(:event, project: project1, author: user, target: closed_issue, action: Event::CLOSED, created_at: Date.new(2016, 12, 30)) } let!(:closed_issue_event) { create(:event, :closed, project: project1, author: user, target: closed_issue, created_at: Date.new(2016, 12, 30)) }
let!(:opened_merge_request_event) { create(:event, project: project2, author: user, target: opened_merge_request, action: Event::CREATED, created_at: Date.new(2017, 1, 31)) } let!(:opened_merge_request_event) { create(:event, :created, project: project2, author: user, target: opened_merge_request, created_at: Date.new(2017, 1, 31)) }
let(:closed_issue2) { create(:closed_issue, project: project1, author: user) } let(:closed_issue2) { create(:closed_issue, project: project1, author: user) }
let(:opened_merge_request2) { create(:merge_request, source_project: project2, author: user) } let(:opened_merge_request2) { create(:merge_request, source_project: project2, author: user) }
let!(:closed_issue_event2) { create(:event, project: project1, author: user, target: closed_issue, action: Event::CLOSED, created_at: Date.new(2016, 2, 2)) } let!(:closed_issue_event2) { create(:event, :closed, project: project1, author: user, target: closed_issue, created_at: Date.new(2016, 2, 2)) }
let!(:opened_merge_request_event2) { create(:event, project: project2, author: user, target: opened_merge_request, action: Event::CREATED, created_at: Date.new(2017, 2, 2)) } let!(:opened_merge_request_event2) { create(:event, :created, project: project2, author: user, target: opened_merge_request, created_at: Date.new(2017, 2, 2)) }
let(:opened_merge_request3) { create(:merge_request, source_project: project1, author: other_user) } let(:opened_merge_request3) { create(:merge_request, source_project: project1, author: other_user) }
let!(:other_developer_event) { create(:event, project: project1, author: other_user, target: opened_merge_request3, action: Event::CREATED) } let!(:other_developer_event) { create(:event, :created, project: project1, author: other_user, target: opened_merge_request3 ) }
let_it_be(:public_project) { create(:project, :public, creator_id: user.id, namespace: user.namespace) } let_it_be(:public_project) { create(:project, :public, creator_id: user.id, namespace: user.namespace) }
let(:confidential_issue) { create(:closed_issue, confidential: true, project: public_project, author: user) } let(:confidential_issue) { create(:closed_issue, confidential: true, project: public_project, author: user) }
let!(:confidential_event) { create(:event, project: public_project, author: user, target: confidential_issue, action: Event::CLOSED) } let!(:confidential_event) { create(:event, :closed, project: public_project, author: user, target: confidential_issue) }
context 'when targeting a user' do context 'when targeting a user' do
it 'returns events between specified dates filtered on action and type' do it 'returns events between specified dates filtered on action and type' do
......
...@@ -42,7 +42,7 @@ describe Gitlab::ContributionsCalendar do ...@@ -42,7 +42,7 @@ describe Gitlab::ContributionsCalendar do
described_class.new(contributor, current_user) described_class.new(contributor, current_user)
end end
def create_event(project, day, hour = 0, action = Event::CREATED, target_symbol = :issue) def create_event(project, day, hour = 0, action = :created, target_symbol = :issue)
@targets ||= {} @targets ||= {}
@targets[project] ||= create(target_symbol, project: project, author: contributor) @targets[project] ||= create(target_symbol, project: project, author: contributor)
...@@ -77,14 +77,14 @@ describe Gitlab::ContributionsCalendar do ...@@ -77,14 +77,14 @@ describe Gitlab::ContributionsCalendar do
end end
it "counts the diff notes on merge request" do it "counts the diff notes on merge request" do
create_event(public_project, today, 0, Event::COMMENTED, :diff_note_on_merge_request) create_event(public_project, today, 0, :commented, :diff_note_on_merge_request)
expect(calendar(contributor).activity_dates[today]).to eq(1) expect(calendar(contributor).activity_dates[today]).to eq(1)
end end
it "counts the discussions on merge requests and issues" do it "counts the discussions on merge requests and issues" do
create_event(public_project, today, 0, Event::COMMENTED, :discussion_note_on_merge_request) create_event(public_project, today, 0, :commented, :discussion_note_on_merge_request)
create_event(public_project, today, 2, Event::COMMENTED, :discussion_note_on_issue) create_event(public_project, today, 2, :commented, :discussion_note_on_issue)
expect(calendar(contributor).activity_dates[today]).to eq(2) expect(calendar(contributor).activity_dates[today]).to eq(2)
end end
......
...@@ -46,12 +46,12 @@ describe ProjectMember do ...@@ -46,12 +46,12 @@ describe ProjectMember do
it "creates an expired event when left due to expiry" do it "creates an expired event when left due to expiry" do
expired = create(:project_member, project: project, expires_at: Time.current - 6.days) expired = create(:project_member, project: project, expires_at: Time.current - 6.days)
expired.destroy expired.destroy
expect(Event.recent.first.action).to eq(Event::EXPIRED) expect(Event.recent.first).to be_expired_action
end end
it "creates a left event when left due to leave" do it "creates a left event when left due to leave" do
maintainer.destroy maintainer.destroy
expect(Event.recent.first.action).to eq(Event::LEFT) expect(Event.recent.first).to be_left_action
end end
end end
......
...@@ -118,8 +118,8 @@ describe PushEvent do ...@@ -118,8 +118,8 @@ describe PushEvent do
end end
describe '.sti_name' do describe '.sti_name' do
it 'returns Event::PUSHED' do it 'returns the integer representation of the :pushed event action' do
expect(described_class.sti_name).to eq(Event::PUSHED) expect(described_class.sti_name).to eq(Event.actions[:pushed])
end end
end end
...@@ -299,7 +299,7 @@ describe PushEvent do ...@@ -299,7 +299,7 @@ describe PushEvent do
describe '#validate_push_action' do describe '#validate_push_action' do
it 'adds an error when the action is not PUSHED' do it 'adds an error when the action is not PUSHED' do
event.action = Event::CREATED event.action = :created
event.validate_push_action event.validate_push_action
expect(event.errors.count).to eq(1) expect(event.errors.count).to eq(1)
......
...@@ -8,7 +8,7 @@ describe UserInteractedProject do ...@@ -8,7 +8,7 @@ describe UserInteractedProject do
let(:event) { build(:event) } let(:event) { build(:event) }
Event::ACTIONS.each do |action| Event.actions.each_key do |action|
context "for all actions (event types)" do context "for all actions (event types)" do
let(:event) { build(:event, action: action) } let(:event) { build(:event, action: action) }
......
...@@ -7,9 +7,9 @@ describe API::Events do ...@@ -7,9 +7,9 @@ describe API::Events do
let(:non_member) { create(:user) } let(:non_member) { create(:user) }
let(:private_project) { create(:project, :private, creator_id: user.id, namespace: user.namespace) } let(:private_project) { create(:project, :private, creator_id: user.id, namespace: user.namespace) }
let(:closed_issue) { create(:closed_issue, project: private_project, author: user) } let(:closed_issue) { create(:closed_issue, project: private_project, author: user) }
let!(:closed_issue_event) { create(:event, project: private_project, author: user, target: closed_issue, action: Event::CLOSED, created_at: Date.new(2016, 12, 30)) } let!(:closed_issue_event) { create(:event, :closed, project: private_project, author: user, target: closed_issue, created_at: Date.new(2016, 12, 30)) }
let(:closed_issue2) { create(:closed_issue, project: private_project, author: non_member) } let(:closed_issue2) { create(:closed_issue, project: private_project, author: non_member) }
let!(:closed_issue_event2) { create(:event, project: private_project, author: non_member, target: closed_issue2, action: Event::CLOSED, created_at: Date.new(2016, 12, 30)) } let!(:closed_issue_event2) { create(:event, :closed, project: private_project, author: non_member, target: closed_issue2, created_at: Date.new(2016, 12, 30)) }
describe 'GET /events' do describe 'GET /events' do
context 'when unauthenticated' do context 'when unauthenticated' do
...@@ -117,7 +117,7 @@ describe API::Events do ...@@ -117,7 +117,7 @@ describe API::Events do
context 'when the list of events includes wiki page events' do context 'when the list of events includes wiki page events' do
it 'returns information about the wiki event', :aggregate_failures do it 'returns information about the wiki event', :aggregate_failures do
page = create(:wiki_page, project: private_project) page = create(:wiki_page, project: private_project)
[Event::CREATED, Event::UPDATED, Event::DESTROYED].each do |action| [:created, :updated, :destroyed].each do |action|
create(:wiki_page_event, wiki_page: page, action: action, author: user) create(:wiki_page_event, wiki_page: page, action: action, author: user)
end end
......
...@@ -7,7 +7,7 @@ describe API::ProjectEvents do ...@@ -7,7 +7,7 @@ describe API::ProjectEvents do
let(:non_member) { create(:user) } let(:non_member) { create(:user) }
let(:private_project) { create(:project, :private, creator_id: user.id, namespace: user.namespace) } let(:private_project) { create(:project, :private, creator_id: user.id, namespace: user.namespace) }
let(:closed_issue) { create(:closed_issue, project: private_project, author: user) } let(:closed_issue) { create(:closed_issue, project: private_project, author: user) }
let!(:closed_issue_event) { create(:event, project: private_project, author: user, target: closed_issue, action: Event::CLOSED, created_at: Date.new(2016, 12, 30)) } let!(:closed_issue_event) { create(:event, project: private_project, author: user, target: closed_issue, action: :closed, created_at: Date.new(2016, 12, 30)) }
describe 'GET /projects/:id/events' do describe 'GET /projects/:id/events' do
context 'when unauthenticated ' do context 'when unauthenticated ' do
...@@ -29,9 +29,9 @@ describe API::ProjectEvents do ...@@ -29,9 +29,9 @@ describe API::ProjectEvents do
context 'with inaccessible events' do context 'with inaccessible events' do
let(:public_project) { create(:project, :public, creator_id: user.id, namespace: user.namespace) } let(:public_project) { create(:project, :public, creator_id: user.id, namespace: user.namespace) }
let(:confidential_issue) { create(:closed_issue, confidential: true, project: public_project, author: user) } let(:confidential_issue) { create(:closed_issue, confidential: true, project: public_project, author: user) }
let!(:confidential_event) { create(:event, project: public_project, author: user, target: confidential_issue, action: Event::CLOSED) } let!(:confidential_event) { create(:event, project: public_project, author: user, target: confidential_issue, action: :closed) }
let(:public_issue) { create(:closed_issue, project: public_project, author: user) } let(:public_issue) { create(:closed_issue, project: public_project, author: user) }
let!(:public_event) { create(:event, project: public_project, author: user, target: public_issue, action: Event::CLOSED) } let!(:public_event) { create(:event, project: public_project, author: user, target: public_issue, action: :closed) }
it 'returns only accessible events' do it 'returns only accessible events' do
get api("/projects/#{public_project.id}/events", non_member) get api("/projects/#{public_project.id}/events", non_member)
...@@ -53,19 +53,19 @@ describe API::ProjectEvents do ...@@ -53,19 +53,19 @@ describe API::ProjectEvents do
before do before do
create(:event, create(:event,
:closed,
project: public_project, project: public_project,
target: create(:issue, project: public_project, title: 'Issue 1'), target: create(:issue, project: public_project, title: 'Issue 1'),
action: Event::CLOSED,
created_at: Date.parse('2018-12-10')) created_at: Date.parse('2018-12-10'))
create(:event, create(:event,
:closed,
project: public_project, project: public_project,
target: create(:issue, confidential: true, project: public_project, title: 'Confidential event'), target: create(:issue, confidential: true, project: public_project, title: 'Confidential event'),
action: Event::CLOSED,
created_at: Date.parse('2018-12-11')) created_at: Date.parse('2018-12-11'))
create(:event, create(:event,
:closed,
project: public_project, project: public_project,
target: create(:issue, project: public_project, title: 'Issue 2'), target: create(:issue, project: public_project, title: 'Issue 2'),
action: Event::CLOSED,
created_at: Date.parse('2018-12-12')) created_at: Date.parse('2018-12-12'))
end end
......
...@@ -174,7 +174,7 @@ describe EventCreateService do ...@@ -174,7 +174,7 @@ describe EventCreateService do
wiki_page?: true, wiki_page?: true,
valid?: true, valid?: true,
persisted?: true, persisted?: true,
action: action, action: action.to_s,
wiki_page: wiki_page, wiki_page: wiki_page,
author: user author: user
) )
...@@ -200,7 +200,7 @@ describe EventCreateService do ...@@ -200,7 +200,7 @@ describe EventCreateService do
end end
end end
(Event::ACTIONS.values - Event::WIKI_ACTIONS).each do |bad_action| (Event.actions.keys - Event::WIKI_ACTIONS).each do |bad_action|
context "The action is #{bad_action}" do context "The action is #{bad_action}" do
it 'raises an error' do it 'raises an error' do
expect { service.wiki_event(meta, user, bad_action) }.to raise_error(described_class::IllegalActionError) expect { service.wiki_event(meta, user, bad_action) }.to raise_error(described_class::IllegalActionError)
......
...@@ -91,7 +91,7 @@ describe Git::BranchHooksService do ...@@ -91,7 +91,7 @@ describe Git::BranchHooksService do
end end
describe 'Push Event' do describe 'Push Event' do
let(:event) { Event.find_by_action(Event::PUSHED) } let(:event) { Event.pushed_action.first }
before do before do
service.execute service.execute
...@@ -101,7 +101,7 @@ describe Git::BranchHooksService do ...@@ -101,7 +101,7 @@ describe Git::BranchHooksService do
it 'generates a push event with one commit' do it 'generates a push event with one commit' do
expect(event).to be_an_instance_of(PushEvent) expect(event).to be_an_instance_of(PushEvent)
expect(event.project).to eq(project) expect(event.project).to eq(project)
expect(event.action).to eq(Event::PUSHED) expect(event).to be_pushed_action
expect(event.push_event_payload).to be_an_instance_of(PushEventPayload) expect(event.push_event_payload).to be_an_instance_of(PushEventPayload)
expect(event.push_event_payload.commit_from).to eq(oldrev) expect(event.push_event_payload.commit_from).to eq(oldrev)
expect(event.push_event_payload.commit_to).to eq(newrev) expect(event.push_event_payload.commit_to).to eq(newrev)
...@@ -117,7 +117,7 @@ describe Git::BranchHooksService do ...@@ -117,7 +117,7 @@ describe Git::BranchHooksService do
it 'generates a push event with more than one commit' do it 'generates a push event with more than one commit' do
expect(event).to be_an_instance_of(PushEvent) expect(event).to be_an_instance_of(PushEvent)
expect(event.project).to eq(project) expect(event.project).to eq(project)
expect(event.action).to eq(Event::PUSHED) expect(event).to be_pushed_action
expect(event.push_event_payload).to be_an_instance_of(PushEventPayload) expect(event.push_event_payload).to be_an_instance_of(PushEventPayload)
expect(event.push_event_payload.commit_from).to be_nil expect(event.push_event_payload.commit_from).to be_nil
expect(event.push_event_payload.commit_to).to eq(newrev) expect(event.push_event_payload.commit_to).to eq(newrev)
...@@ -133,7 +133,7 @@ describe Git::BranchHooksService do ...@@ -133,7 +133,7 @@ describe Git::BranchHooksService do
it 'generates a push event with no commits' do it 'generates a push event with no commits' do
expect(event).to be_an_instance_of(PushEvent) expect(event).to be_an_instance_of(PushEvent)
expect(event.project).to eq(project) expect(event.project).to eq(project)
expect(event.action).to eq(Event::PUSHED) expect(event).to be_pushed_action
expect(event.push_event_payload).to be_an_instance_of(PushEventPayload) expect(event.push_event_payload).to be_an_instance_of(PushEventPayload)
expect(event.push_event_payload.commit_from).to eq(oldrev) expect(event.push_event_payload.commit_from).to eq(oldrev)
expect(event.push_event_payload.commit_to).to be_nil expect(event.push_event_payload.commit_to).to be_nil
......
...@@ -89,20 +89,20 @@ describe Git::WikiPushService::Change do ...@@ -89,20 +89,20 @@ describe Git::WikiPushService::Change do
context 'the page is deleted' do context 'the page is deleted' do
let(:operation) { :deleted } let(:operation) { :deleted }
it { is_expected.to have_attributes(event_action: Event::DESTROYED) } it { is_expected.to have_attributes(event_action: :destroyed) }
end end
context 'the page is added' do context 'the page is added' do
let(:operation) { :added } let(:operation) { :added }
it { is_expected.to have_attributes(event_action: Event::CREATED) } it { is_expected.to have_attributes(event_action: :created) }
end end
%i[renamed modified].each do |op| %i[renamed modified].each do |op|
context "the page is #{op}" do context "the page is #{op}" do
let(:operation) { op } let(:operation) { op }
it { is_expected.to have_attributes(event_action: Event::UPDATED) } it { is_expected.to have_attributes(event_action: :updated) }
end end
end end
end end
......
...@@ -54,7 +54,7 @@ describe Git::WikiPushService, services: true do ...@@ -54,7 +54,7 @@ describe Git::WikiPushService, services: true do
it 'handles all known actions' do it 'handles all known actions' do
run_service run_service
expect(Event.last(count).pluck(:action)).to match_array(Event::WIKI_ACTIONS) expect(Event.last(count).pluck(:action)).to match_array(Event::WIKI_ACTIONS.map(&:to_s))
end end
end end
...@@ -77,7 +77,7 @@ describe Git::WikiPushService, services: true do ...@@ -77,7 +77,7 @@ describe Git::WikiPushService, services: true do
it 'creates appropriate events' do it 'creates appropriate events' do
run_service run_service
expect(Event.last(2)).to all(have_attributes(wiki_page?: true, action: Event::CREATED)) expect(Event.last(2)).to all(have_attributes(wiki_page?: true, action: 'created'))
end end
end end
...@@ -100,7 +100,7 @@ describe Git::WikiPushService, services: true do ...@@ -100,7 +100,7 @@ describe Git::WikiPushService, services: true do
it 'creates a wiki page creation event' do it 'creates a wiki page creation event' do
expect { run_service }.to change(Event, :count).by(1) expect { run_service }.to change(Event, :count).by(1)
expect(Event.last).to have_attributes(wiki_page?: true, action: Event::CREATED) expect(Event.last).to have_attributes(wiki_page?: true, action: 'created')
end end
it 'creates one metadata record' do it 'creates one metadata record' do
...@@ -129,7 +129,7 @@ describe Git::WikiPushService, services: true do ...@@ -129,7 +129,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes( expect(Event.last).to have_attributes(
wiki_page?: true, wiki_page?: true,
action: Event::CREATED action: 'created'
) )
end end
end end
...@@ -158,7 +158,7 @@ describe Git::WikiPushService, services: true do ...@@ -158,7 +158,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes( expect(Event.last).to have_attributes(
wiki_page?: true, wiki_page?: true,
action: Event::UPDATED action: 'updated'
) )
end end
end end
...@@ -182,7 +182,7 @@ describe Git::WikiPushService, services: true do ...@@ -182,7 +182,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes( expect(Event.last).to have_attributes(
wiki_page?: true, wiki_page?: true,
action: Event::UPDATED action: 'updated'
) )
end end
end end
...@@ -206,7 +206,7 @@ describe Git::WikiPushService, services: true do ...@@ -206,7 +206,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes( expect(Event.last).to have_attributes(
wiki_page?: true, wiki_page?: true,
action: Event::DESTROYED action: 'destroyed'
) )
end end
end end
...@@ -218,7 +218,7 @@ describe Git::WikiPushService, services: true do ...@@ -218,7 +218,7 @@ describe Git::WikiPushService, services: true do
message = 'something went very very wrong' message = 'something went very very wrong'
allow_next_instance_of(WikiPages::EventCreateService, current_user) do |service| allow_next_instance_of(WikiPages::EventCreateService, current_user) do |service|
allow(service).to receive(:execute) allow(service).to receive(:execute)
.with(String, WikiPage, Integer) .with(String, WikiPage, Symbol)
.and_return(ServiceResponse.error(message: message)) .and_return(ServiceResponse.error(message: message))
end end
......
...@@ -59,7 +59,7 @@ describe MergeRequests::CreateService, :clean_gitlab_redis_shared_state do ...@@ -59,7 +59,7 @@ describe MergeRequests::CreateService, :clean_gitlab_redis_shared_state do
it 'creates exactly 1 create MR event', :sidekiq_might_not_need_inline do it 'creates exactly 1 create MR event', :sidekiq_might_not_need_inline do
attributes = { attributes = {
action: Event::CREATED, action: :created,
target_id: merge_request.id, target_id: merge_request.id,
target_type: merge_request.class.name target_type: merge_request.class.name
} }
......
...@@ -11,7 +11,7 @@ describe WikiPages::EventCreateService do ...@@ -11,7 +11,7 @@ describe WikiPages::EventCreateService do
describe '#execute' do describe '#execute' do
let_it_be(:page) { create(:wiki_page, project: project) } let_it_be(:page) { create(:wiki_page, project: project) }
let(:slug) { generate(:sluggified_title) } let(:slug) { generate(:sluggified_title) }
let(:action) { Event::CREATED } let(:action) { :created }
let(:response) { subject.execute(slug, page, action) } let(:response) { subject.execute(slug, page, action) }
context 'feature flag is not enabled' do context 'feature flag is not enabled' do
...@@ -38,7 +38,7 @@ describe WikiPages::EventCreateService do ...@@ -38,7 +38,7 @@ describe WikiPages::EventCreateService do
end end
context 'the action is illegal' do context 'the action is illegal' do
let(:action) { Event::WIKI_ACTIONS.max + 1 } let(:action) { :illegal_action }
it 'returns an error' do it 'returns an error' do
expect(response).to be_error expect(response).to be_error
...@@ -58,7 +58,7 @@ describe WikiPages::EventCreateService do ...@@ -58,7 +58,7 @@ describe WikiPages::EventCreateService do
end end
context 'the action is a deletion' do context 'the action is a deletion' do
let(:action) { Event::DESTROYED } let(:action) { :destroyed }
it 'does not synchronize the wiki metadata timestamps with the git commit' do it 'does not synchronize the wiki metadata timestamps with the git commit' do
expect_next_instance_of(WikiPage::Meta) do |instance| expect_next_instance_of(WikiPage::Meta) do |instance|
...@@ -74,7 +74,7 @@ describe WikiPages::EventCreateService do ...@@ -74,7 +74,7 @@ describe WikiPages::EventCreateService do
end end
it 'returns an event in the payload' do it 'returns an event in the payload' do
expect(response.payload).to include(event: have_attributes(author: user, wiki_page?: true, action: action)) expect(response.payload).to include(event: have_attributes(author: user, wiki_page?: true, action: 'created'))
end end
it 'records the slug for the page' do it 'records the slug for the page' do
......
...@@ -45,7 +45,7 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type| ...@@ -45,7 +45,7 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type|
expect { service.execute }.to change { Event.count }.by 1 expect { service.execute }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes( expect(Event.recent.first).to have_attributes(
action: Event::CREATED, action: 'created',
target: have_attributes(canonical_slug: page_title) target: have_attributes(canonical_slug: page_title)
) )
end end
......
...@@ -27,7 +27,7 @@ RSpec.shared_examples 'WikiPages::DestroyService#execute' do |container_type| ...@@ -27,7 +27,7 @@ RSpec.shared_examples 'WikiPages::DestroyService#execute' do |container_type|
expect { service.execute(page) }.to change { Event.count }.by 1 expect { service.execute(page) }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes( expect(Event.recent.first).to have_attributes(
action: Event::DESTROYED, action: 'destroyed',
target: have_attributes(canonical_slug: page.slug) target: have_attributes(canonical_slug: page.slug)
) )
end end
......
...@@ -48,7 +48,7 @@ RSpec.shared_examples 'WikiPages::UpdateService#execute' do |container_type| ...@@ -48,7 +48,7 @@ RSpec.shared_examples 'WikiPages::UpdateService#execute' do |container_type|
expect { service.execute(page) }.to change { Event.count }.by 1 expect { service.execute(page) }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes( expect(Event.recent.first).to have_attributes(
action: Event::UPDATED, action: 'updated',
wiki_page: page, wiki_page: page,
target_title: page.title target_title: page.title
) )
......
...@@ -86,7 +86,7 @@ describe RepositoryCheck::SingleRepositoryWorker do ...@@ -86,7 +86,7 @@ describe RepositoryCheck::SingleRepositoryWorker do
end end
def create_push_event(project) def create_push_event(project)
project.events.create(action: Event::PUSHED, author_id: create(:user).id) project.events.create(action: :pushed, author_id: create(:user).id)
end end
def break_wiki(project) def break_wiki(project)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment