|
32 | 32 | this.set('stateClass', 'close');
|
33 | 33 | return 'Closed';
|
34 | 34 | }
|
35 |
| - if (labelsNames.indexOf('status-sitewait') > -1) { |
| 35 | + if (labelsNames.indexOf('sitewait') > -1) { |
36 | 36 | this.set('stateClass', 'sitewait');
|
37 | 37 | return 'Site Contacted';
|
38 | 38 | }
|
39 |
| - if (labelsNames.indexOf('status-contactready') > -1) { |
| 39 | + if (labelsNames.indexOf('contactready') > -1) { |
40 | 40 | this.set('stateClass', 'ready');
|
41 | 41 | return 'Ready for Outreach';
|
42 | 42 | }
|
43 |
| - if (labelsNames.indexOf('status-needsdiagnosis') > -1) { |
| 43 | + if (labelsNames.indexOf('needsdiagnosis') > -1) { |
44 | 44 | this.set('stateClass', 'need');
|
45 | 45 | return 'Needs Diagnosis';
|
46 | 46 | }
|
47 | 47 | //New is the default value.
|
48 | 48 | this.set('stateClass', 'new');
|
49 | 49 | return 'New Issue';
|
50 | 50 | },
|
| 51 | + // See also issues.AllLabels#removeNamespaces |
| 52 | + removeNamespaces: function(labelsArray) { |
| 53 | + // Return a copy of labelsArray with the namespaces removed. |
| 54 | + var namespaceRegex = /(browser|closed|os|status)-/i; |
| 55 | + var labelsCopy = _.cloneDeep(labelsArray); |
| 56 | + return _.map(labelsCopy, function(labelObject) { |
| 57 | + labelObject.name = labelObject.name.replace(namespaceRegex, ''); |
| 58 | + return labelObject; |
| 59 | + }); |
| 60 | + }, |
51 | 61 | parse: function(response) {
|
| 62 | + var labels = this.removeNamespaces(response.labels); |
52 | 63 | this.set({
|
53 | 64 | body: md.render(response.body),
|
54 | 65 | commentNumber: response.comments,
|
55 | 66 | createdAt: response.created_at.slice(0, 10),
|
56 |
| - issueState: this.getState(response.state, response.labels), |
57 |
| - labels: response.labels, |
| 67 | + issueState: this.getState(response.state, labels), |
| 68 | + labels: labels, |
58 | 69 | number: response.number,
|
59 | 70 | reporter: response.user.login,
|
60 | 71 | reporterAvatar: response.user.avatar_url,
|
|
83 | 94 | });
|
84 | 95 | },
|
85 | 96 | updateLabels: function(labelsArray) {
|
86 |
| - // maybe this should be in a shared config file outside of python/JS |
87 |
| - var statusLabels = ['contactready', 'needscontact', 'needsdiagnosis', 'sitewait', ' closed-duplicate', 'closed-fixed', 'closed-invalid']; |
88 |
| - var browserLabels = ['chrome', 'firefox', 'ie', 'opera', 'safari', 'vivaldi']; |
89 |
| - var osLabels = ['android', 'fxos', 'ios', 'linux', 'mac', 'win']; |
90 |
| - // we check if we need to append the correct string before sending stuff back |
91 |
| - for (var i = labelsArray.length - 1; i >= 0; i--) { |
92 |
| - if (statusLabels.indexOf(labelsArray[i]) !== -1) { |
93 |
| - labelsArray[i] = 'status-'.concat(labelsArray[i]); |
94 |
| - } else if (browserLabels.indexOf(labelsArray[i]) !== -1) { |
95 |
| - labelsArray[i] = 'browser-'.concat(labelsArray[i]); |
96 |
| - } else if (osLabels.indexOf(labelsArray[i]) !== -1) { |
97 |
| - labelsArray[i] = 'os-'.concat(labelsArray[i]); |
98 |
| - } |
99 |
| - } |
100 |
| - var self = this; |
101 |
| - if (!$.isArray(labelsArray)) { |
102 |
| - return; |
103 |
| - } |
| 97 | + var namespaceRegex = '^(browser|closed|os|status)-'; |
| 98 | + var repoLabelsArray = _.pluck(this.get('repoLabels').get('namespacedLabels'), |
| 99 | + 'name'); |
104 | 100 |
|
105 |
| - // save ourselves a request if nothing has changed. |
106 |
| - if (_.isEqual(labelsArray.sort(), |
107 |
| - _.pluck(this.get('labels'), 'name').sort())) { |
| 101 | + // Save ourselves some requests in case nothing has changed. |
| 102 | + if (!$.isArray(labelsArray) || |
| 103 | + _.isEqual(labelsArray.sort(), _.pluck(this.get('labels'), 'name').sort())) { |
108 | 104 | return;
|
109 | 105 | }
|
110 | 106 |
|
| 107 | + // Reconstruct the namespaced labels by comparing the "new" labels |
| 108 | + // against the original namespaced labels from the repo. |
| 109 | + // |
| 110 | + // for each label in the labels array |
| 111 | + // filter over each repoLabel in the repoLabelsArray |
| 112 | + // if a regex from namespaceRegex + label matches against repoLabel |
| 113 | + // return that (and flatten the result because it's now an array of N arrays) |
| 114 | + var labelsToUpdate = _.flatten(_.map(labelsArray, function(label) { |
| 115 | + return _.filter(repoLabelsArray, function(repoLabel) { |
| 116 | + if (new RegExp(namespaceRegex + label + '$', 'i').test(repoLabel)) { |
| 117 | + return repoLabel; |
| 118 | + } |
| 119 | + }); |
| 120 | + })); |
| 121 | + |
111 | 122 | $.ajax({
|
112 | 123 | contentType: 'application/json',
|
113 |
| - data: JSON.stringify(labelsArray), |
| 124 | + data: JSON.stringify(labelsToUpdate), |
114 | 125 | type: 'POST',
|
115 | 126 | url: '/api/issues/' + this.get('number') + '/labels',
|
116 |
| - success: function(response) { |
| 127 | + success: _.bind(function(response) { |
117 | 128 | //update model after success
|
118 |
| - self.set('labels', response); |
119 |
| - }, |
| 129 | + this.set('labels', response); |
| 130 | + }, this), |
120 | 131 | error: function() {
|
121 | 132 | var msg = 'There was an error setting labels.';
|
122 | 133 | wcEvents.trigger('flash:error', {message: msg, timeout: 2000});
|
|
0 commit comments