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
|
define(function () {
return Backbone.Model.extend({
idAttribute: 'key',
defaults: function () {
return {
secondaryLocations: [],
executionFlows: []
};
},
url: function () {
return baseUrl + '/api/issues/show?key=' + this.get('key');
},
urlRoot: function () {
return baseUrl + '/api/issues';
},
parse: function (r) {
if (r.issue) {
var issue = this._injectRelational(r.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._injectRelational(issue, r.users, 'reporter', 'login');
issue = this._injectRelational(issue, r.actionPlans, 'actionPlan', 'key');
issue = this._prepareClosed(issue);
return issue;
} else {
return r;
}
},
_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;
},
_prepareClosed: function (issue) {
if (issue.status === 'CLOSED') {
issue.secondaryLocations = [];
issue.executionFlows = [];
delete issue.textRange;
}
return issue;
},
sync: function (method, model, options) {
var opts = options || {};
opts.contentType = 'application/x-www-form-urlencoded';
if (method === 'read') {
_.extend(opts, {
type: 'GET',
url: this.urlRoot() + '/show',
data: { key: model.id }
});
}
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')
}
});
}
var 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: function (attrs, options) {
for (var 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: function (options) {
var model = this;
var success = function (r) {
var attrs = model.parse(r);
model.reset(attrs);
if (options.success) {
options.success(model, r, options);
}
};
var opts = _.extend({ type: 'POST' }, options, { success: success });
var 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: function (assignee, options) {
var opts = _.extend({
url: this.urlRoot() + '/assign',
data: { issue: this.id, assignee: 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: function (plan, options) {
var opts = _.extend({
url: this.urlRoot() + '/plan',
data: { issue: this.id, plan: 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: function (severity, options) {
var opts = _.extend({
url: this.urlRoot() + '/set_severity',
data: { issue: this.id, severity: 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: function (transition, options) {
var opts = _.extend({
url: this.urlRoot() + '/do_transition',
data: { issue: this.id, transition: transition }
}, options);
return this._action(opts);
},
getLinearLocations: function () {
var textRange = this.get('textRange');
if (!textRange) {
return [];
}
var locations = [];
for (var line = textRange.startLine; line <= textRange.endLine; line++) {
// TODO fix 999999
var from = line === textRange.startLine ? textRange.startOffset : 0,
to = line === textRange.endLine ? textRange.endOffset : 999999;
locations.push({ line: line, from: from, to: to });
}
return locations;
}
});
});
|