Skip to content

Commit 6c21577

Browse files
author
Mike Taylor
committed
Issue #660. Construct a labelsToUpdate array based on the original repo labels.
1 parent 21bc5b2 commit 6c21577

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

webcompat/static/js/lib/models/issue.js

+19-22
Original file line numberDiff line numberDiff line change
@@ -94,34 +94,31 @@
9494
});
9595
},
9696
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');
115100

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())) {
119104
return;
120105
}
121106

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+
122119
$.ajax({
123120
contentType: 'application/json',
124-
data: JSON.stringify(labelsArray),
121+
data: JSON.stringify(labelsToUpdate),
125122
type: 'POST',
126123
url: '/api/issues/' + this.get('number') + '/labels',
127124
success: function(response) {

0 commit comments

Comments
 (0)