2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2020-10-24 23:57:45 +05:30
|
|
|
import CreateItemDropdown from '~/create_item_dropdown';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { createAlert } from '~/flash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2019-07-31 22:56:46 +05:30
|
|
|
import { __ } from '~/locale';
|
2021-03-11 19:13:27 +05:30
|
|
|
import AccessDropdown from '~/projects/settings/access_dropdown';
|
2022-05-07 20:08:51 +05:30
|
|
|
import { initToggle } from '~/toggles';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
export default class ProtectedBranchCreate {
|
2020-10-24 23:57:45 +05:30
|
|
|
constructor(options) {
|
|
|
|
this.hasLicense = options.hasLicense;
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$form = $('.js-new-protected-branch');
|
2021-11-11 11:23:49 +05:30
|
|
|
this.isLocalStorageAvailable = AccessorUtilities.canUseLocalStorage();
|
2018-03-17 18:26:18 +05:30
|
|
|
this.currentProjectUserDefaults = {};
|
2017-09-10 17:25:29 +05:30
|
|
|
this.buildDropdowns();
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
this.forcePushToggle = initToggle(document.querySelector('.js-force-push-toggle'));
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
if (this.hasLicense) {
|
2022-05-07 20:08:51 +05:30
|
|
|
this.codeOwnerToggle = initToggle(document.querySelector('.js-code-owner-toggle'));
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
this.bindEvents();
|
2021-04-17 20:07:23 +05:30
|
|
|
}
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
bindEvents() {
|
|
|
|
this.$form.on('submit', this.onFormSubmit.bind(this));
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
buildDropdowns() {
|
|
|
|
const $allowedToMergeDropdown = this.$form.find('.js-allowed-to-merge');
|
|
|
|
const $allowedToPushDropdown = this.$form.find('.js-allowed-to-push');
|
|
|
|
|
|
|
|
// Cache callback
|
|
|
|
this.onSelectCallback = this.onSelect.bind(this);
|
|
|
|
|
|
|
|
// Allowed to Merge dropdown
|
2020-10-24 23:57:45 +05:30
|
|
|
this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new AccessDropdown({
|
2017-09-10 17:25:29 +05:30
|
|
|
$dropdown: $allowedToMergeDropdown,
|
2020-10-24 23:57:45 +05:30
|
|
|
accessLevelsData: gon.merge_access_levels,
|
2017-09-10 17:25:29 +05:30
|
|
|
onSelect: this.onSelectCallback,
|
2020-10-24 23:57:45 +05:30
|
|
|
accessLevel: ACCESS_LEVELS.MERGE,
|
|
|
|
hasLicense: this.hasLicense,
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
// Allowed to Push dropdown
|
2020-10-24 23:57:45 +05:30
|
|
|
this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new AccessDropdown({
|
2017-09-10 17:25:29 +05:30
|
|
|
$dropdown: $allowedToPushDropdown,
|
2020-10-24 23:57:45 +05:30
|
|
|
accessLevelsData: gon.push_access_levels,
|
2017-09-10 17:25:29 +05:30
|
|
|
onSelect: this.onSelectCallback,
|
2020-10-24 23:57:45 +05:30
|
|
|
accessLevel: ACCESS_LEVELS.PUSH,
|
|
|
|
hasLicense: this.hasLicense,
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
this.createItemDropdown = new CreateItemDropdown({
|
2020-10-24 23:57:45 +05:30
|
|
|
$dropdown: this.$form.find('.js-protected-branch-select'),
|
2019-07-31 22:56:46 +05:30
|
|
|
defaultToggleLabel: __('Protected Branch'),
|
2018-03-17 18:26:18 +05:30
|
|
|
fieldName: 'protected_branch[name]',
|
2017-09-10 17:25:29 +05:30
|
|
|
onSelect: this.onSelectCallback,
|
2018-03-17 18:26:18 +05:30
|
|
|
getData: ProtectedBranchCreate.getProtectedBranches,
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
// Enable submit button after selecting an option
|
2017-09-10 17:25:29 +05:30
|
|
|
onSelect() {
|
2020-10-24 23:57:45 +05:30
|
|
|
const $allowedToMerge = this[`${ACCESS_LEVELS.MERGE}_dropdown`].getSelectedItems();
|
|
|
|
const $allowedToPush = this[`${ACCESS_LEVELS.PUSH}_dropdown`].getSelectedItems();
|
|
|
|
const toggle = !(
|
|
|
|
this.$form.find('input[name="protected_branch[name]"]').val() &&
|
|
|
|
$allowedToMerge.length &&
|
|
|
|
$allowedToPush.length
|
2018-03-17 18:26:18 +05:30
|
|
|
);
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
this.$form.find('button[type="submit"]').attr('disabled', toggle);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static getProtectedBranches(term, callback) {
|
|
|
|
callback(gon.open_branches);
|
|
|
|
}
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
getFormData() {
|
|
|
|
const formData = {
|
|
|
|
authenticity_token: this.$form.find('input[name="authenticity_token"]').val(),
|
|
|
|
protected_branch: {
|
|
|
|
name: this.$form.find('input[name="protected_branch[name]"]').val(),
|
2022-05-07 20:08:51 +05:30
|
|
|
allow_force_push: this.forcePushToggle.value,
|
|
|
|
code_owner_approval_required: this.codeOwnerToggle?.value ?? false,
|
2020-10-24 23:57:45 +05:30
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
Object.keys(ACCESS_LEVELS).forEach((level) => {
|
2020-10-24 23:57:45 +05:30
|
|
|
const accessLevel = ACCESS_LEVELS[level];
|
|
|
|
const selectedItems = this[`${accessLevel}_dropdown`].getSelectedItems();
|
|
|
|
const levelAttributes = [];
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
selectedItems.forEach((item) => {
|
2020-10-24 23:57:45 +05:30
|
|
|
if (item.type === LEVEL_TYPES.USER) {
|
|
|
|
levelAttributes.push({
|
|
|
|
user_id: item.user_id,
|
|
|
|
});
|
|
|
|
} else if (item.type === LEVEL_TYPES.ROLE) {
|
|
|
|
levelAttributes.push({
|
|
|
|
access_level: item.access_level,
|
|
|
|
});
|
|
|
|
} else if (item.type === LEVEL_TYPES.GROUP) {
|
|
|
|
levelAttributes.push({
|
|
|
|
group_id: item.group_id,
|
|
|
|
});
|
2021-01-03 14:25:43 +05:30
|
|
|
} else if (item.type === LEVEL_TYPES.DEPLOY_KEY) {
|
|
|
|
levelAttributes.push({
|
|
|
|
deploy_key_id: item.deploy_key_id,
|
|
|
|
});
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
formData.protected_branch[`${accessLevel}_attributes`] = levelAttributes;
|
|
|
|
});
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFormSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
axios[this.$form.attr('method')](this.$form.attr('action'), this.getFormData())
|
|
|
|
.then(() => {
|
|
|
|
window.location.reload();
|
|
|
|
})
|
2021-09-30 23:02:18 +05:30
|
|
|
.catch(() =>
|
2022-11-25 23:54:43 +05:30
|
|
|
createAlert({
|
2021-09-30 23:02:18 +05:30
|
|
|
message: __('Failed to protect the branch'),
|
|
|
|
}),
|
|
|
|
);
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|