|
94 | 94 | });
|
95 | 95 | },
|
96 | 96 | updateLabels: function(labelsArray) {
|
97 |
| - // maybe this should be in a shared config file outside of python/JS |
98 |
| - var statusLabels = ['contactready', 'needscontact', 'needsdiagnosis', 'sitewait', ' closed-duplicate', 'closed-fixed', 'closed-invalid']; |
99 |
| - var browserLabels = ['chrome', 'firefox', 'ie', 'opera', 'safari', 'vivaldi']; |
100 |
| - var osLabels = ['android', 'fxos', 'ios', 'linux', 'mac', 'win']; |
101 |
| - // we check if we need to append the correct string before sending stuff back |
102 |
| - for (var i = labelsArray.length - 1; i >= 0; i--) { |
103 |
| - if (statusLabels.indexOf(labelsArray[i]) !== -1) { |
104 |
| - labelsArray[i] = 'status-'.concat(labelsArray[i]); |
105 |
| - } else if (browserLabels.indexOf(labelsArray[i]) !== -1) { |
106 |
| - labelsArray[i] = 'browser-'.concat(labelsArray[i]); |
107 |
| - } else if (osLabels.indexOf(labelsArray[i]) !== -1) { |
108 |
| - labelsArray[i] = 'os-'.concat(labelsArray[i]); |
109 |
| - } |
110 |
| - } |
111 |
| - var self = this; |
112 |
| - if (!$.isArray(labelsArray)) { |
113 |
| - return; |
114 |
| - } |
| 97 | + var namespaceRegex = '^(browser|closed|os|status)-'; |
| 98 | + var repoLabelsArray = _.pluck(this.get('repoLabels').get('namespacedLabels'), |
| 99 | + 'name'); |
115 | 100 |
|
116 |
| - // save ourselves a request if nothing has changed. |
117 |
| - if (_.isEqual(labelsArray.sort(), |
118 |
| - _.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())) { |
119 | 104 | return;
|
120 | 105 | }
|
121 | 106 |
|
| 107 | + // for each label in labels array |
| 108 | + // filter over each repoLabel in repoLabelsArray |
| 109 | + // if a regex from namespaceRegex + label matches against repoLabel |
| 110 | + // return that (and flatten the result because it's now an array of 3 arrays) |
| 111 | + var labelsToUpdate = _.flatten(_.map(labelsArray, function(label) { |
| 112 | + return _.filter(repoLabelsArray, function(repoLabel) { |
| 113 | + if (new RegExp(namespaceRegex + label + '$', 'i').test(repoLabel)) { |
| 114 | + return repoLabel; |
| 115 | + } |
| 116 | + }); |
| 117 | + })); |
| 118 | + |
122 | 119 | $.ajax({
|
123 | 120 | contentType: 'application/json',
|
124 |
| - data: JSON.stringify(labelsArray), |
| 121 | + data: JSON.stringify(labelsToUpdate), |
125 | 122 | type: 'POST',
|
126 | 123 | url: '/api/issues/' + this.get('number') + '/labels',
|
127 | 124 | success: function(response) {
|
|
0 commit comments