Commit 2cd7de99 authored by Robert May's avatar Robert May Committed by Douglas Barbosa Alexandre

Add latest commit hash to compare cache key

Adds the latest commit hash for a repository to the compare cache key.

Changelog: fixed
parent eb78edda
......@@ -126,8 +126,9 @@ module API
end
get ':id/repository/compare' do
ff_enabled = Feature.enabled?(:api_caching_rate_limit_repository_compare, user_project, default_enabled: :yaml)
cache_key = [user_project, user_project.repository.commit, :repository_compare, current_user, declared_params]
cache_action_if(ff_enabled, [user_project, :repository_compare, current_user, declared_params], expires_in: 1.minute) do
cache_action_if(ff_enabled, cache_key, expires_in: 1.minute) do
if params[:from_project_id].present?
target_project = MergeRequestTargetProjectFinder
.new(current_user: current_user, source_project: user_project, project_feature: :repository)
......
......@@ -495,6 +495,43 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns a newly created commit", :use_clean_rails_redis_caching do
# Parse the commits ourselves because json_response is cached
def commit_messages(response)
Gitlab::Json.parse(response.body)["commits"].map do |commit|
commit["message"]
end
end
# First trigger the rate limit cache
get api(route, current_user), params: { from: 'master', to: 'feature' }
expect(response).to have_gitlab_http_status(:ok)
expect(commit_messages(response)).not_to include("Cool new commit")
# Then create a new commit via the API
post api("/projects/#{project.id}/repository/commits", user), params: {
branch: "feature",
commit_message: "Cool new commit",
actions: [
{
action: "create",
file_path: "foo/bar/baz.txt",
content: "puts 8"
}
]
}
expect(response).to have_gitlab_http_status(:created)
# Now perform the same query as before, but the cache should have expired
# and our new commit should exist
get api(route, current_user), params: { from: 'master', to: 'feature' }
expect(response).to have_gitlab_http_status(:ok)
expect(commit_messages(response)).to include("Cool new commit")
end
end
context 'when unauthenticated', 'and project is public' 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