2020-08-12 20:10:06 -04:00
|
|
|
import { find } from 'lodash';
|
|
|
|
import AccessDropdown from '~/projects/settings/access_dropdown';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as flash } from '../flash';
|
2019-05-07 13:44:46 -04:00
|
|
|
import { __ } from '~/locale';
|
2016-08-03 14:02:42 -04:00
|
|
|
|
2017-07-19 08:44:15 -04:00
|
|
|
export default class ProtectedBranchEdit {
|
|
|
|
constructor(options) {
|
2020-08-12 20:10:06 -04:00
|
|
|
this.hasLicense = options.hasLicense;
|
|
|
|
|
|
|
|
this.$wraps = {};
|
|
|
|
this.hasChanges = false;
|
2017-07-19 08:44:15 -04:00
|
|
|
this.$wrap = options.$wrap;
|
|
|
|
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
|
|
|
|
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
|
2020-08-12 20:10:06 -04:00
|
|
|
this.$codeOwnerToggle = this.$wrap.find('.js-code-owner-toggle');
|
|
|
|
|
|
|
|
this.$wraps[ACCESS_LEVELS.MERGE] = this.$allowedToMergeDropdown.closest(
|
|
|
|
`.${ACCESS_LEVELS.MERGE}-container`,
|
|
|
|
);
|
|
|
|
this.$wraps[ACCESS_LEVELS.PUSH] = this.$allowedToPushDropdown.closest(
|
|
|
|
`.${ACCESS_LEVELS.PUSH}-container`,
|
|
|
|
);
|
2016-08-03 14:02:42 -04:00
|
|
|
|
2017-07-19 08:44:15 -04:00
|
|
|
this.buildDropdowns();
|
2020-08-12 20:10:06 -04:00
|
|
|
this.bindEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
|
|
|
if (this.hasLicense) {
|
|
|
|
this.$codeOwnerToggle.on('click', this.onCodeOwnerToggleClick.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onCodeOwnerToggleClick() {
|
|
|
|
this.$codeOwnerToggle.toggleClass('is-checked');
|
|
|
|
this.$codeOwnerToggle.prop('disabled', true);
|
|
|
|
|
|
|
|
const formData = {
|
|
|
|
code_owner_approval_required: this.$codeOwnerToggle.hasClass('is-checked'),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.updateCodeOwnerApproval(formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCodeOwnerApproval(formData) {
|
|
|
|
axios
|
|
|
|
.patch(this.$wrap.data('url'), {
|
|
|
|
protected_branch: formData,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.$codeOwnerToggle.prop('disabled', false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2020-08-20 05:09:55 -04:00
|
|
|
flash(__('Failed to update branch!'));
|
2020-08-12 20:10:06 -04:00
|
|
|
});
|
2017-07-19 08:44:15 -04:00
|
|
|
}
|
2016-08-05 02:57:53 -04:00
|
|
|
|
2017-07-19 08:44:15 -04:00
|
|
|
buildDropdowns() {
|
|
|
|
// Allowed to merge dropdown
|
2020-08-12 20:10:06 -04:00
|
|
|
this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new AccessDropdown({
|
|
|
|
accessLevel: ACCESS_LEVELS.MERGE,
|
|
|
|
accessLevelsData: gon.merge_access_levels,
|
2017-07-19 08:44:15 -04:00
|
|
|
$dropdown: this.$allowedToMergeDropdown,
|
2020-08-12 20:10:06 -04:00
|
|
|
onSelect: this.onSelectOption.bind(this),
|
|
|
|
onHide: this.onDropdownHide.bind(this),
|
|
|
|
hasLicense: this.hasLicense,
|
2017-07-19 08:44:15 -04:00
|
|
|
});
|
2016-08-03 14:02:42 -04:00
|
|
|
|
2017-07-19 08:44:15 -04:00
|
|
|
// Allowed to push dropdown
|
2020-08-12 20:10:06 -04:00
|
|
|
this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new AccessDropdown({
|
|
|
|
accessLevel: ACCESS_LEVELS.PUSH,
|
|
|
|
accessLevelsData: gon.push_access_levels,
|
2017-07-19 08:44:15 -04:00
|
|
|
$dropdown: this.$allowedToPushDropdown,
|
2020-08-12 20:10:06 -04:00
|
|
|
onSelect: this.onSelectOption.bind(this),
|
|
|
|
onHide: this.onDropdownHide.bind(this),
|
|
|
|
hasLicense: this.hasLicense,
|
2017-07-19 08:44:15 -04:00
|
|
|
});
|
|
|
|
}
|
2016-08-05 02:57:53 -04:00
|
|
|
|
2020-08-12 20:10:06 -04:00
|
|
|
onSelectOption() {
|
|
|
|
this.hasChanges = true;
|
|
|
|
}
|
2020-07-27 14:09:54 -04:00
|
|
|
|
2020-08-12 20:10:06 -04:00
|
|
|
onDropdownHide() {
|
|
|
|
if (!this.hasChanges) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-27 14:09:54 -04:00
|
|
|
|
2020-08-12 20:10:06 -04:00
|
|
|
this.hasChanges = true;
|
|
|
|
this.updatePermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePermissions() {
|
|
|
|
const formData = Object.keys(ACCESS_LEVELS).reduce((acc, level) => {
|
|
|
|
const accessLevelName = ACCESS_LEVELS[level];
|
|
|
|
const inputData = this[`${accessLevelName}_dropdown`].getInputData(accessLevelName);
|
|
|
|
acc[`${accessLevelName}_attributes`] = inputData;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2017-01-03 11:31:43 -05:00
|
|
|
|
2018-10-10 02:48:36 -04:00
|
|
|
axios
|
|
|
|
.patch(this.$wrap.data('url'), {
|
2020-08-12 20:10:06 -04:00
|
|
|
protected_branch: formData,
|
2018-10-10 02:48:36 -04:00
|
|
|
})
|
2020-08-12 20:10:06 -04:00
|
|
|
.then(({ data }) => {
|
|
|
|
this.hasChanges = false;
|
|
|
|
|
|
|
|
Object.keys(ACCESS_LEVELS).forEach(level => {
|
|
|
|
const accessLevelName = ACCESS_LEVELS[level];
|
|
|
|
|
|
|
|
// The data coming from server will be the new persisted *state* for each dropdown
|
|
|
|
this.setSelectedItemsToDropdown(data[accessLevelName], `${accessLevelName}_dropdown`);
|
|
|
|
});
|
2018-10-10 02:48:36 -04:00
|
|
|
this.$allowedToMergeDropdown.enable();
|
|
|
|
this.$allowedToPushDropdown.enable();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.$allowedToMergeDropdown.enable();
|
|
|
|
this.$allowedToPushDropdown.enable();
|
2020-08-20 05:09:55 -04:00
|
|
|
flash(__('Failed to update branch!'));
|
2020-07-27 20:10:23 -04:00
|
|
|
});
|
2020-07-27 14:09:54 -04:00
|
|
|
}
|
2020-08-12 20:10:06 -04:00
|
|
|
|
|
|
|
setSelectedItemsToDropdown(items = [], dropdownName) {
|
|
|
|
const itemsToAdd = items.map(currentItem => {
|
|
|
|
if (currentItem.user_id) {
|
|
|
|
// Do this only for users for now
|
|
|
|
// get the current data for selected items
|
|
|
|
const selectedItems = this[dropdownName].getSelectedItems();
|
|
|
|
const currentSelectedItem = find(selectedItems, {
|
|
|
|
user_id: currentItem.user_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: currentItem.id,
|
|
|
|
user_id: currentItem.user_id,
|
|
|
|
type: LEVEL_TYPES.USER,
|
|
|
|
persisted: true,
|
|
|
|
name: currentSelectedItem.name,
|
|
|
|
username: currentSelectedItem.username,
|
|
|
|
avatar_url: currentSelectedItem.avatar_url,
|
|
|
|
};
|
|
|
|
} else if (currentItem.group_id) {
|
|
|
|
return {
|
|
|
|
id: currentItem.id,
|
|
|
|
group_id: currentItem.group_id,
|
|
|
|
type: LEVEL_TYPES.GROUP,
|
|
|
|
persisted: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: currentItem.id,
|
|
|
|
access_level: currentItem.access_level,
|
|
|
|
type: LEVEL_TYPES.ROLE,
|
|
|
|
persisted: true,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this[dropdownName].setSelectedItems(itemsToAdd);
|
|
|
|
}
|
2017-07-19 08:44:15 -04:00
|
|
|
}
|