gitlab-org--gitlab-foss/app/controllers/projects_controller.rb

199 lines
5.1 KiB
Ruby
Raw Normal View History

class ProjectsController < ApplicationController
prepend_before_filter :render_go_import, only: [:show]
skip_before_filter :authenticate_user!, only: [:show]
before_filter :project, except: [:new, :create]
before_filter :repository, except: [:new, :create]
2011-10-08 17:36:38 -04:00
# Authorize
before_filter :authorize_admin_project!, only: [:edit, :update, :destroy, :transfer, :archive, :unarchive]
2015-02-18 03:16:42 -05:00
before_filter :set_title, only: [:new, :create]
before_filter :event_filter, only: :show
2011-10-15 11:51:58 -04:00
2013-06-10 14:40:23 -04:00
layout 'navless', only: [:new, :create, :fork]
2012-09-26 16:24:52 -04:00
2011-10-14 17:05:41 -04:00
def new
@project = Project.new
end
def edit
render 'edit', layout: 'project_settings'
2011-10-14 17:05:41 -04:00
end
def create
@project = ::Projects::CreateService.new(current_user, project_params).execute
2011-10-14 17:05:41 -04:00
if @project.saved?
redirect_to(
project_path(@project),
notice: 'Project was successfully created.'
)
else
render 'new'
2011-10-14 17:05:41 -04:00
end
end
2011-10-08 17:36:38 -04:00
2011-10-14 17:05:41 -04:00
def update
status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
2011-10-08 17:36:38 -04:00
respond_to do |format|
if status
flash[:notice] = 'Project was successfully updated.'
format.html do
redirect_to(
edit_project_path(@project),
notice: 'Project was successfully updated.'
)
end
format.js
2011-10-14 17:05:41 -04:00
else
format.html { render 'edit', layout: 'project_settings' }
format.js
2011-10-14 17:05:41 -04:00
end
2011-10-08 17:36:38 -04:00
end
end
def transfer
transfer_params = params.permit(:new_namespace_id)
::Projects::TransferService.new(project, current_user, transfer_params).execute
2014-12-15 08:10:56 -05:00
if @project.errors[:namespace_id].present?
flash[:alert] = @project.errors[:namespace_id].first
end
2011-10-08 17:36:38 -04:00
end
def show
if @project.import_in_progress?
redirect_to namespace_project_import_path(@project.namespace, @project)
return
end
limit = (params[:limit] || 20).to_i
2012-03-02 17:09:17 -05:00
2014-07-15 06:00:51 -04:00
@show_star = !(current_user && current_user.starred?(@project))
2012-03-02 17:09:17 -05:00
respond_to do |format|
2012-06-01 02:42:02 -04:00
format.html do
if @project.repository_exists?
if @project.empty_repo?
render 'projects/empty', layout: user_layout
else
@last_push = current_user.recent_push(@project.id) if current_user
render :show, layout: user_layout
end
else
render 'projects/no_repo', layout: user_layout
end
2012-03-02 17:09:17 -05:00
end
2015-02-18 03:16:42 -05:00
format.json do
@events = @project.events.recent
2015-02-18 12:38:46 -05:00
@events = event_filter.apply_filter(@events).with_associations
2015-02-18 03:16:42 -05:00
@events = @events.limit(limit).offset(params[:offset] || 0)
pager_json('events/_events', @events.count)
end
end
end
2011-10-08 17:36:38 -04:00
def destroy
return access_denied! unless can?(current_user, :remove_project, @project)
2012-12-04 22:14:05 -05:00
2014-06-17 16:49:17 -04:00
::Projects::DestroyService.new(@project, current_user, {}).execute
2011-10-08 17:36:38 -04:00
respond_to do |format|
format.html do
flash[:alert] = 'Project deleted.'
if request.referer.include?('/admin')
redirect_to admin_namespaces_projects_path
else
2015-03-11 00:56:19 -04:00
redirect_to dashboard_path
end
end
2011-10-08 17:36:38 -04:00
end
end
def autocomplete_sources
2014-03-25 07:40:49 -04:00
note_type = params['type']
note_id = params['type_id']
autocomplete = ::Projects::AutocompleteService.new(@project)
participants = ::Projects::ParticipantsService.new(@project, current_user).execute(note_type, note_id)
@suggestions = {
2015-01-23 19:17:27 -05:00
emojis: autocomplete_emojis,
issues: autocomplete.issues,
mergerequests: autocomplete.merge_requests,
members: participants
}
respond_to do |format|
format.json { render json: @suggestions }
end
end
2013-06-07 10:31:12 -04:00
def archive
return access_denied! unless can?(current_user, :archive_project, @project)
@project.archive!
respond_to do |format|
format.html { redirect_to project_path(@project) }
end
end
def unarchive
return access_denied! unless can?(current_user, :archive_project, @project)
@project.unarchive!
respond_to do |format|
format.html { redirect_to project_path(@project) }
end
end
2014-06-26 03:49:14 -04:00
def toggle_star
current_user.toggle_star(@project)
@project.reload
2014-06-26 03:49:14 -04:00
render json: { star_count: @project.star_count }
end
def markdown_preview
render text: view_context.markdown(params[:md_text])
end
2013-06-07 10:31:12 -04:00
private
def set_title
@title = 'New Project'
end
def user_layout
current_user ? 'projects' : 'public_projects'
end
def project_params
params.require(:project).permit(
:name, :path, :description, :issues_tracker, :tag_list,
:issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
:wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar
)
end
2015-01-23 19:17:27 -05:00
def autocomplete_emojis
2015-03-11 19:05:01 -04:00
Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
Emoji.emojis.map do |name, emoji|
2015-01-23 19:17:27 -05:00
{
2015-03-11 19:05:01 -04:00
name: name,
path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
2015-01-23 19:17:27 -05:00
}
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
2011-10-08 17:36:38 -04:00
end