aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/issues/models/issues.js
blob: af974c77404b5e32d0eee432d427158f4528b514 (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
import _ from 'underscore';
import Backbone from 'backbone';
import Issue from './issue';

export default Backbone.Collection.extend({
  model: Issue,

  url: function () {
    return baseUrl + '/api/issues/search';
  },

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

  _injectCommentsRelational: function (issue, users) {
    if (issue.comments) {
      var that = this;
      var newComments = issue.comments.map(function (comment) {
        var 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: function (issue) {
    if (issue.status === 'CLOSED') {
      issue.flows = [];
      delete issue.textRange;
    }
    return issue;
  },

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

  parseIssues: function (r) {
    var that = this;
    return r.issues.map(function (issue, index) {
      _.extend(issue, { index: index });
      issue = that._injectRelational(issue, r.components, 'component', 'key');
      issue = that._injectRelational(issue, r.components, 'project', 'key');
      issue = that._injectRelational(issue, r.components, 'subProject', 'key');
      issue = that._injectRelational(issue, r.rules, 'rule', 'key');
      issue = that._injectRelational(issue, r.users, 'assignee', 'login');
      issue = that._injectRelational(issue, r.users, 'reporter', 'login');
      issue = that._injectRelational(issue, r.actionPlans, 'actionPlan', 'key');
      issue = that._injectCommentsRelational(issue, r.users);
      issue = that._prepareClosed(issue);
      issue = that.ensureTextRange(issue);
      return issue;
    });
  },

  setIndex: function () {
    return this.forEach(function (issue, index) {
      return issue.set({ index: index });
    });
  },

  selectByKeys: function (keys) {
    var that = this;
    keys.forEach(function (key) {
      var issue = that.get(key);
      if (issue) {
        issue.set({ selected: true });
      }
    });
  }
});