diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2017-10-03 18:13:13 -0300 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2017-10-04 00:09:48 -0300 |
commit | 1f54c9216f178e96010b28a74f04bae5848ef15d (patch) | |
tree | 7f5b63c6db133916be3cb20d94d30c0c64dc60d3 | |
parent | ccfe6860079c6c75ab5a1f831cd62af0e355331e (diff) | |
download | gitlab-ce-1f54c9216f178e96010b28a74f04bae5848ef15d.tar.gz |
Reduce method calls while evaluating Projects::MergeRequestsController#show.json36876-mr-show-json-controller-perf-improvements
-rw-r--r-- | app/models/merge_request.rb | 5 | ||||
-rw-r--r-- | app/presenters/merge_request_presenter.rb | 2 | ||||
-rw-r--r-- | app/serializers/merge_request_entity.rb | 2 | ||||
-rw-r--r-- | app/views/projects/issues/_merge_requests.html.haml | 4 | ||||
-rw-r--r-- | spec/controllers/projects/merge_requests_controller_spec.rb | 12 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/entities/merge_request.json | 2 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 43 | ||||
-rw-r--r-- | spec/presenters/merge_request_presenter_spec.rb | 4 | ||||
-rw-r--r-- | spec/serializers/merge_request_entity_spec.rb | 10 |
9 files changed, 53 insertions, 31 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 8d9a30397a9..4a575dc7ba8 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -734,10 +734,9 @@ class MergeRequest < ActiveRecord::Base end def has_ci? - has_ci_integration = source_project.try(:ci_service) - uses_gitlab_ci = all_pipelines.any? + return false if has_no_commits? - (has_ci_integration || uses_gitlab_ci) && commits.any? + !!(head_pipeline_id || all_pipelines.any? || source_project&.ci_service) end def branch_missing? diff --git a/app/presenters/merge_request_presenter.rb b/app/presenters/merge_request_presenter.rb index 2df84e58575..a25882cbb62 100644 --- a/app/presenters/merge_request_presenter.rb +++ b/app/presenters/merge_request_presenter.rb @@ -31,7 +31,7 @@ class MergeRequestPresenter < Gitlab::View::Presenter::Delegated end def remove_wip_path - if can?(current_user, :update_merge_request, merge_request.project) + if work_in_progress? && can?(current_user, :update_merge_request, merge_request.project) remove_wip_project_merge_request_path(project, merge_request) end end diff --git a/app/serializers/merge_request_entity.rb b/app/serializers/merge_request_entity.rb index 07650ce6f20..9c5b1a7d2a7 100644 --- a/app/serializers/merge_request_entity.rb +++ b/app/serializers/merge_request_entity.rb @@ -18,7 +18,6 @@ class MergeRequestEntity < IssuableEntity expose :closed_event, using: EventEntity # User entities - expose :author, using: UserEntity expose :merge_user, using: UserEntity # Diff sha's @@ -26,7 +25,6 @@ class MergeRequestEntity < IssuableEntity merge_request.diff_head_sha if merge_request.diff_head_commit end - expose :merge_commit_sha expose :merge_commit_message expose :head_pipeline, with: PipelineDetailsEntity, as: :pipeline diff --git a/app/views/projects/issues/_merge_requests.html.haml b/app/views/projects/issues/_merge_requests.html.haml index 6a567487514..5f97d31f610 100644 --- a/app/views/projects/issues/_merge_requests.html.haml +++ b/app/views/projects/issues/_merge_requests.html.haml @@ -2,13 +2,13 @@ %h2.merge-requests-title = pluralize(@merge_requests.count, 'Related Merge Request') %ul.unstyled-list.related-merge-requests - - has_any_ci = @merge_requests.any?(&:head_pipeline) + - has_any_head_pipeline = @merge_requests.any?(&:head_pipeline_id) - @merge_requests.each do |merge_request| %li %span.merge-request-ci-status - if merge_request.head_pipeline = render_pipeline_status(merge_request.head_pipeline) - - elsif has_any_ci + - elsif has_any_head_pipeline = icon('blank fw') %span.merge-request-id = merge_request.to_reference diff --git a/spec/controllers/projects/merge_requests_controller_spec.rb b/spec/controllers/projects/merge_requests_controller_spec.rb index 6775012bab5..55b72b7df97 100644 --- a/spec/controllers/projects/merge_requests_controller_spec.rb +++ b/spec/controllers/projects/merge_requests_controller_spec.rb @@ -96,18 +96,6 @@ describe Projects::MergeRequestsController do expect(response).to match_response_schema('entities/merge_request') end end - - context 'number of queries', :request_store do - it 'verifies number of queries' do - # pre-create objects - merge_request - - recorded = ActiveRecord::QueryRecorder.new { go(format: :json) } - - expect(recorded.count).to be_within(5).of(30) - expect(recorded.cached_count).to eq(0) - end - end end describe "as diff" do diff --git a/spec/fixtures/api/schemas/entities/merge_request.json b/spec/fixtures/api/schemas/entities/merge_request.json index 1030f323a1f..ce922ffad17 100644 --- a/spec/fixtures/api/schemas/entities/merge_request.json +++ b/spec/fixtures/api/schemas/entities/merge_request.json @@ -93,7 +93,7 @@ "merge_commit_message_with_description": { "type": "string" }, "diverged_commits_count": { "type": "integer" }, "commit_change_content_path": { "type": "string" }, - "remove_wip_path": { "type": "string" }, + "remove_wip_path": { "type": ["string", "null"] }, "commits_count": { "type": "integer" }, "remove_source_branch": { "type": ["boolean", "null"] }, "merge_ongoing": { "type": "boolean" } diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index d80d5657c42..188a0a98ec3 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -791,6 +791,49 @@ describe MergeRequest do end end + describe '#has_ci?' do + let(:merge_request) { build_stubbed(:merge_request) } + + context 'has ci' do + it 'returns true if MR has head_pipeline_id and commits' do + allow(merge_request).to receive_message_chain(:source_project, :ci_service) { nil } + allow(merge_request).to receive(:head_pipeline_id) { double } + allow(merge_request).to receive(:has_no_commits?) { false } + + expect(merge_request.has_ci?).to be(true) + end + + it 'returns true if MR has any pipeline and commits' do + allow(merge_request).to receive_message_chain(:source_project, :ci_service) { nil } + allow(merge_request).to receive(:head_pipeline_id) { nil } + allow(merge_request).to receive(:has_no_commits?) { false } + allow(merge_request).to receive(:all_pipelines) { [double] } + + expect(merge_request.has_ci?).to be(true) + end + + it 'returns true if MR has CI service and commits' do + allow(merge_request).to receive_message_chain(:source_project, :ci_service) { double } + allow(merge_request).to receive(:head_pipeline_id) { nil } + allow(merge_request).to receive(:has_no_commits?) { false } + allow(merge_request).to receive(:all_pipelines) { [] } + + expect(merge_request.has_ci?).to be(true) + end + end + + context 'has no ci' do + it 'returns false if MR has no CI service nor pipeline, and no commits' do + allow(merge_request).to receive_message_chain(:source_project, :ci_service) { nil } + allow(merge_request).to receive(:head_pipeline_id) { nil } + allow(merge_request).to receive(:all_pipelines) { [] } + allow(merge_request).to receive(:has_no_commits?) { true } + + expect(merge_request.has_ci?).to be(false) + end + end + end + describe '#all_pipelines' do shared_examples 'returning pipelines with proper ordering' do let!(:all_pipelines) do diff --git a/spec/presenters/merge_request_presenter_spec.rb b/spec/presenters/merge_request_presenter_spec.rb index 2187be0190d..5e114434a67 100644 --- a/spec/presenters/merge_request_presenter_spec.rb +++ b/spec/presenters/merge_request_presenter_spec.rb @@ -300,6 +300,10 @@ describe MergeRequestPresenter do described_class.new(resource, current_user: user).remove_wip_path end + before do + allow(resource).to receive(:work_in_progress?).and_return(true) + end + context 'when merge request enabled and has permission' do it 'has remove_wip_path' do allow(project).to receive(:merge_requests_enabled?) { true } diff --git a/spec/serializers/merge_request_entity_spec.rb b/spec/serializers/merge_request_entity_spec.rb index a2fd5b7daae..d14cbe5ff92 100644 --- a/spec/serializers/merge_request_entity_spec.rb +++ b/spec/serializers/merge_request_entity_spec.rb @@ -11,16 +11,6 @@ describe MergeRequestEntity do described_class.new(resource, request: request).as_json end - it 'includes author' do - req = double('request') - - author_payload = UserEntity - .represent(resource.author, request: req) - .as_json - - expect(subject[:author]).to eq(author_payload) - end - it 'includes pipeline' do req = double('request', current_user: user) pipeline = build_stubbed(:ci_pipeline) |