merge_requests_controller.rb 4.16 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3
class MergeRequestsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :project
4
  before_filter :module_enabled
Valery Sizov's avatar
Valery Sizov committed
5
  before_filter :merge_request, :only => [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6 7 8 9
  layout "project"

  # Authorize
  before_filter :add_project_abilities
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10 11 12 13 14 15 16 17 18 19 20 21

  # Allow read any merge_request
  before_filter :authorize_read_merge_request!

  # Allow write(create) merge_request
  before_filter :authorize_write_merge_request!, :only => [:new, :create]

  # Allow modify merge_request
  before_filter :authorize_modify_merge_request!, :only => [:close, :edit, :update, :sort]

  # Allow destroy merge_request
  before_filter :authorize_admin_merge_request!, :only => [:destroy]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22 23

  def index
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
24
    @merge_requests = @project.merge_requests
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25 26

    @merge_requests = case params[:f].to_i
27
                      when 1 then @merge_requests
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28
                      when 2 then @merge_requests.closed
Andrey Janzen's avatar
Andrey Janzen committed
29
                      when 3 then @merge_requests.opened.assigned(current_user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
30
                      else @merge_requests.opened
31
                      end.page(params[:page]).per(20)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32

33
    @merge_requests = @merge_requests.includes(:author, :project).order("closed, created_at desc")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
34 35 36
  end

  def show
37 38 39 40 41 42 43 44
    # Show git not found page if target branch doesnt exist
    return git_not_found! unless @project.repo.heads.map(&:name).include?(@merge_request.target_branch) 

    # 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?
    
    # Build a note object for comment form
45 46
    @note = @project.notes.new(:noteable => @merge_request)

47 48 49
    # Get commits from repository 
    # or from cache if already merged
    @commits = @merge_request.commits
50

51 52
    respond_to do |format|
      format.html
53
      format.js
54 55 56 57
    end
  end

  def diffs
58 59
    @diffs = @merge_request.diffs
    @commit = @merge_request.last_commit
60 61 62

    @comments_allowed = true
    @line_notes = @merge_request.notes.where("line_code is not null")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
63 64 65
  end

  def new
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
66
    @merge_request = @project.merge_requests.new(params[:merge_request])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67 68 69 70 71 72 73 74 75
  end

  def edit
  end

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

76 77 78 79 80
    if @merge_request.save
      @merge_request.reload_code
      redirect_to [@project, @merge_request], notice: 'Merge request was successfully created.'
    else
      render action: "new" 
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
81 82 83 84
    end
  end

  def update
85 86 87 88 89 90
    if @merge_request.update_attributes(params[:merge_request].merge(:author_id_of_changes => current_user.id))
      @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
91 92 93
    end
  end

Valery Sizov's avatar
Valery Sizov committed
94 95 96 97 98 99 100
  def automerge_check
    if @merge_request.unchecked? 
      @merge_request.check_if_can_be_merged
    end
    render :json => {:state => @merge_request.human_state}
  end

randx's avatar
randx committed
101
  def automerge
102
    return access_denied! unless can?(current_user, :accept_mr, @project)
103
    if @merge_request.open? && @merge_request.can_be_merged?
104
      @merge_request.should_remove_source_branch = params[:should_remove_source_branch]
105 106 107 108 109
      @merge_request.automerge!(current_user)
      @status = true
    else
      @status = false
    end
randx's avatar
randx committed
110 111
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
112 113 114 115 116 117 118 119
  def destroy
    @merge_request.destroy

    respond_to do |format|
      format.html { redirect_to project_merge_requests_url(@project) }
    end
  end

120 121 122 123 124 125 126 127
  def branch_from
    @commit = project.commit(params[:ref])
  end

  def branch_to
    @commit = project.commit(params[:ref])
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
128 129 130 131 132
  protected

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

  def authorize_modify_merge_request!
135
    return render_404 unless can?(current_user, :modify_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
136 137 138
  end

  def authorize_admin_merge_request!
139
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
140
  end
141 142 143 144

  def module_enabled
    return render_404 unless @project.merge_requests_enabled
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
145
end