debian-mirror-gitlab/app/controllers/projects_controller.rb

516 lines
15 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
class ProjectsController < Projects::ApplicationController
2018-11-20 20:47:30 +05:30
include API::Helpers::RelatedResourcesHelpers
2016-11-03 12:29:30 +05:30
include IssuableCollections
2015-11-26 14:37:03 +05:30
include ExtractsPath
2018-03-17 18:26:18 +05:30
include PreviewMarkdown
2018-11-08 19:23:39 +05:30
include SendFileUpload
2019-03-02 22:35:43 +05:30
include RecordUserLastActivity
2019-07-31 22:56:46 +05:30
include ImportUrlParams
2020-07-28 23:09:34 +05:30
include FiltersEvents
2015-11-26 14:37:03 +05:30
2018-11-29 20:51:05 +05:30
prepend_before_action(only: [:show]) { authenticate_sessionless_user!(:rss) }
2019-07-07 11:18:12 +05:30
around_action :allow_gitaly_ref_name_caching, only: [:index, :show]
2018-03-17 18:26:18 +05:30
before_action :whitelist_query_limiting, only: [:create]
2019-03-02 22:35:43 +05:30
before_action :authenticate_user!, except: [:index, :show, :activity, :refs, :resolve]
2018-03-17 18:26:18 +05:30
before_action :redirect_git_extension, only: [:show]
2019-03-02 22:35:43 +05:30
before_action :project, except: [:index, :new, :create, :resolve]
before_action :repository, except: [:index, :new, :create, :resolve]
2019-10-12 21:52:04 +05:30
before_action :assign_ref_vars, if: -> { action_name == 'show' && repo_exists? }
before_action :tree,
if: -> { action_name == 'show' && repo_exists? && project_view_files? }
2018-03-17 18:26:18 +05:30
before_action :project_export_enabled, only: [:export, :download_export, :remove_export, :generate_new_export]
2018-11-08 19:23:39 +05:30
before_action :present_project, only: [:edit]
2019-01-03 12:48:30 +05:30
before_action :authorize_download_code!, only: [:refs]
2014-09-02 18:07:02 +05:30
# Authorize
2016-06-22 15:30:34 +05:30
before_action :authorize_admin_project!, only: [:edit, :update, :housekeeping, :download_export, :export, :remove_export, :generate_new_export]
2019-12-04 20:38:33 +05:30
before_action :authorize_archive_project!, only: [:archive, :unarchive]
2015-09-11 14:41:01 +05:30
before_action :event_filter, only: [:show, :activity]
2014-09-02 18:07:02 +05:30
2020-01-01 13:55:28 +05:30
# Project Export Rate Limit
before_action :export_rate_limit, only: [:export, :download_export, :generate_new_export]
2020-06-23 00:09:42 +05:30
# Experiments
before_action only: [:new, :create] do
frontend_experimentation_tracking_data(:new_create_project_ui, 'click_tab')
push_frontend_feature_flag(:new_create_project_ui) if experiment_enabled?(:new_create_project_ui)
2020-10-24 23:57:45 +05:30
end
before_action only: [:edit] do
2020-07-28 23:09:34 +05:30
push_frontend_feature_flag(:service_desk_custom_address, @project)
2020-11-24 15:15:51 +05:30
push_frontend_feature_flag(:approval_suggestions, @project, default_enabled: true)
2020-10-24 23:57:45 +05:30
end
2015-09-11 14:41:01 +05:30
layout :determine_layout
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
def index
redirect_to(current_user ? root_path : explore_root_path)
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def new
2019-07-31 22:56:46 +05:30
@namespace = Namespace.find_by(id: params[:namespace_id]) if params[:namespace_id]
return access_denied! if @namespace && !can?(current_user, :create_projects, @namespace)
2018-03-17 18:26:18 +05:30
2019-07-31 22:56:46 +05:30
@project = Project.new(namespace_id: @namespace&.id)
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def edit
2020-04-08 14:13:33 +05:30
@badge_api_endpoint = expose_path(api_v4_projects_badges_path(id: @project.id))
2020-03-13 15:44:24 +05:30
render_edit
2014-09-02 18:07:02 +05:30
end
def create
2019-04-03 18:18:56 +05:30
@project = ::Projects::CreateService.new(current_user, project_params(attributes: project_params_create_attributes)).execute
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
if @project.saved?
2020-05-24 23:13:21 +05:30
cookies[:issue_board_welcome_hidden] = { path: project_path(@project), value: nil, expires: Time.zone.at(0) }
2017-08-17 22:00:37 +05:30
2015-04-26 12:48:37 +05:30
redirect_to(
2018-03-27 19:54:05 +05:30
project_path(@project, custom_import_params),
2017-09-10 17:25:29 +05:30
notice: _("Project '%{project_name}' was successfully created.") % { project_name: @project.name }
2015-04-26 12:48:37 +05:30
)
else
2018-03-27 19:54:05 +05:30
render 'new', locals: { active_tab: active_new_project_tab }
2014-09-02 18:07:02 +05:30
end
end
def update
2017-08-17 22:00:37 +05:30
result = ::Projects::UpdateService.new(@project, current_user, project_params).execute
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
# Refresh the repo in case anything changed
2017-08-17 22:00:37 +05:30
@repository = @project.repository
2016-06-02 11:05:42 +05:30
2014-09-02 18:07:02 +05:30
respond_to do |format|
2017-08-17 22:00:37 +05:30
if result[:status] == :success
2017-09-10 17:25:29 +05:30
flash[:notice] = _("Project '%{project_name}' was successfully updated.") % { project_name: @project.name }
2015-04-26 12:48:37 +05:30
format.html do
2018-11-20 20:47:30 +05:30
redirect_to(edit_project_path(@project, anchor: 'js-general-project-settings'))
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
else
2018-11-08 19:23:39 +05:30
flash.now[:alert] = result[:message]
2020-11-24 15:15:51 +05:30
@project.reset
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
format.html { render_edit }
2014-09-02 18:07:02 +05:30
end
2016-08-24 12:49:21 +05:30
format.js
2014-09-02 18:07:02 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def transfer
2015-11-26 14:37:03 +05:30
return access_denied! unless can?(current_user, :change_namespace, @project)
2015-09-11 14:41:01 +05:30
namespace = Namespace.find_by(id: params[:new_namespace_id])
::Projects::TransferService.new(project, current_user).execute(namespace)
if @project.errors[:new_namespace].present?
flash[:alert] = @project.errors[:new_namespace].first
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-09-11 14:41:01 +05:30
2015-11-26 14:37:03 +05:30
def remove_fork
return access_denied! unless can?(current_user, :remove_fork_project, @project)
2016-06-02 11:05:42 +05:30
if ::Projects::UnlinkForkService.new(@project, current_user).execute
2017-09-10 17:25:29 +05:30
flash[:notice] = _('The fork relationship has been removed.')
2015-11-26 14:37:03 +05:30
end
end
2015-09-11 14:41:01 +05:30
def activity
respond_to do |format|
format.html
format.json do
load_events
2020-02-01 01:16:34 +05:30
pager_json('events/_events', @events.count { |event| event.visible_to_user?(current_user) })
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
def show
if @project.import_in_progress?
2018-03-27 19:54:05 +05:30
redirect_to project_import_path(@project, custom_import_params)
2014-09-02 18:07:02 +05:30
return
end
2016-04-02 18:10:28 +05:30
if @project.pending_delete?
2017-09-10 17:25:29 +05:30
flash.now[:alert] = _("Project '%{project_name}' queued for deletion.") % { project_name: @project.name }
2016-04-02 18:10:28 +05:30
end
2014-09-02 18:07:02 +05:30
respond_to do |format|
format.html do
2016-06-02 11:05:42 +05:30
@notification_setting = current_user.notification_settings_for(@project) if current_user
2018-03-27 19:54:05 +05:30
@project = @project.present(current_user: current_user)
2016-11-03 12:29:30 +05:30
render_landing_page
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
format.atom do
load_events
2017-09-10 17:25:29 +05:30
render layout: 'xml.atom'
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
def destroy
2015-04-26 12:48:37 +05:30
return access_denied! unless can?(current_user, :remove_project, @project)
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
::Projects::DestroyService.new(@project, current_user, {}).async_execute
2018-03-27 19:54:05 +05:30
flash[:notice] = _("Project '%{project_name}' is in the process of being deleted.") % { project_name: @project.full_name }
2014-09-02 18:07:02 +05:30
2018-11-18 11:00:15 +05:30
redirect_to dashboard_projects_path, status: :found
2015-09-11 14:41:01 +05:30
rescue Projects::DestroyService::DestroyError => ex
2019-12-26 22:10:19 +05:30
redirect_to edit_project_path(@project), status: :found, alert: ex.message
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
def new_issuable_address
2017-08-17 22:00:37 +05:30
return render_404 unless Gitlab::IncomingEmail.supports_issue_creation?
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
current_user.reset_incoming_email_token!
2018-03-17 18:26:18 +05:30
render json: { new_address: @project.new_issuable_address(current_user, params[:issuable_type]) }
2014-09-02 18:07:02 +05:30
end
def archive
2018-11-18 11:00:15 +05:30
::Projects::UpdateService.new(@project, current_user, archived: true).execute
2014-09-02 18:07:02 +05:30
respond_to do |format|
2015-04-26 12:48:37 +05:30
format.html { redirect_to project_path(@project) }
2014-09-02 18:07:02 +05:30
end
end
def unarchive
2018-11-18 11:00:15 +05:30
::Projects::UpdateService.new(@project, current_user, archived: false).execute
2014-09-02 18:07:02 +05:30
respond_to do |format|
2015-04-26 12:48:37 +05:30
format.html { redirect_to project_path(@project) }
2014-09-02 18:07:02 +05:30
end
end
def housekeeping
2019-09-30 21:07:59 +05:30
::Projects::HousekeepingService.new(@project, :gc).execute
2016-06-02 11:05:42 +05:30
redirect_to(
project_path(@project),
2017-09-10 17:25:29 +05:30
notice: _("Housekeeping successfully started")
2016-06-02 11:05:42 +05:30
)
rescue ::Projects::HousekeepingService::LeaseTaken => ex
redirect_to(
2018-11-20 20:47:30 +05:30
edit_project_path(@project, anchor: 'js-project-advanced-settings'),
2016-06-02 11:05:42 +05:30
alert: ex.to_s
)
end
2016-06-22 15:30:34 +05:30
def export
@project.add_export_job(current_user: current_user)
redirect_to(
2018-11-20 20:47:30 +05:30
edit_project_path(@project, anchor: 'js-export-project'),
2020-05-24 23:13:21 +05:30
notice: _("Project export started. A download link will be sent by email and made available on this page.")
2016-06-22 15:30:34 +05:30
)
end
def download_export
2018-11-20 20:47:30 +05:30
if @project.export_file_exists?
send_upload(@project.export_file, attachment: @project.export_file.filename)
2016-06-22 15:30:34 +05:30
else
redirect_to(
2018-11-20 20:47:30 +05:30
edit_project_path(@project, anchor: 'js-export-project'),
2017-09-10 17:25:29 +05:30
alert: _("Project export link has expired. Please generate a new export from your project settings.")
2016-06-22 15:30:34 +05:30
)
end
end
def remove_export
if @project.remove_exports
2017-09-10 17:25:29 +05:30
flash[:notice] = _("Project export has been deleted.")
2016-06-22 15:30:34 +05:30
else
2017-09-10 17:25:29 +05:30
flash[:alert] = _("Project export could not be deleted.")
2016-06-22 15:30:34 +05:30
end
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
redirect_to(edit_project_path(@project, anchor: 'js-export-project'))
2016-06-22 15:30:34 +05:30
end
def generate_new_export
if @project.remove_exports
export
else
redirect_to(
2018-11-20 20:47:30 +05:30
edit_project_path(@project, anchor: 'js-export-project'),
2017-09-10 17:25:29 +05:30
alert: _("Project export could not be deleted.")
2016-06-22 15:30:34 +05:30
)
end
end
2014-09-02 18:07:02 +05:30
def toggle_star
current_user.toggle_star(@project)
2019-07-31 22:56:46 +05:30
@project.reset
2015-09-11 14:41:01 +05:30
render json: {
star_count: @project.star_count
2015-09-11 14:41:01 +05:30
}
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-06-22 15:30:34 +05:30
def refs
2017-09-10 17:25:29 +05:30
find_refs = params['find']
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
find_branches = true
find_tags = true
find_commits = true
unless find_refs.nil?
find_branches = find_refs.include?('branches')
find_tags = find_refs.include?('tags')
find_commits = find_refs.include?('commits')
end
2016-06-22 15:30:34 +05:30
2017-09-10 17:25:29 +05:30
options = {}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
if find_branches
branches = BranchesFinder.new(@repository, params).execute.take(100).map(&:name)
2018-11-08 19:23:39 +05:30
options['Branches'] = branches
2017-09-10 17:25:29 +05:30
end
if find_tags && @repository.tag_count.nonzero?
tags = TagsFinder.new(@repository, params).execute.take(100).map(&:name)
2018-11-08 19:23:39 +05:30
options['Tags'] = tags
2016-06-22 15:30:34 +05:30
end
# If reference is commit id - we should add it to branch/tag selectbox
ref = Addressable::URI.unescape(params[:ref])
2017-09-10 17:25:29 +05:30
if find_commits && ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
2016-06-22 15:30:34 +05:30
options['Commits'] = [ref]
end
render json: options.to_json
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-06-22 15:30:34 +05:30
2019-10-12 21:52:04 +05:30
def resolve
@project = Project.find(params[:id])
if can?(current_user, :read_project, @project)
redirect_to @project
else
render_404
end
end
private
2016-11-03 12:29:30 +05:30
# Render project landing depending of which features are available
2018-12-13 13:39:08 +05:30
# So if page is not available in the list it renders the next page
2016-11-03 12:29:30 +05:30
#
# pages list order: repository readme, wiki home, issues list, customize workflow
def render_landing_page
2017-09-10 17:25:29 +05:30
if can?(current_user, :download_code, @project)
2016-11-03 12:29:30 +05:30
return render 'projects/no_repo' unless @project.repository_exists?
2018-03-17 18:26:18 +05:30
2016-11-03 12:29:30 +05:30
render 'projects/empty' if @project.empty_repo?
else
2018-03-17 18:26:18 +05:30
if can?(current_user, :read_wiki, @project)
2020-06-23 00:09:42 +05:30
@wiki = @project.wiki
@wiki_home = @wiki.find_page('home', params[:version_id])
2016-11-03 12:29:30 +05:30
elsif @project.feature_available?(:issues, current_user)
2018-03-17 18:26:18 +05:30
@issues = issuables_collection.page(params[:page])
2020-06-23 00:09:42 +05:30
@issuable_meta_data = Gitlab::IssuableMetadata.new(current_user, @issues).data
2016-11-03 12:29:30 +05:30
end
render :show
end
end
2018-03-27 19:54:05 +05:30
def finder_type
IssuesFinder
end
2015-09-11 14:41:01 +05:30
def determine_layout
if [:new, :create].include?(action_name.to_sym)
'application'
elsif [:edit, :update].include?(action_name.to_sym)
'project_settings'
else
'project'
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-09-11 14:41:01 +05:30
def load_events
2017-09-10 17:25:29 +05:30
projects = Project.where(id: @project.id)
@events = EventCollection
.new(projects, offset: params[:offset].to_i, filter: event_filter)
.to_a
2020-02-01 01:16:34 +05:30
.map(&:present)
2018-03-17 18:26:18 +05:30
Events::RenderService.new(current_user).execute(@events, atom_request: request.format.atom?)
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2019-04-03 18:18:56 +05:30
def project_params(attributes: [])
2017-08-17 22:00:37 +05:30
params.require(:project)
2019-04-03 18:18:56 +05:30
.permit(project_params_attributes + attributes)
2019-07-31 22:56:46 +05:30
.merge(import_url_params)
2017-08-17 22:00:37 +05:30
end
2016-09-29 09:46:39 +05:30
2017-09-10 17:25:29 +05:30
def project_params_attributes
2017-08-17 22:00:37 +05:30
[
2020-06-23 00:09:42 +05:30
:allow_merge_on_skipped_pipeline,
2017-08-17 22:00:37 +05:30
:avatar,
:build_allow_git_fetch,
:build_coverage_regex,
2018-05-09 12:01:36 +05:30
:build_timeout_human_readable,
2018-03-17 18:26:18 +05:30
:resolve_outdated_diff_discussions,
2016-09-29 09:46:39 +05:30
:container_registry_enabled,
2017-08-17 22:00:37 +05:30
:default_branch,
:description,
2019-10-12 21:52:04 +05:30
:emails_disabled,
2019-07-07 11:18:12 +05:30
:external_authorization_classification_label,
2017-08-17 22:00:37 +05:30
:import_url,
:issues_tracker,
:issues_tracker_id,
:last_activity_at,
:lfs_enabled,
:name,
:only_allow_merge_if_all_discussions_are_resolved,
:only_allow_merge_if_pipeline_succeeds,
:path,
2019-04-03 18:18:56 +05:30
:printing_merge_request_link_enabled,
2017-08-17 22:00:37 +05:30
:public_builds,
2019-12-26 22:10:19 +05:30
:remove_source_branch_after_merge,
2017-08-17 22:00:37 +05:30
:request_access_enabled,
:runners_token,
:tag_list,
:visibility_level,
2017-09-10 17:25:29 +05:30
:template_name,
2019-12-21 20:55:43 +05:30
:template_project_id,
2018-03-17 18:26:18 +05:30
:merge_method,
2018-11-08 19:23:39 +05:30
:initialize_with_readme,
2020-03-13 15:44:24 +05:30
:autoclose_referenced_issues,
:suggestion_commit_message,
2020-10-24 23:57:45 +05:30
:packages_enabled,
2020-07-28 23:09:34 +05:30
:service_desk_enabled,
2017-08-17 22:00:37 +05:30
project_feature_attributes: %i[
builds_access_level
issues_access_level
2020-03-13 15:44:24 +05:30
forking_access_level
2017-08-17 22:00:37 +05:30
merge_requests_access_level
repository_access_level
snippets_access_level
wiki_access_level
2018-12-05 23:21:45 +05:30
pages_access_level
2020-05-24 23:13:21 +05:30
metrics_dashboard_access_level
],
project_setting_attributes: %i[
show_default_award_emojis
2020-07-28 23:09:34 +05:30
squash_option
2017-08-17 22:00:37 +05:30
]
]
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2019-04-03 18:18:56 +05:30
def project_params_create_attributes
[:namespace_id]
end
2018-03-27 19:54:05 +05:30
def custom_import_params
{}
end
def active_new_project_tab
project_params[:import_url].present? ? 'import' : 'blank'
end
2015-11-26 14:37:03 +05:30
def repo_exists?
2018-03-17 18:26:18 +05:30
project.repository_exists? && !project.empty_repo?
2016-11-03 12:29:30 +05:30
rescue Gitlab::Git::Repository::NoRepository
project.repository.expire_exists_cache
false
2015-11-26 14:37:03 +05:30
end
2016-08-24 12:49:21 +05:30
def project_view_files?
2017-08-17 22:00:37 +05:30
if current_user
current_user.project_view == 'files'
else
project_view_files_allowed?
end
2016-08-24 12:49:21 +05:30
end
2016-06-22 15:30:34 +05:30
# Override extract_ref from ExtractsPath, which returns the branch and file path
2015-11-26 14:37:03 +05:30
# for the blob/tree, which in this case is just the root of the default branch.
2016-06-22 15:30:34 +05:30
# This way we avoid to access the repository.ref_names.
def extract_ref(_id)
[get_id, '']
end
# Override get_id from ExtractsPath in this case is just the root of the default branch.
2015-11-26 14:37:03 +05:30
def get_id
project.repository.root_ref
end
2017-08-17 22:00:37 +05:30
def project_view_files_allowed?
!project.empty_repo? && can?(current_user, :download_code, project)
end
def build_canonical_path(project)
params[:namespace_id] = project.namespace.to_param
params[:id] = project.to_param
2018-06-27 16:04:02 +05:30
url_for(safe_params)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def project_export_enabled
render_404 unless Gitlab::CurrentSettings.project_export_enabled?
end
def redirect_git_extension
# Redirect from
# localhost/group/project.git
# to
# localhost/group/project
#
redirect_to request.original_url.sub(%r{\.git/?\Z}, '') if params[:format] == 'git'
end
def whitelist_query_limiting
2019-12-04 20:38:33 +05:30
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/42440')
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
def present_project
@project = @project.present(current_user: current_user)
end
2020-01-01 13:55:28 +05:30
def export_rate_limit
prefixed_action = "project_#{params[:action]}".to_sym
2020-06-23 00:09:42 +05:30
project_scope = params[:action] == :download_export ? @project : nil
if rate_limiter.throttled?(prefixed_action, scope: [current_user, project_scope].compact)
2020-01-01 13:55:28 +05:30
rate_limiter.log_request(request, "#{prefixed_action}_request_limit".to_sym, current_user)
2020-06-23 00:09:42 +05:30
render plain: _('This endpoint has been requested too many times. Try again later.'), status: :too_many_requests
2020-01-01 13:55:28 +05:30
end
end
def rate_limiter
::Gitlab::ApplicationRateLimiter
end
2020-03-13 15:44:24 +05:30
def render_edit
render 'edit'
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
ProjectsController.prepend_if_ee('EE::ProjectsController')