debian-mirror-gitlab/app/assets/javascripts/protected_branches/protected_branch_create.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

192 lines
5.9 KiB
JavaScript
Raw Normal View History

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';
2023-06-20 00:43:36 +05:30
import { createAlert, VARIANT_SUCCESS } from '~/alert';
2021-03-11 19:13:27 +05:30
import AccessorUtilities from '~/lib/utils/accessor';
import axios from '~/lib/utils/axios_utils';
2023-06-20 00:43:36 +05:30
import { __, s__ } 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';
2023-06-20 00:43:36 +05:30
import { expandSection } from '~/settings_panels';
import { scrollToElement } from '~/lib/utils/common_utils';
import {
BRANCH_RULES_ANCHOR,
PROTECTED_BRANCHES_ANCHOR,
IS_PROTECTED_BRANCH_CREATED,
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
}
2023-06-20 00:43:36 +05:30
this.showSuccessAlertIfNeeded();
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
2023-06-20 00:43:36 +05:30
// eslint-disable-next-line class-methods-use-this
expandAndScroll(anchor) {
expandSection(anchor);
scrollToElement(anchor);
}
hasProtectedBranchSuccessAlert() {
return (
window.gon?.features?.branchRules &&
this.isLocalStorageAvailable &&
localStorage.getItem(IS_PROTECTED_BRANCH_CREATED)
);
}
createSuccessAlert() {
this.alert = createAlert({
variant: VARIANT_SUCCESS,
containerSelector: '.js-alert-protected-branch-created-container',
title: s__('ProtectedBranch|View protected branches as branch rules'),
message: s__('ProtectedBranch|Manage branch related settings in one area with branch rules.'),
primaryButton: {
text: s__('ProtectedBranch|View branch rule'),
clickHandler: () => {
this.expandAndScroll(BRANCH_RULES_ANCHOR);
},
},
secondaryButton: {
text: __('Dismiss'),
clickHandler: () => this.alert.dismiss(),
},
});
}
showSuccessAlertIfNeeded() {
if (!this.hasProtectedBranchSuccessAlert()) {
return;
}
this.expandAndScroll(PROTECTED_BRANCHES_ANCHOR);
this.createSuccessAlert();
localStorage.removeItem(IS_PROTECTED_BRANCH_CREATED);
}
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(() => {
2023-06-20 00:43:36 +05:30
if (this.isLocalStorageAvailable) {
localStorage.setItem(IS_PROTECTED_BRANCH_CREATED, 'true');
}
2020-10-24 23:57:45 +05:30
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
}