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

251 lines
6.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class ProjectsController < ApplicationController
2015-11-26 14:37:03 +05:30
include ExtractsPath
prepend_before_action :render_go_import, only: [:show]
2015-09-11 14:41:01 +05:30
skip_before_action :authenticate_user!, only: [:show, :activity]
before_action :project, except: [:new, :create]
before_action :repository, except: [:new, :create]
2015-11-26 14:37:03 +05:30
before_action :assign_ref_vars, :tree, only: [:show], if: :repo_exists?
2014-09-02 18:07:02 +05:30
# Authorize
2015-11-26 14:37:03 +05:30
before_action :authorize_admin_project!, only: [:edit, :update]
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
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
format.js
else
2015-09-11 14:41:01 +05:30
format.html { render 'edit' }
2014-09-02 18:07:02 +05:30
format.js
end
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)
if @project.unlink_fork
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
respond_to do |format|
format.html do
2015-04-26 12:48:37 +05:30
if @project.repository_exists?
if @project.empty_repo?
2015-09-11 14:41:01 +05:30
render 'projects/empty'
2015-04-26 12:48:37 +05:30
else
2015-09-25 12:07:36 +05:30
if current_user
2015-10-24 18:46:33 +05:30
@membership = @project.team.find_member(current_user.id)
2015-09-25 12:07:36 +05:30
end
2015-09-11 14:41:01 +05:30
render :show
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
render 'projects/no_repo'
2014-09-02 18:07:02 +05:30
end
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
::Projects::DestroyService.new(@project, current_user, {}).execute
2015-09-11 14:41:01 +05:30
flash[:alert] = "Project '#{@project.name}' was 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
note_type = params['type']
note_id = params['type_id']
2015-04-26 12:48:37 +05:30
autocomplete = ::Projects::AutocompleteService.new(@project)
participants = ::Projects::ParticipantsService.new(@project, current_user).execute(note_type, note_id)
2014-09-02 18:07:02 +05:30
@suggestions = {
2015-04-26 12:48:37 +05:30
emojis: autocomplete_emojis,
issues: autocomplete.issues,
mergerequests: autocomplete.merge_requests,
2014-09-02 18:07:02 +05:30
members: participants
}
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 toggle_star
current_user.toggle_star(@project)
@project.reload
2015-09-11 14:41:01 +05:30
render json: {
html: view_to_html_string("projects/buttons/_star")
}
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def markdown_preview
2015-09-11 14:41:01 +05:30
text = params[:text]
ext = Gitlab::ReferenceExtractor.new(@project, current_user)
ext.analyze(text)
render json: {
body: view_context.markdown(text),
references: {
users: ext.users.map(&:username)
}
}
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
private
2014-09-02 18:07:02 +05:30
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
params.require(:project).permit(
2015-12-23 02:04:40 +05:30
:name, :path, :description, :issues_tracker, :tag_list, :runners_token,
2014-09-02 18:07:02 +05:30
:issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
2015-11-26 14:37:03 +05:30
:wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar,
2015-12-23 02:04:40 +05:30
:builds_enabled, :build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex,
2014-09-02 18:07:02 +05:30
)
end
2015-04-26 12:48:37 +05:30
def autocomplete_emojis
Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
Emoji.emojis.map do |name, emoji|
{
name: name,
path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
}
end
end
end
def render_go_import
return unless params["go-get"] == "1"
@namespace = params[:namespace_id]
@id = params[:project_id] || params[:id]
@id = @id.gsub(/\.git\Z/, "")
render "go_import", layout: false
end
2015-11-26 14:37:03 +05:30
def repo_exists?
project.repository_exists? && !project.empty_repo?
end
# Override get_id from ExtractsPath, which returns the branch and file path
# for the blob/tree, which in this case is just the root of the default branch.
def get_id
project.repository.root_ref
end
2014-09-02 18:07:02 +05:30
end