gitlab-org--gitlab-foss/app/controllers/projects/branches_controller.rb

112 lines
3.5 KiB
Ruby
Raw Normal View History

class Projects::BranchesController < Projects::ApplicationController
2014-11-13 10:20:43 -05:00
include ActionView::Helpers::SanitizeHelper
include SortingHelper
# Authorize
before_action :require_non_empty_project, except: :create
before_action :authorize_download_code!
before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
def index
@sort = params[:sort].presence || sort_value_name
@branches = BranchesFinder.new(@repository, params).execute
2016-02-12 13:42:25 -05:00
respond_to do |format|
format.html do
paginate_branches
@refs_pipelines = @project.pipelines.latest_successful_for_refs(@branches.map(&:name))
@max_commits = @branches.reduce(0) do |memo, branch|
diverging_commit_counts = repository.diverging_commit_counts(branch)
[memo, diverging_commit_counts[:behind], diverging_commit_counts[:ahead]].max
end
end
format.json do
paginate_branches unless params[:show_all]
render json: @branches.map(&:name)
end
end
end
2013-08-05 10:59:58 -04:00
def recent
@branches = @repository.recent_branches
end
def create
2014-11-13 10:20:43 -05:00
branch_name = sanitize(strip_tags(params[:branch_name]))
branch_name = Addressable::URI.unescape(branch_name)
2016-02-12 13:42:25 -05:00
2017-02-07 10:59:38 -05:00
redirect_to_autodeploy = project.empty_repo? && project.deployment_services.present?
result = CreateBranchService.new(project, current_user).
2014-11-13 10:20:43 -05:00
execute(branch_name, ref)
2016-02-22 03:20:04 -05:00
if params[:issue_iid]
issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid])
2016-02-22 03:20:04 -05:00
SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
2016-02-17 01:11:48 -05:00
end
if result[:status] == :success
@branch = result[:branch]
2017-02-07 10:59:38 -05:00
if redirect_to_autodeploy
redirect_to(
url_to_autodeploy_setup(project, branch_name),
2017-02-07 10:59:38 -05:00
notice: view_context.autodeploy_flash_notice(branch_name))
else
redirect_to namespace_project_tree_path(@project.namespace, @project,
@branch.name)
end
else
@error = result[:message]
render action: 'new'
end
end
def destroy
@branch_name = Addressable::URI.unescape(params[:id])
status = DeleteBranchService.new(project, current_user).execute(@branch_name)
respond_to do |format|
format.html do
redirect_to namespace_project_branches_path(@project.namespace,
@project), status: 303
end
format.js { render nothing: true, status: status[:return_code] }
end
end
2016-02-12 13:42:25 -05:00
def destroy_all_merged
DeleteMergedBranchesService.new(@project, current_user).async_execute
redirect_to namespace_project_branches_path(@project.namespace, @project),
notice: 'Merged branches are being deleted. This can take some time depending on the number of branches. Please refresh the page to see changes.'
end
2016-02-12 13:42:25 -05:00
private
def ref
if params[:ref]
ref_escaped = sanitize(strip_tags(params[:ref]))
Addressable::URI.unescape(ref_escaped)
else
@project.default_branch || 'master'
2016-02-12 13:42:25 -05:00
end
end
2017-02-07 10:59:38 -05:00
def paginate_branches
@branches = Kaminari.paginate_array(@branches).page(params[:page])
end
2017-02-07 10:59:38 -05:00
def url_to_autodeploy_setup(project, branch_name)
namespace_project_new_blob_path(
project.namespace,
project,
branch_name,
file_name: '.gitlab-ci.yml',
commit_message: 'Set up auto deploy',
target_branch: branch_name,
context: 'autodeploy'
)
end
end