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

321 lines
9.2 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
2018-05-09 12:01:36 +05:30
include ChecksCollaboration
2018-03-27 19:54:05 +05:30
include Gitlab::Utils::StrongMemoize
presents :project
2019-01-03 12:48:30 +05:30
AnchorData = Struct.new(:enabled, :label, :link, :class_modifier)
2018-11-20 20:47:30 +05:30
MAX_TAGS_TO_SHOW = 3
2018-03-27 19:54:05 +05:30
def statistics_anchors(show_auto_devops_callout:)
[
2019-01-03 12:48:30 +05:30
readme_anchor_data,
changelog_anchor_data,
contribution_guide_anchor_data,
files_anchor_data,
2018-03-27 19:54:05 +05:30
commits_anchor_data,
branches_anchor_data,
tags_anchor_data,
2019-01-03 12:48:30 +05:30
gitlab_ci_anchor_data,
autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
kubernetes_cluster_anchor_data
].compact.select { |item| item.enabled }
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,
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-01-03 12:48:30 +05:30
].compact.reject { |item| item.enabled }
2018-03-27 19:54:05 +05:30
end
def empty_repo_statistics_anchors
[
2019-01-03 12:48:30 +05:30
files_anchor_data,
2018-11-20 20:47:30 +05:30
commits_anchor_data,
branches_anchor_data,
tags_anchor_data,
2019-01-03 12:48:30 +05:30
autodevops_anchor_data,
kubernetes_cluster_anchor_data
].compact.select { |item| item.enabled }
2018-03-27 19:54:05 +05:30
end
def empty_repo_statistics_buttons
[
new_file_anchor_data,
readme_anchor_data,
autodevops_anchor_data,
kubernetes_cluster_anchor_data
2019-01-03 12:48:30 +05:30
].compact.reject { |item| item.enabled }
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
elsif user_view == "activity"
"activity"
elsif can?(current_user, :read_wiki, project)
"wiki"
elsif feature_available?(:issues, current_user)
"projects/issues/issues"
else
"customize_workflow"
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
def add_changelog_path
add_special_file_path(file_name: 'CHANGELOG')
end
def add_contribution_guide_path
2019-01-03 12:48:30 +05:30
add_special_file_path(file_name: 'CONTRIBUTING.md', commit_message: 'Add contribution guide')
2018-03-27 19:54:05 +05:30
end
def add_ci_yml_path
add_special_file_path(file_name: '.gitlab-ci.yml')
end
def add_readme_path
add_special_file_path(file_name: 'README.md')
end
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-01-03 12:48:30 +05:30
_('Files (%{human_size})') % { human_size: storage_counter(statistics.total_repository_size) },
2018-11-20 20:47:30 +05:30
empty_repo? ? nil : project_tree_path(project))
2018-03-27 19:54:05 +05:30
end
def commits_anchor_data
2018-11-20 20:47:30 +05:30
AnchorData.new(true,
2019-01-03 12:48:30 +05:30
n_('Commit (%{commit_count})', 'Commits (%{commit_count})', statistics.commit_count) % { commit_count: number_with_delimiter(statistics.commit_count) },
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-01-03 12:48:30 +05:30
n_('Branch (%{branch_count})', 'Branches (%{branch_count})', repository.branch_count) % { branch_count: number_with_delimiter(repository.branch_count) },
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-01-03 12:48:30 +05:30
n_('Tag (%{tag_count})', 'Tags (%{tag_count})', repository.tag_count) % { tag_count: number_with_delimiter(repository.tag_count) },
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?
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-01-03 12:48:30 +05:30
_('New file'),
2018-11-20 20:47:30 +05:30
project_new_blob_path(project, default_branch || 'master'),
2018-12-13 13:39:08 +05:30
'success')
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-01-03 12:48:30 +05:30
_('Add Readme'),
2018-11-20 20:47:30 +05:30
add_readme_path)
2018-11-08 19:23:39 +05:30
elsif repository.readme
2019-01-03 12:48:30 +05:30
AnchorData.new(true,
_('Readme'),
default_view != 'readme' ? readme_path : '#readme')
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-01-03 12:48:30 +05:30
_('Add Changelog'),
2018-11-20 20:47:30 +05:30
add_changelog_path)
2018-03-27 19:54:05 +05:30
elsif repository.changelog.present?
2019-01-03 12:48:30 +05:30
AnchorData.new(true,
_('Changelog'),
changelog_path)
2018-03-27 19:54:05 +05:30
end
end
def license_anchor_data
2018-11-20 20:47:30 +05:30
if repository.license_blob.present?
AnchorData.new(true,
2019-01-03 12:48:30 +05:30
license_short_name,
2018-11-20 20:47:30 +05:30
license_path)
else
if current_user && can_current_user_push_to_default_branch?
2019-01-03 12:48:30 +05:30
AnchorData.new(false,
_('Add license'),
2018-11-20 20:47:30 +05:30
add_license_path)
else
2019-01-03 12:48:30 +05:30
AnchorData.new(false,
_('No license. All rights reserved'),
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-01-03 12:48:30 +05:30
_('Add Contribution guide'),
2018-11-20 20:47:30 +05:30
add_contribution_guide_path)
2018-03-27 19:54:05 +05:30
elsif repository.contribution_guide.present?
2019-01-03 12:48:30 +05:30
AnchorData.new(true,
_('Contribution guide'),
2018-11-20 20:47:30 +05:30
contribution_guide_path)
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-01-03 12:48:30 +05:30
AnchorData.new(auto_devops_enabled?,
auto_devops_enabled? ? _('Auto DevOps enabled') : _('Enable Auto DevOps'),
project_settings_ci_cd_path(project, anchor: 'autodevops-settings'))
2018-03-27 19:54:05 +05:30
elsif auto_devops_enabled?
2019-01-03 12:48:30 +05:30
AnchorData.new(true,
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
if current_user && can?(current_user, :create_cluster, project)
2019-01-03 12:48:30 +05:30
cluster_link = clusters.count == 1 ? project_cluster_path(project, clusters.first) : project_clusters_path(project)
2018-03-27 19:54:05 +05:30
if clusters.empty?
2019-01-03 12:48:30 +05:30
cluster_link = new_project_cluster_path(project)
2018-12-23 12:14:25 +05:30
end
2019-01-03 12:48:30 +05:30
AnchorData.new(!clusters.empty?,
clusters.empty? ? _('Add Kubernetes cluster') : _('Kubernetes configured'),
cluster_link)
2018-03-27 19:54:05 +05:30
end
end
def gitlab_ci_anchor_data
if current_user && can_current_user_push_code? && repository.gitlab_ci_yml.blank? && !auto_devops_enabled?
2018-11-20 20:47:30 +05:30
AnchorData.new(false,
2019-01-03 12:48:30 +05:30
_('Set up CI/CD'),
2018-11-20 20:47:30 +05:30
add_ci_yml_path)
2018-03-27 19:54:05 +05:30
elsif repository.gitlab_ci_yml.present?
2019-01-03 12:48:30 +05:30
AnchorData.new(true,
_('CI/CD configuration'),
ci_configuration_path)
2018-03-27 19:54:05 +05:30
end
end
2018-11-20 20:47:30 +05:30
def tags_to_show
2018-12-05 23:21:45 +05:30
project.tag_list.take(MAX_TAGS_TO_SHOW) # rubocop: disable CodeReuse/ActiveRecord
2018-11-20 20:47:30 +05:30
end
def count_of_extra_tags_not_shown
if project.tag_list.count > MAX_TAGS_TO_SHOW
project.tag_list.count - MAX_TAGS_TO_SHOW
else
0
end
end
def has_extra_tags?
count_of_extra_tags_not_shown > 0
end
2018-03-27 19:54:05 +05:30
private
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'
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,
project.default_branch || 'master',
file_name: file_name,
commit_message: commit_message,
branch_name: branch_name
)
end
end