2013-07-16 15:19:07 -04:00
|
|
|
class Projects::BranchesController < Projects::ApplicationController
|
2014-11-13 10:20:43 -05:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2016-08-10 15:15:22 -04:00
|
|
|
include SortingHelper
|
2017-02-06 11:59:54 -05:00
|
|
|
|
2013-07-16 15:19:07 -04:00
|
|
|
# Authorize
|
2017-02-06 11:59:54 -05:00
|
|
|
before_action :require_non_empty_project, except: :create
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :authorize_download_code!
|
2016-09-21 10:15:12 -04:00
|
|
|
before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
|
2013-07-16 15:19:07 -04:00
|
|
|
|
|
|
|
def index
|
2017-01-20 10:55:32 -05:00
|
|
|
@sort = params[:sort].presence || sort_value_recently_updated
|
2016-07-07 12:19:21 -04:00
|
|
|
@branches = BranchesFinder.new(@repository, params).execute
|
2017-06-08 11:33:25 -04:00
|
|
|
@branches = Kaminari.paginate_array(@branches).page(params[:page])
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2016-08-01 15:56:56 -04:00
|
|
|
respond_to do |format|
|
2016-12-22 01:21:40 -05:00
|
|
|
format.html do
|
2017-03-13 12:00:07 -04:00
|
|
|
@refs_pipelines = @project.pipelines.latest_successful_for_refs(@branches.map(&:name))
|
2017-09-19 06:55:37 -04:00
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37429
|
|
|
|
Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
@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
|
|
|
|
|
|
|
|
render
|
2016-12-22 01:21:40 -05:00
|
|
|
end
|
|
|
|
end
|
2016-08-01 15:56:56 -04:00
|
|
|
format.json do
|
2017-03-05 14:45:19 -05:00
|
|
|
render json: @branches.map(&:name)
|
2016-08-01 15:56:56 -04:00
|
|
|
end
|
|
|
|
end
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|
|
|
|
|
2013-08-05 10:59:58 -04:00
|
|
|
def recent
|
|
|
|
@branches = @repository.recent_branches
|
|
|
|
end
|
|
|
|
|
2013-07-16 15:19:07 -04:00
|
|
|
def create
|
2014-11-13 10:20:43 -05:00
|
|
|
branch_name = sanitize(strip_tags(params[:branch_name]))
|
2015-08-02 02:33:33 -04:00
|
|
|
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?
|
2017-02-06 11:59:54 -05:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
result = CreateBranchService.new(project, current_user)
|
|
|
|
.execute(branch_name, ref)
|
2014-10-30 11:28:59 -04:00
|
|
|
|
2016-02-22 03:20:04 -05:00
|
|
|
if params[:issue_iid]
|
2016-11-18 08:51:52 -05:00
|
|
|
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
|
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
if result[:status] == :success
|
|
|
|
if redirect_to_autodeploy
|
|
|
|
redirect_to url_to_autodeploy_setup(project, branch_name),
|
|
|
|
notice: view_context.autodeploy_flash_notice(branch_name)
|
|
|
|
else
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_tree_path(@project, branch_name)
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
@error = result[:message]
|
|
|
|
render action: 'new'
|
|
|
|
end
|
|
|
|
end
|
2017-02-06 11:59:54 -05:00
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
format.json do
|
|
|
|
if result[:status] == :success
|
2017-06-29 13:06:35 -04:00
|
|
|
render json: { name: branch_name, url: project_tree_url(@project, branch_name) }
|
2017-05-04 04:09:21 -04:00
|
|
|
else
|
|
|
|
render json: result[:messsage], status: :unprocessable_entity
|
|
|
|
end
|
2017-02-06 11:59:54 -05:00
|
|
|
end
|
2014-07-27 10:40:00 -04:00
|
|
|
end
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-08-02 02:33:33 -04:00
|
|
|
@branch_name = Addressable::URI.unescape(params[:id])
|
2017-05-08 03:41:58 -04:00
|
|
|
result = DeleteBranchService.new(project, current_user).execute(@branch_name)
|
|
|
|
|
2013-07-16 15:19:07 -04:00
|
|
|
respond_to do |format|
|
2015-01-24 13:02:58 -05:00
|
|
|
format.html do
|
2017-05-08 03:41:58 -04:00
|
|
|
flash_type = result[:status] == :error ? :alert : :notice
|
|
|
|
flash[flash_type] = result[:message]
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_branches_path(@project), status: 303
|
2015-01-24 13:02:58 -05:00
|
|
|
end
|
2017-05-08 03:41:58 -04:00
|
|
|
|
|
|
|
format.js { render nothing: true, status: result[:return_code] }
|
2017-05-09 00:15:34 -04:00
|
|
|
format.json { render json: { message: result[:message] }, status: result[:return_code] }
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2016-09-21 10:15:12 -04:00
|
|
|
def destroy_all_merged
|
|
|
|
DeleteMergedBranchesService.new(@project, current_user).async_execute
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_branches_path(@project),
|
2016-09-21 10:15:12 -04:00
|
|
|
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
|
2017-02-06 11:59:54 -05:00
|
|
|
@project.default_branch || 'master'
|
2016-02-12 13:42:25 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-07 10:59:38 -05:00
|
|
|
|
|
|
|
def url_to_autodeploy_setup(project, branch_name)
|
2017-06-29 13:06:35 -04:00
|
|
|
project_new_blob_path(
|
2017-02-07 10:59:38 -05:00
|
|
|
project,
|
|
|
|
branch_name,
|
|
|
|
file_name: '.gitlab-ci.yml',
|
|
|
|
commit_message: 'Set up auto deploy',
|
|
|
|
target_branch: branch_name,
|
|
|
|
context: 'autodeploy'
|
|
|
|
)
|
|
|
|
end
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|