Commit 4e96b1759cbfd62a015e2f1aaf79d2f6759df185
Exists in
spb-stable
and in
3 other branches
Merge branch 'refactoring/services' of /home/git/repositories/gitlab/gitlabhq
Showing
11 changed files
with
71 additions
and
74 deletions
Show diff stats
app/contexts/commit_load_context.rb
@@ -1,34 +0,0 @@ | @@ -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
app/controllers/projects/commit_controller.rb
@@ -6,34 +6,35 @@ class Projects::CommitController < Projects::ApplicationController | @@ -6,34 +6,35 @@ class Projects::CommitController < Projects::ApplicationController | ||
6 | before_filter :authorize_read_project! | 6 | before_filter :authorize_read_project! |
7 | before_filter :authorize_code_access! | 7 | before_filter :authorize_code_access! |
8 | before_filter :require_non_empty_project | 8 | before_filter :require_non_empty_project |
9 | + before_filter :commit | ||
9 | 10 | ||
10 | def show | 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 | end | 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 | @notes = project.notes.for_commit_id(@commit.id).not_inline.fresh | 27 | @notes = project.notes.for_commit_id(@commit.id).not_inline.fresh |
28 | @noteable = @commit | 28 | @noteable = @commit |
29 | - | ||
30 | @comments_allowed = @reply_allowed = true | 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 | respond_to do |format| | 35 | respond_to do |format| |
35 | format.html do | 36 | format.html do |
36 | - if result[:status] == :huge_commit | 37 | + if @status == :huge_commit |
37 | render "huge_commit" and return | 38 | render "huge_commit" and return |
38 | end | 39 | end |
39 | end | 40 | end |
@@ -42,4 +43,8 @@ class Projects::CommitController < Projects::ApplicationController | @@ -42,4 +43,8 @@ class Projects::CommitController < Projects::ApplicationController | ||
42 | format.patch { render text: @commit.to_patch } | 43 | format.patch { render text: @commit.to_patch } |
43 | end | 44 | end |
44 | end | 45 | end |
46 | + | ||
47 | + def commit | ||
48 | + @commit ||= project.repository.commit(params[:id]) | ||
49 | + end | ||
45 | end | 50 | end |
app/controllers/projects/hooks_controller.rb
@@ -24,15 +24,20 @@ class Projects::HooksController < Projects::ApplicationController | @@ -24,15 +24,20 @@ class Projects::HooksController < Projects::ApplicationController | ||
24 | end | 24 | end |
25 | 25 | ||
26 | def test | 26 | def test |
27 | - TestHookContext.new(project, current_user, params).execute | 27 | + TestHookService.new.execute(hook, current_user) |
28 | 28 | ||
29 | redirect_to :back | 29 | redirect_to :back |
30 | end | 30 | end |
31 | 31 | ||
32 | def destroy | 32 | def destroy |
33 | - @hook = @project.hooks.find(params[:id]) | ||
34 | - @hook.destroy | 33 | + hook.destroy |
35 | 34 | ||
36 | redirect_to project_hooks_path(@project) | 35 | redirect_to project_hooks_path(@project) |
37 | end | 36 | end |
37 | + | ||
38 | + private | ||
39 | + | ||
40 | + def hook | ||
41 | + @hook ||= @project.hooks.find(params[:id]) | ||
42 | + end | ||
38 | end | 43 | end |
app/observers/system_hook_observer.rb
@@ -2,10 +2,16 @@ class SystemHookObserver < BaseObserver | @@ -2,10 +2,16 @@ class SystemHookObserver < BaseObserver | ||
2 | observe :user, :project, :users_project | 2 | observe :user, :project, :users_project |
3 | 3 | ||
4 | def after_create(model) | 4 | def after_create(model) |
5 | - SystemHooksService.execute_hooks_for(model, :create) | 5 | + system_hook_service.execute_hooks_for(model, :create) |
6 | end | 6 | end |
7 | 7 | ||
8 | def after_destroy(model) | 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 | end | 16 | end |
11 | end | 17 | end |
app/services/system_hooks_service.rb
1 | class SystemHooksService | 1 | class SystemHooksService |
2 | - def self.execute_hooks_for(model, event) | 2 | + def execute_hooks_for(model, event) |
3 | execute_hooks(build_event_data(model, event)) | 3 | execute_hooks(build_event_data(model, event)) |
4 | end | 4 | end |
5 | 5 | ||
6 | private | 6 | private |
7 | 7 | ||
8 | - def self.execute_hooks(data) | 8 | + def execute_hooks(data) |
9 | SystemHook.all.each do |sh| | 9 | SystemHook.all.each do |sh| |
10 | async_execute_hook sh, data | 10 | async_execute_hook sh, data |
11 | end | 11 | end |
12 | end | 12 | end |
13 | 13 | ||
14 | - def self.async_execute_hook(hook, data) | 14 | + def async_execute_hook(hook, data) |
15 | Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data) | 15 | Sidekiq::Client.enqueue(SystemHookWorker, hook.id, data) |
16 | end | 16 | end |
17 | 17 | ||
18 | - def self.build_event_data(model, event) | 18 | + def build_event_data(model, event) |
19 | data = { | 19 | data = { |
20 | event_name: build_event_name(model, event), | 20 | event_name: build_event_name(model, event), |
21 | created_at: model.created_at | 21 | created_at: model.created_at |
@@ -51,7 +51,7 @@ class SystemHooksService | @@ -51,7 +51,7 @@ class SystemHooksService | ||
51 | end | 51 | end |
52 | end | 52 | end |
53 | 53 | ||
54 | - def self.build_event_name(model, event) | 54 | + def build_event_name(model, event) |
55 | case model | 55 | case model |
56 | when UsersProject | 56 | when UsersProject |
57 | return "user_add_to_team" if event == :create | 57 | return "user_add_to_team" if event == :create |
features/steps/project/project_hooks.rb
1 | +require 'webmock' | ||
2 | + | ||
1 | class ProjectHooks < Spinach::FeatureSteps | 3 | class ProjectHooks < Spinach::FeatureSteps |
2 | include SharedAuthentication | 4 | include SharedAuthentication |
3 | include SharedProject | 5 | include SharedProject |
4 | include SharedPaths | 6 | include SharedPaths |
5 | include RSpec::Matchers | 7 | include RSpec::Matchers |
6 | include RSpec::Mocks::ExampleMethods | 8 | include RSpec::Mocks::ExampleMethods |
9 | + include WebMock::API | ||
7 | 10 | ||
8 | Given 'project has hook' do | 11 | Given 'project has hook' do |
9 | @hook = create(:project_hook, project: current_project) | 12 | @hook = create(:project_hook, project: current_project) |
@@ -25,8 +28,7 @@ class ProjectHooks < Spinach::FeatureSteps | @@ -25,8 +28,7 @@ class ProjectHooks < Spinach::FeatureSteps | ||
25 | end | 28 | end |
26 | 29 | ||
27 | When 'I click test hook button' do | 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 | click_link 'Test Hook' | 32 | click_link 'Test Hook' |
31 | end | 33 | end |
32 | 34 |
lib/api/repositories.rb
@@ -124,9 +124,9 @@ module API | @@ -124,9 +124,9 @@ module API | ||
124 | # GET /projects/:id/repository/commits/:sha/diff | 124 | # GET /projects/:id/repository/commits/:sha/diff |
125 | get ":id/repository/commits/:sha/diff" do | 125 | get ":id/repository/commits/:sha/diff" do |
126 | sha = params[:sha] | 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 | end | 130 | end |
131 | 131 | ||
132 | # Get a project repository tree | 132 | # Get a project repository tree |
spec/services/system_hooks_service_spec.rb
@@ -24,10 +24,10 @@ describe SystemHooksService do | @@ -24,10 +24,10 @@ describe SystemHooksService do | ||
24 | end | 24 | end |
25 | 25 | ||
26 | def event_data(*args) | 26 | def event_data(*args) |
27 | - SystemHooksService.build_event_data(*args) | 27 | + SystemHooksService.new.send :build_event_data, *args |
28 | end | 28 | end |
29 | 29 | ||
30 | def event_name(*args) | 30 | def event_name(*args) |
31 | - SystemHooksService.build_event_name(*args) | 31 | + SystemHooksService.new.send :build_event_name, *args |
32 | end | 32 | end |
33 | end | 33 | end |
@@ -0,0 +1,14 @@ | @@ -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 |