2013-06-23 12:47:22 -04:00
|
|
|
class Projects::ProtectedBranchesController < Projects::ApplicationController
|
2012-02-15 15:02:33 -05:00
|
|
|
# Authorize
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_admin_project!
|
2016-06-16 03:33:30 -04:00
|
|
|
before_action :load_protected_branch, only: [:show, :update, :destroy]
|
2016-07-29 02:13:07 -04:00
|
|
|
before_action :load_protected_branches, only: [:index]
|
2012-02-15 15:02:33 -05:00
|
|
|
|
2014-05-23 07:25:55 -04:00
|
|
|
layout "project_settings"
|
2012-02-15 16:51:04 -05:00
|
|
|
|
2012-02-15 15:02:33 -05:00
|
|
|
def index
|
|
|
|
@protected_branch = @project.protected_branches.new
|
2016-08-16 01:09:13 -04:00
|
|
|
load_gon_index
|
2012-02-15 15:02:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-08-16 01:09:13 -04:00
|
|
|
@protected_branch = ::ProtectedBranches::CreateService.new(@project, current_user, protected_branch_params).execute
|
2016-07-29 02:13:07 -04:00
|
|
|
if @protected_branch.persisted?
|
2016-07-07 03:36:28 -04:00
|
|
|
redirect_to namespace_project_protected_branches_path(@project.namespace, @project)
|
|
|
|
else
|
2016-07-29 02:13:07 -04:00
|
|
|
load_protected_branches
|
2016-08-16 01:09:13 -04:00
|
|
|
load_gon_index
|
2016-07-07 03:36:28 -04:00
|
|
|
render :index
|
|
|
|
end
|
2012-02-15 15:02:33 -05:00
|
|
|
end
|
|
|
|
|
2016-06-16 03:33:30 -04:00
|
|
|
def show
|
|
|
|
@matching_branches = @protected_branch.matching(@project.repository.branches)
|
|
|
|
end
|
2014-12-26 05:39:12 -05:00
|
|
|
|
2016-06-16 03:33:30 -04:00
|
|
|
def update
|
2016-08-16 01:09:13 -04:00
|
|
|
@protected_branch = ::ProtectedBranches::UpdateService.new(@project, current_user, protected_branch_params).execute(@protected_branch)
|
2016-07-07 03:36:28 -04:00
|
|
|
|
2016-07-29 02:13:07 -04:00
|
|
|
if @protected_branch.valid?
|
2014-12-26 09:37:04 -05:00
|
|
|
respond_to do |format|
|
2016-07-29 02:13:07 -04:00
|
|
|
format.json { render json: @protected_branch, status: :ok }
|
2014-12-26 09:37:04 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
2016-07-29 02:13:07 -04:00
|
|
|
format.json { render json: @protected_branch.errors, status: :unprocessable_entity }
|
2014-12-26 09:37:04 -05:00
|
|
|
end
|
2014-12-26 05:39:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 15:02:33 -05:00
|
|
|
def destroy
|
2016-06-16 03:33:30 -04:00
|
|
|
@protected_branch.destroy
|
2012-02-15 16:51:04 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
2015-01-24 13:02:58 -05:00
|
|
|
format.html { redirect_to namespace_project_protected_branches_path }
|
2016-03-15 21:16:25 -04:00
|
|
|
format.js { head :ok }
|
2012-02-15 16:51:04 -05:00
|
|
|
end
|
2012-02-15 15:02:33 -05:00
|
|
|
end
|
2014-06-26 08:00:09 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-16 03:33:30 -04:00
|
|
|
def load_protected_branch
|
|
|
|
@protected_branch = @project.protected_branches.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2014-06-26 08:00:09 -04:00
|
|
|
def protected_branch_params
|
2016-07-29 02:13:07 -04:00
|
|
|
params.require(:protected_branch).permit(:name,
|
2016-08-16 01:09:13 -04:00
|
|
|
merge_access_levels_attributes: [:access_level, :id],
|
|
|
|
push_access_levels_attributes: [:access_level, :id])
|
2014-06-26 08:00:09 -04:00
|
|
|
end
|
2016-07-07 03:36:28 -04:00
|
|
|
|
|
|
|
def load_protected_branches
|
|
|
|
@protected_branches = @project.protected_branches.order(:name).page(params[:page])
|
|
|
|
end
|
2016-07-29 02:13:07 -04:00
|
|
|
|
2016-08-16 01:09:13 -04:00
|
|
|
def access_levels_options
|
|
|
|
{
|
|
|
|
push_access_levels: ProtectedBranch::PushAccessLevel.human_access_levels.map { |id, text| { id: id, text: text, before_divider: true } },
|
|
|
|
merge_access_levels: ProtectedBranch::MergeAccessLevel.human_access_levels.map { |id, text| { id: id, text: text, before_divider: true } }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_gon_index
|
|
|
|
params = { open_branches: @project.open_branches.map { |br| { text: br.name, id: br.name, title: br.name } } }
|
|
|
|
gon.push(params.merge(access_levels_options))
|
2016-07-29 02:13:07 -04:00
|
|
|
end
|
2012-02-15 15:02:33 -05:00
|
|
|
end
|