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

365 lines
9.7 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
2016-06-22 15:30:34 +05:30
before_action :authenticate_user!, except: [:show, :activity, :refs]
2015-09-11 14:41:01 +05:30
before_action :project, except: [:new, :create]
before_action :repository, except: [: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?]
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
@project = Project.new
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?
redirect_to(
project_path(@project),
2015-09-11 14:41:01 +05:30
notice: "Project '#{@project.name}' was successfully created."
2015-04-26 12:48:37 +05:30
)
else
render 'new'
2014-09-02 18:07:02 +05:30
end
end
def update
status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
2016-06-02 11:05:42 +05:30
# Refresh the repo in case anything changed
@repository = project.repository
2014-09-02 18:07:02 +05:30
respond_to do |format|
if status
2015-09-11 14:41:01 +05:30
flash[:notice] = "Project '#{@project.name}' was successfully updated."
2015-04-26 12:48:37 +05:30
format.html do
redirect_to(
edit_project_path(@project),
2015-09-11 14:41:01 +05:30
notice: "Project '#{@project.name}' was successfully updated."
2015-04-26 12:48:37 +05:30
)
end
2014-09-02 18:07:02 +05:30
else
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
2015-11-26 14:37:03 +05:30
flash[:notice] = 'The fork relationship has been removed.'
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?
2015-04-26 12:48:37 +05:30
redirect_to namespace_project_import_path(@project.namespace, @project)
2014-09-02 18:07:02 +05:30
return
end
2016-04-02 18:10:28 +05:30
if @project.pending_delete?
2016-09-13 17:45:13 +05:30
flash[:alert] = "Project #{@project.name} queued for deletion."
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
render layout: false
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
2016-04-02 18:10:28 +05:30
flash[:alert] = "Project '#{@project.name}' will be deleted."
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
redirect_to dashboard_projects_path
2015-09-11 14:41:01 +05:30
rescue Projects::DestroyService::DestroyError => ex
redirect_to edit_project_path(@project), alert: ex.message
2014-09-02 18:07:02 +05:30
end
def autocomplete_sources
2016-09-13 17:45:13 +05:30
noteable =
case params[:type]
when 'Issue'
2016-11-03 12:29:30 +05:30
IssuesFinder.new(current_user, project_id: @project.id).
2016-09-13 17:45:13 +05:30
execute.find_by(iid: params[:type_id])
when 'MergeRequest'
2016-11-03 12:29:30 +05:30
MergeRequestsFinder.new(current_user, project_id: @project.id).
2016-09-13 17:45:13 +05:30
execute.find_by(iid: params[:type_id])
when 'Commit'
@project.commit(params[:type_id])
else
nil
end
2016-06-02 11:05:42 +05:30
autocomplete = ::Projects::AutocompleteService.new(@project, current_user)
2016-09-13 17:45:13 +05:30
participants = ::Projects::ParticipantsService.new(@project, current_user).execute(noteable)
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
@suggestions = {
emojis: Gitlab::AwardEmoji.urls,
2015-04-26 12:48:37 +05:30
issues: autocomplete.issues,
2016-06-02 11:05:42 +05:30
milestones: autocomplete.milestones,
2015-04-26 12:48:37 +05:30
mergerequests: autocomplete.merge_requests,
2016-06-22 15:30:34 +05:30
labels: autocomplete.labels,
2016-09-13 17:45:13 +05:30
members: participants,
commands: autocomplete.commands(noteable, params[:type])
2014-09-02 18:07:02 +05:30
}
respond_to do |format|
2015-04-26 12:48:37 +05:30
format.json { render json: @suggestions }
2014-09-02 18:07:02 +05:30
end
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),
notice: "Housekeeping successfully started"
)
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),
notice: "Project export started. A download link will be sent by email."
)
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),
alert: "Project export link has expired. Please generate a new export from your project settings."
)
end
end
def remove_export
if @project.remove_exports
flash[:notice] = "Project export has been deleted."
else
flash[:alert] = "Project export could not be deleted."
end
redirect_to(edit_project_path(@project))
end
def generate_new_export
if @project.remove_exports
export
else
redirect_to(
edit_project_path(@project),
alert: "Project export could not be deleted."
)
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-09-13 17:45:13 +05:30
def preview_markdown
2015-09-11 14:41:01 +05:30
text = params[:text]
ext = Gitlab::ReferenceExtractor.new(@project, current_user)
ext.analyze(text, author: current_user)
2015-09-11 14:41:01 +05:30
render json: {
body: view_context.markdown(text),
references: {
users: ext.users.map(&:username)
}
}
2014-09-02 18:07:02 +05:30
end
2016-06-22 15:30:34 +05:30
def refs
options = {
'Branches' => @repository.branch_names,
}
unless @repository.tag_count.zero?
options['Tags'] = VersionSorter.rsort(@repository.tag_names)
end
# If reference is commit id - we should add it to branch/tag selectbox
ref = Addressable::URI.unescape(params[:ref])
if ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
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
if @project.feature_available?(:repository, current_user)
return render 'projects/no_repo' unless @project.repository_exists?
render 'projects/empty' if @project.empty_repo?
else
if @project.wiki_enabled?
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)
@issues = issues_collection
@issues = @issues.page(params[:page])
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
@events = @project.events.recent
@events = event_filter.apply_filter(@events).with_associations
limit = (params[:limit] || 20).to_i
@events = @events.limit(limit).offset(params[:offset] || 0)
2014-09-02 18:07:02 +05:30
end
def project_params
2016-09-29 09:46:39 +05:30
project_feature_attributes =
{
project_feature_attributes:
[
:issues_access_level, :builds_access_level,
2016-11-03 12:29:30 +05:30
:wiki_access_level, :merge_requests_access_level,
:snippets_access_level, :repository_access_level
2016-09-29 09:46:39 +05:30
]
}
2014-09-02 18:07:02 +05:30
params.require(:project).permit(
2015-12-23 02:04:40 +05:30
:name, :path, :description, :issues_tracker, :tag_list, :runners_token,
2016-09-29 09:46:39 +05:30
:container_registry_enabled,
2016-06-02 11:05:42 +05:30
:issues_tracker_id, :default_branch,
2016-09-29 09:46:39 +05:30
:visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar,
:build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex,
:public_builds, :only_allow_merge_if_build_succeeds, :request_access_enabled,
:lfs_enabled, project_feature_attributes
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?
2016-11-03 12:29:30 +05:30
project.repository_exists? && !project.empty_repo? && project.repo
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?
current_user && current_user.project_view == 'files'
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
2014-09-02 18:07:02 +05:30
end