Commit 6f9ba4b8 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin

Refactor visibility level helper

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/250681

* Remove unused methods
  `disallowed_project_visibility_level_description`
  `disallowed_group_visibility_level_description`
  `visibility_level_errors_for_group`

* Move locally used methods to the private scope (for consistency)
  `project_visibility_level_description`
  `group_visibility_level_description`
  `project_visibility_icon_description`
  `group_visibility_icon_description`
parent a7362918
......@@ -26,76 +26,6 @@ module VisibilityLevelHelper
end
end
def project_visibility_level_description(level)
case level
when Gitlab::VisibilityLevel::PRIVATE
_("Project access must be granted explicitly to each user. If this project is part of a group, access will be granted to members of the group.")
when Gitlab::VisibilityLevel::INTERNAL
_("The project can be accessed by any logged in user except external users.")
when Gitlab::VisibilityLevel::PUBLIC
_("The project can be accessed without any authentication.")
end
end
def group_visibility_level_description(level)
case level
when Gitlab::VisibilityLevel::PRIVATE
_("The group and its projects can only be viewed by members.")
when Gitlab::VisibilityLevel::INTERNAL
_("The group and any internal projects can be viewed by any logged in user except external users.")
when Gitlab::VisibilityLevel::PUBLIC
_("The group and any public projects can be viewed without any authentication.")
end
end
# Note: these messages closely mirror the form validation strings found in the project
# model and any changes or additons to these may also need to be made there.
def disallowed_project_visibility_level_description(level, project)
level_name = Gitlab::VisibilityLevel.level_name(level).downcase
reasons = []
instructions = []
unless project.visibility_level_allowed_as_fork?(level)
reasons << "the fork source project has lower visibility"
end
unless project.visibility_level_allowed_by_group?(level)
errors = visibility_level_errors_for_group(project.group, level_name)
reasons << errors[:reason]
instructions << errors[:instruction]
end
reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
"This project cannot be #{level_name}#{reasons}.#{instructions.join}".html_safe
end
# Note: these messages closely mirror the form validation strings found in the group
# model and any changes or additons to these may also need to be made there.
def disallowed_group_visibility_level_description(level, group)
level_name = Gitlab::VisibilityLevel.level_name(level).downcase
reasons = []
instructions = []
unless group.visibility_level_allowed_by_projects?(level)
reasons << "it contains projects with higher visibility"
end
unless group.visibility_level_allowed_by_sub_groups?(level)
reasons << "it contains sub-groups with higher visibility"
end
unless group.visibility_level_allowed_by_parent?(level)
errors = visibility_level_errors_for_group(group.parent, level_name)
reasons << errors[:reason]
instructions << errors[:instruction]
end
reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
"This group cannot be #{level_name}#{reasons}.#{instructions.join}".html_safe
end
def visibility_icon_description(form_model)
if form_model.respond_to?(:visibility_level_allowed_as_fork?)
project_visibility_icon_description(form_model.visibility_level)
......@@ -104,14 +34,6 @@ module VisibilityLevelHelper
end
end
def group_visibility_icon_description(level)
"#{visibility_level_label(level)} - #{group_visibility_level_description(level)}"
end
def project_visibility_icon_description(level)
"#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
end
def visibility_level_label(level)
# The visibility level can be:
# 'VisibilityLevel|Private', 'VisibilityLevel|Internal', 'VisibilityLevel|Public'
......@@ -203,11 +125,33 @@ module VisibilityLevelHelper
current_level
end
def visibility_level_errors_for_group(group, level_name)
group_name = link_to group.name, group_path(group)
change_visibility = link_to 'change the visibility', edit_group_path(group)
def project_visibility_level_description(level)
case level
when Gitlab::VisibilityLevel::PRIVATE
_("Project access must be granted explicitly to each user. If this project is part of a group, access will be granted to members of the group.")
when Gitlab::VisibilityLevel::INTERNAL
_("The project can be accessed by any logged in user except external users.")
when Gitlab::VisibilityLevel::PUBLIC
_("The project can be accessed without any authentication.")
end
end
def group_visibility_level_description(level)
case level
when Gitlab::VisibilityLevel::PRIVATE
_("The group and its projects can only be viewed by members.")
when Gitlab::VisibilityLevel::INTERNAL
_("The group and any internal projects can be viewed by any logged in user except external users.")
when Gitlab::VisibilityLevel::PUBLIC
_("The group and any public projects can be viewed without any authentication.")
end
end
{ reason: "the visibility of #{group_name} is #{group.visibility}",
instruction: " To make this group #{level_name}, you must first #{change_visibility} of the parent group." }
def project_visibility_icon_description(level)
"#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
end
def group_visibility_icon_description(level)
"#{visibility_level_label(level)} - #{group_visibility_level_description(level)}"
end
end
......@@ -35,29 +35,33 @@ RSpec.describe VisibilityLevelHelper do
describe 'visibility_level_description' do
context 'used with a Project' do
it 'delegates projects to #project_visibility_level_description' do
expect(visibility_level_description(Gitlab::VisibilityLevel::PRIVATE, project))
.to match /project/i
let(:descriptions) do
[
visibility_level_description(Gitlab::VisibilityLevel::PRIVATE, project),
visibility_level_description(Gitlab::VisibilityLevel::INTERNAL, project),
visibility_level_description(Gitlab::VisibilityLevel::PUBLIC, project)
]
end
end
context 'used with a Group' do
it 'delegates groups to #group_visibility_level_description' do
expect(visibility_level_description(Gitlab::VisibilityLevel::PRIVATE, group))
.to match /group/i
it 'returns different project related descriptions depending on visibility level' do
expect(descriptions.uniq.size).to eq(descriptions.size)
expect(descriptions).to all match /project/i
end
end
end
describe "#project_visibility_level_description" do
it "describes private projects" do
expect(project_visibility_level_description(Gitlab::VisibilityLevel::PRIVATE))
.to eq _('Project access must be granted explicitly to each user. If this project is part of a group, access will be granted to members of the group.')
end
context 'used with a Group' do
let(:descriptions) do
[
visibility_level_description(Gitlab::VisibilityLevel::PRIVATE, group),
visibility_level_description(Gitlab::VisibilityLevel::INTERNAL, group),
visibility_level_description(Gitlab::VisibilityLevel::PUBLIC, group)
]
end
it "describes public projects" do
expect(project_visibility_level_description(Gitlab::VisibilityLevel::PUBLIC))
.to eq _('The project can be accessed without any authentication.')
it 'returns different group related descriptions depending on visibility level' do
expect(descriptions.uniq.size).to eq(descriptions.size)
expect(descriptions).to all match /group/i
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