2016-08-01 16:54:31 +08:00
class PullRequestsController < ApplicationController
before_filter :find_project_and_repository
2016-08-03 17:37:37 +08:00
before_filter :connect_gitlab , :only = > [ :index , :show , :create , :accept_pull_request , :pull_request_commits , :pull_request_changes , :new ]
2016-08-01 16:54:31 +08:00
layout " base_projects "
2016-08-04 18:30:04 +08:00
include PullRequestsHelper
2016-08-01 16:54:31 +08:00
2016-08-01 17:38:37 +08:00
# 返回json格式
2016-08-01 16:54:31 +08:00
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 " }
end
@requests_opened_count = @requests . count
@requests_merged_count = params [ :type ] ? @requests . count : @g . merge_requests ( @project . gpid ) . select { | request | request . state == " merged " } . count
respond_to do | format |
format . html
format . js
end
2016-08-01 16:54:31 +08:00
end
def new
2016-08-03 17:37:37 +08:00
@rev = @g . branches ( @project . gpid ) . map { | b | b . name }
2016-08-04 18:30:04 +08:00
@target_project = [ ]
if @project . forked_from_project_id
@forked_project = Project . find ( @project . forked_from_project_id )
unless @forked_project . blank?
@target_project << " #{ get_user_name ( @forked_project . user_id ) } / #{ @forked_project . name } "
end
end
# @forked_rev = @g.branches(forked_project.gpid).map{|b| b.name}
@target_project << " #{ get_user_name ( @project . user_id ) } / #{ @project . name } "
2016-08-01 16:54:31 +08:00
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.
2016-08-01 16:54:31 +08:00
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-04 18:30:04 +08:00
target_project = 1549
2016-08-03 11:07:49 +08:00
begin
2016-08-04 17:01:19 +08:00
request = @g . create_merge_request ( @project . gpid , title , User . current . gid , :description = > description , :source_branch = > source_branch , :target_branch = > target_branch )
2016-08-03 13:57:59 +08:00
respond_to do | format |
2016-08-04 10:08:13 +08:00
format . js { redirect_to project_pull_request_path ( request . id , :project_id = > @project . id ) }
2016-08-03 13:57:59 +08:00
end
2016-08-03 11:07:49 +08:00
rescue Exception = > e
@message = e . message
end
2016-08-01 16:54:31 +08:00
end
def show
2016-08-03 15:56:07 +08:00
@type = params [ :type ]
2016-08-03 13:57:59 +08:00
@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-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]
2016-08-03 13:57:59 +08:00
def accept_pull_request
2016-08-04 15:07:15 +08:00
begin
status = @g . accept_merge_rquest ( @project . gpid , params [ :id ] )
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
2016-08-01 16:54:31 +08:00
end
2016-08-03 13:57:59 +08:00
# 获取某次请求的提交次数
def pull_request_commits
2016-08-03 15:56:07 +08:00
@type = parms [ :type ]
2016-08-03 14:43:27 +08:00
@commits = @g . merge_request_commits ( @project . gpid , params [ :id ] . to_i )
2016-08-03 13:57:59 +08:00
end
# 获取某次请求的改动
def pull_request_changes
2016-08-03 16:02:42 +08:00
@changes = @g . merge_request_changes ( @project . gpid , params [ :id ] ) . try ( :changes )
@changes_count = @changes . count
2016-08-03 13:57:59 +08:00
end
2016-08-01 16:54:31 +08:00
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