BigW Consortium Gitlab

creates_commit.rb 3.25 KB
Newer Older
1 2 3 4
module CreatesCommit
  extend ActiveSupport::Concern

  def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil)
5 6 7 8 9 10 11 12 13
    if can?(current_user, :push_code, @project)
      @project_to_commit_into = @project
      @branch_name ||= @ref
    else
      @project_to_commit_into = current_user.fork_of(@project)
      @branch_name ||= @project_to_commit_into.repository.next_branch('patch')
    end

    @start_branch ||= @ref || @branch_name
14 15

    commit_params = @commit_params.merge(
16 17 18
      start_project: @project,
      start_branch: @start_branch,
      branch_name: @branch_name
19 20
    )

21
    result = service.new(@project_to_commit_into, current_user, commit_params).execute
22 23

    if result[:status] == :success
24
      update_flash_notice(success_notice)
25

26 27
      success_path = final_success_path(success_path)

28
      respond_to do |format|
29 30
        format.html { redirect_to success_path }
        format.json { render json: { message: "success", filePath: success_path } }
31 32 33
      end
    else
      flash[:alert] = result[:message]
34 35
      failure_path = failure_path.call if failure_path.respond_to?(:call)

36 37 38 39 40 41 42 43 44 45 46 47 48 49
      respond_to do |format|
        format.html do
          if failure_view
            render failure_view
          else
            redirect_to failure_path
          end
        end
        format.json { render json: { message: "failed", filePath: failure_path } }
      end
    end
  end

  def authorize_edit_tree!
50
    return if can_collaborate_with_project?
51 52 53 54 55 56

    access_denied!
  end

  private

57 58 59 60 61 62 63 64 65 66 67 68 69 70
  def update_flash_notice(success_notice)
    flash[:notice] = success_notice || "Your changes have been successfully committed."

    if create_merge_request?
      if merge_request_exists?
        flash[:notice] = nil
      else
        target = different_project? ? "project" : "branch"
        flash[:notice] << " You can now submit a merge request to get this change into the original #{target}."
      end
    end
  end

  def final_success_path(success_path)
71 72 73 74
    if create_merge_request?
      merge_request_exists? ? existing_merge_request_path : new_merge_request_path
    else
      success_path = success_path.call if success_path.respond_to?(:call)
75

76 77
      success_path
    end
78 79
  end

80
  def new_merge_request_path
81
    project_new_merge_request_path(
82
      @project_to_commit_into,
83
      merge_request: {
84 85 86 87
        source_project_id: @project_to_commit_into.id,
        target_project_id: @project.id,
        source_branch: @branch_name,
        target_branch: @start_branch
88 89 90 91
      }
    )
  end

92
  def existing_merge_request_path
93
    project_merge_request_path(@project, @merge_request)
94 95 96
  end

  def merge_request_exists?
97 98
    return @merge_request if defined?(@merge_request)

99 100
    @merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened
      .find_by(source_project_id: @project_to_commit_into, source_branch: @branch_name, target_branch: @start_branch)
101 102
  end

103
  def different_project?
104
    @project_to_commit_into != @project
105 106 107
  end

  def create_merge_request?
108
    # Even if the field is set, if we're checking the same branch
109 110 111
    # as the target branch in the same project,
    # we don't want to create a merge request.
    params[:create_merge_request].present? &&
112
      (different_project? || @start_branch != @branch_name)
113
  end
114
end