You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull_requests_controller.rb 9.9 kB

​
10 years ago
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
10 years ago
​
​
​
​
10 years ago
​
10 years ago
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. class PullRequestsController < ApplicationController
  2. before_filter :authorize_logged
  3. before_filter :find_project_and_repository
  4. before_filter :connect_gitlab, :only => [:index, :show, :create, :accept_pull_request, :pull_request_commits, :pull_request_changes, :new,
  5. :update_pull_request, :pull_request_comments, :create_pull_request_comment]
  6. layout "base_projects"
  7. include PullRequestsHelper
  8. include ApplicationHelper
  9. # 返回json格式
  10. def index
  11. type = params[:type]
  12. case type
  13. when nil, "1"
  14. @requests = @g.merge_requests(@project.gpid).select{|request| request.state == "opened" || request.state == "reopened"}
  15. when "2"
  16. @requests = @g.merge_requests(@project.gpid).select{|request| request.state == "merged"}
  17. when "3"
  18. @requests = @g.merge_requests(@project.gpid).select{|request| request.state == "closed"}
  19. end
  20. @requests_opened_count = @requests.count
  21. @requests_merged_count = @g.merge_requests(@project.gpid).select{|request| request.state == "merged"}.count
  22. @requests_closed_count = @g.merge_requests(@project.gpid).select{|request| request.state == "closed"}.count
  23. @limit = 10
  24. @is_remote = true
  25. @count = type_count(type, @requests_opened_count, @requests_merged_count, @requests_closed_count)
  26. @pages = Paginator.new @count, @limit, params['page'] || 1
  27. @offset ||= @pages.offset
  28. @requests = paginateHelper @requests, 10
  29. respond_to do |format|
  30. format.html
  31. format.js
  32. end
  33. end
  34. # 主要取源项目和目标项目分支及标识(用户名/版本库名)
  35. def new
  36. identifier = get_rep_identifier_by_project @project
  37. @source_project_name = "#{get_user_name(@project.user_id)}/#{identifier}"
  38. @source_rev = @g.branches(@project.gpid).map{|b| b.name}
  39. # 获取forked源项目信息
  40. if @project.forked_from_project_id
  41. @forked_project = Project.find(@project.forked_from_project_id)
  42. identifier = get_rep_identifier_by_project @forked_project
  43. @forked_project_name = "#{get_user_name(@forked_project.user_id)}/#{identifier}"
  44. @forked_rev = @g.branches(@forked_project.gpid).map{|b| b.name}
  45. end
  46. end
  47. # Creates a merge request.
  48. # 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.
  49. #
  50. # @example
  51. # Gitlab.create_merge_request(5, 'New merge request',
  52. # :source_branch => 'source_branch', :target_branch => 'target_branch')
  53. # Gitlab.create_merge_request(5, 'New merge request',
  54. # :source_branch => 'source_branch', :target_branch => 'target_branch', :assignee_id => 42)
  55. #
  56. # @param [Integer] project The ID of a project.
  57. # @param [String] title The title of a merge request.
  58. # @param [Hash] options A customizable set of options.
  59. # @option options [String] :source_branch (required) The source branch name.
  60. # @option options [String] :target_branch (required) The target branch name.
  61. # @option options [Integer] :assignee_id (optional) The ID of a user to assign merge request.
  62. # @return [Gitlab::ObjectifiedHash] Information about created merge request.
  63. def create
  64. title = params[:title]
  65. description = params[:description]
  66. source_branch = params[:source_branch]
  67. target_branch = params[:target_branch]
  68. begin
  69. # 如果传送了目标项目ID,则PR请求发至目标项目
  70. if params[:forked_project_id] && params[:source_project] == "forked_project_name"
  71. target_project_id = params[:forked_project_id].to_i
  72. 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)
  73. @fork_project_name = Project.find(@project.forked_from_project_id).try(:name)
  74. @fork_pr_message = true if @fork_project_name
  75. else
  76. request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch)
  77. respond_to do |format|
  78. format.js{redirect_to project_pull_request_path(request.id, :project_id => @project.id)}
  79. end
  80. end
  81. rescue Exception => e
  82. @message = e.message
  83. end
  84. end
  85. def show
  86. @type = params[:type]
  87. @request = @g.merge_request(@project.gpid, params[:id])
  88. @commits = @g.merge_request_commits(@project.gpid, params[:id].to_i)
  89. @commits_count = @commits.count
  90. @changes = @g.merge_request_changes(@project.gpid, params[:id]).try(:changes)
  91. @changes_count = @changes.count
  92. @comments = @g.merge_request_comments(@project.gpid, params[:id]).reverse
  93. @comments_count = @comments.count
  94. @limit = 10
  95. @is_remote = true
  96. @count = @comments_count
  97. @pages = Paginator.new @count, @limit, params['page'] || 1
  98. @offset ||= @pages.offset
  99. @comments = paginateHelper @comments, 10
  100. end
  101. # Accept a merge request.
  102. # If merge success you get 200 OK.
  103. # Accept a merge request.
  104. #
  105. # @example
  106. # Gitlab.accept_pull_rquest(5, 1)
  107. #
  108. # @param [Integer] project The ID of a project.
  109. # @param [Integer] id The ID of a merge request.
  110. # @return [Gitlab::ObjectifiedHash]
  111. def accept_pull_request
  112. begin
  113. status = @g.accept_merge_rquest(@project.gpid, params[:id], User.current.gid)
  114. PullRequest.create(:pull_request_id => status.id, :user_id => User.current.id, :gpid => status.project_id)
  115. respond_to do |format|
  116. format.js{redirect_to project_pull_request_path(status.id, :project_id => @project.id)}
  117. end
  118. rescue Exception => e
  119. @message = e.message
  120. end
  121. end
  122. # Updates a merge request.
  123. #
  124. # @example
  125. # Gitlab.update_merge_request(5, 42, :title => 'New title')
  126. #
  127. # @param [Integer] project The ID of a project.
  128. # @param [Integer] id The ID of a merge request.
  129. # @param [Hash] options A customizable set of options.
  130. # @option options [String] :title The title of a merge request.
  131. # @option options [String] :source_branch The source branch name.
  132. # @option options [String] :target_branch The target branch name.
  133. # @option options [Integer] :assignee_id The ID of a user to assign merge request.
  134. # @option options [String] :state_event New state (close|reopen|merge).
  135. # @return [Gitlab::ObjectifiedHash] Information about updated merge request.
  136. def update_pull_request
  137. begin
  138. @g.update_merge_request(@project.gpid, params[:id], User.current.gid, :state_event => params[:state])
  139. respond_to do |format|
  140. format.html{redirect_to project_pull_request_path(params[:id], :project_id => @project.id)}
  141. end
  142. rescue Exception => e
  143. @message = e.message
  144. end
  145. end
  146. # Creates a merge request.
  147. #
  148. # @example
  149. # Gitlab.create_merge_request(5, 'New merge request',
  150. # :source_branch => 'source_branch', :target_branch => 'target_branch')
  151. # Gitlab.create_merge_request(5, 'New merge request',
  152. # :source_branch => 'source_branch', :target_branch => 'target_branch', :assignee_id => 42)
  153. def create_pull_request_comment
  154. content = params[:pull_request_comment]
  155. begin
  156. @comments = @g.create_merge_request_comment(@project.gpid, params[:id], content, User.current.gid)
  157. respond_to do |format|
  158. format.html{redirect_to project_pull_request_path(params[:id], :project_id => @project.id)}
  159. end
  160. rescue Exception => e
  161. @message = e.message
  162. end
  163. end
  164. # Gets the comments on a merge request.
  165. #
  166. # @example
  167. # Gitlab.merge_request_comments(5, 1)
  168. def pull_request_comments
  169. begin
  170. @comments = @g.merge_request_comments(@project.gpid, params[:id]).reverse
  171. @comments_count = @comments.count
  172. @limit = 10
  173. @is_remote = true
  174. @count = @comments_count
  175. @pages = Paginator.new @count, @limit, params['page'] || 1
  176. @offset ||= @pages.offset
  177. @comments = paginateHelper @comments, 10
  178. rescue Exception => e
  179. @message = e.message
  180. end
  181. end
  182. # Get a list of merge request commits.
  183. # Parameters:
  184. # id (required) - The ID of a project
  185. # merge_request_id (required) - The ID of MR
  186. def pull_request_commits
  187. begin
  188. @type = params[:type]
  189. @commits = @g.merge_request_commits(@project.gpid, params[:id])
  190. @commits_count = @commits.count
  191. @limit = 10
  192. @is_remote = true
  193. @count = @commits_count
  194. @pages = Paginator.new @count, @limit, params['page'] || 1
  195. @offset ||= @pages.offset
  196. @commits = paginateHelper @commits, 10
  197. rescue Exception => e
  198. @message = e.message
  199. end
  200. end
  201. # 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.
  202. # Parameters:
  203. # id (required) - The ID of a project
  204. # merge_request_id (required) - The ID of MR
  205. def pull_request_changes
  206. begin
  207. @type = params[:type]
  208. @changes = @g.merge_request_changes(@project.gpid, params[:id]).try(:changes)
  209. @changes_count = @changes.count
  210. @limit = 10
  211. @is_remote = true
  212. @count = @changes_count
  213. @pages = Paginator.new @count, @limit, params['page'] || 1
  214. @offset ||= @pages.offset
  215. @changes = paginateHelper @changes, 10
  216. rescue Exception => e
  217. @message = e.message
  218. end
  219. end
  220. private
  221. def authorize_logged
  222. if !User.current.logged?
  223. redirect_to signin_path
  224. return
  225. end
  226. end
  227. def connect_gitlab
  228. @g = Gitlab.client
  229. end
  230. def find_project_and_repository
  231. @project = Project.find(params[:project_id])
  232. render_404 if @project.gpid.blank?
  233. @repository = Repository.where(:project_id => @project.id, :type => "Repository::Gitlab")
  234. rescue ActiveRecord::RecordNotFound
  235. render_404
  236. end
  237. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...