debian-mirror-gitlab/app/helpers/groups_helper.rb

331 lines
10 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module GroupsHelper
2018-12-13 13:39:08 +05:30
def group_overview_nav_link_paths
%w[
groups#show
2019-07-07 11:18:12 +05:30
groups#details
2018-12-13 13:39:08 +05:30
groups#activity
groups#subgroups
]
end
2020-04-22 19:07:51 +05:30
def group_settings_nav_link_paths
%w[
groups#projects
groups#edit
badges#index
2020-04-25 10:58:03 +05:30
repository#show
2020-04-22 19:07:51 +05:30
ci_cd#show
integrations#index
integrations#edit
ldap_group_links#index
hooks#index
pipeline_quota#index
2021-04-29 21:17:54 +05:30
applications#index
applications#show
applications#edit
2021-03-08 18:12:59 +05:30
packages_and_registries#index
2020-04-22 19:07:51 +05:30
]
2018-03-17 18:26:18 +05:30
end
2019-12-21 20:55:43 +05:30
def group_packages_nav_link_paths
%w[
2020-10-24 23:57:45 +05:30
groups/packages#index
2019-12-21 20:55:43 +05:30
groups/container_registries#index
]
end
def group_container_registry_nav?
Gitlab.config.registry.enabled &&
2020-04-22 19:07:51 +05:30
can?(current_user, :read_container_image, @group)
2019-12-21 20:55:43 +05:30
end
2018-03-27 19:54:05 +05:30
def group_sidebar_links
@group_sidebar_links ||= get_group_sidebar_links
end
def group_sidebar_link?(link)
group_sidebar_links.include?(link)
end
2016-06-02 11:05:42 +05:30
def can_change_group_visibility_level?(group)
can?(current_user, :change_visibility_level, group)
end
2020-05-24 23:13:21 +05:30
def can_update_default_branch_protection?(group)
can?(current_user, :update_default_branch_protection, group)
end
2018-03-17 18:26:18 +05:30
def can_change_share_with_group_lock?(group)
can?(current_user, :change_share_with_group_lock, group)
end
2019-10-12 21:52:04 +05:30
def can_disable_group_emails?(group)
2019-12-21 20:55:43 +05:30
can?(current_user, :set_emails_disabled, group) && !group.parent&.emails_disabled?
2019-10-12 21:52:04 +05:30
end
2018-03-17 18:26:18 +05:30
def group_issues_count(state:)
IssuesFinder
.new(current_user, group_id: @group.id, state: state, non_archived: true, include_subgroups: true)
.execute
.count
end
2021-04-29 21:17:54 +05:30
def group_open_merge_requests_count(group)
if Feature.enabled?(:cached_sidebar_merge_requests_count, group, default_enabled: :yaml)
cached_issuables_count(@group, type: :merge_requests)
2021-03-11 19:13:27 +05:30
else
2021-04-29 21:17:54 +05:30
number_with_delimiter(group_merge_requests_count(state: 'opened'))
2021-03-11 19:13:27 +05:30
end
end
2018-03-17 18:26:18 +05:30
def group_merge_requests_count(state:)
MergeRequestsFinder
.new(current_user, group_id: @group.id, state: state, non_archived: true, include_subgroups: true)
.execute
.count
end
2021-04-29 21:17:54 +05:30
def cached_issuables_count(group, type: nil)
count_service = issuables_count_service_class(type)
return unless count_service.present?
issuables_count = count_service.new(group, current_user).count
format_issuables_count(count_service, issuables_count)
end
2021-04-17 20:07:23 +05:30
def group_dependency_proxy_url(group)
# The namespace path can include uppercase letters, which
# Docker doesn't allow. The proxy expects it to be downcased.
"#{group_url(group).downcase}#{DependencyProxy::URL_SUFFIX}"
end
2018-03-17 18:26:18 +05:30
def group_icon_url(group, options = {})
2015-04-26 12:48:37 +05:30
if group.is_a?(String)
2017-08-17 22:00:37 +05:30
group = Group.find_by_full_path(group)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
group.try(:avatar_url) || ActionController::Base.helpers.image_path('no_group_avatar.png')
2015-09-25 12:07:36 +05:30
end
def group_title(group, name = nil, url = nil)
2017-08-17 22:00:37 +05:30
@has_group_title = true
2018-12-05 23:21:45 +05:30
full_title = []
2015-09-25 12:07:36 +05:30
2021-04-29 21:17:54 +05:30
ancestors = group.ancestors.with_route
ancestors.reverse_each.with_index do |parent, index|
2018-03-17 18:26:18 +05:30
if index > 0
add_to_breadcrumb_dropdown(group_title_link(parent, hidable: false, show_avatar: true, for_dropdown: true), location: :before)
else
2018-12-05 23:21:45 +05:30
full_title << breadcrumb_list_item(group_title_link(parent, hidable: false))
2018-03-17 18:26:18 +05:30
end
2021-01-29 00:20:46 +05:30
push_to_schema_breadcrumb(simple_sanitize(parent.name), group_path(parent))
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
full_title << render("layouts/nav/breadcrumbs/collapsed_dropdown", location: :before, title: _("Show parent subgroups"))
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
full_title << breadcrumb_list_item(group_title_link(group))
2021-01-29 00:20:46 +05:30
push_to_schema_breadcrumb(simple_sanitize(group.name), group_path(group))
if name
full_title << ' &middot; '.html_safe + link_to(simple_sanitize(name), url, class: 'group-path breadcrumb-item-text js-breadcrumb-item-text')
push_to_schema_breadcrumb(simple_sanitize(name), url)
end
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
full_title.join.html_safe
2014-09-02 18:07:02 +05:30
end
2016-09-29 09:46:39 +05:30
def projects_lfs_status(group)
lfs_status =
if group.lfs_enabled?
group.projects.select(&:lfs_enabled?).size
else
group.projects.reject(&:lfs_enabled?).size
end
size = group.projects.size
if lfs_status == size
'for all projects'
else
"for #{lfs_status} out of #{pluralize(size, 'project')}"
end
end
def group_lfs_status(group)
status = group.lfs_enabled? ? 'enabled' : 'disabled'
content_tag(:span, class: "lfs-#{status}") do
"#{status.humanize} #{projects_lfs_status(group)}"
end
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def remove_group_message(group)
2020-10-24 23:57:45 +05:30
_("You are going to remove %{group_name}, this will also delete all of its subgroups and projects. Removed groups CANNOT be restored! Are you ABSOLUTELY sure?") %
2017-09-10 17:25:29 +05:30
{ group_name: group.name }
end
2018-03-17 18:26:18 +05:30
def share_with_group_lock_help_text(group)
return default_help unless group.parent&.share_with_group_lock?
if group.share_with_group_lock?
if can?(current_user, :change_share_with_group_lock, group.parent)
ancestor_locked_but_you_can_override(group)
else
ancestor_locked_so_ask_the_owner(group)
end
else
ancestor_locked_and_has_been_overridden(group)
end
end
def parent_group_options(current_group)
2019-07-07 11:18:12 +05:30
exclude_groups = current_group.self_and_descendants.pluck_primary_key
exclude_groups << current_group.parent_id if current_group.parent_id
groups = GroupsFinder.new(current_user, min_access_level: Gitlab::Access::OWNER, exclude_group_ids: exclude_groups).execute.sort_by(&:human_name).map do |group|
2018-03-17 18:26:18 +05:30
{ id: group.id, text: group.human_name }
end
groups.to_json
end
2020-10-24 23:57:45 +05:30
def group_packages_nav?
group_packages_list_nav? ||
group_container_registry_nav?
end
2021-01-29 00:20:46 +05:30
def group_dependency_proxy_nav?
@group.dependency_proxy_feature_available?
end
2020-10-24 23:57:45 +05:30
def group_packages_list_nav?
@group.packages_feature_enabled?
end
2020-11-24 15:15:51 +05:30
def show_invite_banner?(group)
Feature.enabled?(:invite_your_teammates_banner_a, group) &&
can?(current_user, :admin_group, group) &&
!just_created? &&
!multiple_members?(group)
end
2021-04-29 21:17:54 +05:30
def render_setting_to_allow_project_access_token_creation?(group)
group.root? && current_user.can?(:admin_setting_to_allow_project_access_token_creation, group)
end
2021-01-29 00:20:46 +05:30
def show_thanks_for_purchase_banner?
params.key?(:purchased_quantity) && params[:purchased_quantity].to_i > 0
end
2021-02-22 17:27:13 +05:30
def project_list_sort_by
@group_projects_sort || @sort || params[:sort] || sort_value_recently_created
end
2017-09-10 17:25:29 +05:30
private
2020-11-24 15:15:51 +05:30
def just_created?
flash[:notice] =~ /successfully created/
end
def multiple_members?(group)
group.member_count > 1
end
2018-03-27 19:54:05 +05:30
def get_group_sidebar_links
links = [:overview, :group_members]
2018-11-08 19:23:39 +05:30
resources = [:activity, :issues, :boards, :labels, :milestones,
:merge_requests]
links += resources.select do |resource|
can?(current_user, "read_group_#{resource}".to_sym, @group)
2018-03-27 19:54:05 +05:30
end
2019-09-30 21:07:59 +05:30
if can?(current_user, :read_cluster, @group)
2019-02-15 15:39:39 +05:30
links << :kubernetes
end
2018-03-27 19:54:05 +05:30
if can?(current_user, :admin_group, @group)
links << :settings
end
2020-07-28 23:09:34 +05:30
if can?(current_user, :read_wiki, @group)
links << :wiki
end
2018-03-27 19:54:05 +05:30
links
end
2018-03-17 18:26:18 +05:30
def group_title_link(group, hidable: false, show_avatar: false, for_dropdown: false)
link_to(group_path(group), class: "group-path #{'breadcrumb-item-text' unless for_dropdown} js-breadcrumb-item-text #{'hidable' if hidable}") do
2018-12-05 23:21:45 +05:30
icon = group_icon(group, class: "avatar-tile", width: 15, height: 15) if (group.try(:avatar_url) || show_avatar) && !Rails.env.test?
[icon, simple_sanitize(group.name)].join.html_safe
2017-09-10 17:25:29 +05:30
end
end
2018-03-17 18:26:18 +05:30
def ancestor_group(group)
ancestor = oldest_consecutively_locked_ancestor(group)
if can?(current_user, :read_group, ancestor)
link_to ancestor.name, group_path(ancestor)
else
ancestor.name
end
end
def remove_the_share_with_group_lock_from_ancestor(group)
ancestor = oldest_consecutively_locked_ancestor(group)
text = s_("GroupSettings|remove the share with group lock from %{ancestor_group_name}") % { ancestor_group_name: ancestor.name }
if can?(current_user, :admin_group, ancestor)
link_to text, edit_group_path(ancestor)
else
text
end
end
def oldest_consecutively_locked_ancestor(group)
group.ancestors.find do |group|
!group.has_parent? || !group.parent.share_with_group_lock?
end
end
def default_help
s_("GroupSettings|This setting will be applied to all subgroups unless overridden by a group owner. Groups that already have access to the project will continue to have access unless removed manually.")
end
def ancestor_locked_but_you_can_override(group)
s_("GroupSettings|This setting is applied on %{ancestor_group}. You can override the setting or %{remove_ancestor_share_with_group_lock}.").html_safe % { ancestor_group: ancestor_group(group), remove_ancestor_share_with_group_lock: remove_the_share_with_group_lock_from_ancestor(group) }
end
def ancestor_locked_so_ask_the_owner(group)
s_("GroupSettings|This setting is applied on %{ancestor_group}. To share projects in this group with another group, ask the owner to override the setting or %{remove_ancestor_share_with_group_lock}.").html_safe % { ancestor_group: ancestor_group(group), remove_ancestor_share_with_group_lock: remove_the_share_with_group_lock_from_ancestor(group) }
end
def ancestor_locked_and_has_been_overridden(group)
s_("GroupSettings|This setting is applied on %{ancestor_group} and has been overridden on this subgroup.").html_safe % { ancestor_group: ancestor_group(group) }
end
2021-04-29 21:17:54 +05:30
def issuables_count_service_class(type)
if type == :issues
Groups::OpenIssuesCountService
elsif type == :merge_requests
Groups::MergeRequestsCountService
end
end
def format_issuables_count(count_service, count)
if count > count_service::CACHED_COUNT_THRESHOLD
ActiveSupport::NumberHelper
.number_to_human(
count,
units: { thousand: 'k', million: 'm' }, precision: 1, significant: false, format: '%n%u'
)
else
number_with_delimiter(count)
end
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
GroupsHelper.prepend_if_ee('EE::GroupsHelper')