2014-09-02 18:07:02 +05:30
|
|
|
class Projects::BranchesController < Projects::ApplicationController
|
2015-04-26 12:48:37 +05:30
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2016-09-13 17:45:13 +05:30
|
|
|
include SortingHelper
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
# Authorize
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :require_non_empty_project, except: :create
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_download_code!
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def index
|
2017-09-10 17:25:29 +05:30
|
|
|
@sort = params[:sort].presence || sort_value_recently_updated
|
2018-03-17 18:26:18 +05:30
|
|
|
@branches = BranchesFinder.new(@repository, params.merge(sort: @sort)).execute
|
2017-09-10 17:25:29 +05:30
|
|
|
@branches = Kaminari.paginate_array(@branches).page(params[:page])
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
respond_to do |format|
|
2017-08-17 22:00:37 +05:30
|
|
|
format.html do
|
|
|
|
@refs_pipelines = @project.pipelines.latest_successful_for_refs(@branches.map(&:name))
|
2018-03-17 18:26:18 +05:30
|
|
|
@merged_branch_names =
|
|
|
|
repository.merged_branch_names(@branches.map(&:name))
|
|
|
|
# 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
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
render
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
format.json do
|
2017-08-17 22:00:37 +05:30
|
|
|
render json: @branches.map(&:name)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def recent
|
|
|
|
@branches = @repository.recent_branches
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-04-26 12:48:37 +05:30
|
|
|
branch_name = sanitize(strip_tags(params[:branch_name]))
|
2015-09-11 14:41:01 +05:30
|
|
|
branch_name = Addressable::URI.unescape(branch_name)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
redirect_to_autodeploy = project.empty_repo? && project.deployment_platform.present?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
result = CreateBranchService.new(project, current_user)
|
|
|
|
.execute(branch_name, ref)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
success = (result[:status] == :success)
|
|
|
|
|
|
|
|
if params[:issue_iid] && success
|
2017-01-15 13:20:01 +05:30
|
|
|
issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid])
|
2016-06-02 11:05:42 +05:30
|
|
|
SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2018-03-17 18:26:18 +05:30
|
|
|
if success
|
2017-08-17 22:00:37 +05:30
|
|
|
if redirect_to_autodeploy
|
|
|
|
redirect_to url_to_autodeploy_setup(project, branch_name),
|
|
|
|
notice: view_context.autodeploy_flash_notice(branch_name)
|
|
|
|
else
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_tree_path(@project, branch_name)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
else
|
|
|
|
@error = result[:message]
|
|
|
|
render action: 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
2018-03-17 18:26:18 +05:30
|
|
|
if success
|
2017-09-10 17:25:29 +05:30
|
|
|
render json: { name: branch_name, url: project_tree_url(@project, branch_name) }
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
|
|
|
render json: result[:messsage], status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-09-11 14:41:01 +05:30
|
|
|
@branch_name = Addressable::URI.unescape(params[:id])
|
2017-08-17 22:00:37 +05:30
|
|
|
result = DeleteBranchService.new(project, current_user).execute(@branch_name)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
respond_to do |format|
|
2015-04-26 12:48:37 +05:30
|
|
|
format.html do
|
2017-09-10 17:25:29 +05:30
|
|
|
flash_type = result[:status] == :error ? :alert : :notice
|
|
|
|
flash[flash_type] = result[:message]
|
|
|
|
|
|
|
|
redirect_to project_branches_path(@project), status: 303
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
format.js { render nothing: true, status: result[:return_code] }
|
|
|
|
format.json { render json: { message: result[:message] }, status: result[:return_code] }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def destroy_all_merged
|
|
|
|
DeleteMergedBranchesService.new(@project, current_user).async_execute
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_branches_path(@project),
|
2017-08-17 22:00:37 +05:30
|
|
|
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-06-02 11:05:42 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def ref
|
|
|
|
if params[:ref]
|
|
|
|
ref_escaped = sanitize(strip_tags(params[:ref]))
|
|
|
|
Addressable::URI.unescape(ref_escaped)
|
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
@project.default_branch || 'master'
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def url_to_autodeploy_setup(project, branch_name)
|
2017-09-10 17:25:29 +05:30
|
|
|
project_new_blob_path(
|
2017-08-17 22:00:37 +05:30
|
|
|
project,
|
|
|
|
branch_name,
|
|
|
|
file_name: '.gitlab-ci.yml',
|
|
|
|
commit_message: 'Set up auto deploy',
|
|
|
|
target_branch: branch_name,
|
|
|
|
context: 'autodeploy'
|
|
|
|
)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|