socialforge/app/controllers/pull_requests_controller.rb

249 lines
9.5 KiB
Ruby
Raw Normal View History

2016-08-05 15:23:52 +08:00
class PullRequestsController < ApplicationController
before_filter :find_project_and_repository
before_filter :connect_gitlab, :only => [:index, :show, :create, :accept_pull_request, :pull_request_commits, :pull_request_changes, :new,
:update_pull_request, :pull_request_comments, :create_pull_request_comment]
layout "base_projects"
2016-08-04 18:30:04 +08:00
include PullRequestsHelper
2016-08-05 10:42:06 +08:00
include ApplicationHelper
2016-08-01 17:38:37 +08:00
# 返回json格式
def index
2016-08-03 16:54:37 +08:00
type = params[:type]
case type
when nil, "1"
@requests = @g.merge_requests(@project.gpid).select{|request| request.state == "opened" || request.state == "reopened"}
when "2"
@requests = @g.merge_requests(@project.gpid).select{|request| request.state == "merged"}
2016-08-09 10:29:18 +08:00
when "3"
@requests = @g.merge_requests(@project.gpid).select{|request| request.state == "closed"}
2016-08-03 16:54:37 +08:00
end
@requests_opened_count = @requests.count
2016-08-09 10:29:18 +08:00
@requests_merged_count = @g.merge_requests(@project.gpid).select{|request| request.state == "merged"}.count
@requests_closed_count = @g.merge_requests(@project.gpid).select{|request| request.state == "closed"}.count
2016-08-09 15:28:06 +08:00
@limit = 10
@is_remote = true
@count = type_count(type, @requests_opened_count, @requests_merged_count, @requests_closed_count)
@pages = Paginator.new @count, @limit, params['page'] || 1
@offset ||= @pages.offset
@requests = paginateHelper @requests, 10
2016-08-03 16:54:37 +08:00
respond_to do |format|
format.html
format.js
end
end
2016-08-05 10:42:06 +08:00
# 主要取源项目和目标项目分支及标识(用户名/版本库名)
def new
2016-08-05 10:42:06 +08:00
identifier = get_rep_identifier_by_project @project
@source_project_name = "#{get_user_name(@project.user_id)}/#{identifier}"
@source_rev = @g.branches(@project.gpid).map{|b| b.name}
# 获取forked源项目信息
2016-08-04 18:30:04 +08:00
if @project.forked_from_project_id
@forked_project = Project.find(@project.forked_from_project_id)
2016-08-05 10:42:06 +08:00
identifier = get_rep_identifier_by_project @forked_project
@forked_project_name = "#{get_user_name(@forked_project.user_id)}/#{identifier}"
@forked_rev = @g.branches(@forked_project.gpid).map{|b| b.name}
2016-08-04 18:30:04 +08:00
end
end
2016-08-01 17:38:37 +08:00
# Creates a merge request.
# If the operation is successful, 200 and the newly created merge request is returned. If an error occurs, an error number and a message explaining the reason is returned.
#
# @example
# Gitlab.create_merge_request(5, 'New merge request',
# :source_branch => 'source_branch', :target_branch => 'target_branch')
# Gitlab.create_merge_request(5, 'New merge request',
# :source_branch => 'source_branch', :target_branch => 'target_branch', :assignee_id => 42)
#
# @param [Integer] project The ID of a project.
# @param [String] title The title of a merge request.
# @param [Hash] options A customizable set of options.
# @option options [String] :source_branch (required) The source branch name.
# @option options [String] :target_branch (required) The target branch name.
# @option options [Integer] :assignee_id (optional) The ID of a user to assign merge request.
# @return [Gitlab::ObjectifiedHash] Information about created merge request.
def create
2016-08-01 17:38:37 +08:00
title = params[:title]
description = params[:description]
source_branch = params[:source_branch]
target_branch = params[:target_branch]
2016-08-03 11:07:49 +08:00
begin
2016-08-04 19:32:44 +08:00
# 如果传送了目标项目ID则PR请求发至目标项目
2016-08-05 14:19:09 +08:00
if params[:forked_project_id] && params[:source_project] == "forked_project_name"
2016-08-05 15:23:52 +08:00
target_project_id = params[:forked_project_id].to_i
2016-08-04 19:32:44 +08:00
request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch, :target_project_id => target_project_id)
2016-08-05 15:23:52 +08:00
@fork_project_name = Project.find(@project.forked_from_project_id).try(:name)
@fork_pr_message = true if @fork_project_name
2016-08-04 19:32:44 +08:00
else
request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch)
2016-08-05 15:23:52 +08:00
respond_to do |format|
format.js{redirect_to project_pull_request_path(request.id, :project_id => @project.id)}
end
end
2016-08-03 11:07:49 +08:00
rescue Exception => e
@message = e.message
end
end
def show
2016-08-03 15:56:07 +08:00
@type = params[:type]
@request = @g.merge_request(@project.gpid, params[:id])
2016-08-03 14:43:27 +08:00
@commits = @g.merge_request_commits(@project.gpid, params[:id].to_i)
2016-08-03 16:02:42 +08:00
@commits_count = @commits.count
@changes = @g.merge_request_changes(@project.gpid, params[:id]).try(:changes)
@changes_count = @changes.count
2016-08-09 14:00:36 +08:00
@comments = @g.merge_request_comments(@project.gpid, params[:id])
@comments_count = @comments.count
@limit = 10
@is_remote = true
@count = @comments_count
@pages = Paginator.new @count, @limit, params['page'] || 1
@offset ||= @pages.offset
@comments = paginateHelper @comments, 10
2016-08-01 17:38:37 +08:00
end
# Accept a merge request.
# If merge success you get 200 OK.
# Accept a merge request.
#
# @example
# Gitlab.accept_pull_rquest(5, 1)
#
# @param [Integer] project The ID of a project.
# @param [Integer] id The ID of a merge request.
# @return [Gitlab::ObjectifiedHash]
def accept_pull_request
2016-08-04 15:07:15 +08:00
begin
2016-08-05 20:14:19 +08:00
status = @g.accept_merge_rquest(@project.gpid, params[:id], User.current.gid)
PullRequest.create(:pull_request_id => status.id, :user_id => User.current.id, :gpid => status.project_id)
2016-08-04 15:07:15 +08:00
respond_to do |format|
format.js{redirect_to project_pull_request_path(status.id, :project_id => @project.id)}
end
rescue Exception => e
@message = e.message
2016-08-01 17:38:37 +08:00
end
end
2016-08-09 10:53:44 +08:00
# Updates a merge request.
#
# @example
# Gitlab.update_merge_request(5, 42, :title => 'New title')
#
# @param [Integer] project The ID of a project.
# @param [Integer] id The ID of a merge request.
# @param [Hash] options A customizable set of options.
# @option options [String] :title The title of a merge request.
# @option options [String] :source_branch The source branch name.
# @option options [String] :target_branch The target branch name.
# @option options [Integer] :assignee_id The ID of a user to assign merge request.
# @option options [String] :state_event New state (close|reopen|merge).
# @return [Gitlab::ObjectifiedHash] Information about updated merge request.
2016-08-08 15:23:55 +08:00
def update_pull_request
begin
@g.update_merge_request(@project.gpid, params[:id], :state_event => params[:state])
respond_to do |format|
format.html{redirect_to project_pull_request_path(params[:id], :project_id => @project.id)}
end
rescue Exception => e
@message = e.message
end
end
2016-08-09 10:53:44 +08:00
# Creates a merge request.
#
# @example
# Gitlab.create_merge_request(5, 'New merge request',
# :source_branch => 'source_branch', :target_branch => 'target_branch')
# Gitlab.create_merge_request(5, 'New merge request',
# :source_branch => 'source_branch', :target_branch => 'target_branch', :assignee_id => 42)
def create_pull_request_comment
content = params[:pull_request_comment]
2016-08-09 10:53:44 +08:00
begin
2016-08-09 15:02:15 +08:00
@comments = @g.create_merge_request_comment(@project.gpid, params[:id], content, User.current.gid)
respond_to do |format|
format.html{redirect_to project_pull_request_path(params[:id], :project_id => @project.id)}
end
2016-08-09 10:53:44 +08:00
rescue Exception => e
@message = e.message
end
end
# Gets the comments on a merge request.
#
# @example
# Gitlab.merge_request_comments(5, 1)
def pull_request_comments
begin
@comments = @g.merge_request_comments(@project.gpid, params[:id])
@comments_count = @comments.count
@limit = 10
@is_remote = true
@count = @comments_count
@pages = Paginator.new @count, @limit, params['page'] || 1
@offset ||= @pages.offset
@comments = paginateHelper @comments, 10
2016-08-09 10:53:44 +08:00
rescue Exception => e
@message = e.message
end
end
# Get a list of merge request commits.
# Parameters:
# id (required) - The ID of a project
# merge_request_id (required) - The ID of MR
def pull_request_commits
2016-08-09 10:53:44 +08:00
begin
@commits = @g.merge_request_commits(@project.gpid, params[:id])
2016-08-09 14:00:36 +08:00
@commits_count = @commits.count
@limit = 10
@is_remote = true
@count = @commits_count
@pages = Paginator.new @count, @limit, params['page'] || 1
@offset ||= @pages.offset
@commits = paginateHelper @commits, 10
2016-08-09 10:53:44 +08:00
rescue Exception => e
@message = e.message
end
end
2016-08-09 10:53:44 +08:00
# Shows information about the merge request including its files and changes. With GitLab 8.2 the return fields upvotes and downvotes are deprecated and always return 0.
# Parameters:
# id (required) - The ID of a project
# merge_request_id (required) - The ID of MR
def pull_request_changes
2016-08-09 10:53:44 +08:00
begin
@changes = @g.merge_request_changes(@project.gpid, params[:id]).try(:changes)
@changes_count = @changes.count
@limit = 10
@is_remote = true
@count = @changes_count
@pages = Paginator.new @count, @limit, params['page'] || 1
@offset ||= @pages.offset
@changes = paginateHelper @changes, 10
2016-08-09 10:53:44 +08:00
rescue Exception => e
@message = e.message
end
end
private
def connect_gitlab
@g = Gitlab.client
end
def find_project_and_repository
@project = Project.find(params[:project_id])
render_404 if @project.gpid.blank?
@repository = Repository.where(:project_id => @project.id, :type => "Repository::Gitlab")
rescue ActiveRecord::RecordNotFound
render_404
end
end