Read about our upcoming Code of Conduct on this issue

branches_controller.rb 5.6 KB
Newer Older
1
2
# frozen_string_literal: true

3
class Projects::BranchesController < Projects::ApplicationController
4
  include ActionView::Helpers::SanitizeHelper
5
  include SortingHelper
6

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

12
13
  # Support legacy URLs
  before_action :redirect_for_legacy_index_sort_or_search, only: [:index]
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
14

15
  def index
16
    respond_to do |format|
17
      format.html do
18
19
20
21
22
23
24
        @sort = params[:sort].presence || sort_value_recently_updated
        @mode = params[:state].presence || 'overview'
        @overview_max_branches = 5

        # Fetch branches for the specified mode
        fetch_branches_by_mode

25
        @refs_pipelines = @project.ci_pipelines.latest_successful_for_refs(@branches.map(&:name))
26
        @merged_branch_names = repository.merged_branch_names(@branches.map(&:name))
27
28
29
30
31
32
33

        # n+1: https://gitlab.com/gitlab-org/gitaly/issues/992
        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
34
        end
35

36
37
38
39
        # https://gitlab.com/gitlab-org/gitlab-ce/issues/48097
        Gitlab::GitalyClient.allow_n_plus_1_calls do
          render
        end
40
      end
41
      format.json do
42
43
44
        branches = BranchesFinder.new(@repository, params).execute
        branches = Kaminari.paginate_array(branches).page(params[:page])
        render json: branches.map(&:name)
45
46
      end
    end
47
48
  end

49
50
51
52
  def recent
    @branches = @repository.recent_branches
  end

53
  # rubocop: disable CodeReuse/ActiveRecord
54
  def create
55
    branch_name = sanitize(strip_tags(params[:branch_name]))
56
    branch_name = Addressable::URI.unescape(branch_name)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
57

58
    redirect_to_autodeploy = project.empty_repo? && project.deployment_platform.present?
59

60
61
    result = CreateBranchService.new(project, current_user)
        .execute(branch_name, ref)
62

63
64
65
    success = (result[:status] == :success)

    if params[:issue_iid] && success
66
      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
67
      SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
68
69
    end

70
71
    respond_to do |format|
      format.html do
72
        if success
73
74
75
76
          if redirect_to_autodeploy
            redirect_to url_to_autodeploy_setup(project, branch_name),
              notice: view_context.autodeploy_flash_notice(branch_name)
          else
77
            redirect_to project_tree_path(@project, branch_name)
78
79
80
81
82
83
          end
        else
          @error = result[:message]
          render action: 'new'
        end
      end
84

85
      format.json do
86
        if success
87
          render json: { name: branch_name, url: project_tree_url(@project, branch_name) }
88
89
90
        else
          render json: result[:messsage], status: :unprocessable_entity
        end
91
      end
92
    end
93
  end
94
  # rubocop: enable CodeReuse/ActiveRecord
95
96

  def destroy
97
    @branch_name = Addressable::URI.unescape(params[:id])
98
99
    result = DeleteBranchService.new(project, current_user).execute(@branch_name)

100
    respond_to do |format|
Vinnie Okada's avatar
Vinnie Okada committed
101
      format.html do
102
103
104
        flash_type = result[:status] == :error ? :alert : :notice
        flash[flash_type] = result[:message]

Lin Jen-Shin's avatar
Lin Jen-Shin committed
105
        redirect_to project_branches_path(@project), status: :see_other
Vinnie Okada's avatar
Vinnie Okada committed
106
      end
107

108
      format.js { head result[:return_code] }
Fatih Acet's avatar
Fatih Acet committed
109
      format.json { render json: { message: result[:message] }, status: result[:return_code] }
110
111
    end
  end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
112

113
114
115
  def destroy_all_merged
    DeleteMergedBranchesService.new(@project, current_user).async_execute

116
    redirect_to project_branches_path(@project),
117
118
119
      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
120
121
122
123
124
125
126
  private

  def ref
    if params[:ref]
      ref_escaped = sanitize(strip_tags(params[:ref]))
      Addressable::URI.unescape(ref_escaped)
    else
127
      @project.default_branch || 'master'
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
128
129
    end
  end
Valery Sizov's avatar
Valery Sizov committed
130
131

  def url_to_autodeploy_setup(project, branch_name)
132
    project_new_blob_path(
Valery Sizov's avatar
Valery Sizov committed
133
134
135
136
137
138
139
140
      project,
      branch_name,
      file_name: '.gitlab-ci.yml',
      commit_message: 'Set up auto deploy',
      target_branch: branch_name,
      context: 'autodeploy'
    )
  end
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

  def redirect_for_legacy_index_sort_or_search
    # Normalize a legacy URL with redirect
    if request.format != :json && !params[:state].presence && [:sort, :search, :page].any? { |key| params[key].presence }
      redirect_to project_branches_filtered_path(@project, state: 'all'), notice: 'Update your bookmarked URLs as filtered/sorted branches URL has been changed.'
    end
  end

  def fetch_branches_by_mode
    if @mode == 'overview'
      # overview mode
      @active_branches, @stale_branches = BranchesFinder.new(@repository, sort: sort_value_recently_updated).execute.partition(&:active?)
      # Here we get one more branch to indicate if there are more data we're not showing
      @active_branches = @active_branches.first(@overview_max_branches + 1)
      @stale_branches = @stale_branches.first(@overview_max_branches + 1)
      @branches = @active_branches + @stale_branches
    else
      # active/stale/all view mode
      @branches = BranchesFinder.new(@repository, params.merge(sort: @sort)).execute
      @branches = @branches.select { |b| b.state.to_s == @mode } if %w[active stale].include?(@mode)
      @branches = Kaminari.paginate_array(@branches).page(params[:page])
    end
  end
164
end