2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2020-08-13 00:10:06 +00:00
|
|
|
import CreateItemDropdown from '~/create_item_dropdown';
|
2021-06-21 03:10:27 +00:00
|
|
|
import createFlash from '~/flash';
|
2021-02-14 18:09:20 +00:00
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2019-05-07 17:44:46 +00:00
|
|
|
import { __ } from '~/locale';
|
2021-02-14 18:09:20 +00:00
|
|
|
import AccessDropdown from '~/projects/settings/access_dropdown';
|
2021-02-01 15:08:56 +00:00
|
|
|
import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
|
2017-09-20 18:24:24 +00:00
|
|
|
|
2017-07-19 12:44:15 +00:00
|
|
|
export default class ProtectedBranchCreate {
|
2020-08-13 00:10:06 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.hasLicense = options.hasLicense;
|
|
|
|
|
2017-07-19 12:44:15 +00:00
|
|
|
this.$form = $('.js-new-protected-branch');
|
2021-09-16 18:11:32 +00:00
|
|
|
this.isLocalStorageAvailable = AccessorUtilities.canUseLocalStorage();
|
2017-09-20 18:24:24 +00:00
|
|
|
this.currentProjectUserDefaults = {};
|
2017-07-19 12:44:15 +00:00
|
|
|
this.buildDropdowns();
|
2021-03-11 18:09:23 +00:00
|
|
|
this.$forcePushToggle = this.$form.find('.js-force-push-toggle');
|
2020-08-13 00:10:06 +00:00
|
|
|
this.$codeOwnerToggle = this.$form.find('.js-code-owner-toggle');
|
|
|
|
this.bindEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
2021-03-11 18:09:23 +00:00
|
|
|
this.$forcePushToggle.on('click', this.onForcePushToggleClick.bind(this));
|
2020-08-13 00:10:06 +00:00
|
|
|
if (this.hasLicense) {
|
|
|
|
this.$codeOwnerToggle.on('click', this.onCodeOwnerToggleClick.bind(this));
|
|
|
|
}
|
|
|
|
this.$form.on('submit', this.onFormSubmit.bind(this));
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:09:23 +00:00
|
|
|
onForcePushToggleClick() {
|
|
|
|
this.$forcePushToggle.toggleClass('is-checked');
|
|
|
|
}
|
|
|
|
|
2020-08-13 00:10:06 +00:00
|
|
|
onCodeOwnerToggleClick() {
|
|
|
|
this.$codeOwnerToggle.toggleClass('is-checked');
|
2017-07-19 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-13 00:10:06 +00:00
|
|
|
this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new AccessDropdown({
|
2017-07-19 12:44:15 +00:00
|
|
|
$dropdown: $allowedToMergeDropdown,
|
2020-08-13 00:10:06 +00:00
|
|
|
accessLevelsData: gon.merge_access_levels,
|
2017-07-19 12:44:15 +00:00
|
|
|
onSelect: this.onSelectCallback,
|
2020-08-13 00:10:06 +00:00
|
|
|
accessLevel: ACCESS_LEVELS.MERGE,
|
|
|
|
hasLicense: this.hasLicense,
|
2017-07-19 12:44:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Allowed to Push dropdown
|
2020-08-13 00:10:06 +00:00
|
|
|
this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new AccessDropdown({
|
2017-07-19 12:44:15 +00:00
|
|
|
$dropdown: $allowedToPushDropdown,
|
2020-08-13 00:10:06 +00:00
|
|
|
accessLevelsData: gon.push_access_levels,
|
2017-07-19 12:44:15 +00:00
|
|
|
onSelect: this.onSelectCallback,
|
2020-08-13 00:10:06 +00:00
|
|
|
accessLevel: ACCESS_LEVELS.PUSH,
|
|
|
|
hasLicense: this.hasLicense,
|
2017-07-19 12:44:15 +00:00
|
|
|
});
|
|
|
|
|
2018-01-12 07:32:41 +00:00
|
|
|
this.createItemDropdown = new CreateItemDropdown({
|
2020-08-13 00:10:06 +00:00
|
|
|
$dropdown: this.$form.find('.js-protected-branch-select'),
|
2019-05-07 17:44:46 +00:00
|
|
|
defaultToggleLabel: __('Protected Branch'),
|
2018-01-12 07:32:41 +00:00
|
|
|
fieldName: 'protected_branch[name]',
|
2017-07-19 12:44:15 +00:00
|
|
|
onSelect: this.onSelectCallback,
|
2018-01-12 07:32:41 +00:00
|
|
|
getData: ProtectedBranchCreate.getProtectedBranches,
|
2017-07-19 12:44:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 00:10:06 +00:00
|
|
|
// Enable submit button after selecting an option
|
2017-07-19 12:44:15 +00:00
|
|
|
onSelect() {
|
2020-08-13 00:10:06 +00:00
|
|
|
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
|
2017-10-04 21:56:20 +00:00
|
|
|
);
|
2017-09-20 18:24:24 +00:00
|
|
|
|
2020-08-13 00:10:06 +00:00
|
|
|
this.$form.find('input[type="submit"]').attr('disabled', toggle);
|
2017-09-20 18:24:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 07:32:41 +00:00
|
|
|
static getProtectedBranches(term, callback) {
|
|
|
|
callback(gon.open_branches);
|
|
|
|
}
|
2020-08-13 00:10:06 +00:00
|
|
|
|
|
|
|
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(),
|
2021-03-11 18:09:23 +00:00
|
|
|
allow_force_push: this.$forcePushToggle.hasClass('is-checked'),
|
2020-08-13 00:10:06 +00:00
|
|
|
code_owner_approval_required: this.$codeOwnerToggle.hasClass('is-checked'),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
Object.keys(ACCESS_LEVELS).forEach((level) => {
|
2020-08-13 00:10:06 +00:00
|
|
|
const accessLevel = ACCESS_LEVELS[level];
|
|
|
|
const selectedItems = this[`${accessLevel}_dropdown`].getSelectedItems();
|
|
|
|
const levelAttributes = [];
|
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
selectedItems.forEach((item) => {
|
2020-08-13 00:10:06 +00:00
|
|
|
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,
|
|
|
|
});
|
2020-09-22 09:09:43 +00:00
|
|
|
} else if (item.type === LEVEL_TYPES.DEPLOY_KEY) {
|
|
|
|
levelAttributes.push({
|
|
|
|
deploy_key_id: item.deploy_key_id,
|
|
|
|
});
|
2020-08-13 00:10:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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-06-21 03:10:27 +00:00
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('Failed to protect the branch'),
|
|
|
|
}),
|
|
|
|
);
|
2020-08-13 00:10:06 +00:00
|
|
|
}
|
2017-07-19 12:44:15 +00:00
|
|
|
}
|