aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/controllers/issue_controller.rb
blob: 2ced8040fe118ab6c844d372e72feaad783c88b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2008-2013 SonarSource
# mailto:contact AT sonarsource DOT com
#
# SonarQube is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# SonarQube is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

class IssueController < ApplicationController

  helper SourceHelper

  # GET /issue/show/<key>
  # This URL is used by the Eclipse Plugin
  #
  # ==== Optional parameters
  # 'layout' is false to remove sidebar and headers. Default value is true.
  # 'source' is false to hide source code. Default value is true.
  #
  # ==== Example
  # GET /issue/show/151f6853-58a1-4950-95e3-9866f8be3e35?layout=false&source=false
  #
  def show
    require_parameters :id
    init_issue

    if params[:modal]
      render :partial => 'issue/show_modal'
    elsif request.xhr?
      if params[:only_detail]
        # used when canceling edition of comment -> see issue.js#refreshIssue()
        render :partial => 'issue/issue', :locals => {:issue => @issue_results.first}
      else
        render :partial => 'issue/show'
      end
    else
      render :action => 'show'
    end

  end

  # Form used for: assign, comment, transition, change severity and plan
  def action_form
    verify_ajax_request
    require_parameters :id, :issue

    @issue_result = Api.issues.find(params[:issue])
    @issue = @issue_result.first

    bad_request('Unknown issue') unless @issue

    action_type = params[:id]
    render :partial => "issue/#{action_type}_form"
  end

  def do_action
    verify_ajax_request
    verify_post_request
    require_parameters :id, :issue

    action_type = params[:id]
    issue_key = params[:issue]

    if action_type=='comment'
      issue_result = Internal.issues.addComment(issue_key, params[:text])
    elsif action_type=='assign'
      assignee = (params[:me]=='true' ? current_user.login : params[:assignee])
      issue_result = Internal.issues.assign(issue_key, assignee)
    elsif action_type=='transition'
      issue_result = Internal.issues.doTransition(issue_key, params[:transition])
    elsif action_type=='severity'
      issue_result = Internal.issues.setSeverity(issue_key, params[:severity])
    elsif action_type=='plan'
      issue_result = Internal.issues.plan(issue_key, params[:plan])
    elsif action_type=='unplan'
      issue_result = Internal.issues.plan(issue_key, nil)
    else
      # Execute action defined by plugin
      issue_result = Internal.issues.executeAction(issue_key, action_type)
    end

    if issue_result.ok
      @issue_results = Api.issues.find(issue_key)
      render :partial => 'issue/issue', :locals => {:issue => @issue_results.first}
    else
      @errors = issue_result.errors
      render :partial => 'issue/error', :status => issue_result.httpStatus
    end

  end

  # Form used to edit comment
  def edit_comment_form
    verify_ajax_request
    require_parameters :id

    @comment = Internal.issues.findComment(params[:id])

    render :partial => 'issue/edit_comment_form'
  end

  # Edit and save an existing comment
  def edit_comment
    verify_ajax_request
    verify_post_request
    require_parameters :key

    text = Api::Utils.read_post_request_param(params[:text])
    edit_result = Internal.issues.editComment(params[:key], text)

    if edit_result.ok
      @issue_results = Api.issues.find(edit_result.get.issueKey)
      render :partial => 'issue/issue', :locals => {:issue => @issue_results.issues.get(0)}
    else
      @errors = edit_result.errors
      render :partial => 'issue/error', :status => edit_result.httpStatus
    end
  end

  # Delete an existing comment
  def delete_comment
    verify_post_request
    verify_ajax_request
    require_parameters :id

    comment = Internal.issues.deleteComment(params[:id])

    @issue_results = Api.issues.find(comment.issueKey)
    render :partial => 'issue/issue', :locals => {:issue => @issue_results.issues.get(0)}
  end

  # Form used to create a manual issue
  def create_form
    verify_ajax_request
    require_parameters :component
    render :partial => 'issue/create_form'
  end

  # Create a manual issue
  def create
    verify_post_request
    verify_ajax_request

    component_key = params[:component]
    if Api::Utils.is_integer?(component_key)
      component = Project.find(component_key)
      component_key = (component && component.key)
    end

    issue_result = Internal.issues.create(params.merge({:component => component_key}))
    if issue_result.ok
      @issue_results = Api.issues.find(issue_result.get.key)
      render :partial => 'issue/manual_issue_created', :locals => {:issue => @issue_results.first}
    else
      render :partial => 'shared/result_messages', :status => 500, :locals => {:result => issue_result}
    end
  end


  #
  #
  # ACTIONS FROM THE ISSUES WIDGETS
  #
  #

  def widget_issues_list
    @snapshot = Snapshot.find(params[:snapshot_id]) if params[:snapshot_id]
    @dashboard_configuration = Api::DashboardConfiguration.new(nil, :period_index => params[:period], :snapshot => @snapshot)
    render :partial => 'project/widgets/issues/issues_list'
  end

  # Display the rule description in the issue panel
  def rule
    verify_ajax_request
    require_parameters :id
    rule_key = params[:id].split(':')
    @rule = Rule.first(:conditions => ['plugin_name=? and plugin_rule_key=?', rule_key[0], rule_key[1]], :include => :rule_note)
    render :partial => 'issue/rule'
  end

  # Display the changelog in the issue panel
  def changelog
    verify_ajax_request
    require_parameters :id
    @issue_results = Api.issues.find(params[:id])
    @issue = @issue_results.first()
    @changelog = Internal.issues.changelog(@issue)
    render :partial => 'issue/changelog'
  end

  # Display the technical debt detail in the issue panel
  def technicaldebt
    verify_ajax_request
    require_parameters :id
    @issue_results = Api.issues.find(params[:id])
    @issue = @issue_results.first()

    rule_id = @issue_results.rule(@issue).id
    @requirement = Characteristic.first(
        :conditions => ['characteristics.rule_id=? AND characteristics.enabled=?', rule_id, true],
        :include => [{:parent => :parent}]
    )
    render :partial => 'issue/technicaldebt'
  end


  private

  def init_issue
    @issue_results = Api.issues.find(params[:id])
    @issue = @issue_results.first()

    resource = Project.by_key(@issue.componentKey())
    @snapshot = resource.last_snapshot if resource.last_snapshot
  end

end