rosa-build/app/controllers/projects/projects_controller.rb

207 lines
5.8 KiB
Ruby
Raw Normal View History

class Projects::ProjectsController < Projects::BaseController
2014-06-26 22:21:31 +01:00
include DatatableHelper
include ProjectsHelper
2015-03-04 23:19:19 +00:00
before_action :authenticate_user!
before_action :who_owns, only: [:new, :create, :mass_import, :run_mass_import]
2011-03-10 13:38:50 +00:00
def index
authorize :project
@projects = ProjectPolicy::Scope.new(current_user, Project).membered.search(params[:search])
respond_to do |format|
format.html {
@groups = current_user.groups
2014-01-21 04:51:49 +00:00
@owners = User.where(id: @projects.where(owner_type: 'User').uniq.pluck(:owner_id))
}
format.json {
groups = params[:groups] || []
owners = params[:users] || []
@projects = @projects.by_owners(groups, owners) if groups.present? || owners.present?
@projects_count = @projects.count
@projects = @projects.recent.paginate(page: current_page, per_page: Project.per_page)
}
end
2011-03-10 14:20:09 +00:00
end
def new
authorize :project
@project = Project.new
2013-11-13 22:01:12 +00:00
end
def mass_import
authorize :project
2014-01-21 04:51:49 +00:00
@project = Project.new(mass_import: true)
2013-11-13 22:01:12 +00:00
end
def run_mass_import
@project = Project.new params[:project]
@project.owner = choose_owner
authorize @project
2013-11-13 22:01:12 +00:00
@project.valid?
@project.errors.messages.slice! :url
if @project.errors.messages.blank? # We need only url validation
2013-11-14 21:26:05 +00:00
@project.init_mass_import
2013-11-13 22:01:12 +00:00
flash[:notice] = t('flash.project.mass_import_added_to_queue')
redirect_to projects_path
else
render :mass_import
end
end
def edit
authorize @project
@project_aliases = Project.project_aliases(@project).paginate(page: current_page)
end
def create
@project = Project.new params[:project]
@project.owner = choose_owner
authorize @project
2011-11-30 12:58:14 +00:00
if @project.save
flash[:notice] = t('flash.project.saved')
redirect_to @project
else
flash[:error] = t('flash.project.save_error')
flash[:warning] = @project.errors.full_messages.join('. ')
2014-01-21 04:51:49 +00:00
render action: :new
end
end
def update
authorize @project
params[:project].delete(:maintainer_id) if params[:project][:maintainer_id].blank?
2014-02-18 19:16:23 +00:00
respond_to do |format|
format.html do
if @project.update_attributes(params[:project])
flash[:notice] = t('flash.project.saved')
redirect_to @project
else
@project.save
flash[:error] = t('flash.project.save_error')
flash[:warning] = @project.errors.full_messages.join('. ')
render action: :edit
end
end
format.json do
if @project.update_attributes(params[:project])
render json: { notice: I18n.t('flash.project.saved') }
2014-02-18 19:16:23 +00:00
else
render json: { error: I18n.t('flash.project.save_error') }, status: 422
2014-02-18 19:16:23 +00:00
end
end
end
end
def schedule
authorize @project
2014-02-18 19:16:23 +00:00
p_to_r = @project.project_to_repositories.where(repository_id: params[:repository_id]).first
unless p_to_r.repository.publish_without_qa
authorize p_to_r.repository.platform, :local_admin_manage?
end
2014-02-18 19:16:23 +00:00
p_to_r.user_id = current_user.id
p_to_r.enabled = params[:enabled].present?
p_to_r.auto_publish = params[:auto_publish].present?
p_to_r.save
if p_to_r.save
render json: { notice: I18n.t('flash.project.saved') }.to_json
else
render json: { error: I18n.t('flash.project.save_error') }.to_json, status: 422
end
end
def destroy
authorize @project
@project.destroy
flash[:notice] = t("flash.project.destroyed")
redirect_to @project.owner
end
2015-01-26 13:52:09 +00:00
def fork(is_alias = false)
2012-04-09 18:56:03 +01:00
owner = (Group.find params[:group] if params[:group].present?) || current_user
authorize owner, :write?
if forked = @project.fork(owner, new_name: params[:fork_name], is_alias: is_alias) and forked.valid?
2014-01-21 04:51:49 +00:00
redirect_to forked, notice: t("flash.project.forked")
else
flash[:warning] = t("flash.project.fork_error")
flash[:error] = forked.errors.full_messages.join("\n")
redirect_to @project
end
end
2015-01-26 13:52:09 +00:00
def alias
authorize @project
2015-01-26 13:52:09 +00:00
fork(true)
end
def possible_forks
2015-03-17 00:55:04 +00:00
authorize @project
2014-01-21 04:51:49 +00:00
render partial: 'projects/git/base/forks', layout: false,
locals: { owner: current_user, name: (params[:name].presence || @project.name) }
end
def sections
authorize @project, :update?
2014-11-12 17:30:27 +00:00
if request.patch?
if @project.update_attributes(params[:project])
flash[:notice] = t('flash.project.saved')
redirect_to sections_project_path(@project)
else
@project.save
flash[:error] = t('flash.project.save_error')
end
end
end
def remove_user
authorize @project
2014-06-26 13:16:35 +01:00
@project.relations.by_actor(current_user).destroy_all
respond_to do |format|
format.html do
flash[:notice] = t("flash.project.user_removed")
redirect_to projects_path
end
format.json { render nothing: true }
end
end
2011-10-17 15:21:29 +01:00
def autocomplete_maintainers
authorize @project
term, limit = params[:query], params[:limit] || 10
items = User.member_of_project(@project)
.where("users.name ILIKE ? OR users.uname ILIKE ?", "%#{term}%", "%#{term}%")
2014-11-11 16:59:49 +00:00
.limit(limit).map { |u| {name: u.fullname, id: u.id} }
2014-01-21 04:51:49 +00:00
render json: items
end
2012-08-29 15:58:58 +01:00
def preview
authorize @project
2015-03-17 18:27:11 +00:00
respond_to do |format|
format.json {}
format.html {render inline: view_context.markdown(params[:text]), layout: false}
end
2012-08-29 15:58:58 +01:00
end
2012-10-01 17:03:08 +01:00
def refs_list
authorize @project
2012-10-01 17:03:08 +01:00
refs = @project.repo.branches_and_tags.map(&:name)
@selected = params[:selected] if refs.include?(params[:selected])
@selected ||= @project.resolve_default_branch
2014-01-21 04:51:49 +00:00
render layout: false
2012-10-01 17:03:08 +01:00
end
2012-08-29 15:58:58 +01:00
protected
2013-11-13 22:01:12 +00:00
def who_owns
@who_owns = (@project.try(:owner_type) == 'User' ? :me : :group)
end
def choose_owner
if params[:who_owns] == 'group'
Group.find(params[:owner_id])
else
current_user
end
end
2011-03-10 11:35:46 +00:00
end