gitlabhq/app/controllers/merge_requests_controller.rb

127 lines
3.6 KiB
Ruby
Raw Normal View History

2011-11-28 08:39:43 +01:00
class MergeRequestsController < ApplicationController
before_filter :authenticate_user!
before_filter :project
before_filter :module_enabled
2011-11-28 21:16:57 +01:00
before_filter :merge_request, :only => [:edit, :update, :destroy, :show, :commits, :diffs]
2011-11-28 08:39:43 +01:00
layout "project"
# Authorize
before_filter :add_project_abilities
2011-12-15 22:57:46 +01:00
# 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]
2011-11-28 08:39:43 +01:00
def index
2011-11-28 19:30:35 +01:00
@merge_requests = @project.merge_requests
2012-01-21 22:17:29 +01:00
@merge_requests = case params[:f].to_i
2012-02-18 13:12:48 +01:00
when 1 then @merge_requests
2012-01-21 22:17:29 +01:00
when 2 then @merge_requests.closed
2012-02-18 13:12:48 +01:00
when 2 then @merge_requests.opened.assigned(current_user)
2012-01-21 22:17:29 +01:00
else @merge_requests.opened
end
@merge_requests = @merge_requests.includes(:author, :project).order("created_at desc")
2011-11-28 08:39:43 +01:00
end
def show
unless @project.repo.heads.map(&:name).include?(@merge_request.target_branch) &&
@project.repo.heads.map(&:name).include?(@merge_request.source_branch)
git_not_found! and return
2011-11-28 08:39:43 +01:00
end
2011-11-28 21:16:57 +01:00
@note = @project.notes.new(:noteable => @merge_request)
@commits = @project.repo.
commits_between(@merge_request.target_branch, @merge_request.source_branch).
map {|c| Commit.new(c)}.
sort_by(&:created_at).
reverse
2012-01-28 15:47:55 +01:00
render_full_content
2011-11-28 21:16:57 +01:00
respond_to do |format|
format.html
2012-02-24 08:16:06 +01:00
format.js
2011-11-28 21:16:57 +01:00
end
end
def diffs
2011-11-29 19:06:37 +01:00
@diffs = @merge_request.diffs
@commit = @merge_request.last_commit
@comments_allowed = true
@line_notes = @merge_request.notes.where("line_code is not null")
2011-11-28 08:39:43 +01:00
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].merge(:author_id_of_changes => current_user.id))
2011-11-28 08:39:43 +01:00
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
2011-12-15 22:57:46 +01:00
def authorize_modify_merge_request!
return render_404 unless can?(current_user, :modify_merge_request, @merge_request)
2011-12-15 22:57:46 +01:00
end
def authorize_admin_merge_request!
return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
2011-12-15 22:57:46 +01:00
end
def module_enabled
return render_404 unless @project.merge_requests_enabled
end
2011-11-28 08:39:43 +01:00
end