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
# rubocop: disable CodeReuse/ActiveRecord
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
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -10,35 +10,24 @@ class Event < ApplicationRecord
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(
created: CREATED,
updated: UPDATED,
closed: CLOSED,
reopened: REOPENED,
pushed: PUSHED,
commented: COMMENTED,
merged: MERGED,
joined: JOINED,
left: LEFT,
destroyed: DESTROYED,
expired: EXPIRED,
approved: APPROVED
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
).freeze
WIKI_ACTIONS = [CREATED, UPDATED, DESTROYED].freeze
private_constant :ACTIONS
WIKI_ACTIONS = [:created, :updated, :destroyed].freeze
TARGET_TYPES = HashWithIndifferentAccess.new(
issue: Issue,
......@@ -54,6 +43,8 @@ class Event < ApplicationRecord
RESET_PROJECT_ACTIVITY_INTERVAL = 1.hour
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 :title, to: :issue, prefix: true, allow_nil: true
delegate :title, to: :merge_request, prefix: true, allow_nil: true
......@@ -83,8 +74,6 @@ class Event < ApplicationRecord
# Scopes
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') }
# Needed to implement feature flag: can be removed when feature flag is removed
......@@ -115,7 +104,7 @@ class Event < ApplicationRecord
end
def find_sti_class(action)
if action.to_i == PUSHED
if actions.fetch(action, action) == actions[:pushed] # action can be integer or symbol
PushEvent
else
Event
......@@ -125,19 +114,15 @@ class Event < ApplicationRecord
# Update Gitlab::ContributionsCalendar#activity_dates if this changes
def contributions
where("action = ? OR (target_type IN (?) AND action IN (?)) OR (target_type = ? AND action = ?)",
Event::PUSHED,
%w(MergeRequest Issue), [Event::CREATED, Event::CLOSED, Event::MERGED],
"Note", Event::COMMENTED)
actions[:pushed],
%w(MergeRequest Issue), [actions[:created], actions[:closed], actions[:merged]],
"Note", actions[:commented])
end
def limit_recent(limit = 20, offset = nil)
recent.limit(limit).offset(offset)
end
def actions
ACTIONS.keys
end
def target_types
TARGET_TYPES.keys
end
......@@ -161,46 +146,10 @@ class Event < ApplicationRecord
target.try(:title)
end
def created_action?
action == CREATED
end
def push_action?
false
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?
joined_action? || left_action? || expired_action?
end
......@@ -210,11 +159,11 @@ class Event < ApplicationRecord
end
def created_wiki_page?
wiki_page? && action == CREATED
wiki_page? && created_action?
end
def updated_wiki_page?
wiki_page? && action == UPDATED
wiki_page? && updated_action?
end
def created_target?
......
......@@ -896,11 +896,11 @@ class MergeRequest < ApplicationRecord
end
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
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
def work_in_progress?
......
......@@ -445,7 +445,7 @@ class Project < ApplicationRecord
scope :archived, -> { where(archived: true) }
scope :non_archived, -> { where(archived: false) }
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 :inc_routes, -> { includes(:route, namespace: :route) }
scope :with_statistics, -> { includes(:statistics) }
......
......@@ -68,7 +68,7 @@ class PushEvent < Event
end
def self.sti_name
PUSHED
actions[:pushed]
end
def push_action?
......@@ -111,7 +111,7 @@ class PushEvent < Event
end
def validate_push_action
return if action == PUSHED
return if pushed_action?
errors.add(:action, "the action #{action.inspect} is not valid")
end
......
......@@ -13,79 +13,79 @@ class EventCreateService
def open_issue(issue, current_user)
create_resource_event(issue, current_user, :opened)
create_record_event(issue, current_user, Event::CREATED)
create_record_event(issue, current_user, :created)
end
def close_issue(issue, current_user)
create_resource_event(issue, current_user, :closed)
create_record_event(issue, current_user, Event::CLOSED)
create_record_event(issue, current_user, :closed)
end
def reopen_issue(issue, current_user)
create_resource_event(issue, current_user, :reopened)
create_record_event(issue, current_user, Event::REOPENED)
create_record_event(issue, current_user, :reopened)
end
def open_mr(merge_request, current_user)
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
def close_mr(merge_request, current_user)
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
def reopen_mr(merge_request, current_user)
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
def merge_mr(merge_request, current_user)
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
def open_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::CREATED)
create_record_event(milestone, current_user, :created)
end
def close_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::CLOSED)
create_record_event(milestone, current_user, :closed)
end
def reopen_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::REOPENED)
create_record_event(milestone, current_user, :reopened)
end
def destroy_milestone(milestone, current_user)
create_record_event(milestone, current_user, Event::DESTROYED)
create_record_event(milestone, current_user, :destroyed)
end
def leave_note(note, current_user)
create_record_event(note, current_user, Event::COMMENTED)
create_record_event(note, current_user, :commented)
end
def join_project(project, current_user)
create_event(project, current_user, Event::JOINED)
create_event(project, current_user, :joined)
end
def leave_project(project, current_user)
create_event(project, current_user, Event::LEFT)
create_event(project, current_user, :left)
end
def expired_leave_project(project, current_user)
create_event(project, current_user, Event::EXPIRED)
create_event(project, current_user, :expired)
end
def create_project(project, current_user)
create_event(project, current_user, Event::CREATED)
create_event(project, current_user, :created)
end
def push(project, current_user, push_data)
......@@ -100,7 +100,7 @@ class EventCreateService
#
# @param [WikiPage::Meta] wiki_page_meta The event target
# @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
def wiki_event(wiki_page_meta, author, action)
......@@ -114,7 +114,7 @@ class EventCreateService
event = create_record_event(wiki_page_meta, author, action)
# 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
event.update_columns(updated_at: time_stamp, created_at: time_stamp)
end
......@@ -125,9 +125,9 @@ class EventCreateService
private
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
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
Event.for_wiki_meta(wiki_page_meta).created_at(wiki_page_meta.updated_at).first
end
......@@ -142,7 +142,7 @@ class EventCreateService
# when creating push payload data will result in the event creation being
# rolled back as well.
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
......
......@@ -21,11 +21,11 @@ module Git
def event_action
case raw_change.operation
when :added
Event::CREATED
:created
when :deleted
Event::DESTROYED
:destroyed
else
Event::UPDATED
:updated
end
end
......
......@@ -22,7 +22,7 @@ module WikiPages
end
def event_action
Event::CREATED
:created
end
end
end
......@@ -19,7 +19,7 @@ module WikiPages
end
def event_action
Event::DESTROYED
:destroyed
end
end
end
......@@ -22,7 +22,7 @@ module WikiPages
end
def event_action
Event::UPDATED
:updated
end
def slug_for_page(page)
......
......@@ -302,7 +302,7 @@ you will see two pushes with the same "from" SHA:
```ruby
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)
end
```
......@@ -311,7 +311,7 @@ GitLab 9.5 and above:
```ruby
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)
end
```
......
......@@ -14,7 +14,7 @@ class MergeRequestsComplianceFinder < MergeRequestsFinder
lateral = Event
.select(:created_at, :target_id)
.where('projects.id = project_id')
.merged
.merged_action
.recent
.limit(1)
.to_sql
......
......@@ -53,7 +53,7 @@ class Burndown
def closed_issues_events_count
strong_memoize(:closed_issues_events_count) do
Event.closed.where(target: closed_issues).count
Event.closed_action.where(target: closed_issues).count
end
end
......@@ -76,7 +76,7 @@ class Burndown
strong_memoize(:milestone_events_per_issue) do
Event
.where(target: issues, action: [Event::CLOSED, Event::REOPENED])
.where(target: issues, action: [:closed, :reopened])
.where('created_at <= ?', end_date.end_of_day)
.order(:created_at)
.group_by(&:target_id)
......@@ -85,7 +85,7 @@ class Burndown
# Use issue creation date as the source of truth for created events
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
# Use issue events as the source of truth for events other than 'created'
......@@ -101,10 +101,10 @@ class Burndown
# 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
# state.
next if event.action == previous_action
next if event.action.to_s == previous_action.to_s
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
......@@ -113,11 +113,11 @@ class Burndown
return [] unless issue.closed?
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
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
def filter_issues_created_before(date, issues)
......
......@@ -10,10 +10,6 @@ module EE
scope :issues, -> { where(target_type: 'Issue') }
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_target_type_action, -> { group(:author_id, :target_type, :action).count }
scope :epics, -> { where(target_type: 'Epic') }
......@@ -46,9 +42,5 @@ module EE
def epic?
target_type == 'Epic'
end
def approved_action?
action == ::Event::APPROVED
end
end
end
......@@ -3,19 +3,19 @@
module EE
module EventCreateService
def open_epic(epic, current_user)
create_record_event(epic, current_user, ::Event::CREATED)
create_record_event(epic, current_user, :created)
end
def close_epic(epic, current_user)
create_record_event(epic, current_user, ::Event::CLOSED)
create_record_event(epic, current_user, :closed)
end
def reopen_epic(epic, current_user)
create_record_event(epic, current_user, ::Event::REOPENED)
create_record_event(epic, current_user, :reopened)
end
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
......@@ -15,68 +15,68 @@ module Gitlab
end
def push_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash|
hash[author_id] = count if target_type.nil? && action.eql?(Event::PUSHED)
all_counts.each_with_object({}) do |(event, count), hash|
hash[event.author_id] = count if event.target_type.nil? && event.pushed_action?
end
end
def issues_created_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash|
hash[author_id] = count if target_type.eql?(Issue.name) && action.eql?(Event::CREATED)
all_counts.each_with_object({}) do |(event, count), hash|
hash[event.author_id] = count if event.issue? && event.created_action?
end
end
def issues_closed_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash|
hash[author_id] = count if target_type.eql?(Issue.name) && action.eql?(Event::CLOSED)
all_counts.each_with_object({}) do |(event, count), hash|
hash[event.author_id] = count if event.issue? && event.closed_action?
end
end
def merge_requests_created_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash|
hash[author_id] = count if target_type.eql?(MergeRequest.name) && action.eql?(Event::CREATED)
all_counts.each_with_object({}) do |(event, count), hash|
hash[event.author_id] = count if event.merge_request? && event.created_action?
end
end
def merge_requests_merged_by_author_count
all_counts.each_with_object({}) do |((author_id, target_type, action), count), hash|
hash[author_id] = count if target_type.eql?(MergeRequest.name) && action.eql?(Event::MERGED)
all_counts.each_with_object({}) do |(event, count), hash|
hash[event.author_id] = count if event.merge_request? && event.merged_action?
end
end
def total_events_by_author_count
all_counts.each_with_object({}) do |((author_id, _target_type, _action), count), hash|
hash[author_id] ||= 0
hash[author_id] += count
all_counts.each_with_object({}) do |(event, count), hash|
hash[event.author_id] ||= 0
hash[event.author_id] += count
end
end
def total_push_author_count
all_counts.count { |(_, _, action), _| action.eql?(Event::PUSHED) }
all_counts.count { |event, _| event.pushed_action? }
end
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
def total_commit_count
PushEventPayload.commit_count_for(base_query.code_push)
PushEventPayload.commit_count_for(base_query.pushed_action)
end
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
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
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
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
def users
......@@ -112,8 +112,8 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def base_query
Event
.where(action: ::Event::PUSHED).or(
Event.where(target_type: [::MergeRequest.name, ::Issue.name], action: [::Event::CREATED, ::Event::CLOSED, ::Event::MERGED])
.where(action: :pushed).or(
Event.where(target_type: [::MergeRequest.name, ::Issue.name], action: [:created, :closed, :merged])
)
.where(Event.arel_table[:created_at].gteq(from))
.joins(:project)
......@@ -121,13 +121,21 @@ module Gitlab
end
# 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:
# {
# [user1_id, target_type, action] => count,
# [user2_id, target_type, action] => count
# }
def all_counts
@all_counts ||= Rails.cache.fetch(cache_key, expires_in: 1.minute) do
def raw_counts
Rails.cache.fetch(cache_key, expires_in: 1.minute) do
base_query.totals_by_author_target_type_action
end
end
......
......@@ -23,7 +23,7 @@ describe Groups::ContributionAnalyticsController do
end
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
end
......@@ -124,10 +124,10 @@ describe Groups::ContributionAnalyticsController do
before do
sign_in(user)
create_event(user, project, issue, Event::CLOSED)
create_event(user2, project, issue, Event::CLOSED)
create_event(user2, project, merge_request, Event::CREATED)
create_event(user3, project, merge_request, Event::CREATED)
create_event(user, project, issue, :closed)
create_event(user2, project, issue, :closed)
create_event(user2, project, merge_request, :created)
create_event(user3, project, merge_request, :created)
create_push_event(user, project)
create_push_event(user3, project)
end
......@@ -168,7 +168,7 @@ describe Groups::ContributionAnalyticsController do
subgroup = create(:group, parent: group)
subproject = create(:project, :repository, group: subgroup)
create_event(user, subproject, issue, Event::CLOSED)
create_event(user, subproject, issue, :closed)
create_push_event(user, subproject)
get :show, params: { group_id: group.path }, format: :json
......@@ -184,7 +184,7 @@ describe Groups::ContributionAnalyticsController do
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)
get :show, params: { group_id: empty_group.path }, format: :json
......
......@@ -6,9 +6,10 @@ FactoryBot.modify do
group
author(factory: :user)
target(factory: :epic)
action { Event::CREATED }
action { :created }
project { nil }
end
trait(:approved) { action { Event::APPROVED } }
trait(:approved) { action { :approved } }
end
end
......@@ -28,26 +28,26 @@ describe Gitlab::ContributionAnalytics::DataCollector do
end
end
context 'deriving various counts from #all_counts' do
let(:all_counts) do
context 'deriving various counts from #raw_counts' do
let(:raw_counts) do
{
[1, nil, Event::PUSHED] => 2,
[2, nil, Event::PUSHED] => 2,
[1, MergeRequest.name, Event::MERGED] => 2,
[4, MergeRequest.name, Event::MERGED] => 2,
[5, MergeRequest.name, Event::CREATED] => 0,
[6, MergeRequest.name, Event::CREATED] => 1,
[10, Issue.name, Event::CLOSED] => 10,
[11, Issue.name, Event::CLOSED] => 11
[1, nil, Event.actions[:pushed]] => 2,
[2, nil, Event.actions[:pushed]] => 2,
[1, MergeRequest.name, Event.actions[:merged]] => 2,
[4, MergeRequest.name, Event.actions[:merged]] => 2,
[5, MergeRequest.name, Event.actions[:created]] => 0,
[6, MergeRequest.name, Event.actions[:created]] => 1,
[10, Issue.name, Event.actions[:closed]] => 10,
[11, Issue.name, Event.actions[:closed]] => 11
}
end
let(:data_collector) { described_class.new(group: Group.new) }
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
describe 'extracts correct counts from all_counts' do
describe 'extracts correct counts from raw_counts' do
it 'for #push_by_author_count' do
expect(data_collector.push_by_author_count).to eq({ 1 => 2, 2 => 2 })
end
......@@ -73,7 +73,7 @@ describe Gitlab::ContributionAnalytics::DataCollector do
end
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.total_push_author_count).to eq(0)
......
......@@ -102,7 +102,7 @@ describe Burndown do
end
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)
event2 = create(:closed_issue_event, target: milestone.issues.first, created_at: milestone.start_date + 3.minutes)
......@@ -112,7 +112,7 @@ describe Burndown do
context "when all closed issues do not have closed events" 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
it "considers closed_at as milestone start date" do
......@@ -137,7 +137,7 @@ describe Burndown do
context "when one but not all closed issues does not have a closed event" 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)
aggregate_failures do
......
......@@ -12,7 +12,7 @@ describe EventCreateService do
it "creates new event" do
event = service.open_epic(epic, epic.author)
expect_event(event, Event::CREATED)
expect_event(event, 'created')
end
end
......@@ -20,7 +20,7 @@ describe EventCreateService do
it "creates new event" do
event = service.close_epic(epic, epic.author)
expect_event(event, Event::CLOSED)
expect_event(event, 'closed')
end
end
......@@ -28,7 +28,7 @@ describe EventCreateService do
it "creates new event" do
event = service.reopen_epic(epic, epic.author)
expect_event(event, Event::REOPENED)
expect_event(event, 'reopened')
end
end
......@@ -38,7 +38,7 @@ describe EventCreateService do
event = service.leave_note(note, epic.author)
expect_event(event, Event::COMMENTED)
expect_event(event, 'commented')
end
end
......@@ -60,7 +60,7 @@ describe EventCreateService do
it 'creates new event' do
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
......
......@@ -27,15 +27,15 @@ class EventFilter
case filter
when PUSH
events.where(action: Event::PUSHED)
events.pushed_action
when MERGED
events.where(action: Event::MERGED)
events.merged_action
when COMMENTS
events.where(action: Event::COMMENTED)
events.commented_action
when TEAM
events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED])
events.where(action: [:joined, :left, :expired])
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
wiki_events(events)
else
......
......@@ -24,13 +24,13 @@ module Gitlab
# project_features for the (currently) 3 different contribution types
date_from = 1.year.ago
repo_events = event_counts(date_from, :repository)
.having(action: Event::PUSHED)
.having(action: :pushed)
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)
.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)
.having(action: [Event::COMMENTED])
.having(action: :commented)
events = Event
.from_union([repo_events, issue_events, mr_events, note_events])
......
......@@ -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
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)
issue.reload
......
......@@ -4,27 +4,27 @@ FactoryBot.define do
factory :event do
project
author(factory: :user) { project.creator }
action { Event::JOINED }
trait(:created) { action { Event::CREATED } }
trait(:updated) { action { Event::UPDATED } }
trait(:closed) { action { Event::CLOSED } }
trait(:reopened) { action { Event::REOPENED } }
trait(:pushed) { action { Event::PUSHED } }
trait(:commented) { action { Event::COMMENTED } }
trait(:merged) { action { Event::MERGED } }
trait(:joined) { action { Event::JOINED } }
trait(:left) { action { Event::LEFT } }
trait(:destroyed) { action { Event::DESTROYED } }
trait(:expired) { action { Event::EXPIRED } }
action { :joined }
trait(:created) { action { :created } }
trait(:updated) { action { :updated } }
trait(:closed) { action { :closed } }
trait(:reopened) { action { :reopened } }
trait(:pushed) { action { :pushed } }
trait(:commented) { action { :commented } }
trait(:merged) { action { :merged } }
trait(:joined) { action { :joined } }
trait(:left) { action { :left } }
trait(:destroyed) { action { :destroyed } }
trait(:expired) { action { :expired } }
factory :closed_issue_event do
action { Event::CLOSED }
action { :closed }
target factory: :closed_issue
end
factory :wiki_page_event do
action { Event::CREATED }
action { :created }
project { @overrides[:wiki_page]&.container || create(:project, :wiki_repo) }
target { create(:wiki_page_meta, :for_wiki_page, wiki_page: wiki_page) }
......@@ -39,7 +39,7 @@ FactoryBot.define do
note { create(:note, author: author, project: project, noteable: design) }
end
action { Event::COMMENTED }
action { :commented }
target { note }
end
end
......@@ -47,7 +47,7 @@ FactoryBot.define do
factory :push_event, class: 'PushEvent' do
project factory: :project_empty_repo
author(factory: :user) { project.creator }
action { Event::PUSHED }
action { :pushed }
end
factory :push_event_payload do
......
......@@ -59,7 +59,7 @@ describe 'Contributions Calendar', :js do
def note_comment_contribution
note_comment_params = {
project: contributed_project,
action: Event::COMMENTED,
action: :commented,
target: issue_note,
author_id: user.id
}
......
......@@ -12,7 +12,7 @@ describe 'Tooltips on .timeago dates', :js do
before do
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)
sign_in user
......
......@@ -18,7 +18,7 @@ describe 'Project member activity', :js do
context 'when a user joins the project' do
before do
visit_activities_and_wait_with_event(Event::JOINED)
visit_activities_and_wait_with_event(:joined)
end
it "presents the correct message" do
......@@ -29,7 +29,7 @@ describe 'Project member activity', :js do
context 'when a user leaves the project' do
before do
visit_activities_and_wait_with_event(Event::LEFT)
visit_activities_and_wait_with_event(:left)
end
it "presents the correct message" do
......@@ -40,7 +40,7 @@ describe 'Project member activity', :js do
context 'when a users membership expires for the project' do
before do
visit_activities_and_wait_with_event(Event::EXPIRED)
visit_activities_and_wait_with_event(:expired)
end
it "presents the correct message" do
......
......@@ -11,18 +11,18 @@ describe EventsFinder do
let(:closed_issue) { create(:closed_issue, project: project1, 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!(: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!(: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, :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(: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!(: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!(: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, :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!(: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(: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
it 'returns events between specified dates filtered on action and type' do
......
......@@ -42,7 +42,7 @@ describe Gitlab::ContributionsCalendar do
described_class.new(contributor, current_user)
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[project] ||= create(target_symbol, project: project, author: contributor)
......@@ -77,14 +77,14 @@ describe Gitlab::ContributionsCalendar do
end
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)
end
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, 2, Event::COMMENTED, :discussion_note_on_issue)
create_event(public_project, today, 0, :commented, :discussion_note_on_merge_request)
create_event(public_project, today, 2, :commented, :discussion_note_on_issue)
expect(calendar(contributor).activity_dates[today]).to eq(2)
end
......
......@@ -46,12 +46,12 @@ describe ProjectMember 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.destroy
expect(Event.recent.first.action).to eq(Event::EXPIRED)
expect(Event.recent.first).to be_expired_action
end
it "creates a left event when left due to leave" do
maintainer.destroy
expect(Event.recent.first.action).to eq(Event::LEFT)
expect(Event.recent.first).to be_left_action
end
end
......
......@@ -118,8 +118,8 @@ describe PushEvent do
end
describe '.sti_name' do
it 'returns Event::PUSHED' do
expect(described_class.sti_name).to eq(Event::PUSHED)
it 'returns the integer representation of the :pushed event action' do
expect(described_class.sti_name).to eq(Event.actions[:pushed])
end
end
......@@ -299,7 +299,7 @@ describe PushEvent do
describe '#validate_push_action' do
it 'adds an error when the action is not PUSHED' do
event.action = Event::CREATED
event.action = :created
event.validate_push_action
expect(event.errors.count).to eq(1)
......
......@@ -8,7 +8,7 @@ describe UserInteractedProject do
let(:event) { build(:event) }
Event::ACTIONS.each do |action|
Event.actions.each_key do |action|
context "for all actions (event types)" do
let(:event) { build(:event, action: action) }
......
......@@ -7,9 +7,9 @@ describe API::Events do
let(:non_member) { create(:user) }
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_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_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
context 'when unauthenticated' do
......@@ -117,7 +117,7 @@ describe API::Events do
context 'when the list of events includes wiki page events' do
it 'returns information about the wiki event', :aggregate_failures do
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)
end
......
......@@ -7,7 +7,7 @@ describe API::ProjectEvents do
let(:non_member) { create(:user) }
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_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
context 'when unauthenticated ' do
......@@ -29,9 +29,9 @@ describe API::ProjectEvents do
context 'with inaccessible events' do
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_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_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
get api("/projects/#{public_project.id}/events", non_member)
......@@ -53,19 +53,19 @@ describe API::ProjectEvents do
before do
create(:event,
:closed,
project: public_project,
target: create(:issue, project: public_project, title: 'Issue 1'),
action: Event::CLOSED,
created_at: Date.parse('2018-12-10'))
create(:event,
:closed,
project: public_project,
target: create(:issue, confidential: true, project: public_project, title: 'Confidential event'),
action: Event::CLOSED,
created_at: Date.parse('2018-12-11'))
create(:event,
:closed,
project: public_project,
target: create(:issue, project: public_project, title: 'Issue 2'),
action: Event::CLOSED,
created_at: Date.parse('2018-12-12'))
end
......
......@@ -174,7 +174,7 @@ describe EventCreateService do
wiki_page?: true,
valid?: true,
persisted?: true,
action: action,
action: action.to_s,
wiki_page: wiki_page,
author: user
)
......@@ -200,7 +200,7 @@ describe EventCreateService do
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
it 'raises an error' do
expect { service.wiki_event(meta, user, bad_action) }.to raise_error(described_class::IllegalActionError)
......
......@@ -91,7 +91,7 @@ describe Git::BranchHooksService do
end
describe 'Push Event' do
let(:event) { Event.find_by_action(Event::PUSHED) }
let(:event) { Event.pushed_action.first }
before do
service.execute
......@@ -101,7 +101,7 @@ describe Git::BranchHooksService do
it 'generates a push event with one commit' do
expect(event).to be_an_instance_of(PushEvent)
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.commit_from).to eq(oldrev)
expect(event.push_event_payload.commit_to).to eq(newrev)
......@@ -117,7 +117,7 @@ describe Git::BranchHooksService do
it 'generates a push event with more than one commit' do
expect(event).to be_an_instance_of(PushEvent)
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.commit_from).to be_nil
expect(event.push_event_payload.commit_to).to eq(newrev)
......@@ -133,7 +133,7 @@ describe Git::BranchHooksService do
it 'generates a push event with no commits' do
expect(event).to be_an_instance_of(PushEvent)
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.commit_from).to eq(oldrev)
expect(event.push_event_payload.commit_to).to be_nil
......
......@@ -89,20 +89,20 @@ describe Git::WikiPushService::Change do
context 'the page is deleted' do
let(:operation) { :deleted }
it { is_expected.to have_attributes(event_action: Event::DESTROYED) }
it { is_expected.to have_attributes(event_action: :destroyed) }
end
context 'the page is added' do
let(:operation) { :added }
it { is_expected.to have_attributes(event_action: Event::CREATED) }
it { is_expected.to have_attributes(event_action: :created) }
end
%i[renamed modified].each do |op|
context "the page is #{op}" do
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
......
......@@ -54,7 +54,7 @@ describe Git::WikiPushService, services: true do
it 'handles all known actions' do
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
......@@ -77,7 +77,7 @@ describe Git::WikiPushService, services: true do
it 'creates appropriate events' do
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
......@@ -100,7 +100,7 @@ describe Git::WikiPushService, services: true do
it 'creates a wiki page creation event' do
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
it 'creates one metadata record' do
......@@ -129,7 +129,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes(
wiki_page?: true,
action: Event::CREATED
action: 'created'
)
end
end
......@@ -158,7 +158,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes(
wiki_page?: true,
action: Event::UPDATED
action: 'updated'
)
end
end
......@@ -182,7 +182,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes(
wiki_page?: true,
action: Event::UPDATED
action: 'updated'
)
end
end
......@@ -206,7 +206,7 @@ describe Git::WikiPushService, services: true do
expect(Event.last).to have_attributes(
wiki_page?: true,
action: Event::DESTROYED
action: 'destroyed'
)
end
end
......@@ -218,7 +218,7 @@ describe Git::WikiPushService, services: true do
message = 'something went very very wrong'
allow_next_instance_of(WikiPages::EventCreateService, current_user) do |service|
allow(service).to receive(:execute)
.with(String, WikiPage, Integer)
.with(String, WikiPage, Symbol)
.and_return(ServiceResponse.error(message: message))
end
......
......@@ -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
attributes = {
action: Event::CREATED,
action: :created,
target_id: merge_request.id,
target_type: merge_request.class.name
}
......
......@@ -11,7 +11,7 @@ describe WikiPages::EventCreateService do
describe '#execute' do
let_it_be(:page) { create(:wiki_page, project: project) }
let(:slug) { generate(:sluggified_title) }
let(:action) { Event::CREATED }
let(:action) { :created }
let(:response) { subject.execute(slug, page, action) }
context 'feature flag is not enabled' do
......@@ -38,7 +38,7 @@ describe WikiPages::EventCreateService do
end
context 'the action is illegal' do
let(:action) { Event::WIKI_ACTIONS.max + 1 }
let(:action) { :illegal_action }
it 'returns an error' do
expect(response).to be_error
......@@ -58,7 +58,7 @@ describe WikiPages::EventCreateService do
end
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
expect_next_instance_of(WikiPage::Meta) do |instance|
......@@ -74,7 +74,7 @@ describe WikiPages::EventCreateService do
end
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
it 'records the slug for the page' do
......
......@@ -45,7 +45,7 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type|
expect { service.execute }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes(
action: Event::CREATED,
action: 'created',
target: have_attributes(canonical_slug: page_title)
)
end
......
......@@ -27,7 +27,7 @@ RSpec.shared_examples 'WikiPages::DestroyService#execute' do |container_type|
expect { service.execute(page) }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes(
action: Event::DESTROYED,
action: 'destroyed',
target: have_attributes(canonical_slug: page.slug)
)
end
......
......@@ -48,7 +48,7 @@ RSpec.shared_examples 'WikiPages::UpdateService#execute' do |container_type|
expect { service.execute(page) }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes(
action: Event::UPDATED,
action: 'updated',
wiki_page: page,
target_title: page.title
)
......
......@@ -86,7 +86,7 @@ describe RepositoryCheck::SingleRepositoryWorker do
end
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
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