merge_requests_controller.rb 2.61 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 :merge_request, :only => [:edit, :update, :destroy, :show, :commits, :diffs]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6 7 8 9 10 11 12
  layout "project"

  # Authorize
  before_filter :add_project_abilities
  before_filter :authorize_read_project!
  before_filter :authorize_write_project!, :only => [:new, :create, :edit, :update]

  def index
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
    @merge_requests = @project.merge_requests
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14 15 16 17 18 19 20 21
  end

  def show
    unless @project.repo.heads.map(&:name).include?(@merge_request.target_branch) && 
      @project.repo.heads.map(&:name).include?(@merge_request.source_branch)
      head(404)and return 
    end

22 23 24 25 26 27 28 29 30 31
    @notes = @merge_request.notes.inc_author.order("created_at DESC").limit(20)
    @note = @project.notes.new(:noteable => @merge_request)

    respond_to do |format|
      format.html
      format.js { respond_with_notes }
    end
  end

  def commits
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32
    @commits = @project.repo.commits_between(@merge_request.target_branch, @merge_request.source_branch).map {|c| Commit.new(c)}
33 34 35 36
    render :template => "merge_requests/_commits", :layout => false
  end

  def diffs
37 38 39
    @diffs = @merge_request.diffs
    @commit = @merge_request.last_commit

40
    render :template => "merge_requests/_diffs", :layout => false
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  end

  def new
    @merge_request = @project.merge_requests.new
  end

  def edit
  end

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

    respond_to do |format|
      if @merge_request.save
        format.html { redirect_to [@project, @merge_request], notice: 'Merge request was successfully created.' }
        format.json { render json: @merge_request, status: :created, location: @merge_request }
      else
        format.html { render action: "new" }
        format.json { render json: @merge_request.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @merge_request.update_attributes(params[:merge_request])
        format.html { redirect_to [@project, @merge_request], notice: 'Merge request was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @merge_request.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @merge_request.destroy

    respond_to do |format|
      format.html { redirect_to project_merge_requests_url(@project) }
      format.json { head :ok }
    end
  end

  protected

  def merge_request
    @merge_request ||= @project.merge_requests.find(params[:id])
  end
end