2016-11-28 15:25:26 +08:00
# encoding: utf-8
2016-11-17 11:32:39 +08:00
# 如果你对改模块任何功能不清楚,请不要随便改
# @Hjqreturn
2016-08-01 16:54:31 +08:00
class PullRequestsController < ApplicationController
2016-08-25 10:04:51 +08:00
before_filter :authorize_logged
2016-08-01 16:54:31 +08:00
before_filter :find_project_and_repository
2016-11-25 20:13:37 +08:00
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 , :compare_pull_request ]
2016-12-15 17:12:41 +08:00
before_filter :member_allowed , :only = > [ :new , :create , :create_pull_request_comment ]
2016-11-25 20:13:37 +08:00
before_filter :manager_allowed , :only = > [ :accept_pull_request ]
2016-08-25 10:04:51 +08:00
2016-08-01 16:54:31 +08:00
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 16:54:31 +08:00
2016-11-28 15:25:26 +08:00
require 'ostruct'
2018-06-08 14:51:47 +08:00
require 'nokogiri'
require 'json'
require 'open-uri'
require 'uri'
2016-11-28 15:25:26 +08:00
2016-08-01 17:38:37 +08:00
# 返回json格式
2016-08-01 16:54:31 +08:00
def index
2016-10-14 17:23:51 +08:00
# project_menu_type 为了控制base顶部导航
2016-10-10 10:41:41 +08:00
@project_menu_type = 6
2016-10-14 17:23:51 +08:00
# 不符合pullrequest条件的给出提示
2016-10-26 13:45:15 +08:00
@allow_to_pull_request = allow_pull_request ( @project ) > 0
2016-10-10 10:41:41 +08:00
2016-08-03 16:54:37 +08:00
type = params [ :type ]
2016-10-28 15:47:05 +08:00
merge_requests = @g . merge_requests ( @project . gpid )
merge_requests_count = merge_requests . count
2016-08-03 16:54:37 +08:00
case type
when nil , " 1 "
2016-10-28 15:47:05 +08:00
@requests = merge_requests . select { | request | request . state == " opened " || request . state == " reopened " }
2016-10-14 17:23:51 +08:00
# 更新统计数字
2016-10-28 15:47:05 +08:00
project_score = @project . project_score . update_column ( :pull_request_num , merge_requests_count )
2016-08-03 16:54:37 +08:00
when " 2 "
2016-10-28 15:47:05 +08:00
@requests = merge_requests . select { | request | request . state == " merged " }
2016-08-09 10:29:18 +08:00
when " 3 "
2016-10-28 15:47:05 +08:00
@requests = merge_requests . select { | request | request . state == " closed " }
2016-08-03 16:54:37 +08:00
end
@requests_opened_count = @requests . count
2016-10-28 15:47:05 +08:00
@requests_merged_count = merge_requests . select { | request | request . state == " merged " } . count
@requests_closed_count = merge_requests . select { | request | request . state == " closed " } . count
2016-08-09 15:28:06 +08:00
2016-12-02 20:00:15 +08:00
@limit = 20
2016-08-09 15:28:06 +08:00
@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
2016-12-02 20:00:15 +08:00
@requests = paginateHelper @requests , 20
2016-08-03 16:54:37 +08:00
respond_to do | format |
format . html
format . js
end
2016-08-01 16:54:31 +08:00
end
2016-08-05 10:42:06 +08:00
# 主要取源项目和目标项目分支及标识(用户名/版本库名)
2016-11-18 16:08:50 +08:00
# @tip 为空的时候表明发送的pr请求有改动, 为1的时候源分支和目标分支没有改动, 则不能成功创建
2016-08-01 16:54:31 +08:00
def new
2016-10-24 11:33:06 +08:00
# project_menu_type 为了控制base顶部导航
@project_menu_type = 6
2016-11-18 16:08:50 +08:00
@tip = params [ :show_tip ]
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
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-11-17 17:30:15 +08:00
target_project_id = params [ :target_project_id ]
2018-06-08 11:02:17 +08:00
# begin
2018-06-07 16:39:09 +08:00
@gitlab_address = Redmine :: Configuration [ 'gitlab_address' ]
2018-06-08 10:47:54 +08:00
identifier = @project . repository . try ( :identifier )
logger . info ( " 1111111111111111 #{ identifier } " )
2018-06-07 16:39:09 +08:00
git_url = @gitlab_address . to_s + " / " + @project . owner . to_s + " / " + identifier + " . " + " git "
2018-06-08 15:04:37 +08:00
job_name = " pr- " + " #{ Time . now . to_i } "
2016-11-17 17:30:15 +08:00
# 如果分支有改动
if compare_pull_request ( source_branch , target_project_id , target_branch )
2018-06-08 17:00:10 +08:00
logger . info ( " 2222222222222222222222 " )
# 自动生成配置Jenkinsjob, 在merge事件的时候触发jenkins、sonar
# merge发送的时候还没有接收, 所以分析对象应该源项目
language = swith_language_type ( @project . language )
2019-09-06 17:15:23 +08:00
# exec_jenkins(source_branch, @project.gpid, git_url, job_name, language)
2018-06-08 17:00:10 +08:00
logger . info ( " 33333333333333333333 " )
qa = QualityAnalysis . create ( :project_id = > @project . id , :author_login = > User . current . login , :sonar_version = > 1 , :path = > " ./ " ,
:branch = > source_branch , :language = > " java " , :sonar_name = > " #{ job_name } " )
logger . info ( " 4444444444444444444444444 " )
2018-06-07 16:39:09 +08:00
2016-11-17 11:32:39 +08:00
# 如果传送了目标项目ID即向fork源项目发送请求
2016-11-22 11:06:18 +08:00
# if params[:forked_project_id] && params[:source_project] == "forked_project_name"
2016-11-30 09:51:24 +08:00
if ! params [ :target_project_id ] . blank? && params [ :target_project_id ] . to_i != @project . id
2016-11-22 15:05:33 +08:00
target_project_id = params [ :forked_project_id ] . to_i
2016-11-17 11:32:39 +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-11-24 10:11:59 +08:00
@fork_project_name = Project . find ( params [ :target_project_id ] ) . try ( :name )
2016-11-17 11:32:39 +08:00
@fork_pr_message = true if @fork_project_name
2016-12-01 11:31:48 +08:00
# 向管理员发送消息
2016-12-02 14:42:52 +08:00
send_message_to_manager ( params [ :target_project_id ] . to_i , request . id , 1 )
2018-06-07 16:39:09 +08:00
pr = PullRequest . create ( :pull_request_id = > request . id , :user_id = > User . current . id , :status = > 1 , :project_id = > target_project_id , :title = > title )
2016-11-17 11:32:39 +08:00
else
2016-12-01 11:31:48 +08:00
@project_member = Member . where ( :project_id = > @project . id )
2016-11-17 11:32:39 +08:00
request = @g . create_merge_request ( @project . gpid , title , User . current . gid , :description = > description , :source_branch = > source_branch , :target_branch = > target_branch )
2016-11-29 12:25:15 +08:00
# 发送消息
2016-12-02 14:42:52 +08:00
send_message_to_manager ( @project . id , request . id , 1 )
2016-11-29 12:25:15 +08:00
# 创建Trustie数据
2018-06-07 16:39:09 +08:00
pr = PullRequest . create ( :pull_request_id = > request . id , :user_id = > User . current . id , :status = > 1 , :project_id = > @project . id , :title = > title )
2016-11-17 11:32:39 +08:00
respond_to do | format |
format . js { redirect_to project_pull_request_path ( request . id , :project_id = > @project . id ) }
end
2016-08-05 15:23:52 +08:00
end
2018-06-08 17:14:57 +08:00
# 关联pr和qa
qa . update_attribute ( :pull_request_id , request . try ( :id ) )
2016-11-17 17:30:15 +08:00
else
2016-11-18 16:08:50 +08:00
tip = 1
2016-11-17 17:30:15 +08:00
respond_to do | format |
2016-11-18 16:08:50 +08:00
format . js { redirect_to new_project_pull_request_path ( :show_tip = > tip ) }
2016-11-17 17:30:15 +08:00
end
2016-08-03 13:57:59 +08:00
end
2018-06-08 11:02:17 +08:00
# rescue Exception => e
# logger.info("create PR failed ####{e}")
# end
2016-08-01 16:54:31 +08:00
end
2018-06-08 17:00:10 +08:00
def exec_jenkins branch , gpid , git_url , job_name , language
# 发送PR之前先发送创建Jenkins Job、gitlab hook, 调用sonar自动分析代码
2018-06-08 11:02:17 +08:00
logger . info ( " 5555555555555555 #{ branch } , git_url #{ git_url } , job_name #{ job_name } " )
2018-06-07 16:39:09 +08:00
@jenkins_address = Redmine :: Configuration [ 'jenkins_address' ]
jenkins_username = Redmine :: Configuration [ 'jenkins_username' ]
jenkins_password = Redmine :: Configuration [ 'jenkins_password' ]
# connect jenkins
@client_jenkins = JenkinsApi :: Client . new ( :server_url = > @jenkins_address , :username = > jenkins_username , :password = > jenkins_password )
@g = Gitlab . client
2018-06-08 17:00:10 +08:00
language = language . present? ? language : " java "
2018-06-07 16:39:09 +08:00
path = " ./ "
# qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
version = 1
2018-07-04 08:57:24 +08:00
if language == " java "
properties = " sonar.projectKey= #{ job_name }
sonar . projectName = #{job_name}
sonar . projectVersion = #{version}
sonar . sources = #{path}
sonar . language = #{language.downcase}
sonar . sourceEncoding = utf - 8
sonar . java . binaries = . / "
else
properties = " sonar.projectKey= #{ job_name }
2018-06-07 16:39:09 +08:00
sonar . projectName = #{job_name}
sonar . projectVersion = #{version}
sonar . sources = #{path}
sonar . language = #{language.downcase}
sonar . sourceEncoding = utf - 8 "
2018-07-04 08:57:24 +08:00
end
2018-06-07 16:39:09 +08:00
# 替换配置文件
@doc = Nokogiri :: XML ( File . open ( File . join ( Rails . root , 'tmp' , 'config.xml' ) ) )
@doc . at_xpath ( " //hudson.plugins.git.UserRemoteConfig/url " ) . content = git_url
@doc . at_xpath ( " //hudson.plugins.git.BranchSpec/name " ) . content = " */ #{ branch } "
@doc . at_xpath ( " //hudson.plugins.sonar.SonarRunnerBuilder/properties " ) . content = properties # sonar-properties
2018-06-08 11:02:17 +08:00
logger . info ( " 6666666666666666666666666 " )
2018-06-07 16:39:09 +08:00
# jenkins job创建
jenkins_job = @client_jenkins . job . create ( " #{ job_name } " , @doc . to_xml )
2018-06-08 11:02:17 +08:00
logger . info ( " 777777777777777777 " )
2018-06-07 16:39:09 +08:00
# 将地址作为hook值添加到gitlab
# @g.add_project_hook(@project.gpid, (@jenkins_address + "/project/#{job_name}"), merge_requests_events: true)
# job创建完成后自动运行job,如果运行成功则返回‘ 200’
code = @client_jenkins . job . build ( " #{ job_name } " )
end
2016-11-17 11:32:39 +08:00
# Compare branch for MR
# 判断源分支和目标分支是否有改动
2016-11-17 17:30:15 +08:00
# status 为true 表示有改动; false:便是没有改动
2016-11-17 11:32:39 +08:00
def compare_pull_request source_branch , target_project , target_branch
2016-11-17 17:30:15 +08:00
user_name_source = @project . owner . try ( :login )
2016-11-17 11:32:39 +08:00
identifier = @repository . identifier . downcase
2016-11-17 17:30:15 +08:00
git_source_tree = '--git-dir=/home/git/repositories/' + user_name_source + '/' + identifier + '.git'
2016-11-17 11:32:39 +08:00
if target_project
2016-11-18 16:08:50 +08:00
forked_source_project = Project . find ( target_project )
user_name_target = forked_source_project . owner . try ( :login )
2016-11-17 17:30:15 +08:00
git_target_tree = '--git-dir=/home/git/repositories/' + user_name_target + '/' + identifier + '.git'
2018-06-08 11:26:47 +08:00
# git_sourse_commit_id = @g.get_branch_commit_id(@project.gpid, git_source_tree, source_branch)
2018-06-08 14:38:55 +08:00
git_sourse_commit_id = @g . commits ( @project . gpid , :ref_name = > source_branch ) . first . try ( :id )
2018-06-08 11:26:47 +08:00
# git_target_commit_id = @g.get_branch_commit_id(forked_source_project.gpid, git_target_tree, target_branch)
2018-06-08 14:38:55 +08:00
git_target_commit_id = @g . commits ( forked_source_project . gpid , :ref_name = > target_branch ) . first . try ( :id )
2016-11-17 17:30:15 +08:00
else
2018-06-08 14:38:55 +08:00
git_sourse_commit_id = @g . commits ( @project . gpid , :ref_name = > source_branch ) . first . try ( :id )
git_target_commit_id = @g . commits ( @project . gpid , :ref_name = > target_branch ) . first . try ( :id )
2016-11-17 11:32:39 +08:00
end
2018-06-08 14:38:55 +08:00
logger . info ( " compare source_branch # # # # # # # # # # # # # # # #{ source_branch } " )
logger . info ( " compare target_project # # # # # # # # # # # # # # # #{ target_project } " )
logger . info ( " compare git_sourse_commit_id # # # # # # # # # # # # # # # #{ git_sourse_commit_id } " )
logger . info ( " compare git_target_commit_id # # # # # # # # # # # # # # # #{ git_target_commit_id } " )
2018-06-08 11:26:47 +08:00
status = ( git_sourse_commit_id == git_target_commit_id ? false : true )
2016-11-17 11:32:39 +08:00
end
2018-06-10 20:41:21 +08:00
# 获取jenkins输出结果
def pr_console
job_name = params [ :job_name ]
2018-06-10 21:35:39 +08:00
pr = PullRequest . where ( :pull_request_id = > params [ :pr_id ] ) . first
2018-06-10 20:41:21 +08:00
jenkins_address = Redmine :: Configuration [ 'jenkins_address' ]
jenkins_username = Redmine :: Configuration [ 'jenkins_username' ]
jenkins_password = Redmine :: Configuration [ 'jenkins_password' ]
# connect jenkins
client_jenkins = JenkinsApi :: Client . new ( :server_url = > jenkins_address , :username = > jenkins_username , :password = > jenkins_password )
2018-06-10 20:56:29 +08:00
@output = client_jenkins . job . get_console_output ( " #{ job_name } " , build_num = 0 , start = 0 , mode = 'html' ) [ " output " ]
2018-06-11 09:53:03 +08:00
@output = @output . force_encoding ( " UTF-8 " ) if @output . present?
2018-06-10 20:41:21 +08:00
end
2016-11-17 11:32:39 +08:00
# @project_menu_type 为了控制base顶部导航
# merge_when_succeeds
2016-08-01 16:54:31 +08:00
def show
2016-11-17 11:32:39 +08:00
# compare_pull_request source_project, source_branch, target_project, target_branch
# compare_pull_request
2017-01-10 14:22:27 +08:00
# 如何从个人主页点击进入,则将该消息设为“已读”
ForgeMessage . where ( :forge_message_id = > params [ :id ] , :forge_message_type = > " PullRequest " , :user_id = > User . current . id , :viewed = > false ) . update_all ( :viewed = > true )
2016-10-24 11:33:06 +08:00
@project_menu_type = 6
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
2016-10-26 15:40:44 +08:00
# @commits_day = @commits.chunk
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-11 14:16:13 +08:00
@comments = @g . merge_request_comments ( @project . gpid , params [ :id ] ) . reverse
2016-08-09 14:00:36 +08:00
@comments_count = @comments . count
2016-08-09 16:11:49 +08:00
@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]
2016-08-03 13:57:59 +08:00
def accept_pull_request
2016-08-04 15:07:15 +08:00
begin
2016-12-15 17:03:59 +08:00
@status = @g . accept_merge_rquest ( @project . gpid , params [ :id ] , User . current . gid )
2017-01-11 09:27:37 +08:00
user = User . find_by_login ( @status . author . try ( :username ) )
2017-01-13 10:14:10 +08:00
# 更新管理员的pullrequest消息
update_pullrequest_message ( params [ :id ] . to_i , @project . id , " PullRequest " , 2 )
2017-01-11 09:27:37 +08:00
# 接受后,给用户发消息
if ForgeMessage . where ( :forge_message_id = > params [ :id ] . to_i , :project_id = > @project . id , :forge_message_type = > " PullRequest " , :user_id = > user . id ) . count == 0
send_message_to_author ( @project . id , @status . author . try ( :username ) , params [ :id ] , 2 )
end
2016-08-04 15:07:15 +08:00
respond_to do | format |
2016-12-15 17:03:59 +08:00
format . js { redirect_to project_pull_request_path ( @status . id , :project_id = > @project . id ) }
2016-08-04 15:07:15 +08:00
end
2016-12-15 17:03:59 +08:00
rescue Gitlab :: Error :: Forbidden = > e
@message = l ( :label_pull_request_forbidden )
rescue Gitlab :: Error :: DataNotAccepted = > e
@message = l ( :label_pull_request_datanotaccepted )
rescue Gitlab :: Error :: NotFound = > e
@message = l ( :label_pull_request_notfound )
2016-08-04 15:07:15 +08:00
rescue Exception = > e
2019-10-17 14:19:20 +08:00
logger . info e
2016-08-01 17:38:37 +08:00
end
2016-08-01 16:54:31 +08:00
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
2016-12-02 14:42:52 +08:00
request = @g . update_merge_request ( @project . gpid , params [ :id ] , User . current . gid , :state_event = > params [ :state ] )
user = User . find_by_login ( request . author . try ( :username ) )
status = params [ :state ] == " close " ? 4 : 3
2017-01-11 09:27:37 +08:00
#send_message_to_manager(@project.id, params[:id], status)
2017-01-13 10:14:10 +08:00
update_pullrequest_message ( request . id , @project . id , " PullRequest " , status )
2017-01-11 09:27:37 +08:00
# 给作者发送消息,如何已经发送了消息,则不发
if is_project_manager? ( User . current . id , @project . id ) && ForgeMessage . where ( :forge_message_id = > request . id , :project_id = > @project . id , :forge_message_type = > " PullRequest " , :user_id = > user . id ) . count == 0
2016-12-02 16:18:31 +08:00
send_message_to_author ( @project . id , user . login , request . id , status )
2016-12-01 11:31:48 +08:00
end
2016-08-08 15:23:55 +08:00
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
2016-08-09 14:51:48 +08:00
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 )
2016-08-09 14:51:48 +08:00
respond_to do | format |
2016-10-25 15:34:32 +08:00
format . js { redirect_to project_pull_request_path ( params [ :id ] , :project_id = > @project . id ) }
2016-08-09 14:51:48 +08:00
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
2016-11-25 11:32:45 +08:00
# Gitlab.merge_request_comments(5, 1)
2016-08-09 10:53:44 +08:00
def pull_request_comments
begin
2016-08-11 14:16:13 +08:00
@comments = @g . merge_request_comments ( @project . gpid , params [ :id ] ) . reverse
2016-08-09 16:11:49 +08:00
@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
2016-08-03 13:57:59 +08:00
def pull_request_commits
2016-08-09 10:53:44 +08:00
begin
2016-08-11 14:16:13 +08:00
@type = params [ :type ]
2016-08-09 10:53:44 +08:00
@commits = @g . merge_request_commits ( @project . gpid , params [ :id ] )
2016-08-09 14:00:36 +08:00
@commits_count = @commits . count
2016-08-09 16:11:49 +08:00
@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
2016-08-03 13:57:59 +08:00
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
2016-08-03 13:57:59 +08:00
def pull_request_changes
2016-08-11 14:16:13 +08:00
@type = params [ :type ]
2016-08-09 10:53:44 +08:00
@changes = @g . merge_request_changes ( @project . gpid , params [ :id ] ) . try ( :changes )
2016-11-28 15:25:26 +08:00
2016-08-09 16:11:49 +08:00
2016-11-29 17:05:14 +08:00
2016-08-09 10:53:44 +08:00
@changes_count = @changes . count
2016-08-09 16:11:49 +08:00
@limit = 10
@is_remote = true
@count = @changes_count
@pages = Paginator . new @count , @limit , params [ 'page' ] || 1
@offset || = @pages . offset
@changes = paginateHelper @changes , 10
2016-11-28 15:25:26 +08:00
2016-08-09 10:53:44 +08:00
end
2016-08-03 13:57:59 +08:00
2016-08-01 16:54:31 +08:00
private
2016-11-25 20:13:37 +08:00
# post 相关操作权限控制
# 项目管理员可操作
def manager_allowed
unless is_project_manager? ( User . current . id , @project . id )
return render_403
2016-11-30 09:42:29 +08:00
end
2016-11-25 20:13:37 +08:00
end
# 项目成员可操作
def member_allowed
unless User . current . member_of? ( @project )
return render_403
end
end
2016-11-29 17:02:36 +08:00
def send_message user_id , project_id , pull_request_id
2016-11-30 08:37:56 +08:00
ForgeMessage . create ( :user_id = > user_id , :project_id = > project_id ,
2016-11-29 17:02:36 +08:00
:forge_message_id = > pull_request_id , :forge_message_type = > " PullRequest " , :viewed = > true )
2016-11-29 12:25:15 +08:00
end
2016-12-02 14:42:52 +08:00
def send_message_to_manager project_id , pull_request_id , status
project = Project . find ( project_id )
project . members . each do | member |
if is_project_manager? ( member . user_id , project_id ) && User . current . id != member . user_id
add_message ( member . user_id , project_id , pull_request_id , status )
end
end
end
def send_message_to_author ( project_id , user_login , request_id , status )
user = get_user_by_login_and ( user_login )
if user . id != User . current . id
add_message ( user . id , project_id , request_id , status )
end
end
def add_message ( user_id , project_id , pull_request_id , status )
ForgeMessage . create ( :user_id = > user_id ,
:project_id = > project_id ,
:forge_message_id = > pull_request_id ,
:forge_message_type = > " PullRequest " ,
2017-01-10 14:22:27 +08:00
:viewed = > false ,
2016-12-02 16:18:31 +08:00
:status = > status ,
:operate_user_id = > User . current . id ,
)
2016-11-29 12:25:15 +08:00
end
2017-01-13 10:14:10 +08:00
def update_pullrequest_message forge_message_id , project_id , forge_message_type , status
# 更新这条pullrequest消息所有相关的信息
ForgeMessage . where ( :forge_message_id = > forge_message_id , :project_id = > project_id , :forge_message_type = > forge_message_type ) . update_all ( :status = > status , :operate_user_id = > User . current . id , :updated_at = > Time . now )
# 更新自己的消息为已读
ForgeMessage . where ( :forge_message_id = > forge_message_id , :project_id = > project_id , :forge_message_type = > forge_message_type , :user_id = > User . current . id ) . update_all ( :viewed = > true )
# 更新pullrequest的status字段
PullRequest . where ( :pull_request_id = > forge_message_id ) . update_all ( :status = > status )
end
2016-08-25 10:04:51 +08:00
def authorize_logged
if ! User . current . logged?
redirect_to signin_path
return
end
end
2016-08-01 16:54:31 +08:00
def connect_gitlab
@g = Gitlab . client
end
def find_project_and_repository
@project = Project . find ( params [ :project_id ] )
render_404 if @project . gpid . blank?
2016-11-17 11:32:39 +08:00
@repository = Repository . where ( :project_id = > @project . id , :type = > " Repository::Gitlab " ) . first
2016-08-01 16:54:31 +08:00
rescue ActiveRecord :: RecordNotFound
render_404
end
end