Commit 07d05d2d authored by Douwe Maan's avatar Douwe Maan

Move all event creation to EventCreateService.

parent 529188e4
...@@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController ...@@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController
tag = @repository.find_tag(params[:id]) tag = @repository.find_tag(params[:id])
if tag && @repository.rm_tag(tag.name) if tag && @repository.rm_tag(tag.name)
Event.create_ref_event(@project, current_user, tag, 'rm', 'refs/tags') EventCreateService.new.push_ref(@project, current_user, tag, 'rm', 'refs/tags')
end end
respond_to do |format| respond_to do |format|
......
...@@ -49,29 +49,6 @@ class Event < ActiveRecord::Base ...@@ -49,29 +49,6 @@ class Event < ActiveRecord::Base
scope :in_projects, ->(project_ids) { where(project_id: project_ids).recent } scope :in_projects, ->(project_ids) { where(project_id: project_ids).recent }
class << self class << self
def create_ref_event(project, user, ref, action = 'add', prefix = 'refs/heads')
commit = project.repository.commit(ref.target)
if action.to_s == 'add'
before = '00000000'
after = commit.id
else
before = commit.id
after = '00000000'
end
Event.create(
project: project,
action: Event::PUSHED,
data: {
ref: "#{prefix}/#{ref.name}",
before: before,
after: after
},
author_id: user.id
)
end
def reset_event_cache_for(target) def reset_event_cache_for(target)
Event.where(target_id: target.id, target_type: target.class.to_s). Event.where(target_id: target.id, target_type: target.class.to_s).
order('id DESC').limit(100). order('id DESC').limit(100).
......
...@@ -114,12 +114,8 @@ class ProjectMember < Member ...@@ -114,12 +114,8 @@ class ProjectMember < Member
end end
def post_create_hook def post_create_hook
Event.create(
project_id: self.project.id,
action: Event::JOINED,
author_id: self.user.id
)
event_service.join_project(self.project, self.user)
notification_service.new_team_member(self) unless owner? notification_service.new_team_member(self) unless owner?
system_hook_service.execute_hooks_for(self, :create) system_hook_service.execute_hooks_for(self, :create)
end end
...@@ -129,15 +125,14 @@ class ProjectMember < Member ...@@ -129,15 +125,14 @@ class ProjectMember < Member
end end
def post_destroy_hook def post_destroy_hook
Event.create( event_service.leave_project(self.project, self.user)
project_id: self.project.id,
action: Event::LEFT,
author_id: self.user.id
)
system_hook_service.execute_hooks_for(self, :destroy) system_hook_service.execute_hooks_for(self, :destroy)
end end
def event_service
EventCreateService.new
end
def notification_service def notification_service
NotificationService.new NotificationService.new
end end
......
...@@ -17,7 +17,7 @@ class CreateBranchService < BaseService ...@@ -17,7 +17,7 @@ class CreateBranchService < BaseService
new_branch = repository.find_branch(branch_name) new_branch = repository.find_branch(branch_name)
if new_branch if new_branch
Event.create_ref_event(project, current_user, new_branch, 'add') EventCreateService.new.push_ref(project, current_user, new_branch, 'add')
return success(new_branch) return success(new_branch)
else else
return error('Invalid reference name') return error('Invalid reference name')
......
...@@ -26,7 +26,7 @@ class CreateTagService < BaseService ...@@ -26,7 +26,7 @@ class CreateTagService < BaseService
project.gitlab_ci_service.async_execute(push_data) project.gitlab_ci_service.async_execute(push_data)
end end
Event.create_ref_event(project, current_user, new_tag, 'add', 'refs/tags') EventCreateService.new.push_ref(project, current_user, new_tag, 'add', 'refs/tags')
success(new_tag) success(new_tag)
else else
error('Invalid reference name') error('Invalid reference name')
......
...@@ -25,7 +25,7 @@ class DeleteBranchService < BaseService ...@@ -25,7 +25,7 @@ class DeleteBranchService < BaseService
end end
if repository.rm_branch(branch_name) if repository.rm_branch(branch_name)
Event.create_ref_event(project, current_user, branch, 'rm') EventCreateService.new.push_ref(project, current_user, branch, 'rm')
success('Branch was removed') success('Branch was removed')
else else
return error('Failed to remove branch') return error('Failed to remove branch')
......
...@@ -7,58 +7,94 @@ ...@@ -7,58 +7,94 @@
# #
class EventCreateService class EventCreateService
def open_issue(issue, current_user) def open_issue(issue, current_user)
create_event(issue, current_user, Event::CREATED) create_record_event(issue, current_user, Event::CREATED)
end end
def close_issue(issue, current_user) def close_issue(issue, current_user)
create_event(issue, current_user, Event::CLOSED) create_record_event(issue, current_user, Event::CLOSED)
end end
def reopen_issue(issue, current_user) def reopen_issue(issue, current_user)
create_event(issue, current_user, Event::REOPENED) create_record_event(issue, current_user, Event::REOPENED)
end end
def open_mr(merge_request, current_user) def open_mr(merge_request, current_user)
create_event(merge_request, current_user, Event::CREATED) create_record_event(merge_request, current_user, Event::CREATED)
end end
def close_mr(merge_request, current_user) def close_mr(merge_request, current_user)
create_event(merge_request, current_user, Event::CLOSED) create_record_event(merge_request, current_user, Event::CLOSED)
end end
def reopen_mr(merge_request, current_user) def reopen_mr(merge_request, current_user)
create_event(merge_request, current_user, Event::REOPENED) create_record_event(merge_request, current_user, Event::REOPENED)
end end
def merge_mr(merge_request, current_user) def merge_mr(merge_request, current_user)
create_event(merge_request, current_user, Event::MERGED) create_record_event(merge_request, current_user, Event::MERGED)
end end
def open_milestone(milestone, current_user) def open_milestone(milestone, current_user)
create_event(milestone, current_user, Event::CREATED) create_record_event(milestone, current_user, Event::CREATED)
end end
def close_milestone(milestone, current_user) def close_milestone(milestone, current_user)
create_event(milestone, current_user, Event::CLOSED) create_record_event(milestone, current_user, Event::CLOSED)
end end
def reopen_milestone(milestone, current_user) def reopen_milestone(milestone, current_user)
create_event(milestone, current_user, Event::REOPENED) create_record_event(milestone, current_user, Event::REOPENED)
end end
def leave_note(note, current_user) def leave_note(note, current_user)
create_event(note, current_user, Event::COMMENTED) create_record_event(note, current_user, Event::COMMENTED)
end
def join_project(project, current_user)
create_event(project, current_user, Event::JOINED)
end
def leave_project(project, current_user)
create_event(project, current_user, Event::LEFT)
end
def push_ref(project, current_user, ref, action = 'add', prefix = 'refs/heads')
commit = project.repository.commit(ref.target)
if action.to_s == 'add'
before = '00000000'
after = commit.id
else
before = commit.id
after = '00000000'
end
data = {
ref: "#{prefix}/#{ref.name}",
before: before,
after: after
}
push(project, current_user, data)
end
def push(project, current_user, push_data)
create_event(project, current_user, Event::PUSHED, data: push_data)
end end
private private
def create_event(record, current_user, status) def create_record_event(record, current_user, status)
Event.create( create_event(record.project, current_user, status, target_id: record.id, target_type: record.class.name)
project: record.project, end
target_id: record.id,
target_type: record.class.name, def create_event(project, current_user, status, attributes = {})
attributes.reverse_merge!(
project: project,
action: status, action: status,
author_id: current_user.id author_id: current_user.id
) )
Event.create(attributes)
end end
end end
...@@ -52,7 +52,7 @@ class GitPushService ...@@ -52,7 +52,7 @@ class GitPushService
end end
@push_data = post_receive_data(oldrev, newrev, ref) @push_data = post_receive_data(oldrev, newrev, ref)
create_push_event(@push_data) EventCreateService.new.push(project, user, @push_data)
project.execute_hooks(@push_data.dup, :push_hooks) project.execute_hooks(@push_data.dup, :push_hooks)
project.execute_services(@push_data.dup) project.execute_services(@push_data.dup)
end end
...@@ -60,15 +60,6 @@ class GitPushService ...@@ -60,15 +60,6 @@ class GitPushService
protected protected
def create_push_event(push_data)
Event.create!(
project: project,
action: Event::PUSHED,
data: push_data,
author_id: push_data[:user_id]
)
end
# Extract any GFM references from the pushed commit messages. If the configured issue-closing regex is matched, # Extract any GFM references from the pushed commit messages. If the configured issue-closing regex is matched,
# close the referenced Issue. Create cross-reference Notes corresponding to any other referenced Mentionables. # close the referenced Issue. Create cross-reference Notes corresponding to any other referenced Mentionables.
def process_commit_messages(ref) def process_commit_messages(ref)
......
...@@ -5,7 +5,7 @@ class GitTagPushService ...@@ -5,7 +5,7 @@ class GitTagPushService
@project, @user = project, user @project, @user = project, user
@push_data = create_push_data(oldrev, newrev, ref) @push_data = create_push_data(oldrev, newrev, ref)
create_push_event EventCreateService.new.push(project, user, @push_data)
project.repository.expire_cache project.repository.expire_cache
project.execute_hooks(@push_data.dup, :tag_push_hooks) project.execute_hooks(@push_data.dup, :tag_push_hooks)
...@@ -22,13 +22,4 @@ class GitTagPushService ...@@ -22,13 +22,4 @@ class GitTagPushService
Gitlab::PushDataBuilder. Gitlab::PushDataBuilder.
build(project, user, oldrev, newrev, ref, []) build(project, user, oldrev, newrev, ref, [])
end end
def create_push_event
Event.create!(
project: project,
action: Event::PUSHED,
data: push_data,
author_id: push_data[:user_id]
)
end
end end
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