Commit 4e96b1759cbfd62a015e2f1aaf79d2f6759df185

Authored by Dmitriy Zaporozhets
2 parents 1fc42d99 4e2c34d8

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

app/contexts/commit_load_context.rb
... ... @@ -1,34 +0,0 @@
1   -class CommitLoadContext < BaseContext
2   - def execute
3   - result = {
4   - commit: nil,
5   - suppress_diff: false,
6   - line_notes: [],
7   - notes_count: 0,
8   - note: nil,
9   - status: :ok
10   - }
11   -
12   - commit = project.repository.commit(params[:id])
13   -
14   - if commit
15   - line_notes = project.notes.for_commit_id(commit.id).inline
16   -
17   - result[:commit] = commit
18   - result[:note] = project.build_commit_note(commit)
19   - result[:line_notes] = line_notes
20   - result[:notes_count] = project.notes.for_commit_id(commit.id).count
21   - result[:branches] = project.repository.branch_names_contains(commit.id)
22   -
23   - begin
24   - result[:suppress_diff] = true if commit.diff_suppress? && !params[:force_show_diff]
25   - result[:force_suppress_diff] = commit.diff_force_suppress?
26   - rescue Grit::Git::GitTimeout
27   - result[:suppress_diff] = true
28   - result[:status] = :huge_commit
29   - end
30   - end
31   -
32   - result
33   - end
34   -end
app/contexts/test_hook_context.rb
... ... @@ -1,7 +0,0 @@
1   -class TestHookContext < BaseContext
2   - def execute
3   - hook = project.hooks.find(params[:id])
4   - data = GitPushService.new.sample_data(project, current_user)
5   - hook.execute(data)
6   - end
7   -end
app/controllers/projects/commit_controller.rb
... ... @@ -6,34 +6,35 @@ class Projects::CommitController &lt; Projects::ApplicationController
6 6 before_filter :authorize_read_project!
7 7 before_filter :authorize_code_access!
8 8 before_filter :require_non_empty_project
  9 + before_filter :commit
9 10  
10 11 def show
11   - result = CommitLoadContext.new(project, current_user, params).execute
  12 + return git_not_found! unless @commit
12 13  
13   - @commit = result[:commit]
  14 + @line_notes = project.notes.for_commit_id(commit.id).inline
  15 + @branches = project.repository.branch_names_contains(commit.id)
14 16  
15   - if @commit.nil?
16   - git_not_found!
17   - return
  17 + begin
  18 + @suppress_diff = true if commit.diff_suppress? && !params[:force_show_diff]
  19 + @force_suppress_diff = commit.diff_force_suppress?
  20 + rescue Grit::Git::GitTimeout
  21 + @suppress_diff = true
  22 + @status = :huge_commit
18 23 end
19 24  
20   - @suppress_diff = result[:suppress_diff]
21   - @force_suppress_diff = result[:force_suppress_diff]
22   -
23   - @note = result[:note]
24   - @line_notes = result[:line_notes]
25   - @branches = result[:branches]
26   - @notes_count = result[:notes_count]
  25 + @note = project.build_commit_note(commit)
  26 + @notes_count = project.notes.for_commit_id(commit.id).count
27 27 @notes = project.notes.for_commit_id(@commit.id).not_inline.fresh
28 28 @noteable = @commit
29   -
30 29 @comments_allowed = @reply_allowed = true
31   - @comments_target = { noteable_type: 'Commit',
32   - commit_id: @commit.id }
  30 + @comments_target = {
  31 + noteable_type: 'Commit',
  32 + commit_id: @commit.id
  33 + }
33 34  
34 35 respond_to do |format|
35 36 format.html do
36   - if result[:status] == :huge_commit
  37 + if @status == :huge_commit
37 38 render "huge_commit" and return
38 39 end
39 40 end
... ... @@ -42,4 +43,8 @@ class Projects::CommitController &lt; Projects::ApplicationController
42 43 format.patch { render text: @commit.to_patch }
43 44 end
44 45 end
  46 +
  47 + def commit
  48 + @commit ||= project.repository.commit(params[:id])
  49 + end
45 50 end
... ...
app/controllers/projects/hooks_controller.rb
... ... @@ -24,15 +24,20 @@ class Projects::HooksController &lt; Projects::ApplicationController
24 24 end
25 25  
26 26 def test
27   - TestHookContext.new(project, current_user, params).execute
  27 + TestHookService.new.execute(hook, current_user)
28 28  
29 29 redirect_to :back
30 30 end
31 31  
32 32 def destroy
33   - @hook = @project.hooks.find(params[:id])
34   - @hook.destroy
  33 + hook.destroy
35 34  
36 35 redirect_to project_hooks_path(@project)
37 36 end
  37 +
  38 + private
  39 +
  40 + def hook
  41 + @hook ||= @project.hooks.find(params[:id])
  42 + end
