Commit 1de21943 authored by Tim Zallmann's avatar Tim Zallmann

Merge branch 'ee-31031-convert-protected-branches-es6' into 'master'

EE - Convert Protected Branches feature JS code to ES6

See merge request !2481
parents 69067637 1067010e
export const ACCESS_LEVELS = {
MERGE: 'merge_access_levels',
PUSH: 'push_access_levels',
};
export const LEVEL_TYPES = {
ROLE: 'role',
USER: 'user',
GROUP: 'group',
};
export const LEVEL_ID_PROP = {
ROLE: 'access_level',
USER: 'user_id',
GROUP: 'group_id',
};
export const ACCESS_LEVEL_NONE = 0;
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
import './protected_branch_access_dropdown';
import './protected_branch_create'; import ProtectedBranchCreate from './protected_branch_create';
import './protected_branch_dropdown'; import ProtectedBranchEditList from './protected_branch_edit_list';
import './protected_branch_edit';
import './protected_branch_edit_list';
$(() => { $(() => {
const protectedBranchCreate = new gl.ProtectedBranchCreate(); const protectedBranchCreate = new ProtectedBranchCreate();
const protectedBranchEditList = new gl.ProtectedBranchEditList(); const protectedBranchEditList = new ProtectedBranchEditList();
}); });
/* eslint-disable no-new, arrow-parens, no-param-reassign, comma-dangle, guard-for-in, no-restricted-syntax, max-len */
/* global ProtectedBranchDropdown */
/* global Flash */ /* global Flash */
(global => { import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
global.gl = global.gl || {}; import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
import ProtectedBranchDropdown from './protected_branch_dropdown';
const ACCESS_LEVELS = {
MERGE: 'merge_access_levels', export default class ProtectedBranchCreate {
PUSH: 'push_access_levels', constructor() {
}; this.$form = $('.js-new-protected-branch');
this.buildDropdowns();
const LEVEL_TYPES = { this.$branchInput = this.$form.find('input[name="protected_branch[name]"]');
ROLE: 'role', this.bindEvents();
USER: 'user', }
GROUP: 'group'
}; bindEvents() {
this.$form.on('submit', this.onFormSubmit.bind(this));
gl.ProtectedBranchCreate = class { }
constructor() {
this.$wrap = this.$form = $('.js-new-protected-branch'); buildDropdowns() {
this.buildDropdowns(); const $allowedToMergeDropdown = this.$form.find('.js-allowed-to-merge');
this.$branchInput = this.$wrap.find('input[name="protected_branch[name]"]'); const $allowedToPushDropdown = this.$form.find('.js-allowed-to-push');
this.bindEvents();
} // Cache callback
this.onSelectCallback = this.onSelect.bind(this);
bindEvents() {
this.$form.on('submit', this.onFormSubmit.bind(this)); // Allowed to Merge dropdown
} this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new ProtectedBranchAccessDropdown({
$dropdown: $allowedToMergeDropdown,
buildDropdowns() { accessLevelsData: gon.merge_access_levels,
const $allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge'); onSelect: this.onSelectCallback,
const $allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push'); accessLevel: ACCESS_LEVELS.MERGE,
});
// Cache callback
this.onSelectCallback = this.onSelect.bind(this); // Allowed to Push dropdown
this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new ProtectedBranchAccessDropdown({
// Allowed to Merge dropdown $dropdown: $allowedToPushDropdown,
this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new gl.ProtectedBranchAccessDropdown({ accessLevelsData: gon.push_access_levels,
$dropdown: $allowedToMergeDropdown, onSelect: this.onSelectCallback,
accessLevelsData: gon.merge_access_levels, accessLevel: ACCESS_LEVELS.PUSH,
onSelect: this.onSelectCallback, });
accessLevel: ACCESS_LEVELS.MERGE
}); this.protectedBranchDropdown = new ProtectedBranchDropdown({
$dropdown: this.$form.find('.js-protected-branch-select'),
// Allowed to Push dropdown onSelect: this.onSelectCallback,
this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new gl.ProtectedBranchAccessDropdown({ });
$dropdown: $allowedToPushDropdown, }
accessLevelsData: gon.push_access_levels,
onSelect: this.onSelectCallback, // Enable submit button after selecting an option
accessLevel: ACCESS_LEVELS.PUSH onSelect() {
}); const $allowedToMerge = this[`${ACCESS_LEVELS.MERGE}_dropdown`].getSelectedItems();
const $allowedToPush = this[`${ACCESS_LEVELS.PUSH}_dropdown`].getSelectedItems();
// Protected branch dropdown const toggle = !(this.$form.find('input[name="protected_branch[name]"]').val() && $allowedToMerge.length && $allowedToPush.length);
new window.ProtectedBranchDropdown({
$dropdown: this.$wrap.find('.js-protected-branch-select'), this.$form.find('input[type="submit"]').attr('disabled', toggle);
onSelect: this.onSelectCallback }
});
} getFormData() {
const formData = {
// Enable submit button after selecting an option authenticity_token: this.$form.find('input[name="authenticity_token"]').val(),
onSelect() { protected_branch: {
const $allowedToMerge = this[`${ACCESS_LEVELS.MERGE}_dropdown`].getSelectedItems(); name: this.$form.find('input[name="protected_branch[name]"]').val(),
const $allowedToPush = this[`${ACCESS_LEVELS.PUSH}_dropdown`].getSelectedItems(); },
const toggle = !(this.$wrap.find('input[name="protected_branch[name]"]').val() && $allowedToMerge.length && $allowedToPush.length); };
this.$form.find('input[type="submit"]').attr('disabled', toggle); Object.keys(ACCESS_LEVELS).forEach((level) => {
} const accessLevel = ACCESS_LEVELS[level];
const selectedItems = this[`${accessLevel}_dropdown`].getSelectedItems();
getFormData() { const levelAttributes = [];
const formData = {
authenticity_token: this.$form.find('input[name="authenticity_token"]').val(), selectedItems.forEach((item) => {
protected_branch: { if (item.type === LEVEL_TYPES.USER) {
name: this.$wrap.find('input[name="protected_branch[name]"]').val(), levelAttributes.push({
} user_id: item.user_id,
}; });
} else if (item.type === LEVEL_TYPES.ROLE) {
for (const ACCESS_LEVEL in ACCESS_LEVELS) { levelAttributes.push({
const selectedItems = this[`${ACCESS_LEVELS[ACCESS_LEVEL]}_dropdown`].getSelectedItems(); access_level: item.access_level,
const levelAttributes = []; });
} else if (item.type === LEVEL_TYPES.GROUP) {
for (let i = 0; i < selectedItems.length; i += 1) { levelAttributes.push({
const current = selectedItems[i]; group_id: item.group_id,
});
if (current.type === LEVEL_TYPES.USER) {
levelAttributes.push({
user_id: selectedItems[i].user_id
});
} else if (current.type === LEVEL_TYPES.ROLE) {
levelAttributes.push({
access_level: selectedItems[i].access_level
});
} else if (current.type === LEVEL_TYPES.GROUP) {
levelAttributes.push({
group_id: selectedItems[i].group_id
});
}
} }
formData.protected_branch[`${ACCESS_LEVELS[ACCESS_LEVEL]}_attributes`] = levelAttributes;
}
return formData;
}
onFormSubmit(e) {
e.preventDefault();
$.ajax({
url: this.$form.attr('action'),
method: this.$form.attr('method'),
data: this.getFormData()
})
.success(() => {
location.reload();
})
.fail(() => {
new Flash('Failed to protect the branch');
}); });
}
}; formData.protected_branch[`${accessLevel}_attributes`] = levelAttributes;
})(window); });
return formData;
}
onFormSubmit(e) {
e.preventDefault();
$.ajax({
url: this.$form.attr('action'),
method: this.$form.attr('method'),
data: this.getFormData(),
})
.success(() => {
location.reload();
})
.fail(() => new Flash('Failed to protect the branch'));
}
}
/* eslint-disable comma-dangle, no-unused-vars */ export default class ProtectedBranchDropdown {
/**
class ProtectedBranchDropdown { * @param {Object} options containing
* `$dropdown` target element
* `onSelect` event callback
* $dropdown must be an element created using `dropdown_tag()` rails helper
*/
constructor(options) { constructor(options) {
this.onSelect = options.onSelect; this.onSelect = options.onSelect;
this.$dropdown = options.$dropdown; this.$dropdown = options.$dropdown;
...@@ -12,7 +16,7 @@ class ProtectedBranchDropdown { ...@@ -12,7 +16,7 @@ class ProtectedBranchDropdown {
this.bindEvents(); this.bindEvents();
// Hide footer // Hide footer
this.$dropdownFooter.addClass('hidden'); this.toggleFooter(true);
} }
buildDropdown() { buildDropdown() {
...@@ -21,7 +25,7 @@ class ProtectedBranchDropdown { ...@@ -21,7 +25,7 @@ class ProtectedBranchDropdown {
filterable: true, filterable: true,
remote: false, remote: false,
search: { search: {
fields: ['title'] fields: ['title'],
}, },
selectable: true, selectable: true,
toggleLabel(selected) { toggleLabel(selected) {
...@@ -36,10 +40,9 @@ class ProtectedBranchDropdown { ...@@ -36,10 +40,9 @@ class ProtectedBranchDropdown {
}, },
onFilter: this.toggleCreateNewButton.bind(this), onFilter: this.toggleCreateNewButton.bind(this),
clicked: (options) => { clicked: (options) => {
const { $el, e } = options; options.e.preventDefault();
e.preventDefault();
this.onSelect(); this.onSelect();
} },
}); });
} }
...@@ -64,20 +67,22 @@ class ProtectedBranchDropdown { ...@@ -64,20 +67,22 @@ class ProtectedBranchDropdown {
} }
toggleCreateNewButton(branchName) { toggleCreateNewButton(branchName) {
this.selectedBranch = {
title: branchName,
id: branchName,
text: branchName
};
if (branchName) { if (branchName) {
this.selectedBranch = {
title: branchName,
id: branchName,
text: branchName,
};
this.$dropdownContainer this.$dropdownContainer
.find('.js-create-new-protected-branch code') .find('.js-create-new-protected-branch code')
.text(branchName); .text(branchName);
} }
this.$dropdownFooter.toggleClass('hidden', !branchName); this.toggleFooter(!branchName);
} }
}
window.ProtectedBranchDropdown = ProtectedBranchDropdown; toggleFooter(toggleState) {
this.$dropdownFooter.toggleClass('hidden', toggleState);
}
}
/* eslint-disable no-new, arrow-parens, no-param-reassign, comma-dangle, dot-notation, no-unused-vars, no-restricted-syntax, guard-for-in, max-len */ /* eslint-disable no-new */
/* global Flash */ /* global Flash */
(global => { import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
global.gl = global.gl || {}; import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
const ACCESS_LEVELS = { export default class ProtectedBranchEdit {
MERGE: 'merge_access_levels', constructor(options) {
PUSH: 'push_access_levels', this.$wraps = {};
}; this.hasChanges = false;
this.$wrap = options.$wrap;
const LEVEL_TYPES = { this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
ROLE: 'role', this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
USER: 'user',
GROUP: 'group' this.$wraps[ACCESS_LEVELS.MERGE] = this.$allowedToMergeDropdown.closest(`.${ACCESS_LEVELS.MERGE}-container`);
}; this.$wraps[ACCESS_LEVELS.PUSH] = this.$allowedToPushDropdown.closest(`.${ACCESS_LEVELS.PUSH}-container`);
gl.ProtectedBranchEdit = class { this.buildDropdowns();
constructor(options) { }
this.$wraps = {};
this.hasChanges = false; buildDropdowns() {
this.$wrap = options.$wrap; // Allowed to merge dropdown
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge'); this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new ProtectedBranchAccessDropdown({
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push'); accessLevel: ACCESS_LEVELS.MERGE,
accessLevelsData: gon.merge_access_levels,
this.$wraps[ACCESS_LEVELS.MERGE] = this.$allowedToMergeDropdown.closest(`.${ACCESS_LEVELS.MERGE}-container`); $dropdown: this.$allowedToMergeDropdown,
this.$wraps[ACCESS_LEVELS.PUSH] = this.$allowedToPushDropdown.closest(`.${ACCESS_LEVELS.PUSH}-container`); onSelect: this.onSelectOption.bind(this),
onHide: this.onDropdownHide.bind(this),
this.buildDropdowns(); });
}
// Allowed to push dropdown
buildDropdowns() { this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new ProtectedBranchAccessDropdown({
// Allowed to merge dropdown accessLevel: ACCESS_LEVELS.PUSH,
this['merge_access_levels_dropdown'] = new gl.ProtectedBranchAccessDropdown({ accessLevelsData: gon.push_access_levels,
accessLevel: ACCESS_LEVELS.MERGE, $dropdown: this.$allowedToPushDropdown,
accessLevelsData: gon.merge_access_levels, onSelect: this.onSelectOption.bind(this),
$dropdown: this.$allowedToMergeDropdown, onHide: this.onDropdownHide.bind(this),
onSelect: this.onSelectOption.bind(this), });
onHide: this.onDropdownHide.bind(this) }
});
onSelectOption() {
// Allowed to push dropdown this.hasChanges = true;
this['push_access_levels_dropdown'] = new gl.ProtectedBranchAccessDropdown({ }
accessLevel: ACCESS_LEVELS.PUSH,
accessLevelsData: gon.push_access_levels, onDropdownHide() {
$dropdown: this.$allowedToPushDropdown, if (!this.hasChanges) {
onSelect: this.onSelectOption.bind(this), return;
onHide: this.onDropdownHide.bind(this)
});
}
onSelectOption(item, $el, dropdownInstance) {
this.hasChanges = true;
}
onDropdownHide() {
if (!this.hasChanges) return;
this.hasChanges = true;
this.updatePermissions();
}
updatePermissions() {
const formData = {};
for (const ACCESS_LEVEL in ACCESS_LEVELS) {
const accessLevelName = ACCESS_LEVELS[ACCESS_LEVEL];
formData[`${accessLevelName}_attributes`] = this[`${accessLevelName}_dropdown`].getInputData(accessLevelName);
}
return $.ajax({
type: 'POST',
url: this.$wrap.data('url'),
dataType: 'json',
data: {
_method: 'PATCH',
protected_branch: formData
},
success: (response) => {
this.hasChanges = false;
for (const ACCESS_LEVEL in ACCESS_LEVELS) {
const accessLevelName = ACCESS_LEVELS[ACCESS_LEVEL];
// The data coming from server will be the new persisted *state* for each dropdown
this.setSelectedItemsToDropdown(response[accessLevelName], `${accessLevelName}_dropdown`);
}
},
error() {
$.scrollTo(0);
new Flash('Failed to update branch!');
}
}).always(() => {
this.$allowedToMergeDropdown.enable();
this.$allowedToPushDropdown.enable();
});
} }
setSelectedItemsToDropdown(items = [], dropdownName) { this.hasChanges = true;
const itemsToAdd = []; this.updatePermissions();
}
for (let i = 0; i < items.length; i += 1) {
let itemToAdd; updatePermissions() {
const currentItem = items[i]; const formData = Object.keys(ACCESS_LEVELS).reduce((acc, level) => {
/* eslint-disable no-param-reassign */
if (currentItem.user_id) { const accessLevelName = ACCESS_LEVELS[level];
// Do this only for users for now const inputData = this[`${accessLevelName}_dropdown`].getInputData(accessLevelName);
// get the current data for selected items acc[`${accessLevelName}_attributes`] = inputData;
const selectedItems = this[dropdownName].getSelectedItems();
const currentSelectedItem = _.findWhere(selectedItems, { user_id: currentItem.user_id }); return acc;
}, {});
itemToAdd = {
id: currentItem.id, return $.ajax({
user_id: currentItem.user_id, type: 'POST',
type: LEVEL_TYPES.USER, url: this.$wrap.data('url'),
persisted: true, dataType: 'json',
name: currentSelectedItem.name, data: {
username: currentSelectedItem.username, _method: 'PATCH',
avatar_url: currentSelectedItem.avatar_url protected_branch: formData,
}; },
} else if (currentItem.group_id) { success: (response) => {
itemToAdd = { this.hasChanges = false;
id: currentItem.id,
group_id: currentItem.group_id, Object.keys(ACCESS_LEVELS).forEach((level) => {
type: LEVEL_TYPES.GROUP, const accessLevelName = ACCESS_LEVELS[level];
persisted: true
}; // The data coming from server will be the new persisted *state* for each dropdown
} else { this.setSelectedItemsToDropdown(response[accessLevelName], `${accessLevelName}_dropdown`);
itemToAdd = { });
id: currentItem.id, },
access_level: currentItem.access_level, error() {
type: LEVEL_TYPES.ROLE, new Flash('Failed to update branch!', null, $('.js-protected-branches-list'));
persisted: true },
}; }).always(() => {
} this.$allowedToMergeDropdown.enable();
this.$allowedToPushDropdown.enable();
itemsToAdd.push(itemToAdd); });
}
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 = _.findWhere(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,
};
} }
this[dropdownName].setSelectedItems(itemsToAdd); return {
} id: currentItem.id,
}; access_level: currentItem.access_level,
})(window); type: LEVEL_TYPES.ROLE,
persisted: true,
};
});
this[dropdownName].setSelectedItems(itemsToAdd);
}
}
/* eslint-disable arrow-parens, no-param-reassign, no-new, comma-dangle */ /* eslint-disable no-new */
(global => { import ProtectedBranchEdit from './protected_branch_edit';
global.gl = global.gl || {};
gl.ProtectedBranchEditList = class { export default class ProtectedBranchEditList {
constructor() { constructor() {
this.$wrap = $('.protected-branches-list'); this.$wrap = $('.protected-branches-list');
this.initEditForm();
}
// Build edit forms initEditForm() {
this.$wrap.find('.js-protected-branch-edit-form').each((i, el) => { // Build edit forms
new gl.ProtectedBranchEdit({ this.$wrap.find('.js-protected-branch-edit-form').each((i, el) => {
$wrap: $(el) new ProtectedBranchEdit({
}); $wrap: $(el),
}); });
} });
}; }
})(window); }
...@@ -8,4 +8,10 @@ export const LEVEL_TYPES = { ...@@ -8,4 +8,10 @@ export const LEVEL_TYPES = {
GROUP: 'group', GROUP: 'group',
}; };
export const LEVEL_ID_PROP = {
ROLE: 'access_level',
USER: 'user_id',
GROUP: 'group_id',
};
export const ACCESS_LEVEL_NONE = 0; export const ACCESS_LEVEL_NONE = 0;
/* eslint-disable no-underscore-dangle, class-methods-use-this */ /* eslint-disable no-underscore-dangle, class-methods-use-this */
/* global Flash */ /* global Flash */
import { ACCESS_LEVELS, LEVEL_TYPES, ACCESS_LEVEL_NONE } from './constants'; import { LEVEL_TYPES, LEVEL_ID_PROP, ACCESS_LEVEL_NONE } from './constants';
export default class ProtectedTagAccessDropdown { export default class ProtectedTagAccessDropdown {
constructor(options) { constructor(options) {
...@@ -11,7 +11,6 @@ export default class ProtectedTagAccessDropdown { ...@@ -11,7 +11,6 @@ export default class ProtectedTagAccessDropdown {
accessLevelsData, accessLevelsData,
} = options; } = options;
this.options = options; this.options = options;
this.isAllowedToCreateDropdown = false;
this.groups = []; this.groups = [];
this.accessLevel = accessLevel; this.accessLevel = accessLevel;
this.accessLevelsData = accessLevelsData.roles; this.accessLevelsData = accessLevelsData.roles;
...@@ -25,10 +24,7 @@ export default class ProtectedTagAccessDropdown { ...@@ -25,10 +24,7 @@ export default class ProtectedTagAccessDropdown {
this.setSelectedItems([]); this.setSelectedItems([]);
this.persistPreselectedItems(); this.persistPreselectedItems();
if (ACCESS_LEVELS.CREATE === this.accessLevel) { this.noOneObj = this.accessLevelsData.find(level => level.id === ACCESS_LEVEL_NONE);
this.isAllowedToCreateDropdown = true;
this.noOneObj = this.accessLevelsData.find(level => level.id === ACCESS_LEVEL_NONE);
}
this.initDropdown(); this.initDropdown();
} }
...@@ -56,26 +52,24 @@ export default class ProtectedTagAccessDropdown { ...@@ -56,26 +52,24 @@ export default class ProtectedTagAccessDropdown {
e.preventDefault(); e.preventDefault();
if ($el.is('.is-active')) { if ($el.is('.is-active')) {
if (self.isAllowedToCreateDropdown) { if (item.id === self.noOneObj.id) {
if (item.id === self.noOneObj.id) { self.accessLevelsData.forEach((level) => {
self.accessLevelsData.forEach((level) => { if (level.id !== item.id) {
if (level.id !== item.id) { self.removeSelectedItem(level);
self.removeSelectedItem(level);
}
});
self.$wrap.find(`.item-${item.type}`).removeClass('is-active');
} else {
const $noOne = self.$wrap.find(`.is-active.item-${item.type}[data-role-id="${self.noOneObj.id}"]`);
if ($noOne.length) {
$noOne.removeClass('is-active');
self.removeSelectedItem(self.noOneObj);
} }
});
self.$wrap.find(`.item-${item.type}`).removeClass('is-active');
} else {
const $noOne = self.$wrap.find(`.is-active.item-${item.type}[data-role-id="${self.noOneObj.id}"]`);
if ($noOne.length) {
$noOne.removeClass('is-active');
self.removeSelectedItem(self.noOneObj);
} }
$el.addClass(`is-active item-${item.type}`);
} }
$el.addClass(`is-active item-${item.type}`);
self.addSelectedItem(item); self.addSelectedItem(item);
} else { } else {
self.removeSelectedItem(item); self.removeSelectedItem(item);
...@@ -151,8 +145,24 @@ export default class ProtectedTagAccessDropdown { ...@@ -151,8 +145,24 @@ export default class ProtectedTagAccessDropdown {
let index = -1; let index = -1;
const selectedItems = this.getAllSelectedItems(); const selectedItems = this.getAllSelectedItems();
// Compare IDs based on selectedItem.type
selectedItems.forEach((item, i) => { selectedItems.forEach((item, i) => {
if (selectedItem.id === item.access_level) { let comparator;
switch (selectedItem.type) {
case LEVEL_TYPES.ROLE:
comparator = LEVEL_ID_PROP.ROLE;
break;
case LEVEL_TYPES.GROUP:
comparator = LEVEL_ID_PROP.GROUP;
break;
case LEVEL_TYPES.USER:
comparator = LEVEL_ID_PROP.USER;
break;
default:
break;
}
if (selectedItem.id === item[comparator]) {
index = i; index = i;
} }
}); });
......
...@@ -8,7 +8,7 @@ export default class ProtectedTagCreate { ...@@ -8,7 +8,7 @@ export default class ProtectedTagCreate {
constructor() { constructor() {
this.$form = $('.js-new-protected-tag'); this.$form = $('.js-new-protected-tag');
this.buildDropdowns(); this.buildDropdowns();
this.$branchTag = this.$form.find('input[name="protected_tag[name]"]'); this.$tagInput = this.$form.find('input[name="protected_tag[name]"]');
this.bindEvents(); this.bindEvents();
} }
......
...@@ -110,7 +110,7 @@ describe 'Branches' do ...@@ -110,7 +110,7 @@ describe 'Branches' do
project.add_user(user, :developer) project.add_user(user, :developer)
end end
it 'does not allow devleoper to removes protected branch', js: true do it 'does not allow devleoper to remove protected branch', js: true do
visit project_branches_path(project) visit project_branches_path(project)
fill_in 'branch-search', with: 'fix' fill_in 'branch-search', with: 'fix'
......
...@@ -2,7 +2,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -2,7 +2,7 @@ shared_examples "protected branches > access control > EE" do
[['merge', ProtectedBranch::MergeAccessLevel], ['push', ProtectedBranch::PushAccessLevel]].each do |git_operation, access_level_class| [['merge', ProtectedBranch::MergeAccessLevel], ['push', ProtectedBranch::PushAccessLevel]].each do |git_operation, access_level_class|
# Need to set a default for the `git_operation` access level that _isn't_ being tested # Need to set a default for the `git_operation` access level that _isn't_ being tested
other_git_operation = git_operation == 'merge' ? 'push' : 'merge' other_git_operation = git_operation == 'merge' ? 'push' : 'merge'
roles = git_operation == 'merge' ? access_level_class.human_access_levels : access_level_class.human_access_levels.except(0) roles_except_noone = access_level_class.human_access_levels.except(0)
let(:users) { create_list(:user, 5) } let(:users) { create_list(:user, 5) }
let(:groups) { create_list(:group, 5) } let(:groups) { create_list(:group, 5) }
...@@ -22,7 +22,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -22,7 +22,7 @@ shared_examples "protected branches > access control > EE" do
set_protected_branch_name('master') set_protected_branch_name('master')
set_allowed_to(git_operation, users.map(&:name)) set_allowed_to(git_operation, users.map(&:name))
set_allowed_to(git_operation, groups.map(&:name)) set_allowed_to(git_operation, groups.map(&:name))
set_allowed_to(git_operation, roles.values) roles_except_noone.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name) }
set_allowed_to(other_git_operation) set_allowed_to(other_git_operation)
click_on "Protect" click_on "Protect"
...@@ -31,7 +31,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -31,7 +31,7 @@ shared_examples "protected branches > access control > EE" do
expect(ProtectedBranch.count).to eq(1) expect(ProtectedBranch.count).to eq(1)
access_levels = last_access_levels(git_operation) access_levels = last_access_levels(git_operation)
roles.each { |(access_type_id, _)| expect(access_levels.map(&:access_level)).to include(access_type_id) } roles_except_noone.each { |(access_type_id, _)| expect(access_levels.map(&:access_level)).to include(access_type_id) }
users.each { |user| expect(access_levels.map(&:user_id)).to include(user.id) } users.each { |user| expect(access_levels.map(&:user_id)).to include(user.id) }
groups.each { |group| expect(access_levels.map(&:group_id)).to include(group.id) } groups.each { |group| expect(access_levels.map(&:group_id)).to include(group.id) }
end end
...@@ -46,14 +46,14 @@ shared_examples "protected branches > access control > EE" do ...@@ -46,14 +46,14 @@ shared_examples "protected branches > access control > EE" do
set_allowed_to(git_operation, users.map(&:name), form: ".js-protected-branch-edit-form") set_allowed_to(git_operation, users.map(&:name), form: ".js-protected-branch-edit-form")
set_allowed_to(git_operation, groups.map(&:name), form: ".js-protected-branch-edit-form") set_allowed_to(git_operation, groups.map(&:name), form: ".js-protected-branch-edit-form")
set_allowed_to(git_operation, roles.values, form: ".js-protected-branch-edit-form") roles_except_noone.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name, form: ".js-protected-branch-edit-form") }
wait_for_requests wait_for_requests
expect(ProtectedBranch.count).to eq(1) expect(ProtectedBranch.count).to eq(1)
access_levels = last_access_levels(git_operation) access_levels = last_access_levels(git_operation)
roles.each { |(access_type_id, _)| expect(access_levels.map(&:access_level)).to include(access_type_id) } roles_except_noone.each { |(access_type_id, _)| expect(access_levels.map(&:access_level)).to include(access_type_id) }
users.each { |user| expect(access_levels.map(&:user_id)).to include(user.id) } users.each { |user| expect(access_levels.map(&:user_id)).to include(user.id) }
groups.each { |group| expect(access_levels.map(&:group_id)).to include(group.id) } groups.each { |group| expect(access_levels.map(&:group_id)).to include(group.id) }
end end
...@@ -63,7 +63,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -63,7 +63,7 @@ shared_examples "protected branches > access control > EE" do
set_protected_branch_name('master') set_protected_branch_name('master')
users.each { |user| set_allowed_to(git_operation, user.name) } users.each { |user| set_allowed_to(git_operation, user.name) }
roles.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name) } roles_except_noone.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name) }
groups.each { |group| set_allowed_to(git_operation, group.name) } groups.each { |group| set_allowed_to(git_operation, group.name) }
set_allowed_to(other_git_operation) set_allowed_to(other_git_operation)
...@@ -71,7 +71,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -71,7 +71,7 @@ shared_examples "protected branches > access control > EE" do
users.each { |user| set_allowed_to(git_operation, user.name, form: ".js-protected-branch-edit-form") } users.each { |user| set_allowed_to(git_operation, user.name, form: ".js-protected-branch-edit-form") }
groups.each { |group| set_allowed_to(git_operation, group.name, form: ".js-protected-branch-edit-form") } groups.each { |group| set_allowed_to(git_operation, group.name, form: ".js-protected-branch-edit-form") }
roles.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name, form: ".js-protected-branch-edit-form") } roles_except_noone.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name, form: ".js-protected-branch-edit-form") }
wait_for_requests wait_for_requests
...@@ -89,7 +89,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -89,7 +89,7 @@ shared_examples "protected branches > access control > EE" do
# Create Protected Branch # Create Protected Branch
set_protected_branch_name('master') set_protected_branch_name('master')
set_allowed_to(git_operation, roles.values) roles_except_noone.each { |(_, access_type_name)| set_allowed_to(git_operation, access_type_name) }
set_allowed_to(other_git_operation) set_allowed_to(other_git_operation)
click_on 'Protect' click_on 'Protect'
...@@ -119,7 +119,7 @@ shared_examples "protected branches > access control > EE" do ...@@ -119,7 +119,7 @@ shared_examples "protected branches > access control > EE" do
expect(ProtectedBranch.count).to eq(1) expect(ProtectedBranch.count).to eq(1)
access_levels = last_access_levels(git_operation) access_levels = last_access_levels(git_operation)
roles.each { |(access_type_id, _)| expect(access_levels.map(&:access_level)).to include(access_type_id) } roles_except_noone.each { |(access_type_id, _)| expect(access_levels.map(&:access_level)).to include(access_type_id) }
expect(access_levels.map(&:user_id)).to include(users.last.id) expect(access_levels.map(&:user_id)).to include(users.last.id)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment