Commit ee3e0405 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Ensure forks count cache refresh for source project

Projects::ForksService#execute used to only refresh the
cache when marking a project as fork of another, but
didn't when just making simple fork to a new project.

This commit fixes that by ensuring a cache refresh
in any successful scenario.
parent ad72bab6
...@@ -3,11 +3,16 @@ ...@@ -3,11 +3,16 @@
module Projects module Projects
class ForkService < BaseService class ForkService < BaseService
def execute(fork_to_project = nil) def execute(fork_to_project = nil)
if fork_to_project forked_project =
link_existing_project(fork_to_project) if fork_to_project
else link_existing_project(fork_to_project)
fork_new_project else
end fork_new_project
end
refresh_forks_count if forked_project&.saved?
forked_project
end end
private private
...@@ -92,8 +97,7 @@ module Projects ...@@ -92,8 +97,7 @@ module Projects
def link_fork_network(fork_to_project) def link_fork_network(fork_to_project)
return if fork_to_project.errors.any? return if fork_to_project.errors.any?
fork_to_project.fork_network_member.save && fork_to_project.fork_network_member.save
refresh_forks_count
end end
def refresh_forks_count def refresh_forks_count
......
---
title: Ensure forks count cache refresh for source project
merge_request: 21771
author:
type: fixed
...@@ -6,6 +6,16 @@ describe Projects::ForkService do ...@@ -6,6 +6,16 @@ describe Projects::ForkService do
include ProjectForksHelper include ProjectForksHelper
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
shared_examples 'forks count cache refresh' do
it 'flushes the forks count cache of the source project', :clean_gitlab_redis_cache do
expect(from_project.forks_count).to be_zero
fork_project(from_project, to_user)
expect(from_project.forks_count).to eq(1)
end
end
context 'when forking a new project' do context 'when forking a new project' do
describe 'fork by user' do describe 'fork by user' do
before do before do
...@@ -40,6 +50,11 @@ describe Projects::ForkService do ...@@ -40,6 +50,11 @@ describe Projects::ForkService do
end end
end end
it_behaves_like 'forks count cache refresh' do
let(:from_project) { @from_project }
let(:to_user) { @to_user }
end
describe "successfully creates project in the user namespace" do describe "successfully creates project in the user namespace" do
let(:to_project) { fork_project(@from_project, @to_user, namespace: @to_user.namespace) } let(:to_project) { fork_project(@from_project, @to_user, namespace: @to_user.namespace) }
...@@ -62,12 +77,9 @@ describe Projects::ForkService do ...@@ -62,12 +77,9 @@ describe Projects::ForkService do
expect(@from_project.avatar.file).to be_exists expect(@from_project.avatar.file).to be_exists
end end
it 'flushes the forks count cache of the source project' do it_behaves_like 'forks count cache refresh' do
expect(@from_project.forks_count).to be_zero let(:from_project) { @from_project }
let(:to_user) { @to_user }
fork_project(@from_project, @to_user)
expect(@from_project.forks_count).to eq(1)
end end
it 'creates a fork network with the new project and the root project set' do it 'creates a fork network with the new project and the root project set' do
...@@ -102,6 +114,11 @@ describe Projects::ForkService do ...@@ -102,6 +114,11 @@ describe Projects::ForkService do
it 'sets the forked_from_project on the membership' do it 'sets the forked_from_project on the membership' do
expect(to_project.fork_network_member.forked_from_project).to eq(from_forked_project) expect(to_project.fork_network_member.forked_from_project).to eq(from_forked_project)
end end
it_behaves_like 'forks count cache refresh' do
let(:from_project) { from_forked_project }
let(:to_user) { @to_user }
end
end end
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