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.

issues_controller.rb 38 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
10 years ago
​
10 years ago
​
10 years ago
​
​
​
​
​
10 years ago
​
​
10 years ago
​
10 years ago
​
10 years ago
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
10 years ago
10 years ago
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
10 years ago
​
​
​
10 years ago
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
11 years ago
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
11 years ago
​
​
​
​
​
11 years ago
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class IssuesController < ApplicationController
  18. layout 'base_projects'#Added by young
  19. default_search_scope :issues
  20. before_filter :authorize1, :only => [:show]
  21. before_filter :find_issue, :only => [:show, :edit, :update,:add_journal, :add_journal_in_org]
  22. before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
  23. before_filter :find_project, :only => [:new, :create, :update_form, :issue_commits, :commit_for_issue, :issue_commit_delete]
  24. #before_filter :authorize, :except => [:index, :show]
  25. before_filter :authorize, :except => [:index,:add_journal, :add_journal_in_org,:delete_journal,:reply,:add_reply, :issue_commits, :commit_for_issue, :issue_commit_delete]
  26. before_filter :find_optional_project, :only => [:index]
  27. before_filter :check_for_default_issue_status, :only => [:new, :create]
  28. before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
  29. accept_rss_auth :index, :show
  30. accept_api_auth :index, :show, :create, :update, :destroy
  31. rescue_from Query::StatementInvalid, :with => :query_statement_invalid
  32. helper :journals
  33. helper :projects
  34. include ProjectsHelper
  35. helper :custom_fields
  36. include CustomFieldsHelper
  37. helper :issue_relations
  38. include IssueRelationsHelper
  39. helper :watchers
  40. include WatchersHelper
  41. helper :attachments
  42. include AttachmentsHelper
  43. helper :queries
  44. include QueriesHelper
  45. helper :repositories
  46. include RepositoriesHelper
  47. helper :sort
  48. include SortHelper
  49. include IssuesHelper
  50. helper :timelog
  51. include Redmine::Export::PDF
  52. helper :project_score
  53. include ApplicationHelper
  54. # issue和代码提交id关联模块 --> over
  55. # 获取某个项目的commit_ids
  56. def issue_commits
  57. begin
  58. return render_404 if @project.gpid.nil?
  59. @issue_commit_ids = (params[:issue_commit_ids].is_a?(Array) ? params[:issue_commit_ids] : params[:issue_commit_ids].split(",")) unless params[:issue_commit_ids].nil?
  60. search = params[:search].to_s.strip
  61. @type = params[:type]
  62. limit = 15
  63. g = Gitlab.client
  64. g_project = g.project(@project.gpid)
  65. rev = params[:branch].nil? ? g_project.default_branch : params[:branch]
  66. @project_branches = g.branches(@project.gpid)
  67. @branch_names = @project_branches.map{|b| b.name}
  68. @default_branch = g_project.default_branch
  69. # 搜索的分页需要单独处理,因为搜索不容易获取总数
  70. if search.present?
  71. @commits = g.commits(@project.gpid, ref_name:rev, :search => search)
  72. @commits_count = @commits.count
  73. @commits_pages = Paginator.new @commits_count, limit, params['page'] || 1
  74. @offset ||= @commits_pages.offset
  75. @commits = paginateHelper @commits,limit
  76. else
  77. @commits = g.commits(@project.gpid, page:(params[:page].to_i - 1).to_s, ref_name:rev, :search => search)
  78. @commits_count = g.user_static(@project.gpid, :rev => rev).count
  79. @commits_pages = Redmine::Pagination::Paginator.new @commits_count, limit, params[:page]
  80. end
  81. rescue Exception => e
  82. puts e
  83. end
  84. end
  85. # 选择对应的Commit
  86. def commit_for_issue
  87. history_commit_ids = params[:issue_for_commit_ids].split(",") unless params[:issue_for_commit_ids].nil?
  88. @issue_commit_ids = (history_commit_ids.blank? ? params[:checkbox1] : params[:checkbox1] | history_commit_ids).uniq
  89. end
  90. def issue_commit_delete
  91. commit_id = params[:commit_id].split(",")
  92. issue_commit_ids = params[:issue_commit_ids]
  93. # issue_id存在则为issue详情或者编辑的时候,否则为新建Issue
  94. # 编辑和详情的时候需要在数据库中删除记录,新建的时候在内存中删除
  95. if params[:issue_id]
  96. commit_issue = CommitIssues.where(:commit_id => commit_id, :issue_id => params[:issue_id], :project_id => @project.id).first
  97. commit_issue.delete if commit_issue
  98. end
  99. @issue_commit_ids = issue_commit_ids - commit_id
  100. end
  101. # over
  102. def index
  103. # 顶部导航
  104. @project_menu_type = 2
  105. # 为了性能所有用了两种模式,issue的@query查询所有的没有优势
  106. # 但是对过滤条件很有有时
  107. if params[:set_filter] != "1"
  108. @project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'
  109. if User.current.member_of?(@project)
  110. @issues_filter = Issue.where(:project_id => @project.id).order('updated_on desc')
  111. else
  112. @issues_filter = Issue.where(:project_id => @project.id, :is_private => 0).order('updated_on desc')
  113. end
  114. open_and_close_num(@project)
  115. @issue_count = @issues_filter.count
  116. @limit = 10
  117. @is_remote = true
  118. @issue_pages = Paginator.new @issue_count, @limit, params['page'] || 1
  119. @offset ||= @issue_pages.offset
  120. @issues = paginateHelper @issues_filter, @limit
  121. respond_to do |format|
  122. format.js
  123. format.html { render :template => 'issues/index', :layout => @project_base_tag }#by young
  124. format.api {Issue.load_visible_relations(@issues) if include_in_api_response?('relations')}
  125. format.xls {filename = "#{@project.name.to_s}_#{l(:label_issue_list_xls)}.xls"
  126. send_data(issue_list_xls(@issues_filter), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename))
  127. }
  128. end
  129. else
  130. retrieve_query
  131. sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  132. sort_update(@query.sortable_columns)
  133. @query.sort_criteria = sort_criteria.to_a
  134. @project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'
  135. if @query.valid?
  136. @tracker_id = params[:tracker_id]
  137. @assign_to_id = params[:assigned_to_id]
  138. @author_id = params[:author_id]
  139. @priority_id = params[:priority_id]
  140. @status_id = params[:status_id]
  141. @subject = params[:subject]
  142. @done_ratio = params[:done_ratio]
  143. @fixed_version_id = params[:fixed_version_id]
  144. @issue_count = @query.issue_count
  145. @test = params[:test]
  146. @project_sort = 'issues.updated_on desc'
  147. if params[:test] != "0"
  148. case @test
  149. when "1"
  150. @project_sort = 'issues.created_on desc'
  151. when "2"
  152. @project_sort = 'issues.created_on asc'
  153. when "3"
  154. @project_sort = 'issues.updated_on desc'
  155. when "4"
  156. @project_sort = 'issues.updated_on asc'
  157. end
  158. end
  159. open_and_close_num(@project)
  160. @issues_filter_assign_count = @query.issues.select{|issue| issue.assigned_to_id == User.current.id }.count
  161. @issues_filter_author_count = @query.issues.select{|issue| issue.author_id == User.current.id }.count
  162. @issues_filter = @query.issues(:order => @project_sort)
  163. @limit = 10
  164. @is_remote = true
  165. @issue_pages = Paginator.new @issue_count, @limit, params['page'] || 1
  166. @offset ||= @issue_pages.offset
  167. @issues = paginateHelper @issues_filter, @limit
  168. respond_to do |format|
  169. format.js
  170. format.html { render :template => 'issues/index', :layout => @project_base_tag }#by young
  171. format.api {Issue.load_visible_relations(@issues) if include_in_api_response?('relations')}
  172. # format.json { render :json => @issues.map { |issue| issue.to_json}} #:json => @issues.map { |issue| issue.to_json}
  173. format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
  174. format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
  175. format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
  176. format.xls {filename = "#{@project.name.to_s}_#{l(:label_issue_list_xls)}.xls"
  177. send_data(issue_list_xls(@issues_filter), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename))
  178. }
  179. end
  180. else
  181. respond_to do |format|
  182. format.html { render(:template => 'issues/index', :layout => @project_base_tag) }#by young
  183. format.any(:atom, :csv, :pdf) { render(:nothing => true) }
  184. format.api { render_validation_errors(@query) }
  185. format.js
  186. end
  187. end
  188. end
  189. rescue ActiveRecord::RecordNotFound
  190. render_404
  191. end
  192. # 获取issue的开启统计数
  193. def open_and_close_num project
  194. if User.current.member_of?(project)
  195. @issue_open_count = Issue.where("project_id=#{project.id} and status_id in (1,2,3,4,6)").count
  196. @issue_close_count = Issue.where(:project_id => project.id, :status_id => 5 ).count
  197. else
  198. @issue_open_count = Issue.where("project_id=#{project.id} and status_id in (1,2,3,4,6) and is_private = 0").count
  199. @issue_close_count = Issue.where(:project_id => project.id, :status_id => 5, :is_private => 0).count
  200. end
  201. end
  202. def show
  203. # 顶部导航
  204. @project_menu_type = 2
  205. # 打开编辑内容
  206. @is_edit = true unless params[:edit].nil?
  207. # 当前用户查看指派给他的缺陷消息,则设置消息为已读
  208. query = ForgeMessage.where("forge_message_type =? and user_id =? and forge_message_id =?", "Issue", User.current, @issue).first
  209. query.update_attribute(:viewed, true) unless query.nil?
  210. # issue 关联的commit
  211. commit_issues = CommitIssues.where(:issue_id => @issue.id, :project_id => @issue.project_id)
  212. @issue_commit_ids = commit_issues.map{|commit_issue| commit_issue.commit_id}
  213. # issue 新建的at消息
  214. User.current.at_messages.unviewed('Issue', @issue.id).each {|x| x.viewed!}
  215. # 回复的at消息
  216. @issue.journals.each do |j|
  217. User.current.at_messages.unviewed('Journal', j.id).each {|x| x.viewed!}
  218. end
  219. # 缺陷状态消息更新
  220. query_journals_ids = @issue.journals.map{|journal| journal.id}
  221. if query_journals_ids.length > 0
  222. query_journals = ForgeMessage.where("user_id =? and forge_message_type =? and forge_message_id in (#{query_journals_ids.join(",")})", User.current.id, "Journal")
  223. query_journals.update_all(:viewed => true)
  224. end
  225. @jour_reply = Journal.new
  226. @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
  227. @journals.each_with_index {|j,i| j.indice = i+1}
  228. @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
  229. @journals.reverse! if User.current.wants_comments_in_reverse_order?
  230. @changesets = @issue.changesets.visible.all
  231. @changesets.reverse! if User.current.wants_comments_in_reverse_order?
  232. @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
  233. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  234. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  235. @priorities = IssuePriority.active
  236. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  237. @project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'#by young
  238. @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
  239. @journal = Journal.new(:journalized => @issue)
  240. respond_to do |format|
  241. format.html {
  242. retrieve_previous_and_next_issue_ids
  243. render :template => 'issues/show', :layout => @project_base_tag#by young
  244. }
  245. format.api
  246. format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
  247. format.pdf {
  248. pdf = issue_to_pdf(@issue, :journals => @journals)
  249. send_data(pdf, :type => 'application/pdf', :filename => filename_for_content_disposition("#{@project.identifier}-#{@issue.id}.pdf") )
  250. }
  251. end
  252. end
  253. # Add a new issue
  254. # The new issue will be created from an existing one if copy_from parameter is given
  255. def new
  256. # 顶部导航
  257. @project_menu_type = 2
  258. respond_to do |format|
  259. format.html { render :action => 'new', :layout => 'base_projects' }
  260. end
  261. end
  262. # 用户发布新issue
  263. def create
  264. call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
  265. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  266. # 给该issue在它所在的项目中所有的issues中所在的位置给一个序号
  267. @issue.project_issues_index = @issue.project.issues.last.nil? ? 1 : @issue.project.issues.last.project_issues_index + 1
  268. @issue.fixed_version_id = nil if @issue.fixed_version_id == 0
  269. @issue.assigned_to_id = nil if @issue.assigned_to_id == 0
  270. if @issue.save
  271. # 关联commmit
  272. update_issue_commit params[:commit_ids]
  273. #params[:issue][:assigned_to_id] = nil if params[:issue][:assigned_to_id].to_i == 0
  274. senduser = User.find(params[:issue][:assigned_to_id])
  275. issue_id = @issue.id
  276. issue_title = params[:issue][:subject]
  277. priority_id = params[:issue][:priority_id]
  278. ps = ProjectsService.new
  279. if senduser.id != User.current.id
  280. ps.send_wechat_project_issue_notice senduser,@issue.project,issue_id,issue_title,priority_id
  281. end
  282. call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
  283. respond_to do |format|
  284. format.html {
  285. render_attachment_warning_if_needed(@issue)
  286. #flash[:notice] = l(:label_successful_create)
  287. #flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("#{@issue.subject}", issue_path(@issue), :title => @issue.subject))
  288. #flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
  289. if params[:continue]
  290. attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
  291. redirect_to new_project_issue_url(@issue.project, :issue => attrs)
  292. else
  293. redirect_to issue_url(@issue.id)
  294. end
  295. }
  296. format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
  297. end
  298. return
  299. else
  300. respond_to do |format|
  301. format.html { render :action => 'new' }
  302. format.api { render_validation_errors(@issue) }
  303. end
  304. end
  305. end
  306. def edit
  307. # 修改实例变量的值
  308. return unless update_issue_from_params
  309. # issue 关联的commit
  310. commit_issues = CommitIssues.where(:issue_id => @issue.id, :project_id => @issue.project_id)
  311. @issue_commit_ids = commit_issues.map{|commit_issue| commit_issue.commit_id}
  312. respond_to do |format|
  313. format.html {render :layout => 'base_projects' }#added by young
  314. format.xml { }
  315. end
  316. end
  317. # 用户编辑更改issue
  318. def update
  319. # params[:assigned_to_id] = nil if params[:assigned_to_id].to_i == 0
  320. # params[:fixed_version_id] = nil if params[:fixed_version_id].to_i == 0
  321. if params[:issue_detail]
  322. issue = Issue.find(params[:id])
  323. issue = update_user_issue_detail(issue, params)
  324. @saved = update_user_issue_detail(issue, params)
  325. return
  326. else
  327. # 修改实例变量的值
  328. return unless update_issue_from_params
  329. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  330. @issue.assigned_to_id = nil if @issue.assigned_to_id == 0
  331. @issue.fixed_version_id = nil if @issue.fixed_version_id == 0
  332. saved = false
  333. begin
  334. @saved = @issue.save_issue_with_child_records(params, @time_entry)
  335. rescue ActiveRecord::StaleObjectError
  336. @conflict = true
  337. if params[:last_journal_id]
  338. @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
  339. @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
  340. end
  341. end
  342. end
  343. if @saved
  344. # 更新commit关联情况
  345. update_issue_commit params[:commit_ids]
  346. #修改界面增加跟踪者
  347. watcherlist = @issue.watcher_users
  348. select_users = []
  349. if params[:issue]
  350. if params[:issue][:watcher_user_ids]
  351. params[:issue][:watcher_user_ids].each do |user_id|
  352. select_users << User.find(user_id)
  353. end
  354. end
  355. end
  356. select_users.each do |user|
  357. if watcherlist.include? user
  358. else
  359. @issue.add_watcher user
  360. end
  361. end
  362. watcherlist.each do |user|
  363. if select_users.include? user
  364. else
  365. @issue.remove_watcher user
  366. end
  367. end
  368. render_attachment_warning_if_needed(@issue)
  369. reply_id = params[:reference_user_id].to_i
  370. if reply_id > 0
  371. JournalReply.add_reply(@issue.current_journal.id, reply_id, User.current.id)
  372. end
  373. #flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? 去掉这个notice,因为现在更新都是ajax操作
  374. respond_to do |format|
  375. format.js
  376. format.html { redirect_to issue_url(@issue.id) }
  377. format.api { render_api_ok }
  378. end
  379. else
  380. respond_to do |format|
  381. format.js
  382. format.html { render :action => 'edit' }
  383. format.api { render_validation_errors(@issue) }
  384. end
  385. end
  386. end
  387. # 保存issue的时候相关的commit操作
  388. # commit_ids => "9b9845ff,poor56el"
  389. def update_issue_commit commit_ids
  390. # 关联commmit
  391. commit_ids = params[:commit_ids]
  392. unless commit_ids.blank?
  393. commit_ids = commit_ids.split(",").uniq
  394. if params[:action] == "update"
  395. exist_commit_ids = CommitIssues.where(:issue_id => @issue, :project_id => @issue.project_id)
  396. unless exist_commit_ids.blank?
  397. exist_commit_ids = exist_commit_ids.map{|commit| commit.commit_id}
  398. commit_ids = commit_ids - exist_commit_ids
  399. end
  400. end
  401. commit_ids.each do |commit_id|
  402. CommitIssues.create(:commit_id => commit_id, :project_id => @issue.project_id, :issue_id => @issue.id)
  403. end
  404. end
  405. end
  406. def update_user_issue_detail(issue, params)
  407. case params[:type]
  408. when "status"
  409. issue.update_attribute(:status_id, params[:status_id])
  410. when "assigned"
  411. issue.update_attribute(:assigned_to_id, params[:assigned_to_id])
  412. when "ratio"
  413. issue.update_attribute(:done_ratio, params[:done_ratio])
  414. when "prior"
  415. issue.update_attribute(:priority_id, params[:priority_id])
  416. end
  417. end
  418. # Updates the issue form when changing the project, status or tracker
  419. # on issue creation/update
  420. def update_form
  421. end
  422. # Bulk edit/copy a set of issues
  423. def bulk_edit
  424. @issues.sort!
  425. @copy = params[:copy].present?
  426. @notes = params[:notes]
  427. if User.current.allowed_to?(:move_issues, @projects)
  428. @allowed_projects = Issue.allowed_target_projects_on_move
  429. if params[:issue]
  430. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
  431. if @target_project
  432. target_projects = [@target_project]
  433. end
  434. end
  435. end
  436. target_projects ||= @projects
  437. if @copy
  438. @available_statuses = [IssueStatus.default]
  439. else
  440. @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
  441. end
  442. @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
  443. @assignables = target_projects.map(&:assignable_users).reduce(:&)
  444. @trackers = target_projects.map(&:trackers).reduce(:&)
  445. @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  446. @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  447. if @copy
  448. @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
  449. @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
  450. end
  451. @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
  452. render :layout => false if request.xhr?
  453. end
  454. def bulk_update
  455. @issues.sort!
  456. @copy = params[:copy].present?
  457. attributes = parse_params_for_bulk_issue_attributes(params)
  458. unsaved_issue_ids = []
  459. moved_issues = []
  460. if @copy && params[:copy_subtasks].present?
  461. # Descendant issues will be copied with the parent task
  462. # Don't copy them twice
  463. @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
  464. end
  465. @issues.each do |issue|
  466. issue.reload
  467. if @copy
  468. issue = issue.copy({},
  469. :attachments => params[:copy_attachments].present?,
  470. :subtasks => params[:copy_subtasks].present?
  471. )
  472. end
  473. journal = issue.init_journal(User.current, params[:notes])
  474. issue.safe_attributes = attributes
  475. call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
  476. if issue.save
  477. moved_issues << issue
  478. else
  479. # Keep unsaved issue ids to display them in flash error
  480. unsaved_issue_ids << issue.id
  481. end
  482. end
  483. set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
  484. if params[:follow]
  485. if @issues.size == 1 && moved_issues.size == 1
  486. redirect_to issue_url(moved_issues.first)
  487. elsif moved_issues.map(&:project).uniq.size == 1
  488. redirect_to project_issues_url(moved_issues.map(&:project).first)
  489. end
  490. else
  491. redirect_back_or_default _project_issues_path(@project)
  492. end
  493. end
  494. def destroy
  495. # 增加删除页面类型,如果是个人主页,则返回该主页,项目动态则返回项目动态页眉
  496. page_classify = params[:page_classify] unless params[:page_classify].nil?
  497. page_id = params[:page_id] unless params[:page_id].nil?
  498. @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
  499. if @hours > 0
  500. case params[:todo]
  501. when 'destroy'
  502. # nothing to do
  503. when 'nullify'
  504. TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
  505. when 'reassign'
  506. reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
  507. if reassign_to.nil?
  508. flash.now[:error] = l(:error_issue_not_found_in_project)
  509. return
  510. else
  511. TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
  512. end
  513. else
  514. # display the destroy form if it's a user request
  515. return unless api_request?
  516. end
  517. end
  518. @issues.each do |issue|
  519. begin
  520. issue.reload.destroy
  521. rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
  522. # nothing to do, issue was already deleted (eg. by a parent)
  523. end
  524. end
  525. respond_to do |format|
  526. if page_classify
  527. format.html { redirect_back_or_default _project_issues_path(@project, page_classify, page_id) }
  528. else
  529. format.html { redirect_back_or_default _project_issues_path(@project) }
  530. end
  531. format.api { render_api_ok }
  532. end
  533. end
  534. def add_journal
  535. if User.current.logged?
  536. jour = Journal.new
  537. jour.user_id = User.current.id
  538. jour.notes = params[:notes]
  539. jour.journalized = @issue
  540. jour.save_attachments(params[:attachments])
  541. jour.save
  542. update_user_activity(@issue.class,@issue.id)
  543. update_forge_activity(@issue.class,@issue.id)
  544. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  545. @user_activity_id = params[:user_activity_id]
  546. if params[:issue_id]
  547. @issue_id = params[:issue_id]
  548. end
  549. @priorities = IssuePriority.active
  550. respond_to do |format|
  551. format.js
  552. end
  553. end
  554. end
  555. def add_journal_in_org
  556. if User.current.logged?
  557. jour = Journal.new
  558. jour.user_id = User.current.id
  559. jour.notes = params[:notes]
  560. jour.journalized = @issue
  561. jour.save
  562. org_activity = OrgActivity.where("org_act_type='Issue' and org_act_id =#{@issue.id}").first
  563. org_activity.updated_at = jour.created_on
  564. org_activity.save
  565. @user_activity_id = params[:user_activity_id]
  566. respond_to do |format|
  567. format.js
  568. end
  569. end
  570. end
  571. #对某个journ回复,显示回复框
  572. def reply
  573. @issue = Issue.find(params[:id])
  574. @jour = Journal.find(params[:journal_id])
  575. respond_to do |format|
  576. format.js
  577. end
  578. end
  579. #给issue添加journ。回复内容包含 对某个被回复的journ的内容
  580. def add_reply
  581. if User.current.logged?
  582. jour = Journal.find(params[:journal_id])
  583. @issue = Issue.find params[:id]
  584. @project = @issue.project
  585. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  586. @priorities = IssuePriority.active
  587. new_jour = Journal.new
  588. new_jour.user_id = User.current.id
  589. new_jour.reply_id = params[:journal_id]
  590. new_jour.parent_id = jour.id
  591. new_jour.notes = params[:content]
  592. new_jour.journalized = @issue
  593. new_jour.save_attachments(params[:attachments])
  594. # new_jour = @issue.journals.build(:user_id => User.current.id, :reply_id => params[:journal_id], :notes => params[:content], :parent_id => jour.id)
  595. @user_activity_id = params[:user_activity_id]
  596. if new_jour.save
  597. update_user_activity(@issue.class,@issue.id)
  598. update_forge_activity(@issue.class,@issue.id)
  599. respond_to do |format|
  600. format.js
  601. end
  602. end
  603. end
  604. end
  605. # 需要刷新详情页面,代码同步一致
  606. # 获取project和issue状态是为了刷新页面
  607. # 值所以用delete是因为issue和journal在act_as_attachment中是同一个类型
  608. # 非动态页面直接刷新,动态页面手动刷新
  609. def delete_journal
  610. @is_project = params[:is_project]
  611. @issue = Issue.find(params[:id])
  612. begin
  613. forge_acts = ForgeMessage.where(:forge_message_type => "Journal", :forge_message_id => params[:journal_id])
  614. forge_acts.destroy_all unless forge_acts.empty?
  615. at_message = AtMessage.where(:at_message_type => "Journal", :at_message_id => params[:journal_id])
  616. at_message.destroy_all unless at_message.empty?
  617. Journal.delete(params[:journal_id])
  618. rescue Exception => e
  619. puts e
  620. end
  621. @user_activity_id = params[:user_activity_id]
  622. respond_to do |format|
  623. if @user_activity_id
  624. format.js
  625. else
  626. format.html{ redirect_to issue_url(@issue)}
  627. end
  628. end
  629. end
  630. def statistics
  631. @project = Project.find(params[:id])
  632. if @project.nil?
  633. render_404
  634. end
  635. retrieve_query
  636. sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  637. sort_update(@query.sortable_columns)
  638. @query.sort_criteria = sort_criteria.to_a
  639. @project_base_tag = 'base_projects'
  640. if @query.valid?
  641. @tracker_id = params[:tracker_id]
  642. @assign_to_id = params[:assigned_to_id]
  643. @author_id = params[:author_id]
  644. @priority_id = params[:priority_id]
  645. @status_id = params[:status_id]
  646. @subject = params[:subject]
  647. @done_ratio = params[:done_ratio]
  648. @fixed_version_id = params[:fixed_version_id]
  649. @issue_count = @query.issue_count
  650. @test = params[:test]
  651. @project_sort = 'issues.updated_on desc'
  652. if params[:test] != "0"
  653. case @test
  654. when "1"
  655. @project_sort = 'issues.created_on desc'
  656. when "2"
  657. @project_sort = 'issues.created_on asc'
  658. when "3"
  659. @project_sort = 'issues.updated_on desc'
  660. when "4"
  661. @project_sort = 'issues.updated_on asc'
  662. end
  663. end
  664. # 搜索结果
  665. # SELECT assigned_to_id, count(*) as ac FROM `issues` where project_id = @project.id group by assigned_to_id order by ac desc;
  666. @issues_filter = @query.issues.sort_by{ |i| Issue.where(:project_id => @project.id , :assigned_to_id => i.assigned_to_id).count }.reverse
  667. # @issues_filter = @query.issues(:order => @project_sort)
  668. # if params[:type] == 1 || params[:type].nil?
  669. # @results = @issues_filter
  670. # elsif params[:type] == "2"
  671. # @results = @issues_filter.collect{|result| result.status_id !=5 }
  672. # elsif params[:type] == "3"
  673. # @results = @issues_filter.collect{|result| result.status_id !=5 }
  674. # end
  675. #统计
  676. @results = {}
  677. #统计total
  678. @alltotal = {}
  679. for i in 0..5 do
  680. @alltotal[i] = 0
  681. end
  682. @opentotal = {}
  683. for i in 0..5 do
  684. @opentotal[i] = 0
  685. end
  686. @closetotal = {}
  687. for i in 0..5 do
  688. @closetotal[i] = 0
  689. end
  690. #开启关闭
  691. @issue_open_count = 0
  692. @issue_close_count = 0
  693. @issues_filter.each do |issue|
  694. @alltotal[0] = @alltotal[0] + 1
  695. @alltotal[issue.tracker_id.to_i] = @alltotal[issue.tracker_id.to_i] + 1
  696. user_id = issue.assigned_to_id
  697. if issue.assigned_to_id.nil?
  698. user_id = 0
  699. end
  700. if !@results[user_id].nil?
  701. @results[user_id][0] = @results[user_id][0] + 1
  702. @results[user_id][issue.tracker_id.to_i] = @results[user_id][issue.tracker_id.to_i] + 1
  703. if issue.status_id.to_i == 5
  704. @issue_close_count = @issue_close_count + 1
  705. @results[user_id][12] = @results[user_id][12]+1
  706. @results[user_id][12+issue.tracker_id.to_i] = @results[user_id][12+issue.tracker_id.to_i]+1
  707. @closetotal[0] = @closetotal[0] + 1
  708. @closetotal[issue.tracker_id.to_i] = @closetotal[issue.tracker_id.to_i] + 1
  709. else
  710. @issue_open_count = @issue_open_count + 1
  711. @results[user_id][6] = @results[user_id][6]+1
  712. @results[user_id][6+issue.tracker_id.to_i] = @results[user_id][6+issue.tracker_id.to_i]+1
  713. @opentotal[0] = @opentotal[0] + 1
  714. @opentotal[issue.tracker_id.to_i] = @opentotal[issue.tracker_id.to_i] + 1
  715. end
  716. else
  717. @results[user_id] = {}
  718. tmpuser = User.find(user_id)
  719. @results[user_id][:name] = tmpuser.nil? ? "" : tmpuser.show_name
  720. #所有的
  721. @results[user_id][0] = 1
  722. for i in 1..17 do
  723. @results[user_id][i] = 0
  724. end
  725. # @results[user_id][1] = 0
  726. # @results[user_id][2] = 0
  727. # @results[user_id][3] = 0
  728. # @results[user_id][4] = 0
  729. # @results[user_id][5] = 0
  730. @results[user_id][issue.tracker_id.to_i] = 1
  731. #开启的 status_id = 12346
  732. # @results[user_id][6] = 0
  733. # @results[user_id][7] = 0
  734. # @results[user_id][8] = 0
  735. # @results[user_id][9] = 0
  736. # @results[user_id][10] = 0
  737. # @results[user_id][11] = 0
  738. #关闭的 status_id = 5
  739. # @results[user_id][12] = 0
  740. # @results[user_id][13] = 0
  741. # @results[user_id][14] = 0
  742. # @results[user_id][15] = 0
  743. # @results[user_id][16] = 0
  744. # @results[user_id][17] = 0
  745. if issue.status_id.to_i == 5
  746. @results[user_id][12] = 1
  747. @results[user_id][12+issue.tracker_id.to_i] = 1
  748. @issue_close_count = @issue_close_count+1
  749. @closetotal[0] = @closetotal[0] + 1
  750. @closetotal[issue.tracker_id.to_i] = @closetotal[issue.tracker_id.to_i] + 1
  751. else
  752. @issue_open_count = @issue_open_count+1
  753. @results[user_id][6] = 1
  754. @results[user_id][6+issue.tracker_id.to_i] = 1
  755. @opentotal[0] = @opentotal[0] + 1
  756. @opentotal[issue.tracker_id.to_i] = @opentotal[issue.tracker_id.to_i] + 1
  757. end
  758. end
  759. end
  760. respond_to do |format|
  761. format.js
  762. end
  763. else
  764. render_404
  765. end
  766. end
  767. private
  768. def find_project
  769. project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
  770. @project = Project.find(project_id)
  771. rescue ActiveRecord::RecordNotFound
  772. render_404
  773. end
  774. def retrieve_previous_and_next_issue_ids
  775. retrieve_query_from_session
  776. if @query
  777. sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  778. sort_update(@query.sortable_columns, 'issues_index_sort')
  779. limit = 500
  780. issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
  781. if (idx = issue_ids.index(@issue.id)) && idx < limit
  782. if issue_ids.size < 500
  783. @issue_position = idx + 1
  784. @issue_count = issue_ids.size
  785. end
  786. @prev_issue_id = issue_ids[idx - 1] if idx > 0
  787. @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
  788. end
  789. end
  790. end
  791. # Used by #edit and #update to set some common instance variables
  792. # from the params
  793. # TODO: Refactor, not everything in here is needed by #edit
  794. def update_issue_from_params
  795. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  796. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  797. @time_entry.attributes = params[:time_entry]
  798. # 更新issue状态时,journal表产生记录,返回@current_journal
  799. @issue.init_journal(User.current)
  800. issue_attributes = params[:issue]
  801. if issue_attributes && params[:conflict_resolution]
  802. case params[:conflict_resolution]
  803. when 'overwrite'
  804. issue_attributes = issue_attributes.dup
  805. issue_attributes.delete(:lock_version)
  806. when 'add_notes'
  807. issue_attributes = issue_attributes.slice(:notes)
  808. when 'cancel'
  809. redirect_to issue_url(@issue)
  810. return false
  811. end
  812. end
  813. if params[:action] == "update"
  814. senduser = User.find(params[:issue][:assigned_to_id])
  815. if senduser.id != User.current.id && @issue.assigned_to_id != params[:issue][:assigned_to_id].to_i
  816. issue_id = @issue.id
  817. issue_title = params[:issue][:subject]
  818. priority_id = params[:issue][:priority_id]
  819. ps = ProjectsService.new
  820. ps.send_wechat_project_issue_notice senduser,@issue.project,issue_id,issue_title,priority_id
  821. end
  822. end
  823. @issue.safe_attributes = issue_attributes
  824. @priorities = IssuePriority.active
  825. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  826. true
  827. end
  828. # TODO: Refactor, lots of extra code in here
  829. # TODO: Changing tracker on an existing issue should not trigger this
  830. def build_new_issue_from_params
  831. if params[:id].blank?
  832. @issue = Issue.new
  833. if params[:copy_from]
  834. begin
  835. @copy_from = Issue.visible.find(params[:copy_from])
  836. @copy_attachments = params[:copy_attachments].present? || request.get?
  837. @copy_subtasks = params[:copy_subtasks].present? || request.get?
  838. @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
  839. rescue ActiveRecord::RecordNotFound
  840. render_404
  841. return
  842. end
  843. end
  844. @issue.project = @project
  845. else
  846. @issue = @project.issues.visible.find(params[:id])
  847. end
  848. @issue.project = @project
  849. @issue.author ||= User.current
  850. # Tracker must be set before custom field values
  851. @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
  852. if @issue.tracker.nil?
  853. render_error l(:error_no_tracker_in_project)
  854. return false
  855. end
  856. @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
  857. @issue.safe_attributes = params[:issue]
  858. @priorities = IssuePriority.active
  859. @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
  860. @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
  861. end
  862. def check_for_default_issue_status
  863. if IssueStatus.default.nil?
  864. render_error l(:error_no_default_issue_status)
  865. return false
  866. end
  867. end
  868. def parse_params_for_bulk_issue_attributes(params)
  869. attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
  870. attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
  871. if custom = attributes[:custom_field_values]
  872. custom.reject! {|k,v| v.blank?}
  873. custom.keys.each do |k|
  874. if custom[k].is_a?(Array)
  875. custom[k] << '' if custom[k].delete('__none__')
  876. else
  877. custom[k] = '' if custom[k] == '__none__'
  878. end
  879. end
  880. end
  881. attributes
  882. end
  883. end

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