38 43 end
... ...
app/observers/system_hook_observer.rb
... ... @@ -2,10 +2,16 @@ class SystemHookObserver &lt; BaseObserver
2 2 observe :user, :project, :users_project
3 3  
4 4 def after_create(model)
5   - SystemHooksService.execute_hooks_for(model, :create)
  5 + system_hook_service.execute_hooks_for(model, :create)
6 6 end
7 7  
8 8 def after_destroy(model)
9   - SystemHooksService.execute_hooks_for(model, :destroy)
  9 + system_hook_service.execute_hooks_for(model, :destroy)
  10 + end
  11 +
  12 + private
  13 +
  14 + def system_hook_service
  15 + SystemHooksService.new
10 16 end
11 17 end
... ...
app/services/system_hooks_service.rb
1 1 class SystemHooksService
2   - def self.execute_hooks_for(model, event)
  2 + def execute_hooks_for(model, event)
3 3 execute_hooks(build_event_data(model, event))
4 4 end
5 5  
6 6 private
7 7  
8   - def self.execute_hooks(data)
  8 + def execute_hooks(data)
9 9 SystemHook.all.each do |sh|
10 10 async_execute_hook sh, data
11 11 end
12 12 end
13 13  
14   - def self.async_execute_hook(hook, data)
  14 + def async_execute_hook(hook, data)
15 15 Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data)
16 16 end
17 17  
18   - def self.build_event_data(model, event)
  18 + def build_event_data(model, event)
19 19 data = {
20 20 event_name: build_event_name(model, event),
21 21 created_at: model.created_at
... ... @@ -51,7 +51,7 @@ class SystemHooksService
51 51 end
52 52 end
53 53  
54   - def self.build_event_name(model, event)
  54 + def build_event_name(model, event)
55 55 case model
56 56 when UsersProject
57 57 return "user_add_to_team" if event == :create
... ...
app/services/test_hook_service.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +class TestHookService
  2 + def execute(hook, current_user)
  3 + data = GitPushService.new.sample_data(hook.project, current_user)
  4 + hook.execute(data)
  5 + end
  6 +end
... ...
features/steps/project/project_hooks.rb
  1 +require 'webmock'
  2 +
1 3 class ProjectHooks < Spinach::FeatureSteps
2 4 include SharedAuthentication
3 5 include SharedProject
4 6 include SharedPaths
5 7 include RSpec::Matchers
6 8 include RSpec::Mocks::ExampleMethods
  9 + include WebMock::API
7 10  
8 11 Given 'project has hook' do
9 12 @hook = create(:project_hook, project: current_project)
... ... @@ -25,8 +28,7 @@ class ProjectHooks &lt; Spinach::FeatureSteps
25 28 end
26 29  
27 30 When 'I click test hook button' do
28   - test_hook_context = double(execute: true)
29   - TestHookContext.should_receive(:new).and_return(test_hook_context)
  31 + stub_request(:post, @hook.url).to_return(status: 200)
30 32 click_link 'Test Hook'
31 33 end
32 34  
... ...
lib/api/repositories.rb
... ... @@ -124,9 +124,9 @@ module API
124 124 # GET /projects/:id/repository/commits/:sha/diff
125 125 get ":id/repository/commits/:sha/diff" do
126 126 sha = params[:sha]
127   - result = CommitLoadContext.new(user_project, current_user, {id: sha}).execute
128   - not_found! "Commit" unless result[:commit]
129   - result[:commit].diffs
  127 + commit = user_project.repository.commit(sha)
  128 + not_found! "Commit" unless commit
  129 + commit.diffs
130 130 end
131 131  
132 132 # Get a project repository tree
... ...
spec/services/system_hooks_service_spec.rb
... ... @@ -24,10 +24,10 @@ describe SystemHooksService do
24 24 end
25 25  
26 26 def event_data(*args)
27   - SystemHooksService.build_event_data(*args)
  27 + SystemHooksService.new.send :build_event_data, *args
28 28 end
29 29  
30 30 def event_name(*args)
31   - SystemHooksService.build_event_name(*args)
  31 + SystemHooksService.new.send :build_event_name, *args
32 32 end
33 33 end
... ...
spec/services/test_hook_service_spec.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +require 'spec_helper'
  2 +
  3 +describe TestHookService do
  4 + let (:user) { create :user }
  5 + let (:project) { create :project_with_code }
  6 + let (:hook) { create :project_hook, project: project }
  7 +
  8 + describe :execute do
  9 + it "should execute successfully" do
  10 + stub_request(:post, hook.url).to_return(status: 200)
  11 + TestHookService.new.execute(hook, user).should be_true
  12 + end
  13 + end
  14 +end
... ...