aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/issue/models/issue.js
blob: 8a282647b41d3dd1688a3c7d5ad198766908d14e (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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.
 */
import _ from 'underscore';
import Backbone from 'backbone';

export default Backbone.Model.extend({
  idAttribute: 'key',

  defaults () {
    return {
      flows: []
    };
  },

  url () {
    return window.baseUrl + '/api/issues';
  },

  urlRoot () {
    return window.baseUrl + '/api/issues';
  },

  parse (r) {
    let issue = _.size(r.issues) > 0 ? r.issues[0] : r.issue;
    if (issue) {
      issue = this._injectRelational(issue, r.components, 'component', 'key');
      issue = this._injectRelational(issue, r.components, 'project', 'key');
      issue = this._injectRelational(issue, r.components, 'subProject', 'key');
      issue = this._injectRelational(issue, r.rules, 'rule', 'key');
      issue = this._injectRelational(issue, r.users, 'assignee', 'login');
      issue = this._injectCommentsRelational(issue, r.users);
      issue = this._prepareClosed(issue);
      issue = this.ensureTextRange(issue);
      return issue;
    } else {
      return r;
    }
  },

  _injectRelational (issue, source, baseField, lookupField) {
    const baseValue = issue[baseField];
    if (baseValue != null && _.size(source)) {
      const lookupValue = _.find(source, function (candidate) {
        return candidate[lookupField] === baseValue;
      });
      if (lookupValue != null) {
        Object.keys(lookupValue).forEach(function (key) {
          const newKey = baseField + key.charAt(0).toUpperCase() + key.slice(1);
          issue[newKey] = lookupValue[key];
        });
      }
    }
    return issue;
  },

  _injectCommentsRelational (issue, users) {
    if (issue.comments) {
      const that = this;
      const newComments = issue.comments.map(function (comment) {
        let newComment = _.extend({}, comment, { author: comment.login });
        delete newComment.login;
        newComment = that._injectRelational(newComment, users, 'author', 'login');
        return newComment;
      });
      issue = _.extend({}, issue, { comments: newComments });
    }
    return issue;
  },

  _prepareClosed (issue) {
    if (issue.status === 'CLOSED') {
      issue.flows = [];
      delete issue.textRange;
    }
    return issue;
  },

  ensureTextRange (issue) {
    if (issue.line && !issue.textRange) {
      // FIXME 999999
      issue.textRange = {
        startLine: issue.line,
        endLine: issue.line,
        startOffset: 0,
        endOffset: 999999
      };
    }
    return issue;
  },

  sync (method, model, options) {
    const opts = options || {};
    opts.contentType = 'application/x-www-form-urlencoded';
    if (method === 'read') {
      _.extend(opts, {
        type: 'GET',
        url: this.urlRoot() + '/search',
        data: {
          issues: model.id,
          additionalFields: '_all'
        }
      });
    }
    if (method === 'create') {
      _.extend(opts, {
        type: 'POST',
        url: this.urlRoot() + '/create',
        data: {
          component: model.get('component'),
          line: model.get('line'),
          message: model.get('message'),
          rule: model.get('rule'),
          severity: model.get('severity')
        }
      });
    }
    const xhr = options.xhr = Backbone.ajax(opts);
    model.trigger('request', model, xhr, opts);
    return xhr;
  },

  /**
   * Reset issue attributes (delete old, replace with new)
   * @param attrs
   * @param options
   * @returns {Object}
   */
  reset (attrs, options) {
    for (const key in this.attributes) {
      if (this.attributes.hasOwnProperty(key) && !(key in attrs)) {
        attrs[key] = void 0;
      }
    }
    return this.set(attrs, options);
  },

  /**
   * Do an action over an issue
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   * @private
   */
  _action (options) {
    const model = this;
    const success = function (r) {
      const attrs = model.parse(r);
      model.reset(attrs);
      if (options.success) {
        options.success(model, r, options);
      }
    };
    const opts = _.extend({ type: 'POST' }, options, { success });
    const xhr = options.xhr = Backbone.ajax(opts);
    model.trigger('request', model, xhr, opts);
    return xhr;
  },

  /**
   * Assign issue
   * @param {String|null} assignee Assignee, can be null to unassign issue
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   */
  assign (assignee, options) {
    const opts = _.extend({
      url: this.urlRoot() + '/assign',
      data: { issue: this.id, assignee }
    }, options);
    return this._action(opts);
  },

  /**
   * Plan issue
   * @param {String|null} plan Action Plan, can be null to unplan issue
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   */
  plan (plan, options) {
    const opts = _.extend({
      url: this.urlRoot() + '/plan',
      data: { issue: this.id, plan }
    }, options);
    return this._action(opts);
  },

  /**
   * Set severity of issue
   * @param {String|null} severity Severity
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   */
  setSeverity (severity, options) {
    const opts = _.extend({
      url: this.urlRoot() + '/set_severity',
      data: { issue: this.id, severity }
    }, options);
    return this._action(opts);
  },

  /**
   * Do transition on issue
   * @param {String|null} transition Transition
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   */
  transition (transition, options) {
    const that = this;
    const opts = _.extend({
      url: this.urlRoot() + '/do_transition',
      data: { issue: this.id, transition }
    }, options);
    return this._action(opts).done(function () {
      that.trigger('transition', transition);
    });
  },


  /**
   * Set type of issue
   * @param {String|null} issueType Issue type
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   */
  setType (issueType, options) {
    const opts = _.extend({
      url: this.urlRoot() + '/set_type',
      data: { issue: this.id, type: issueType }
    }, options);
    return this._action(opts);
  },


  /**
   * Do a custom (plugin) action
   * @param {String} actionKey Action Key
   * @param {Object|null} options Options for jQuery ajax
   * @returns {jqXHR}
   */
  customAction (actionKey, options) {
    const opts = _.extend({
      type: 'POST',
      url: this.urlRoot() + '/do_action',
      data: { issue: this.id, actionKey }
    }, options);
    const xhr = Backbone.ajax(opts);
    this.trigger('request', this, xhr, opts);
    return xhr;
  },

  getLinearLocations () {
    const textRange = this.get('textRange');
    if (!textRange) {
      return [];
    }
    const locations = [];
    for (let line = textRange.startLine; line <= textRange.endLine; line++) {
      // TODO fix 999999
      const from = line === textRange.startLine ? textRange.startOffset : 0;
      const to = line === textRange.endLine ? textRange.endOffset : 999999;
      locations.push({ line, from, to });
    }
    return locations;
  }
});