2018-07-24 06:00:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-20 09:56:36 -05:00
|
|
|
class ProjectPresenter < Gitlab::View::Presenter::Delegated
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include GitlabRoutingHelper
|
|
|
|
include StorageHelper
|
|
|
|
include TreeHelper
|
2018-12-07 09:11:42 -05:00
|
|
|
include IconsHelper
|
2018-04-18 06:44:12 -04:00
|
|
|
include ChecksCollaboration
|
2018-02-20 17:04:10 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-02-20 09:56:36 -05:00
|
|
|
|
|
|
|
presents :project
|
|
|
|
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData = Struct.new(:is_link, :label, :link, :class_modifier, :icon)
|
2019-01-10 15:08:41 -05:00
|
|
|
MAX_TOPICS_TO_SHOW = 3
|
2018-09-06 03:27:39 -04:00
|
|
|
|
2018-12-07 09:11:42 -05:00
|
|
|
def statistic_icon(icon_name = 'plus-square-o')
|
|
|
|
sprite_icon(icon_name, size: 16, css_class: 'icon append-right-4')
|
|
|
|
end
|
|
|
|
|
2018-02-20 13:51:52 -05:00
|
|
|
def statistics_anchors(show_auto_devops_callout:)
|
2018-02-20 09:56:36 -05:00
|
|
|
[
|
2018-12-07 09:11:42 -05:00
|
|
|
license_anchor_data,
|
2018-02-20 09:56:36 -05:00
|
|
|
commits_anchor_data,
|
|
|
|
branches_anchor_data,
|
|
|
|
tags_anchor_data,
|
2018-12-07 09:11:42 -05:00
|
|
|
files_anchor_data
|
|
|
|
].compact.select(&:is_link)
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
2018-02-20 13:51:52 -05:00
|
|
|
def statistics_buttons(show_auto_devops_callout:)
|
2018-02-20 09:56:36 -05:00
|
|
|
[
|
2018-07-03 07:58:03 -04:00
|
|
|
readme_anchor_data,
|
2018-02-20 09:56:36 -05:00
|
|
|
changelog_anchor_data,
|
|
|
|
contribution_guide_anchor_data,
|
|
|
|
autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
|
|
|
|
kubernetes_cluster_anchor_data,
|
2018-10-12 20:54:08 -04:00
|
|
|
gitlab_ci_anchor_data
|
2018-12-07 09:11:42 -05:00
|
|
|
].compact.reject(&:is_link)
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
2018-02-20 13:51:52 -05:00
|
|
|
def empty_repo_statistics_anchors
|
2018-02-20 09:56:36 -05:00
|
|
|
[
|
2018-12-07 09:11:42 -05:00
|
|
|
license_anchor_data,
|
2018-09-06 03:27:39 -04:00
|
|
|
commits_anchor_data,
|
|
|
|
branches_anchor_data,
|
|
|
|
tags_anchor_data,
|
2018-12-07 09:11:42 -05:00
|
|
|
files_anchor_data
|
|
|
|
].compact.select { |item| item.is_link }
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
2018-02-20 13:51:52 -05:00
|
|
|
def empty_repo_statistics_buttons
|
2018-02-20 09:56:36 -05:00
|
|
|
[
|
|
|
|
new_file_anchor_data,
|
|
|
|
readme_anchor_data,
|
2018-12-07 09:11:42 -05:00
|
|
|
changelog_anchor_data,
|
|
|
|
contribution_guide_anchor_data,
|
2018-02-20 09:56:36 -05:00
|
|
|
autodevops_anchor_data,
|
|
|
|
kubernetes_cluster_anchor_data
|
2018-12-07 09:11:42 -05:00
|
|
|
].compact.reject { |item| item.is_link }
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
2018-02-20 11:30:49 -05:00
|
|
|
def default_view
|
2018-02-20 09:56:36 -05:00
|
|
|
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
|
|
|
|
|
2018-02-20 16:59:19 -05:00
|
|
|
def add_changelog_path
|
|
|
|
add_special_file_path(file_name: 'CHANGELOG')
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_contribution_guide_path
|
2018-12-07 09:11:42 -05:00
|
|
|
add_special_file_path(file_name: 'CONTRIBUTING.md', commit_message: 'Add CONTRIBUTING')
|
2018-02-20 16:59:19 -05:00
|
|
|
end
|
|
|
|
|
2018-02-20 09:56:36 -05:00
|
|
|
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?
|
2018-02-20 17:04:10 -05:00
|
|
|
strong_memoize(:can_current_user_push_code) do
|
|
|
|
if empty_repo?
|
|
|
|
can?(current_user, :push_code, project)
|
|
|
|
else
|
2018-02-22 11:56:38 -05:00
|
|
|
can_current_user_push_to_branch?(default_branch)
|
2018-02-20 17:04:10 -05:00
|
|
|
end
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-22 11:56:38 -05:00
|
|
|
def can_current_user_push_to_branch?(branch)
|
2018-04-18 06:44:12 -04:00
|
|
|
user_access(project).can_push_to_branch?(branch)
|
|
|
|
end
|
2018-02-22 11:56:38 -05:00
|
|
|
|
2018-04-18 06:44:12 -04:00
|
|
|
def can_current_user_push_to_default_branch?
|
|
|
|
can_current_user_push_to_branch?(default_branch)
|
2018-02-22 11:56:38 -05:00
|
|
|
end
|
|
|
|
|
2018-02-20 09:56:36 -05:00
|
|
|
def files_anchor_data
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(true,
|
2018-12-07 09:11:42 -05:00
|
|
|
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-09-06 03:27:39 -04:00
|
|
|
empty_repo? ? nil : project_tree_path(project))
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def commits_anchor_data
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(true,
|
2018-12-07 09:11:42 -05:00
|
|
|
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-09-06 03:27:39 -04:00
|
|
|
empty_repo? ? nil : project_commits_path(project, repository.root_ref))
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def branches_anchor_data
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(true,
|
2018-12-07 09:11:42 -05:00
|
|
|
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-09-06 03:27:39 -04:00
|
|
|
empty_repo? ? nil : project_branches_path(project))
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def tags_anchor_data
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(true,
|
2018-12-07 09:11:42 -05:00
|
|
|
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-09-06 03:27:39 -04:00
|
|
|
empty_repo? ? nil : project_tags_path(project))
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_file_anchor_data
|
2018-04-18 06:44:12 -04:00
|
|
|
if current_user && can_current_user_push_to_default_branch?
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(false,
|
2018-12-07 09:11:42 -05:00
|
|
|
statistic_icon + _('New file'),
|
2018-09-06 03:27:39 -04:00
|
|
|
project_new_blob_path(project, default_branch || 'master'),
|
2018-10-29 07:27:47 -04:00
|
|
|
'success')
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def readme_anchor_data
|
2018-07-03 07:58:03 -04:00
|
|
|
if current_user && can_current_user_push_to_default_branch? && repository.readme.nil?
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(false,
|
2018-12-07 09:11:42 -05:00
|
|
|
statistic_icon + _('Add README'),
|
2018-09-06 03:27:39 -04:00
|
|
|
add_readme_path)
|
2018-07-03 07:58:03 -04:00
|
|
|
elsif repository.readme
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(false,
|
|
|
|
statistic_icon('doc-text') + _('README'),
|
|
|
|
default_view != 'readme' ? readme_path : '#readme',
|
|
|
|
'default',
|
|
|
|
'doc-text')
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def changelog_anchor_data
|
2018-04-18 06:44:12 -04:00
|
|
|
if current_user && can_current_user_push_to_default_branch? && repository.changelog.blank?
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(false,
|
2018-12-07 09:11:42 -05:00
|
|
|
statistic_icon + _('Add CHANGELOG'),
|
2018-09-06 03:27:39 -04:00
|
|
|
add_changelog_path)
|
2018-02-20 09:56:36 -05:00
|
|
|
elsif repository.changelog.present?
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(false,
|
|
|
|
statistic_icon('doc-text') + _('CHANGELOG'),
|
|
|
|
changelog_path,
|
|
|
|
'default')
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def license_anchor_data
|
2018-12-07 09:11:42 -05:00
|
|
|
icon = statistic_icon('scale')
|
|
|
|
|
2018-09-06 03:27:39 -04:00
|
|
|
if repository.license_blob.present?
|
|
|
|
AnchorData.new(true,
|
2018-12-07 09:11:42 -05:00
|
|
|
icon + content_tag(:strong, license_short_name, class: 'project-stat-value'),
|
2018-09-06 03:27:39 -04:00
|
|
|
license_path)
|
|
|
|
else
|
|
|
|
if current_user && can_current_user_push_to_default_branch?
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(true,
|
|
|
|
content_tag(:span, icon + _('Add license'), class: 'add-license-link d-flex'),
|
2018-09-06 03:27:39 -04:00
|
|
|
add_license_path)
|
|
|
|
else
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(true,
|
|
|
|
icon + content_tag(:strong, _('No license. All rights reserved'), class: 'project-stat-value'),
|
2018-09-06 03:27:39 -04:00
|
|
|
nil)
|
|
|
|
end
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def contribution_guide_anchor_data
|
2018-04-18 06:44:12 -04:00
|
|
|
if current_user && can_current_user_push_to_default_branch? && repository.contribution_guide.blank?
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(false,
|
2018-12-07 09:11:42 -05:00
|
|
|
statistic_icon + _('Add CONTRIBUTING'),
|
2018-09-06 03:27:39 -04:00
|
|
|
add_contribution_guide_path)
|
2018-02-20 09:56:36 -05:00
|
|
|
elsif repository.contribution_guide.present?
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(false,
|
|
|
|
statistic_icon('doc-text') + _('CONTRIBUTING'),
|
2019-02-11 20:05:17 -05:00
|
|
|
contribution_guide_path,
|
|
|
|
'default')
|
2018-02-20 09:56:36 -05:00
|
|
|
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
|
2018-12-07 09:11:42 -05:00
|
|
|
if auto_devops_enabled?
|
|
|
|
AnchorData.new(false,
|
|
|
|
statistic_icon('doc-text') + _('Auto DevOps enabled'),
|
|
|
|
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-02-20 09:56:36 -05:00
|
|
|
elsif auto_devops_enabled?
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(false,
|
2018-09-06 03:27:39 -04:00
|
|
|
_('Auto DevOps enabled'),
|
|
|
|
nil)
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def kubernetes_cluster_anchor_data
|
|
|
|
if current_user && can?(current_user, :create_cluster, project)
|
|
|
|
|
|
|
|
if clusters.empty?
|
2018-12-07 09:11:42 -05:00
|
|
|
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)
|
2018-02-20 09:56:36 -05:00
|
|
|
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(false,
|
|
|
|
_('Kubernetes configured'),
|
|
|
|
cluster_link,
|
|
|
|
'default')
|
|
|
|
end
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_ci_anchor_data
|
|
|
|
if current_user && can_current_user_push_code? && repository.gitlab_ci_yml.blank? && !auto_devops_enabled?
|
2018-09-06 03:27:39 -04:00
|
|
|
AnchorData.new(false,
|
2018-12-07 09:11:42 -05:00
|
|
|
statistic_icon + _('Set up CI/CD'),
|
2018-09-06 03:27:39 -04:00
|
|
|
add_ci_yml_path)
|
2018-02-20 09:56:36 -05:00
|
|
|
elsif repository.gitlab_ci_yml.present?
|
2018-12-07 09:11:42 -05:00
|
|
|
AnchorData.new(false,
|
|
|
|
statistic_icon('doc-text') + _('CI/CD configuration'),
|
|
|
|
ci_configuration_path,
|
|
|
|
'default')
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-10 15:08:41 -05:00
|
|
|
def topics_to_show
|
|
|
|
project.tag_list.take(MAX_TOPICS_TO_SHOW) # rubocop: disable CodeReuse/ActiveRecord
|
2018-09-06 03:27:39 -04:00
|
|
|
end
|
|
|
|
|
2019-01-10 15:08:41 -05:00
|
|
|
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-09-06 03:27:39 -04:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-10 15:08:41 -05:00
|
|
|
def has_extra_topics?
|
|
|
|
count_of_extra_topics_not_shown > 0
|
2018-09-06 03:27:39 -04:00
|
|
|
end
|
|
|
|
|
2018-02-20 16:59:19 -05:00
|
|
|
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
|
2018-02-20 09:56:36 -05:00
|
|
|
end
|