BigW Consortium Gitlab

branches_controller.rb 2.62 KB
Newer Older
1
class Projects::BranchesController < Projects::ApplicationController
2
  include ActionView::Helpers::SanitizeHelper
3
  include SortingHelper
4
  # Authorize
5 6
  before_action :require_non_empty_project
  before_action :authorize_download_code!
7
  before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
8 9

  def index
10
    @sort = params[:sort].presence || sort_value_name
11
    @branches = BranchesFinder.new(@repository, params).execute
12
    @branches = Kaminari.paginate_array(@branches).page(params[:page])
13

14
    @max_commits = @branches.reduce(0) do |memo, branch|
15 16 17
      diverging_commit_counts = repository.diverging_commit_counts(branch)
      [memo, diverging_commit_counts[:behind], diverging_commit_counts[:ahead]].max
    end
18 19 20 21

    respond_to do |format|
      format.html
      format.json do
Connor Shea committed
22
        render json: @repository.branch_names
23 24
      end
    end
25 26
  end

27 28 29 30
  def recent
    @branches = @repository.recent_branches
  end

31
  def create
32
    branch_name = sanitize(strip_tags(params[:branch_name]))
33
    branch_name = Addressable::URI.unescape(branch_name)
34

35
    result = CreateBranchService.new(project, current_user).
36
        execute(branch_name, ref)
37

38
    if params[:issue_iid]
39
      issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid])
40
      SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
41 42
    end

43 44
    if result[:status] == :success
      @branch = result[:branch]
Vinnie Okada committed
45 46
      redirect_to namespace_project_tree_path(@project.namespace, @project,
                                              @branch.name)
47 48 49 50
    else
      @error = result[:message]
      render action: 'new'
    end
51 52 53
  end

  def destroy
54 55
    @branch_name = Addressable::URI.unescape(params[:id])
    status = DeleteBranchService.new(project, current_user).execute(@branch_name)
56
    respond_to do |format|
Vinnie Okada committed
57 58
      format.html do
        redirect_to namespace_project_branches_path(@project.namespace,
59
                                                    @project), status: 303
Vinnie Okada committed
60
      end
61
      format.js { render nothing: true, status: status[:return_code] }
62 63
    end
  end
64

65 66 67 68 69 70 71
  def destroy_all_merged
    DeleteMergedBranchesService.new(@project, current_user).async_execute

    redirect_to namespace_project_branches_path(@project.namespace, @project),
      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

72 73 74 75 76 77 78 79 80 81
  private

  def ref
    if params[:ref]
      ref_escaped = sanitize(strip_tags(params[:ref]))
      Addressable::URI.unescape(ref_escaped)
    else
      @project.default_branch
    end
  end
82
end