Commit 93fc192d authored by Stan Hu's avatar Stan Hu

Merge branch 'id-cache-pipelines-for-widget' into 'master'

Move pipelines from widget.json to cached_widget.json

See merge request gitlab-org/gitlab!38273
parents c3577731 6e4f6960
...@@ -46,6 +46,12 @@ class MergeRequestPollCachedWidgetEntity < IssuableEntity ...@@ -46,6 +46,12 @@ class MergeRequestPollCachedWidgetEntity < IssuableEntity
end end
end end
expose :actual_head_pipeline, as: :pipeline, if: -> (mr, _) {
Feature.enabled?(:merge_request_cached_pipeline_serializer, mr.project) && presenter(mr).can_read_pipeline?
} do |merge_request, options|
MergeRequests::PipelineEntity.represent(merge_request.actual_head_pipeline, options)
end
# Paths # Paths
# #
expose :target_branch_commits_path do |merge_request| expose :target_branch_commits_path do |merge_request|
......
...@@ -19,7 +19,9 @@ class MergeRequestPollWidgetEntity < Grape::Entity ...@@ -19,7 +19,9 @@ class MergeRequestPollWidgetEntity < Grape::Entity
# User entities # User entities
expose :merge_user, using: UserEntity expose :merge_user, using: UserEntity
expose :actual_head_pipeline, as: :pipeline, if: -> (mr, _) { presenter(mr).can_read_pipeline? } do |merge_request, options| expose :actual_head_pipeline, as: :pipeline, if: -> (mr, _) {
Feature.disabled?(:merge_request_cached_pipeline_serializer, mr.project) && presenter(mr).can_read_pipeline?
} do |merge_request, options|
MergeRequests::PipelineEntity.represent(merge_request.actual_head_pipeline, options) MergeRequests::PipelineEntity.represent(merge_request.actual_head_pipeline, options)
end end
......
...@@ -32,11 +32,18 @@ module Ci ...@@ -32,11 +32,18 @@ module Ci
Gitlab::Routing.url_helpers.project_new_merge_request_path(project, format: :json) Gitlab::Routing.url_helpers.project_new_merge_request_path(project, format: :json)
end end
def pipelines_project_merge_request_path(merge_request)
Gitlab::Routing.url_helpers.pipelines_project_merge_request_path(merge_request.target_project, merge_request, format: :json)
end
def merge_request_widget_path(merge_request)
Gitlab::Routing.url_helpers.cached_widget_project_json_merge_request_path(merge_request.project, merge_request, format: :json)
end
def each_pipelines_merge_request_path(pipeline) def each_pipelines_merge_request_path(pipeline)
pipeline.all_merge_requests.each do |merge_request| pipeline.all_merge_requests.each do |merge_request|
path = Gitlab::Routing.url_helpers.pipelines_project_merge_request_path(merge_request.target_project, merge_request, format: :json) yield(pipelines_project_merge_request_path(merge_request))
yield(merge_request_widget_path(merge_request))
yield(path)
end end
end end
......
---
name: merge_request_cached_pipeline_serializer
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/38273
rollout_issue_url:
type: development
group: group::source code
default_enabled: false
...@@ -213,4 +213,55 @@ RSpec.describe MergeRequestPollCachedWidgetEntity do ...@@ -213,4 +213,55 @@ RSpec.describe MergeRequestPollCachedWidgetEntity do
end end
end end
end end
describe 'pipeline' do
let_it_be(:pipeline) { create(:ci_empty_pipeline, project: project, ref: resource.source_branch, sha: resource.source_branch_sha, head_pipeline_of: resource) }
before do
allow_any_instance_of(MergeRequestPresenter).to receive(:can?).and_call_original
allow_any_instance_of(MergeRequestPresenter).to receive(:can?).with(user, :read_pipeline, anything).and_return(can_access)
end
context 'when user has access to pipelines' do
let(:can_access) { true }
context 'when is up to date' do
let(:req) { double('request', current_user: user, project: project) }
it 'returns pipeline' do
pipeline_payload =
MergeRequests::PipelineEntity
.represent(pipeline, request: req)
.as_json
expect(subject[:pipeline]).to eq(pipeline_payload)
end
context 'when merge_request_cached_pipeline_serializer is disabled' do
it 'does not return pipeline' do
stub_feature_flags(merge_request_cached_pipeline_serializer: false)
expect(subject[:pipeline]).to be_nil
end
end
end
context 'when user does not have access to pipelines' do
let(:can_access) { false }
let(:req) { double('request', current_user: user, project: project) }
it 'does not have pipeline' do
expect(subject[:pipeline]).to eq(nil)
end
end
context 'when is not up to date' do
it 'returns nil' do
pipeline.update!(sha: "not up to date")
expect(subject[:pipeline]).to eq(nil)
end
end
end
end
end end
...@@ -222,13 +222,21 @@ RSpec.describe MergeRequestPollWidgetEntity do ...@@ -222,13 +222,21 @@ RSpec.describe MergeRequestPollWidgetEntity do
context 'when is up to date' do context 'when is up to date' do
let(:req) { double('request', current_user: user, project: project) } let(:req) { double('request', current_user: user, project: project) }
it 'returns pipeline' do it 'does not return pipeline' do
pipeline_payload = expect(subject[:pipeline]).to be_nil
MergeRequests::PipelineEntity end
.represent(pipeline, request: req)
.as_json context 'when merge_request_cached_pipeline_serializer is disabled' do
it 'returns detailed info about pipeline' do
stub_feature_flags(merge_request_cached_pipeline_serializer: false)
pipeline_payload =
MergeRequests::PipelineEntity
.represent(pipeline, request: req)
.as_json
expect(subject[:pipeline]).to eq(pipeline_payload) expect(subject[:pipeline]).to eq(pipeline_payload)
end
end end
it 'returns ci_status' do it 'returns ci_status' do
...@@ -249,10 +257,6 @@ RSpec.describe MergeRequestPollWidgetEntity do ...@@ -249,10 +257,6 @@ RSpec.describe MergeRequestPollWidgetEntity do
let(:result) { false } let(:result) { false }
let(:req) { double('request', current_user: user, project: project) } let(:req) { double('request', current_user: user, project: project) }
it 'does not have pipeline' do
expect(subject[:pipeline]).to eq(nil)
end
it 'does not return ci_status' do it 'does not return ci_status' do
expect(subject[:ci_status]).to eq(nil) expect(subject[:ci_status]).to eq(nil)
end end
......
...@@ -26,9 +26,11 @@ RSpec.describe Ci::ExpirePipelineCacheService do ...@@ -26,9 +26,11 @@ RSpec.describe Ci::ExpirePipelineCacheService do
project = merge_request.target_project project = merge_request.target_project
merge_request_pipelines_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/pipelines.json" merge_request_pipelines_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/pipelines.json"
merge_request_widget_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/cached_widget.json"
allow_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch) allow_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch)
expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(merge_request_pipelines_path) expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(merge_request_pipelines_path)
expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(merge_request_widget_path)
subject.execute(merge_request.all_pipelines.last) subject.execute(merge_request.all_pipelines.last)
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