Commit 4e96b175 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'refactoring/services' of /home/git/repositories/gitlab/gitlabhq

parents 1fc42d99 4e2c34d8
class CommitLoadContext < BaseContext
def execute
result = {
commit: nil,
suppress_diff: false,
line_notes: [],
notes_count: 0,
note: nil,
status: :ok
}
commit = project.repository.commit(params[:id])
if commit
line_notes = project.notes.for_commit_id(commit.id).inline
result[:commit] = commit
result[:note] = project.build_commit_note(commit)
result[:line_notes] = line_notes
result[:notes_count] = project.notes.for_commit_id(commit.id).count
result[:branches] = project.repository.branch_names_contains(commit.id)
begin
result[:suppress_diff] = true if commit.diff_suppress? && !params[:force_show_diff]
result[:force_suppress_diff] = commit.diff_force_suppress?
rescue Grit::Git::GitTimeout
result[:suppress_diff] = true
result[:status] = :huge_commit
end
end
result
end
end
class TestHookContext < BaseContext
def execute
hook = project.hooks.find(params[:id])
data = GitPushService.new.sample_data(project, current_user)
hook.execute(data)
end
end
...@@ -6,34 +6,35 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -6,34 +6,35 @@ class Projects::CommitController < Projects::ApplicationController
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_code_access! before_filter :authorize_code_access!
before_filter :require_non_empty_project before_filter :require_non_empty_project
before_filter :commit
def show def show
result = CommitLoadContext.new(project, current_user, params).execute return git_not_found! unless @commit
@commit = result[:commit] @line_notes = project.notes.for_commit_id(commit.id).inline
@branches = project.repository.branch_names_contains(commit.id)
if @commit.nil? begin
git_not_found! @suppress_diff = true if commit.diff_suppress? && !params[:force_show_diff]
return @force_suppress_diff = commit.diff_force_suppress?
rescue Grit::Git::GitTimeout
@suppress_diff = true
@status = :huge_commit
end end
@suppress_diff = result[:suppress_diff] @note = project.build_commit_note(commit)
@force_suppress_diff = result[:force_suppress_diff] @notes_count = project.notes.for_commit_id(commit.id).count
@note = result[:note]
@line_notes = result[:line_notes]
@branches = result[:branches]
@notes_count = result[:notes_count]
@notes = project.notes.for_commit_id(@commit.id).not_inline.fresh @notes = project.notes.for_commit_id(@commit.id).not_inline.fresh
@noteable = @commit @noteable = @commit
@comments_allowed = @reply_allowed = true @comments_allowed = @reply_allowed = true
@comments_target = { noteable_type: 'Commit', @comments_target = {
commit_id: @commit.id } noteable_type: 'Commit',
commit_id: @commit.id
}
respond_to do |format| respond_to do |format|
format.html do format.html do
if result[:status] == :huge_commit if @status == :huge_commit
render "huge_commit" and return render "huge_commit" and return
end end
end end
...@@ -42,4 +43,8 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -42,4 +43,8 @@ class Projects::CommitController < Projects::ApplicationController
format.patch { render text: @commit.to_patch } format.patch { render text: @commit.to_patch }
end end
end end
def commit
@commit ||= project.repository.commit(params[:id])
end
end end
...@@ -24,15 +24,20 @@ class Projects::HooksController < Projects::ApplicationController ...@@ -24,15 +24,20 @@ class Projects::HooksController < Projects::ApplicationController
end end
def test def test
TestHookContext.new(project, current_user, params).execute TestHookService.new.execute(hook, current_user)
redirect_to :back redirect_to :back
end end
def destroy def destroy
@hook = @project.hooks.find(params[:id]) hook.destroy
@hook.destroy
redirect_to project_hooks_path(@project) redirect_to project_hooks_path(@project)
end end
private
def hook
@hook ||= @project.hooks.find(params[:id])
end
end end
...@@ -2,10 +2,16 @@ class SystemHookObserver < BaseObserver ...@@ -2,10 +2,16 @@ class SystemHookObserver < BaseObserver
observe :user, :project, :users_project observe :user, :project, :users_project
def after_create(model) def after_create(model)
SystemHooksService.execute_hooks_for(model, :create) system_hook_service.execute_hooks_for(model, :create)
end end
def after_destroy(model) def after_destroy(model)
SystemHooksService.execute_hooks_for(model, :destroy) system_hook_service.execute_hooks_for(model, :destroy)
end
private
def system_hook_service
SystemHooksService.new
end end
end end
class SystemHooksService class SystemHooksService
def self.execute_hooks_for(model, event) def execute_hooks_for(model, event)
execute_hooks(build_event_data(model, event)) execute_hooks(build_event_data(model, event))
end end
private private
def self.execute_hooks(data) def execute_hooks(data)
SystemHook.all.each do |sh| SystemHook.all.each do |sh|
async_execute_hook sh, data async_execute_hook sh, data
end end
end end
def self.async_execute_hook(hook, data) def async_execute_hook(hook, data)
Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data) Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data)
end end
def self.build_event_data(model, event) def build_event_data(model, event)
data = { data = {
event_name: build_event_name(model, event), event_name: build_event_name(model, event),
created_at: model.created_at created_at: model.created_at
...@@ -51,7 +51,7 @@ class SystemHooksService ...@@ -51,7 +51,7 @@ class SystemHooksService
end end
end end
def self.build_event_name(model, event) def build_event_name(model, event)
case model case model
when UsersProject when UsersProject
return "user_add_to_team" if event == :create return "user_add_to_team" if event == :create
......
class TestHookService
def execute(hook, current_user)
data = GitPushService.new.sample_data(hook.project, current_user)
hook.execute(data)
end
end
require 'webmock'
class ProjectHooks < Spinach::FeatureSteps class ProjectHooks < Spinach::FeatureSteps
include SharedAuthentication include SharedAuthentication
include SharedProject include SharedProject
include SharedPaths include SharedPaths
include RSpec::Matchers include RSpec::Matchers
include RSpec::Mocks::ExampleMethods include RSpec::Mocks::ExampleMethods
include WebMock::API
Given 'project has hook' do Given 'project has hook' do
@hook = create(:project_hook, project: current_project) @hook = create(:project_hook, project: current_project)
...@@ -25,8 +28,7 @@ class ProjectHooks < Spinach::FeatureSteps ...@@ -25,8 +28,7 @@ class ProjectHooks < Spinach::FeatureSteps
end end
When 'I click test hook button' do When 'I click test hook button' do
test_hook_context = double(execute: true) stub_request(:post, @hook.url).to_return(status: 200)
TestHookContext.should_receive(:new).and_return(test_hook_context)
click_link 'Test Hook' click_link 'Test Hook'
end end
......
...@@ -124,9 +124,9 @@ module API ...@@ -124,9 +124,9 @@ module API
# GET /projects/:id/repository/commits/:sha/diff # GET /projects/:id/repository/commits/:sha/diff
get ":id/repository/commits/:sha/diff" do get ":id/repository/commits/:sha/diff" do
sha = params[:sha] sha = params[:sha]
result = CommitLoadContext.new(user_project, current_user, {id: sha}).execute commit = user_project.repository.commit(sha)
not_found! "Commit" unless result[:commit] not_found! "Commit" unless commit
result[:commit].diffs commit.diffs
end end
# Get a project repository tree # Get a project repository tree
......
...@@ -24,10 +24,10 @@ describe SystemHooksService do ...@@ -24,10 +24,10 @@ describe SystemHooksService do
end end
def event_data(*args) def event_data(*args)
SystemHooksService.build_event_data(*args) SystemHooksService.new.send :build_event_data, *args
end end
def event_name(*args) def event_name(*args)
SystemHooksService.build_event_name(*args) SystemHooksService.new.send :build_event_name, *args
end end
end end
require 'spec_helper'
describe TestHookService do
let (:user) { create :user }
let (:project) { create :project_with_code }
let (:hook) { create :project_hook, project: project }
describe :execute do
it "should execute successfully" do
stub_request(:post, hook.url).to_return(status: 200)
TestHookService.new.execute(hook, user).should be_true
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