branches_controller.rb 3.93 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
        @merged_branch_names =
          repository.merged_branch_names(@branches.map(&:name))
20 21 22 23 24 25 26 27
        # 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
28 29
        end
      end
30
      format.json do
31
        render json: @branches.map(&:name)
32 33
      end
    end
34 35
  end

36 37 38 39
  def recent
    @branches = @repository.recent_branches
  end

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

44
    redirect_to_autodeploy = project.empty_repo? && project.deployment_platform.present?
45

46 47
    result = CreateBranchService.new(project, current_user)
        .execute(branch_name, ref)
48

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
49
    if params[:issue_iid]
50
      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
51
      SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
52 53
    end

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

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

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

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

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

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

96 97 98
  def destroy_all_merged
    DeleteMergedBranchesService.new(@project, current_user).async_execute

99
    redirect_to project_branches_path(@project),
100 101 102
      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
103 104 105 106 107 108 109
  private

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

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