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

133 lines
3.6 KiB
Ruby
Raw Normal View History

class Projects::ProjectsController < Projects::BaseController
include ProjectsHelper
2015-03-04 23:19:19 +00:00
before_action :authenticate_user!
skip_before_action :authenticate_user!, only: [:commit, :diff]
2015-03-04 23:19:19 +00:00
before_action :who_owns, only: [:new, :create, :mass_import, :run_mass_import]
2011-03-10 13:38:50 +00:00
def index
authorize :project
respond_to do |format|
2016-05-04 16:49:30 +01:00
format.html
format.json {
2016-04-29 15:10:30 +01:00
if not params[:search].present?
@projects = Project.find(current_user.build_lists.group(:project_id).limit(10).pluck(:project_id))
else
@projects = ProjectPolicy::Scope.new(current_user, Project).membered.search(params[:search]).limit(20)
end
}
end
2011-03-10 14:20:09 +00:00
end
2016-04-29 15:10:30 +01:00
def dashboard
authorize :project
2013-11-13 22:01:12 +00:00
end
2016-04-29 15:10:30 +01:00
def new
2015-12-16 18:36:38 +00:00
authorize :project
2016-04-29 15:10:30 +01:00
@project = Project.new
2015-12-16 18:36:38 +00:00
end
def edit
authorize @project
end
def create
@project = Project.new project_params
@project.owner = choose_owner
authorize @project
2011-11-30 12:58:14 +00:00
if @project.save
flash[:notice] = t('flash.project.saved')
2016-06-15 01:17:42 +01:00
redirect_to project_build_lists_path(@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(project_params)
2014-02-18 19:16:23 +00:00
flash[:notice] = t('flash.project.saved')
2016-04-29 15:10:30 +01:00
redirect_to root_path
2014-02-18 19:16:23 +00:00
else
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(project_params)
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
p_to_r = @project.project_to_repositories.find_by(repository_id: params[:repository_id])
2014-02-18 19:16:23 +00:00
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
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
2016-03-20 09:24:23 +00:00
def commit
redirect_to 'https://github.com/' + @project.github_get_organization + '/' + @project.name + '/commit/' + params[:sha]
2012-08-29 15:58:58 +01:00
end
2016-03-20 09:24:23 +00:00
def diff
redirect_to 'https://github.com/' + @project.github_get_organization + '/' + @project.name + '/compare/' + params[:diff]
end
protected
def project_params
subject_params(Project)
end
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