debian-mirror-gitlab/app/presenters/project_presenter.rb

434 lines
14 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
class ProjectPresenter < Gitlab::View::Presenter::Delegated
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::UrlHelper
include GitlabRoutingHelper
include StorageHelper
include TreeHelper
2019-02-15 15:39:39 +05:30
include IconsHelper
2020-11-24 15:15:51 +05:30
include BlobHelper
2018-05-09 12:01:36 +05:30
include ChecksCollaboration
2018-03-27 19:54:05 +05:30
include Gitlab::Utils::StrongMemoize
presents :project
2021-01-29 00:20:46 +05:30
AnchorData = Struct.new(:is_link, :label, :link, :class_modifier, :icon, :itemprop)
2019-03-02 22:35:43 +05:30
MAX_TOPICS_TO_SHOW = 3
2018-11-20 20:47:30 +05:30
2019-02-15 15:39:39 +05:30
def statistic_icon(icon_name = 'plus-square-o')
2020-10-24 23:57:45 +05:30
sprite_icon(icon_name, css_class: 'icon gl-mr-2')
2019-02-15 15:39:39 +05:30
end
2018-03-27 19:54:05 +05:30
def statistics_anchors(show_auto_devops_callout:)
[
commits_anchor_data,
branches_anchor_data,
tags_anchor_data,
2020-03-13 15:44:24 +05:30
files_anchor_data,
2020-04-22 19:07:51 +05:30
storage_anchor_data,
2020-03-13 15:44:24 +05:30
releases_anchor_data
2019-02-15 15:39:39 +05:30
].compact.select(&:is_link)
2018-03-27 19:54:05 +05:30
end
def statistics_buttons(show_auto_devops_callout:)
[
2018-11-08 19:23:39 +05:30
readme_anchor_data,
2019-12-26 22:10:19 +05:30
license_anchor_data,
2018-03-27 19:54:05 +05:30
changelog_anchor_data,
contribution_guide_anchor_data,
autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
kubernetes_cluster_anchor_data,
2018-12-13 13:39:08 +05:30
gitlab_ci_anchor_data
2019-07-31 22:56:46 +05:30
].compact.reject(&:is_link).sort_by.with_index { |item, idx| [item.class_modifier ? 0 : 1, idx] }
2018-03-27 19:54:05 +05:30
end
def empty_repo_statistics_anchors
2019-12-26 22:10:19 +05:30
[]
2018-03-27 19:54:05 +05:30
end
def empty_repo_statistics_buttons
[
new_file_anchor_data,
readme_anchor_data,
2019-12-26 22:10:19 +05:30
license_anchor_data,
2019-02-15 15:39:39 +05:30
changelog_anchor_data,
2019-12-21 20:55:43 +05:30
contribution_guide_anchor_data,
gitlab_ci_anchor_data
2019-02-15 15:39:39 +05:30
].compact.reject { |item| item.is_link }
2018-03-27 19:54:05 +05:30
end
def default_view
return anonymous_project_view unless current_user
user_view = current_user.project_view
if can?(current_user, :download_code, project)
user_view
2021-01-03 14:25:43 +05:30
elsif user_view == 'activity'
'activity'
elsif project.wiki_repository_exists? && can?(current_user, :read_wiki, project)
'wiki'
elsif can?(current_user, :read_issue, project)
'projects/issues/issues'
2018-03-27 19:54:05 +05:30
else
2021-01-03 14:25:43 +05:30
'activity'
2018-03-27 19:54:05 +05:30
end
end
def readme_path
filename_path(:readme)
end
def changelog_path
filename_path(:changelog)
end
def license_path
filename_path(:license_blob)
end
def ci_configuration_path
filename_path(:gitlab_ci_yml)
end
def contribution_guide_path
if project && contribution_guide = repository.contribution_guide
project_blob_path(
project,
tree_join(project.default_branch,
contribution_guide.name)
)
end
end
def add_license_path
add_special_file_path(file_name: 'LICENSE')
end
2021-01-03 14:25:43 +05:30
def add_license_ide_path
ide_edit_path(project, default_branch_or_master, 'LICENSE')
end
2018-03-27 19:54:05 +05:30
def add_changelog_path
add_special_file_path(file_name: 'CHANGELOG')
end
2021-01-03 14:25:43 +05:30
def add_changelog_ide_path
ide_edit_path(project, default_branch_or_master, 'CHANGELOG')
end
2018-03-27 19:54:05 +05:30
def add_contribution_guide_path
2019-02-15 15:39:39 +05:30
add_special_file_path(file_name: 'CONTRIBUTING.md', commit_message: 'Add CONTRIBUTING')
2018-03-27 19:54:05 +05:30
end
2021-01-03 14:25:43 +05:30
def add_contribution_guide_ide_path
ide_edit_path(project, default_branch_or_master, 'CONTRIBUTING.md')
2020-11-24 15:15:51 +05:30
end
2021-01-03 14:25:43 +05:30
def add_ci_yml_path
add_special_file_path(file_name: ci_config_path_or_default)
2018-03-27 19:54:05 +05:30
end
def add_readme_path
add_special_file_path(file_name: 'README.md')
end
2021-01-03 14:25:43 +05:30
def add_readme_ide_path
ide_edit_path(project, default_branch_or_master, 'README.md')
end
2018-03-27 19:54:05 +05:30
def license_short_name
license = repository.license
license&.nickname || license&.name || 'LICENSE'
end
def can_current_user_push_code?
strong_memoize(:can_current_user_push_code) do
if empty_repo?
can?(current_user, :push_code, project)
else
can_current_user_push_to_branch?(default_branch)
end
end
end
def can_current_user_push_to_branch?(branch)
2018-05-09 12:01:36 +05:30
user_access(project).can_push_to_branch?(branch)
end
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
def can_current_user_push_to_default_branch?
can_current_user_push_to_branch?(default_branch)
2018-03-27 19:54:05 +05:30
end
def files_anchor_data
2018-11-20 20:47:30 +05:30
AnchorData.new(true,
2019-02-15 15:39:39 +05:30
statistic_icon('doc-code') +
_('%{strong_start}%{human_size}%{strong_end} Files').html_safe % {
human_size: storage_counter(statistics.total_repository_size),
strong_start: '<strong class="project-stat-value">'.html_safe,
strong_end: '</strong>'.html_safe
},
2018-11-20 20:47:30 +05:30
empty_repo? ? nil : project_tree_path(project))
2018-03-27 19:54:05 +05:30
end
2020-04-22 19:07:51 +05:30
def storage_anchor_data
AnchorData.new(true,
statistic_icon('disk') +
_('%{strong_start}%{human_size}%{strong_end} Storage').html_safe % {
human_size: storage_counter(statistics.storage_size),
strong_start: '<strong class="project-stat-value">'.html_safe,
strong_end: '</strong>'.html_safe
},
empty_repo? ? nil : project_tree_path(project))
end
2020-03-13 15:44:24 +05:30
def releases_anchor_data
return unless can?(current_user, :read_release, project)
releases_count = project.releases.count
return if releases_count < 1
AnchorData.new(true,
statistic_icon('rocket') +
n_('%{strong_start}%{release_count}%{strong_end} Release', '%{strong_start}%{release_count}%{strong_end} Releases', releases_count).html_safe % {
release_count: number_with_delimiter(releases_count),
strong_start: '<strong class="project-stat-value">'.html_safe,
strong_end: '</strong>'.html_safe
},
project_releases_path(project))
end
2018-03-27 19:54:05 +05:30
def commits_anchor_data
2018-11-20 20:47:30 +05:30
AnchorData.new(true,
2019-02-15 15:39:39 +05:30
statistic_icon('commit') +
n_('%{strong_start}%{commit_count}%{strong_end} Commit', '%{strong_start}%{commit_count}%{strong_end} Commits', statistics.commit_count).html_safe % {
commit_count: number_with_delimiter(statistics.commit_count),
strong_start: '<strong class="project-stat-value">'.html_safe,
strong_end: '</strong>'.html_safe
},
2018-11-20 20:47:30 +05:30
empty_repo? ? nil : project_commits_path(project, repository.root_ref))
2018-03-27 19:54:05 +05:30
end
def branches_anchor_data
2018-11-20 20:47:30 +05:30
AnchorData.new(true,
2019-02-15 15:39:39 +05:30
statistic_icon('branch') +
n_('%{strong_start}%{branch_count}%{strong_end} Branch', '%{strong_start}%{branch_count}%{strong_end} Branches', repository.branch_count).html_safe % {
branch_count: number_with_delimiter(repository.branch_count),
strong_start: '<strong class="project-stat-value">'.html_safe,
strong_end: '</strong>'.html_safe
},
2018-11-20 20:47:30 +05:30
empty_repo? ? nil : project_branches_path(project))
2018-03-27 19:54:05 +05:30
end
def tags_anchor_data
2018-11-20 20:47:30 +05:30
AnchorData.new(true,
2019-02-15 15:39:39 +05:30
statistic_icon('label') +
n_('%{strong_start}%{tag_count}%{strong_end} Tag', '%{strong_start}%{tag_count}%{strong_end} Tags', repository.tag_count).html_safe % {
tag_count: number_with_delimiter(repository.tag_count),
strong_start: '<strong class="project-stat-value">'.html_safe,
strong_end: '</strong>'.html_safe
},
2018-11-20 20:47:30 +05:30
empty_repo? ? nil : project_tags_path(project))
2018-03-27 19:54:05 +05:30
end
def new_file_anchor_data
2018-05-09 12:01:36 +05:30
if current_user && can_current_user_push_to_default_branch?
2021-01-03 14:25:43 +05:30
new_file_path = empty_repo? ? ide_edit_path(project, default_branch_or_master) : project_new_blob_path(project, default_branch_or_master)
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-02-15 15:39:39 +05:30
statistic_icon + _('New file'),
2021-01-03 14:25:43 +05:30
new_file_path,
2020-03-13 15:44:24 +05:30
'missing')
2018-03-27 19:54:05 +05:30
end
end
def readme_anchor_data
2018-11-08 19:23:39 +05:30
if current_user && can_current_user_push_to_default_branch? && repository.readme.nil?
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-02-15 15:39:39 +05:30
statistic_icon + _('Add README'),
2021-01-03 14:25:43 +05:30
empty_repo? ? add_readme_ide_path : add_readme_path)
2018-11-08 19:23:39 +05:30
elsif repository.readme
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
statistic_icon('doc-text') + _('README'),
default_view != 'readme' ? readme_path : '#readme',
'default',
'doc-text')
2018-03-27 19:54:05 +05:30
end
end
def changelog_anchor_data
2018-05-09 12:01:36 +05:30
if current_user && can_current_user_push_to_default_branch? && repository.changelog.blank?
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-02-15 15:39:39 +05:30
statistic_icon + _('Add CHANGELOG'),
2021-01-03 14:25:43 +05:30
empty_repo? ? add_changelog_ide_path : add_changelog_path)
2018-03-27 19:54:05 +05:30
elsif repository.changelog.present?
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
statistic_icon('doc-text') + _('CHANGELOG'),
changelog_path,
'default')
2018-03-27 19:54:05 +05:30
end
end
def license_anchor_data
2019-02-15 15:39:39 +05:30
icon = statistic_icon('scale')
2018-11-20 20:47:30 +05:30
if repository.license_blob.present?
2019-12-26 22:10:19 +05:30
AnchorData.new(false,
icon + content_tag(:span, license_short_name, class: 'project-stat-value'),
license_path,
2021-01-29 00:20:46 +05:30
'default',
nil,
'license')
2018-11-20 20:47:30 +05:30
else
if current_user && can_current_user_push_to_default_branch?
2019-12-26 22:10:19 +05:30
AnchorData.new(false,
content_tag(:span, statistic_icon + _('Add LICENSE'), class: 'add-license-link d-flex'),
2021-01-03 14:25:43 +05:30
empty_repo? ? add_license_ide_path : add_license_path)
2018-11-20 20:47:30 +05:30
else
2019-12-26 22:10:19 +05:30
AnchorData.new(false,
icon + content_tag(:span, _('No license. All rights reserved'), class: 'project-stat-value'),
2018-11-20 20:47:30 +05:30
nil)
end
2018-03-27 19:54:05 +05:30
end
end
def contribution_guide_anchor_data
2018-05-09 12:01:36 +05:30
if current_user && can_current_user_push_to_default_branch? && repository.contribution_guide.blank?
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-02-15 15:39:39 +05:30
statistic_icon + _('Add CONTRIBUTING'),
2021-01-03 14:25:43 +05:30
empty_repo? ? add_contribution_guide_ide_path : add_contribution_guide_path)
2018-03-27 19:54:05 +05:30
elsif repository.contribution_guide.present?
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
statistic_icon('doc-text') + _('CONTRIBUTING'),
2019-07-07 11:18:12 +05:30
contribution_guide_path,
'default')
2018-03-27 19:54:05 +05:30
end
end
def autodevops_anchor_data(show_auto_devops_callout: false)
if current_user && can?(current_user, :admin_pipeline, project) && repository.gitlab_ci_yml.blank? && !show_auto_devops_callout
2019-02-15 15:39:39 +05:30
if auto_devops_enabled?
AnchorData.new(false,
2019-07-31 22:56:46 +05:30
statistic_icon('settings') + _('Auto DevOps enabled'),
2019-02-15 15:39:39 +05:30
project_settings_ci_cd_path(project, anchor: 'autodevops-settings'),
'default')
else
AnchorData.new(false,
statistic_icon + _('Enable Auto DevOps'),
project_settings_ci_cd_path(project, anchor: 'autodevops-settings'))
end
2018-03-27 19:54:05 +05:30
elsif auto_devops_enabled?
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
2018-11-20 20:47:30 +05:30
_('Auto DevOps enabled'),
nil)
2018-03-27 19:54:05 +05:30
end
end
def kubernetes_cluster_anchor_data
2020-03-13 15:44:24 +05:30
if can_instantiate_cluster?
2018-03-27 19:54:05 +05:30
if clusters.empty?
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
statistic_icon + _('Add Kubernetes cluster'),
new_project_cluster_path(project))
else
cluster_link = clusters.count == 1 ? project_cluster_path(project, clusters.first) : project_clusters_path(project)
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
2020-03-13 15:44:24 +05:30
_('Kubernetes'),
2019-02-15 15:39:39 +05:30
cluster_link,
'default')
end
2018-03-27 19:54:05 +05:30
end
end
def gitlab_ci_anchor_data
2020-03-13 15:44:24 +05:30
if cicd_missing?
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-02-15 15:39:39 +05:30
statistic_icon + _('Set up CI/CD'),
2021-01-03 14:25:43 +05:30
add_ci_yml_path)
2018-03-27 19:54:05 +05:30
elsif repository.gitlab_ci_yml.present?
2019-02-15 15:39:39 +05:30
AnchorData.new(false,
statistic_icon('doc-text') + _('CI/CD configuration'),
ci_configuration_path,
'default')
2018-03-27 19:54:05 +05:30
end
end
2019-03-02 22:35:43 +05:30
def topics_to_show
project.tag_list.take(MAX_TOPICS_TO_SHOW) # rubocop: disable CodeReuse/ActiveRecord
2018-11-20 20:47:30 +05:30
end
2019-07-07 11:18:12 +05:30
def topics_not_shown
project.tag_list - topics_to_show
end
2019-03-02 22:35:43 +05:30
def count_of_extra_topics_not_shown
if project.tag_list.count > MAX_TOPICS_TO_SHOW
project.tag_list.count - MAX_TOPICS_TO_SHOW
2018-11-20 20:47:30 +05:30
else
0
end
end
2019-03-02 22:35:43 +05:30
def has_extra_topics?
count_of_extra_topics_not_shown > 0
2018-11-20 20:47:30 +05:30
end
2020-03-13 15:44:24 +05:30
def can_setup_review_app?
strong_memoize(:can_setup_review_app) do
(can_instantiate_cluster? && all_clusters_empty?) || cicd_missing?
end
end
def all_clusters_empty?
strong_memoize(:all_clusters_empty) do
project.all_clusters.empty?
end
end
2018-03-27 19:54:05 +05:30
private
2020-03-13 15:44:24 +05:30
def cicd_missing?
current_user && can_current_user_push_code? && repository.gitlab_ci_yml.blank? && !auto_devops_enabled?
end
def can_instantiate_cluster?
current_user && can?(current_user, :create_cluster, project)
end
2018-03-27 19:54:05 +05:30
def filename_path(filename)
if blob = repository.public_send(filename) # rubocop:disable GitlabSecurity/PublicSend
project_blob_path(
project,
tree_join(default_branch, blob.name)
)
end
end
def anonymous_project_view
if !project.empty_repo? && can?(current_user, :download_code, project)
'files'
2021-01-03 14:25:43 +05:30
elsif project.wiki_repository_exists? && can?(current_user, :read_wiki, project)
'wiki'
elsif can?(current_user, :read_issue, project)
'projects/issues/issues'
2018-03-27 19:54:05 +05:30
else
'activity'
end
end
def add_special_file_path(file_name:, commit_message: nil, branch_name: nil)
commit_message ||= s_("CommitMessage|Add %{file_name}") % { file_name: file_name }
project_new_blob_path(
project,
2020-11-24 15:15:51 +05:30
default_branch_or_master,
2018-03-27 19:54:05 +05:30
file_name: file_name,
commit_message: commit_message,
branch_name: branch_name
)
end
end
2019-12-04 20:38:33 +05:30
ProjectPresenter.prepend_if_ee('EE::ProjectPresenter')