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

184 lines
4.9 KiB
Ruby
Raw Normal View History

class ProjectsController < ApplicationController
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]
2011-10-15 11:51:58 -04:00
2013-06-10 14:40:23 -04:00
layout 'navless', only: [:new, :create, :fork]
2013-06-07 10:31:12 -04:00
before_filter :set_title, only: [:new, :create]
2012-09-26 16:24:52 -04:00
2011-10-14 17:05:41 -04:00
def new
@project = Project.new
end
def edit
2013-06-19 15:44:57 -04:00
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.'
2013-05-24 17:07:19 -04:00
format.html { redirect_to edit_project_path(@project), notice: 'Project was successfully updated.' }
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
::Projects::TransferService.new(project, current_user, project_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 project_import_path(@project)
return
end
limit = (params[:limit] || 20).to_i
@events = @project.events.recent
@events = event_filter.apply_filter(@events)
@events = @events.limit(limit).offset(params[:offset] || 0)
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
format.json { pager_json("events/_events", @events.count) }
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_projects_path
else
redirect_to projects_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']
participants = ::Projects::ParticipantsService.new(@project).execute(note_type, note_id)
@suggestions = {
2014-03-24 09:22:11 -04:00
emojis: Emoji.names.map { |e| { name: e, path: view_context.image_url("emoji/#{e}.png") } },
issues: @project.issues.select([:iid, :title, :description]),
mergerequests: @project.merge_requests.select([:iid, :title, :description]),
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 }
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 }
end
end
def upload_image
2014-05-26 08:53:34 -04:00
link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute
respond_to do |format|
2014-05-26 08:53:34 -04:00
if link_to_image
format.json { render json: { link: link_to_image } }
else
format.json { render json: "Invalid file.", status: :unprocessable_entity }
end
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 upload_path
base_dir = FileUploader.generate_dir
File.join(repository.path_with_namespace, base_dir)
end
def accepted_images
%w(png jpg jpeg gif)
end
2013-06-07 10:31:12 -04:00
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
)
end
2011-10-08 17:36:38 -04:00
end