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

414 lines
11 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
class ProjectsController < Projects::ApplicationController
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
2015-11-26 14:37:03 +05:30
2018-03-17 18:26:18 +05:30
before_action :whitelist_query_limiting, only: [:create]
2017-08-17 22:00:37 +05:30
before_action :authenticate_user!, except: [:index, :show, :activity, :refs]
2018-03-17 18:26:18 +05:30
before_action :redirect_git_extension, only: [:show]
2017-08-17 22:00:37 +05:30
before_action :project, except: [:index, :new, :create]
before_action :repository, except: [:index, :new, :create]
2016-08-24 12:49:21 +05:30
before_action :assign_ref_vars, only: [:show], if: :repo_exists?
2016-09-29 09:46:39 +05:30
before_action :tree, only: [:show], if: [:repo_exists?, :project_view_files?]
2018-03-17 18:26:18 +05:30
before_action :lfs_blob_ids, only: [:show], if: [:repo_exists?, :project_view_files?]
before_action :project_export_enabled, only: [:export, :download_export, :remove_export, :generate_new_export]
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]
2015-09-11 14:41:01 +05:30
before_action :event_filter, only: [:show, :activity]
2014-09-02 18:07:02 +05:30
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
2014-09-02 18:07:02 +05:30
def new
2018-03-17 18:26:18 +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)
@project = Project.new(namespace_id: namespace&.id)
2014-09-02 18:07:02 +05:30
end
def edit
2015-09-11 14:41:01 +05:30
render 'edit'
2014-09-02 18:07:02 +05:30
end
def create
@project = ::Projects::CreateService.new(current_user, project_params).execute
2015-04-26 12:48:37 +05:30
if @project.saved?
2017-08-17 22:00:37 +05:30
cookies[:issue_board_welcome_hidden] = { path: project_path(@project), value: nil, expires: Time.at(0) }
2015-04-26 12:48:37 +05:30
redirect_to(
project_path(@project),
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
render 'new'
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
2017-08-17 22:00:37 +05:30
redirect_to(edit_project_path(@project))
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
else
2017-09-10 17:25:29 +05:30
flash[:alert] = result[:message]
2015-09-11 14:41:01 +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
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
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
pager_json('events/_events', @events.count)
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?
2017-09-10 17:25:29 +05:30
redirect_to project_import_path(@project)
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
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-17 18:26:18 +05:30
flash[:notice] = _("Project '%{project_name}' is in the process of being deleted.") % { project_name: @project.name_with_namespace }
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
redirect_to dashboard_projects_path, status: 302
2015-09-11 14:41:01 +05:30
rescue Projects::DestroyService::DestroyError => ex
2017-09-10 17:25:29 +05:30
redirect_to edit_project_path(@project), status: 302, 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
2015-04-26 12:48:37 +05:30
return access_denied! unless can?(current_user, :archive_project, @project)
2015-11-26 14:37:03 +05:30
2015-04-26 12:48:37 +05:30
@project.archive!
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
2015-04-26 12:48:37 +05:30
return access_denied! unless can?(current_user, :archive_project, @project)
2015-11-26 14:37:03 +05:30
2015-04-26 12:48:37 +05:30
@project.unarchive!
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
::Projects::HousekeepingService.new(@project).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(
edit_project_path(@project),
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(
edit_project_path(@project),
2017-09-10 17:25:29 +05:30
notice: _("Project export started. A download link will be sent by email.")
2016-06-22 15:30:34 +05:30
)
end
def download_export
export_project_path = @project.export_project_path
if export_project_path
send_file export_project_path, disposition: 'attachment'
else
redirect_to(
edit_project_path(@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
2016-06-22 15:30:34 +05:30
redirect_to(edit_project_path(@project))
end
def generate_new_export
if @project.remove_exports
export
else
redirect_to(
edit_project_path(@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)
@project.reload
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
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)
options[s_('RefSwitcher|Branches')] = branches
end
if find_tags && @repository.tag_count.nonzero?
tags = TagsFinder.new(@repository, params).execute.take(100).map(&:name)
options[s_('RefSwitcher|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
2015-04-26 12:48:37 +05:30
private
2014-09-02 18:07:02 +05:30
2016-11-03 12:29:30 +05:30
# Render project landing depending of which features are available
# So if page is not availble in the list it renders the next page
#
# 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)
2016-11-24 13:41:30 +05:30
@project_wiki = @project.wiki
@wiki_home = @project_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
@finder_type = IssuesFinder
@issues = issuables_collection.page(params[:page])
2017-08-17 22:00:37 +05:30
@collection_type = 'Issue'
@issuable_meta_data = issuable_meta_data(@issues, @collection_type)
2016-11-03 12:29:30 +05:30
end
render :show
end
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
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
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
def project_params
2017-08-17 22:00:37 +05:30
params.require(:project)
2017-09-10 17:25:29 +05:30
.permit(project_params_attributes)
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
[
:avatar,
:build_allow_git_fetch,
:build_coverage_regex,
:build_timeout_in_minutes,
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,
:import_url,
:issues_tracker,
:issues_tracker_id,
:last_activity_at,
:lfs_enabled,
:name,
:namespace_id,
:only_allow_merge_if_all_discussions_are_resolved,
:only_allow_merge_if_pipeline_succeeds,
:printing_merge_request_link_enabled,
:path,
:public_builds,
:request_access_enabled,
:runners_token,
:tag_list,
:visibility_level,
2017-09-10 17:25:29 +05:30
:template_name,
2018-03-17 18:26:18 +05:30
:merge_method,
2017-08-17 22:00:37 +05:30
project_feature_attributes: %i[
builds_access_level
issues_access_level
merge_requests_access_level
repository_access_level
snippets_access_level
wiki_access_level
]
]
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
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
url_for(params)
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
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42440')
end
2014-09-02 18:07:02 +05:30
end