Commit 85ec9310 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin

Fix visibility level validation for deep nested forks

Closes to https://gitlab.com/gitlab-org/gitlab/-/issues/293870

**Problem**

`forked_from_project` value missing during the validation of the
forked project. It makes validation to use the visibility of the root
project instead of the visibility of the parent project.

**Solution**

Assign `forked_from_project` value before the fork creation.
parent 36d7c7ca
......@@ -34,11 +34,6 @@ module Projects
new_project = CreateService.new(current_user, new_fork_params).execute
return new_project unless new_project.persisted?
# Set the forked_from_project relation after saving to avoid having to
# reload the project to reset the association information and cause an
# extra query.
new_project.forked_from_project = @project
builds_access_level = @project.project_feature.builds_access_level
new_project.project_feature.update(builds_access_level: builds_access_level)
......@@ -47,6 +42,7 @@ module Projects
def new_fork_params
new_params = {
forked_from_project: @project,
visibility_level: allowed_visibility_level,
description: @project.description,
name: target_name,
......
---
title: Fix visibility level validation for deep nested forks
merge_request: 50081
author:
type: fixed
......@@ -116,6 +116,24 @@ RSpec.describe Projects::ForkService do
expect(to_project.fork_network_member.forked_from_project).to eq(from_forked_project)
end
context 'when the forked project has higher visibility than the root project' do
let(:root_project) { create(:project, :public) }
it 'successfully creates a fork of the fork with correct visibility' do
forked_project = fork_project(root_project, @to_user, using_service: true)
root_project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
# Forked project visibility is not affected by root project visibility change
expect(forked_project).to have_attributes(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
fork_of_the_fork = fork_project(forked_project, @to_user, namespace: other_namespace, using_service: true)
expect(fork_of_the_fork).to be_valid
expect(fork_of_the_fork).to have_attributes(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
end
end
it_behaves_like 'forks count cache refresh' do
let(:from_project) { from_forked_project }
let(:to_user) { @to_user }
......
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