merge_requests_controller.rb 4.28 KB
Newer Older
1
class MergeRequestsController < ProjectResourceController
2
  before_filter :module_enabled
3
  before_filter :merge_request, only: [:edit, :update, :show, :commits, :diffs, :automerge, :automerge_check, :ci_status]
4
  before_filter :validates_merge_request, only: [:show, :diffs]
5
  before_filter :define_show_vars, only: [:show, :diffs]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6 7 8 9 10

  # Allow read any merge_request
  before_filter :authorize_read_merge_request!

  # Allow write(create) merge_request
11
  before_filter :authorize_write_merge_request!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13

  # Allow modify merge_request
14
  before_filter :authorize_modify_merge_request!, only: [:close, :edit, :update, :sort]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16
  def index
17
    @merge_requests = MergeRequestsLoadContext.new(project, current_user, params).execute
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18 19 20
  end

  def show
Riyad Preukschas's avatar
Riyad Preukschas committed
21 22 23
    @target_type = :merge_request
    @target_id = @merge_request.id

24 25
    respond_to do |format|
      format.html
26
      format.js
27

28 29 30
      format.diff  { render text: @merge_request.to_diff }
      format.patch { render text: @merge_request.to_patch }
    end
randx's avatar
randx committed
31 32
  end

33
  def diffs
34 35
    @diffs = @merge_request.diffs
    @commit = @merge_request.last_commit
36

37 38 39
    @comments_allowed = @reply_allowed = true
    @comments_target  = { noteable_type: 'MergeRequest',
                          noteable_id: @merge_request.id }
40
    @line_notes = @merge_request.notes.where("line_code is not null")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41 42 43
  end

  def new
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44
    @merge_request = @project.merge_requests.new(params[:merge_request])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
45 46 47 48 49 50 51 52 53
  end

  def edit
  end

  def create
    @merge_request = @project.merge_requests.new(params[:merge_request])
    @merge_request.author = current_user

54 55 56 57
    if @merge_request.save
      @merge_request.reload_code
      redirect_to [@project, @merge_request], notice: 'Merge request was successfully created.'
    else
58
      render action: "new"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59 60 61 62
    end
  end

  def update
63
    if @merge_request.update_attributes(params[:merge_request].merge(author_id_of_changes: current_user.id))
64 65 66 67 68
      @merge_request.reload_code
      @merge_request.mark_as_unchecked
      redirect_to [@project, @merge_request], notice: 'Merge request was successfully updated.'
    else
      render action: "edit"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
69 70 71
    end
  end

Valery Sizov's avatar
Valery Sizov committed
72
  def automerge_check
73
    if @merge_request.unchecked?
Valery Sizov's avatar
Valery Sizov committed
74 75
      @merge_request.check_if_can_be_merged
    end
Andrew8xx8's avatar
Andrew8xx8 committed
76
    render json: {merge_status: @merge_request.human_merge_status}
77
  rescue Gitlab::SatelliteNotExistError
Andrew8xx8's avatar
Andrew8xx8 committed
78
    render json: {merge_status: :no_satellite}
Valery Sizov's avatar
Valery Sizov committed
79 80
  end

randx's avatar
randx committed
81
  def automerge
82
    return access_denied! unless can?(current_user, :accept_mr, @project)
83
    if @merge_request.opened? && @merge_request.can_be_merged?
84
      @merge_request.should_remove_source_branch = params[:should_remove_source_branch]
85 86 87 88 89
      @merge_request.automerge!(current_user)
      @status = true
    else
      @status = false
    end
randx's avatar
randx committed
90 91
  end

92
  def branch_from
93
    @commit = @repository.commit(params[:ref])
Cyril's avatar
Cyril committed
94
    @commit = CommitDecorator.decorate(@commit)
95 96 97
  end

  def branch_to
98
    @commit = @repository.commit(params[:ref])
Cyril's avatar
Cyril committed
99
    @commit = CommitDecorator.decorate(@commit)
100 101
  end

102 103 104 105 106 107 108
  def ci_status
    status = project.gitlab_ci_service.commit_status(merge_request.last_commit.sha)
    response = { status: status }

    render json: response
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
109 110 111 112 113
  protected

  def merge_request
    @merge_request ||= @project.merge_requests.find(params[:id])
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
114 115

  def authorize_modify_merge_request!
116
    return render_404 unless can?(current_user, :modify_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
117 118 119
  end

  def authorize_admin_merge_request!
120
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
121
  end
122 123 124 125

  def module_enabled
    return render_404 unless @project.merge_requests_enabled
  end
126 127 128

  def validates_merge_request
    # Show git not found page if target branch doesnt exist
129
    return git_not_found! unless @project.repo.heads.map(&:name).include?(@merge_request.target_branch)
130 131 132 133 134 135 136 137

    # Show git not found page if source branch doesnt exist
    # and there is no saved commits between source & target branch
    return git_not_found! if !@project.repo.heads.map(&:name).include?(@merge_request.source_branch) && @merge_request.commits.blank?
  end

  def define_show_vars
    # Build a note object for comment form
138
    @note = @project.notes.new(noteable: @merge_request)
139

140
    # Get commits from repository
141 142
    # or from cache if already merged
    @commits = @merge_request.commits
143
    @commits = CommitDecorator.decorate(@commits)
144
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
145
end