branches_controller.rb 3.84 KB
Newer Older
1
class Projects::BranchesController < Projects::ApplicationController
2
  include ActionView::Helpers::SanitizeHelper
3
  include SortingHelper
4

5
  # Authorize
6
  before_action :require_non_empty_project, except: :create
7
  before_action :authorize_download_code!
8
  before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
9 10

  def index
11
    @sort = params[:sort].presence || sort_value_recently_updated
12
    @branches = BranchesFinder.new(@repository, params.merge(sort: @sort)).execute
13
    @branches = Kaminari.paginate_array(@branches).page(params[:page])
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
14

15
    respond_to do |format|
16
      format.html do
17
        @refs_pipelines = @project.pipelines.latest_successful_for_refs(@branches.map(&:name))
18 19 20 21 22 23 24 25
        # 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
26 27
        end
      end
28
      format.json do
29
        render json: @branches.map(&:name)
30 31
      end
    end
32 33
  end

34 35 36 37
  def recent
    @branches = @repository.recent_branches
  end

38
  def create
39
    branch_name = sanitize(strip_tags(params[:branch_name]))
40
    branch_name = Addressable::URI.unescape(branch_name)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
41

Valery Sizov's avatar
Valery Sizov committed
42
    redirect_to_autodeploy = project.empty_repo? && project.deployment_services.present?
43

44 45
    result = CreateBranchService.new(project, current_user)
        .execute(branch_name, ref)
46

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
47
    if params[:issue_iid]
48
      issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid])
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
49
      SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
50 51
    end

52 53 54 55 56 57 58
    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
59
            redirect_to project_tree_path(@project, branch_name)
60 61 62 63 64 65
          end
        else
          @error = result[:message]
          render action: 'new'
        end
      end
66

67 68
      format.json do
        if result[:status] == :success
69
          render json: { name: branch_name, url: project_tree_url(@project, branch_name) }
70 71 72
        else
          render json: result[:messsage], status: :unprocessable_entity
        end
73
      end
74
    end
75 76 77
  end

  def destroy
78
    @branch_name = Addressable::URI.unescape(params[:id])
79 80
    result = DeleteBranchService.new(project, current_user).execute(@branch_name)

81
    respond_to do |format|
Vinnie Okada's avatar
Vinnie Okada committed
82
      format.html do
83 84 85
        flash_type = result[:status] == :error ? :alert : :notice
        flash[flash_type] = result[:message]

86
        redirect_to project_branches_path(@project), status: 303
Vinnie Okada's avatar
Vinnie Okada committed
87
      end
88 89

      format.js { render nothing: true, status: result[:return_code] }
Fatih Acet's avatar
Fatih Acet committed
90
      format.json { render json: { message: result[:message] }, status: result[:return_code] }
91 92
    end
  end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
93

94 95 96
  def destroy_all_merged
    DeleteMergedBranchesService.new(@project, current_user).async_execute

97
    redirect_to project_branches_path(@project),
98 99 100
      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

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
101 102 103 104 105 106 107
  private

  def ref
    if params[:ref]
      ref_escaped = sanitize(strip_tags(params[:ref]))
      Addressable::URI.unescape(ref_escaped)
    else
108
      @project.default_branch || 'master'
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
109 110
    end
  end
Valery Sizov's avatar
Valery Sizov committed
111 112

  def url_to_autodeploy_setup(project, branch_name)
113
    project_new_blob_path(
Valery Sizov's avatar
Valery Sizov committed
114 115 116 117 118 119 120 121
      project,
      branch_name,
      file_name: '.gitlab-ci.yml',
      commit_message: 'Set up auto deploy',
      target_branch: branch_name,
      context: 'autodeploy'
    )
  end
122
end