2014-09-02 18:07:02 +05:30
|
|
|
class Projects::ProtectedBranchesController < Projects::ApplicationController
|
|
|
|
# Authorize
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_admin_project!
|
2016-08-24 12:49:21 +05:30
|
|
|
before_action :load_protected_branch, only: [:show, :update, :destroy]
|
2016-09-13 17:45:13 +05:30
|
|
|
before_action :load_protected_branches, only: [:index]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
layout "project_settings"
|
|
|
|
|
|
|
|
def index
|
|
|
|
@protected_branch = @project.protected_branches.new
|
2016-09-13 17:45:13 +05:30
|
|
|
load_gon_index
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-09-13 17:45:13 +05:30
|
|
|
@protected_branch = ::ProtectedBranches::CreateService.new(@project, current_user, protected_branch_params).execute
|
|
|
|
if @protected_branch.persisted?
|
|
|
|
redirect_to namespace_project_protected_branches_path(@project.namespace, @project)
|
|
|
|
else
|
|
|
|
load_protected_branches
|
|
|
|
load_gon_index
|
|
|
|
render :index
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def show
|
|
|
|
@matching_branches = @protected_branch.matching(@project.repository.branches)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def update
|
2016-09-13 17:45:13 +05:30
|
|
|
@protected_branch = ::ProtectedBranches::UpdateService.new(@project, current_user, protected_branch_params).execute(@protected_branch)
|
|
|
|
|
|
|
|
if @protected_branch.valid?
|
2015-04-26 12:48:37 +05:30
|
|
|
respond_to do |format|
|
2016-08-24 12:49:21 +05:30
|
|
|
format.json { render json: @protected_branch, status: :ok }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
2016-08-24 12:49:21 +05:30
|
|
|
format.json { render json: @protected_branch.errors, status: :unprocessable_entity }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2016-08-24 12:49:21 +05:30
|
|
|
@protected_branch.destroy
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
respond_to do |format|
|
2015-04-26 12:48:37 +05:30
|
|
|
format.html { redirect_to namespace_project_protected_branches_path }
|
2016-06-02 11:05:42 +05:30
|
|
|
format.js { head :ok }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def load_protected_branch
|
|
|
|
@protected_branch = @project.protected_branches.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def protected_branch_params
|
2016-09-13 17:45:13 +05:30
|
|
|
params.require(:protected_branch).permit(:name,
|
|
|
|
merge_access_levels_attributes: [:access_level, :id],
|
|
|
|
push_access_levels_attributes: [:access_level, :id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_protected_branches
|
|
|
|
@protected_branches = @project.protected_branches.order(:name).page(params[:page])
|
|
|
|
end
|
|
|
|
|
|
|
|
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))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|