Read about our upcoming Code of Conduct on this issue

Commit dd02a50a authored by Oswaldo Ferreira's avatar Oswaldo Ferreira
Browse files

Move button list logic to project presenter

parent cda3fee7f5d8
...@@ -34,7 +34,7 @@ def current_action?(*args) ...@@ -34,7 +34,7 @@ def current_action?(*args)
def project_icon(project_id, options = {}) def project_icon(project_id, options = {})
project = project =
if project_id.is_a?(Project) if project_id.is_a?(Project) || project_id.is_a?(ProjectPresenter)
project_id project_id
else else
Project.find_by_full_path(project_id) Project.find_by_full_path(project_id)
......
...@@ -10,12 +10,6 @@ def filter_branches_path(options = {}) ...@@ -10,12 +10,6 @@ def filter_branches_path(options = {})
project_branches_path(@project, @id, options) project_branches_path(@project, @id, options)
end end
def can_push_branch?(project, branch_name)
return false unless project.repository.branch_exists?(branch_name)
::Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(branch_name)
end
def project_branches def project_branches
options_for_select(@project.repository.branch_names, @project.default_branch) options_for_select(@project.repository.branch_names, @project.default_branch)
end end
......
...@@ -48,30 +48,4 @@ def user_application_theme ...@@ -48,30 +48,4 @@ def user_application_theme
def user_color_scheme def user_color_scheme
Gitlab::ColorSchemes.for_user(current_user).css_class Gitlab::ColorSchemes.for_user(current_user).css_class
end end
def default_project_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 @project.feature_available?(:issues, current_user)
"projects/issues/issues"
else
"customize_workflow"
end
end
def anonymous_project_view
if !@project.empty_repo? && can?(current_user, :download_code, @project)
'files'
else
'activity'
end
end
end end
...@@ -153,11 +153,6 @@ def can_change_visibility_level?(project, current_user) ...@@ -153,11 +153,6 @@ def can_change_visibility_level?(project, current_user)
end end
end end
def license_short_name(project)
license = project.repository.license
license&.nickname || license&.name || 'LICENSE'
end
def last_push_event def last_push_event
current_user&.recent_push(@project) current_user&.recent_push(@project)
end end
...@@ -386,55 +381,6 @@ def project_last_activity(project) ...@@ -386,55 +381,6 @@ def project_last_activity(project)
end end
end end
def add_special_file_path(project, file_name:, commit_message: nil, branch_name: nil, context: 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,
context: context
)
end
def add_koding_stack_path(project)
project_new_blob_path(
project,
project.default_branch || 'master',
file_name: '.koding.yml',
commit_message: "Add Koding stack script",
content: <<-CONTENT.strip_heredoc
provider:
aws:
access_key: '${var.aws_access_key}'
secret_key: '${var.aws_secret_key}'
resource:
aws_instance:
#{project.path}-vm:
instance_type: t2.nano
user_data: |-
# Created by GitLab UI for :>
echo _KD_NOTIFY_@Installing Base packages...@
apt-get update -y
apt-get install git -y
echo _KD_NOTIFY_@Cloning #{project.name}...@
export KODING_USER=${var.koding_user_username}
export REPO_URL=#{root_url}${var.koding_queryString_repo}.git
export BRANCH=${var.koding_queryString_branch}
sudo -i -u $KODING_USER git clone $REPO_URL -b $BRANCH
echo _KD_NOTIFY_@#{project.name} cloned.@
CONTENT
)
end
def koding_project_url(project = nil, branch = nil, sha = nil) def koding_project_url(project = nil, branch = nil, sha = nil)
if project if project
import_path = "/Home/Stacks/import" import_path = "/Home/Stacks/import"
...@@ -451,36 +397,6 @@ def koding_project_url(project = nil, branch = nil, sha = nil) ...@@ -451,36 +397,6 @@ def koding_project_url(project = nil, branch = nil, sha = nil)
Gitlab::CurrentSettings.koding_url Gitlab::CurrentSettings.koding_url
end end
def contribution_guide_path(project)
if project && contribution_guide = project.repository.contribution_guide
project_blob_path(
project,
tree_join(project.default_branch,
contribution_guide.name)
)
end
end
def readme_path(project)
filename_path(project, :readme)
end
def changelog_path(project)
filename_path(project, :changelog)
end
def license_path(project)
filename_path(project, :license_blob)
end
def version_path(project)
filename_path(project, :version)
end
def ci_configuration_path(project)
filename_path(project, :gitlab_ci_yml)
end
def project_wiki_path_with_version(proj, page, version, is_newest) def project_wiki_path_with_version(proj, page, version, is_newest)
url_params = is_newest ? {} : { version_id: version } url_params = is_newest ? {} : { version_id: version }
project_wiki_path(proj, page, url_params) project_wiki_path(proj, page, url_params)
...@@ -506,15 +422,6 @@ def current_ref ...@@ -506,15 +422,6 @@ def current_ref
@ref || @repository.try(:root_ref) @ref || @repository.try(:root_ref)
end end
def filename_path(project, filename)
if project && blob = project.repository.public_send(filename) # rubocop:disable GitlabSecurity/PublicSend
project_blob_path(
project,
tree_join(project.default_branch, blob.name)
)
end
end
def sanitize_repo_path(project, message) def sanitize_repo_path(project, message)
return '' unless message.present? return '' unless message.present?
...@@ -604,218 +511,4 @@ def find_file_path ...@@ -604,218 +511,4 @@ def find_file_path
project_find_file_path(@project, ref) project_find_file_path(@project, ref)
end end
def can_current_user_push_code?(project)
project.empty_repo? ? can?(current_user, :push_code, project) : can_push_branch?(project, project.default_branch)
end
def files_anchor_data(project)
{
enabled: true,
label: _('Files (%{human_size})') % { human_size: storage_counter(@project.statistics.total_repository_size) },
link: project_tree_path(@project)
}
end
def commits_anchor_data(project)
{
enabled: true,
label: n_('Commit (%{commit_count})', 'Commits (%{commit_count})', @project.statistics.commit_count) % { commit_count: number_with_delimiter(@project.statistics.commit_count) },
link: project_commits_path(@project, current_ref)
}
end
def branches_anchor_data(project)
{
enabled: true,
label: n_('Branch (%{branch_count})', 'Branches (%{branch_count})', @repository.branch_count) % { branch_count: number_with_delimiter(@repository.branch_count) },
link: project_branches_path(@project)
}
end
def tags_anchor_data(project)
{
enabled: true,
label: n_('Tag (%{tag_count})', 'Tags (%{tag_count})', @repository.tag_count) % { tag_count: number_with_delimiter(@repository.tag_count) },
link: project_tags_path(@project)
}
end
def new_file_anchor_data(project)
if current_user && can_current_user_push_code?(project)
{
enabled: false,
label: _('New file'),
link: project_new_blob_path(project, project.default_branch || 'master'),
class_modifier: 'new'
}
end
end
def readme_anchor_data(project)
if current_user && can_current_user_push_code?(project) && project.repository.readme.blank?
{
enabled: false,
label: _('Add Readme'),
link: add_special_file_path(project, file_name: 'README.md')
}
elsif project.repository.readme.present?
{
enabled: true,
label: _('Readme'),
link: default_project_view != 'readme' ? readme_path(@project) : '#readme'
}
end
end
def changelog_anchor_data(project)
if current_user && can_current_user_push_code?(project) && project.repository.changelog.blank?
{
enabled: false,
label: _('Add Changelog'),
link: add_special_file_path(project, file_name: 'CHANGELOG')
}
elsif project.repository.changelog.present?
{
enabled: true,
label: _('Changelog'),
link: changelog_path(project)
}
end
end
def license_anchor_data(project)
if current_user && can_current_user_push_code?(project) && project.repository.license_blob.blank?
{
enabled: false,
label: _('Add License'),
link: add_special_file_path(project, file_name: 'LICENSE')
}
elsif project.repository.license_blob.present?
{
enabled: true,
label: license_short_name(project),
link: license_path(project)
}
end
end
def contribution_guide_anchor_data(project)
if current_user && can_current_user_push_code?(project) && project.repository.contribution_guide.blank?
{
enabled: false,
label: _('Add Contribution guide'),
link: add_special_file_path(project, file_name: 'CONTRIBUTING.md', commit_message: 'Add contribution guide')
}
elsif project.repository.contribution_guide.present?
{
enabled: true,
label: _('Contribution guide'),
link: contribution_guide_path(@project)
}
end
end
def autodevops_anchor_data(project, ignore_callout: false)
if current_user && can?(current_user, :admin_pipeline, project) && project.repository.gitlab_ci_yml.blank? && (ignore_callout || !show_auto_devops_callout?(project))
{
enabled: project.auto_devops_enabled?,
label: project.auto_devops_enabled? ? _('Auto DevOps enabled') : _('Enable Auto DevOps'),
link: project_settings_ci_cd_path(project, anchor: 'js-general-pipeline-settings')
}
elsif project.auto_devops_enabled?
{
enabled: true,
label: _('Auto DevOps enabled'),
link: nil
}
end
end
def kubernetes_cluster_anchor_data(project)
if current_user && can?(current_user, :create_cluster, project)
cluster_link = project.clusters.size == 1 ? project_cluster_path(project, project.clusters.first) : project_clusters_path(project)
if project.clusters.empty?
cluster_link = new_project_cluster_path(project)
end
{
enabled: !project.clusters.empty?,
label: project.clusters.empty? ? _('Add Kubernetes cluster') : n_('Kubernetes cluster', 'Kubernetes clusters', project.clusters.size),
link: cluster_link
}
end
end
def gitlab_ci_anchor_data(project)
if current_user && can_current_user_push_code?(project) && project.repository.gitlab_ci_yml.blank? && !project.auto_devops_enabled?
{
enabled: false,
label: _('Set up CI/CD'),
link: add_special_file_path(project, file_name: '.gitlab-ci.yml')
}
elsif project.repository.gitlab_ci_yml.present?
{
enabled: true,
label: _('CI/CD configuration'),
link: ci_configuration_path(@project)
}
end
end
def koding_anchor_data(project)
if current_user && can_current_user_push_code?(project) && koding_enabled? && project.repository.koding_yml.blank?
{
enabled: false,
label: _('Set up Koding'),
link: add_koding_stack_path(project)
}
end
end
def empty_project_stat_anchor_items(project)
[
autodevops_anchor_data(project, ignore_callout: true),
kubernetes_cluster_anchor_data(project)
].compact.reject { |i| !i[:enabled] }
end
def empty_project_stat_button_items(project)
[
new_file_anchor_data(project),
readme_anchor_data(project),
license_anchor_data(project),
autodevops_anchor_data(project, ignore_callout: true),
kubernetes_cluster_anchor_data(project)
].compact.reject { |i| i[:enabled] }
end
def project_stat_anchor_items(project)
[
files_anchor_data(project),
commits_anchor_data(project),
branches_anchor_data(project),
tags_anchor_data(project),
readme_anchor_data(project),
changelog_anchor_data(project),
license_anchor_data(project),
contribution_guide_anchor_data(project),
gitlab_ci_anchor_data(project),
autodevops_anchor_data(project),
kubernetes_cluster_anchor_data(project)
].compact.reject { |i| !i[:enabled] }
end
def project_stat_button_items(project)
[
changelog_anchor_data(project),
license_anchor_data(project),
contribution_guide_anchor_data(project),
autodevops_anchor_data(project),
kubernetes_cluster_anchor_data(project),
gitlab_ci_anchor_data(project),
koding_anchor_data(project)
].compact.reject { |i| i[:enabled] }
end
end end
...@@ -55,7 +55,7 @@ def can_edit_tree?(project = nil, ref = nil) ...@@ -55,7 +55,7 @@ def can_edit_tree?(project = nil, ref = nil)
def tree_edit_branch(project = @project, ref = @ref) def tree_edit_branch(project = @project, ref = @ref)
return unless can_edit_tree?(project, ref) return unless can_edit_tree?(project, ref)
if can_push_branch?(project, ref) if project.user_can_push_to_branch?(current_user, ref)
ref ref
else else
project = tree_edit_project(project) project = tree_edit_project(project)
......
...@@ -15,6 +15,7 @@ class Project < ActiveRecord::Base ...@@ -15,6 +15,7 @@ class Project < ActiveRecord::Base
include ValidAttribute include ValidAttribute
include ProjectFeaturesCompatibility include ProjectFeaturesCompatibility
include SelectForProjectAuthorization include SelectForProjectAuthorization
include Presentable
include Routable include Routable
include GroupDescendant include GroupDescendant
include Gitlab::SQL::Pattern include Gitlab::SQL::Pattern
...@@ -1015,6 +1016,12 @@ def user_can_push_to_empty_repo?(user) ...@@ -1015,6 +1016,12 @@ def user_can_push_to_empty_repo?(user)
!ProtectedBranch.default_branch_protected? || team.max_member_access(user.id) > Gitlab::Access::DEVELOPER !ProtectedBranch.default_branch_protected? || team.max_member_access(user.id) > Gitlab::Access::DEVELOPER
end end
def user_can_push_to_branch?(user, branch_name)
return false unless repository.branch_exists?(branch_name)
::Gitlab::UserAccess.new(user, project: self).can_push_to_branch?(branch_name)
end
def forked? def forked?
return true if fork_network && fork_network.root_project != self return true if fork_network && fork_network.root_project != self
......
class ProjectPresenter < Gitlab::View::Presenter::Delegated
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::UrlHelper
include GitlabRoutingHelper
include StorageHelper
include TreeHelper
presents :project
def project_stat_anchor_items(show_auto_devops_callout:)
[
files_anchor_data,
commits_anchor_data,
branches_anchor_data,
tags_anchor_data,
readme_anchor_data,
changelog_anchor_data,
license_anchor_data,
contribution_guide_anchor_data,
gitlab_ci_anchor_data,
autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
kubernetes_cluster_anchor_data
].compact.reject { |i| !i[:enabled] }
end
def project_stat_button_items(show_auto_devops_callout:)
[
changelog_anchor_data,
license_anchor_data,
contribution_guide_anchor_data,
autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
kubernetes_cluster_anchor_data,
gitlab_ci_anchor_data,
koding_anchor_data
].compact.reject { |i| i[:enabled] }
end
def empty_project_stat_anchor_items
[
autodevops_anchor_data,
kubernetes_cluster_anchor_data
].compact.reject { |i| !i[:enabled] }
end
def empty_project_stat_button_items
[
new_file_anchor_data,
readme_anchor_data,
license_anchor_data,
autodevops_anchor_data,
kubernetes_cluster_anchor_data
].compact.reject { |i| i[:enabled] }
end
def default_project_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_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 add_koding_stack_path
project_new_blob_path(
project,
default_branch || 'master',
file_name: '.koding.yml',
commit_message: "Add Koding stack script",
content: <<-CONTENT.strip_heredoc
provider:
aws:
access_key: '${var.aws_access_key}'
secret_key: '${var.aws_secret_key}'
resource:
aws_instance:
#{project.path}-vm:
instance_type: t2.nano
user_data: |-
# Created by GitLab UI for :>
echo _KD_NOTIFY_@Installing Base packages...@
apt-get update -y
apt-get install git -y
echo _KD_NOTIFY_@Cloning #{project.name}...@
export KODING_USER=${var.koding_user_username}
export REPO_URL=#{root_url}${var.koding_queryString_repo}.git
export BRANCH=${var.koding_queryString_branch}
sudo -i -u $KODING_USER git clone $REPO_URL -b $BRANCH
echo _KD_NOTIFY_@#{project.name} cloned.@
CONTENT
)
end