Commit 458c3df2 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'cache-entities-compare-api' into 'master'

Cache entities for compare API [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!64418
parents f4945746 ecbf3707
......@@ -25,6 +25,10 @@ class Compare
@straight = straight
end
def cache_key
[@project, :compare, diff_refs.hash]
end
def commits
@commits ||= Commit.decorate(@compare.commits, project)
end
......
---
name: api_caching_repository_compare
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64418
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/334264
milestone: '14.1'
type: development
group: group::source code
default_enabled: false
......@@ -138,7 +138,11 @@ module API
compare = CompareService.new(user_project, params[:to]).execute(target_project, params[:from], straight: params[:straight])
if compare
present compare, with: Entities::Compare
if Feature.enabled?(:api_caching_repository_compare, user_project, default_enabled: :yaml)
present_cached compare, with: Entities::Compare, expires_in: 1.day, cache_context: nil
else
present compare, with: Entities::Compare
end
else
not_found!("Ref")
end
......
......@@ -13,7 +13,15 @@ RSpec.describe Compare do
let(:raw_compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, start_commit.id, head_commit.id) }
subject { described_class.new(raw_compare, project) }
subject(:compare) { described_class.new(raw_compare, project) }
describe '#cache_key' do
subject { compare.cache_key }
it { is_expected.to include(project) }
it { is_expected.to include(:compare) }
it { is_expected.to include(compare.diff_refs.hash) }
end
describe '#start_commit' do
it 'returns raw compare base commit' do
......
......@@ -488,6 +488,17 @@ RSpec.describe API::Repositories do
let(:current_user) { nil }
end
end
context 'api_caching_repository_compare is disabled' do
before do
stub_feature_flags(api_caching_repository_compare: false)
end
it_behaves_like 'repository compare' do
let(:project) { create(:project, :public, :repository) }
let(:current_user) { nil }
end
end
end
describe 'GET /projects/:id/repository/contributors' do
......
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