Commit 870559f9 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

fix protected branches sane defaults feature on EE

parent e69fe059
/* global Flash */
import AccessorUtilities from '~/lib/utils/accessor';
import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
import ProtectedBranchDropdown from './protected_branch_dropdown';
const PB_LOCAL_STORAGE_KEY = 'protected-branches-defaults';
export default class ProtectedBranchCreate {
constructor() {
this.$form = $('.js-new-protected-branch');
this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
this.currentProjectUserDefaults = {};
this.buildDropdowns();
this.$branchInput = this.$form.find('input[name="protected_branch[name]"]');
this.bindEvents();
......@@ -43,14 +48,21 @@ export default class ProtectedBranchCreate {
$dropdown: this.$form.find('.js-protected-branch-select'),
onSelect: this.onSelectCallback,
});
this.loadPreviousSelection();
}
// Enable submit button after selecting an option
onSelect() {
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);
const toggle = !(
this.$form.find('input[name="protected_branch[name]"]').val() &&
$allowedToMerge.length &&
$allowedToPush.length
);
this.savePreviousSelection($allowedToMerge, $allowedToPush);
this.$form.find('input[type="submit"]').attr('disabled', toggle);
}
......@@ -89,6 +101,20 @@ export default class ProtectedBranchCreate {
return formData;
}
loadPreviousSelection() {
if (this.isLocalStorageAvailable) {
const savedDefaults = JSON.parse(window.localStorage.getItem(PB_LOCAL_STORAGE_KEY));
if (savedDefaults != null) {
this[`${ACCESS_LEVELS.MERGE}_dropdown`].setSelectedItems(savedDefaults.merge);
let updatedLabel = this[`${ACCESS_LEVELS.MERGE}_dropdown`].toggleLabel();
this[`${ACCESS_LEVELS.MERGE}_dropdown`].$dropdown.find('.dropdown-toggle-text').text(updatedLabel);
this[`${ACCESS_LEVELS.PUSH}_dropdown`].setSelectedItems(savedDefaults.push);
updatedLabel = this[`${ACCESS_LEVELS.PUSH}_dropdown`].toggleLabel();
this[`${ACCESS_LEVELS.PUSH}_dropdown`].$dropdown.find('.dropdown-toggle-text').text(updatedLabel);
}
}
}
onFormSubmit(e) {
e.preventDefault();
......@@ -102,4 +128,14 @@ export default class ProtectedBranchCreate {
})
.fail(() => new Flash('Failed to protect the branch'));
}
savePreviousSelection(mergeSelection, pushSelection) {
if (this.isLocalStorageAvailable) {
const branchDefaults = {
merge: mergeSelection || [],
push: pushSelection || [],
};
window.localStorage.setItem(PB_LOCAL_STORAGE_KEY, JSON.stringify(branchDefaults));
}
}
}
require 'spec_helper'
feature 'Protected Branches', :js do
<<<<<<< HEAD
include EE::ProtectedBranchHelpers
=======
>>>>>>> ce/master
let(:user) { create(:user) }
let(:admin) { create(:admin) }
let(:project) { create(:project, :repository) }
......@@ -21,7 +18,6 @@ feature 'Protected Branches', :js do
create(:protected_branch, project: project, name: 'fix')
expect(ProtectedBranch.count).to eq(1)
end
<<<<<<< HEAD
it 'does not allow developer to removes protected branch' do
visit project_branches_path(project)
......@@ -34,42 +30,6 @@ feature 'Protected Branches', :js do
end
end
context 'logged in as master' do
before do
project.add_master(user)
sign_in(user)
=======
it 'does not allow developer to removes protected branch' do
visit project_branches_path(project)
fill_in 'branch-search', with: 'fix'
find('#branch-search').native.send_keys(:enter)
expect(page).to have_css('.btn-remove.disabled')
end
>>>>>>> ce/master
end
end
<<<<<<< HEAD
describe 'Delete protected branch' do
before do
create(:protected_branch, project: project, name: 'fix')
expect(ProtectedBranch.count).to eq(1)
end
it 'removes branch after modal confirmation' do
visit project_branches_path(project)
fill_in 'branch-search', with: 'fix'
find('#branch-search').native.send_keys(:enter)
expect(page).to have_content('fix')
expect(find('.all-branches')).to have_selector('li', count: 1)
page.find('[data-target="#modal-delete-branch"]').trigger(:click)
=======
context 'logged in as master' do
before do
project.add_master(user)
......@@ -92,7 +52,6 @@ feature 'Protected Branches', :js do
expect(find('.all-branches')).to have_selector('li', count: 1)
page.find('[data-target="#modal-delete-branch"]').trigger(:click)
>>>>>>> ce/master
expect(page).to have_css('.js-delete-branch[disabled]')
fill_in 'delete_branch_input', with: 'fix'
click_link 'Delete protected branch'
......@@ -103,12 +62,37 @@ feature 'Protected Branches', :js do
expect(page).to have_content('No branches to show')
end
end
describe "Saved defaults" do
it "keeps the allowed to merge and push dropdowns defaults based on the previous selection" do
visit project_protected_branches_path(project)
set_protected_branch_name('some-branch')
form = '.js-new-protected-branch'
within form do
find(".js-allowed-to-merge").trigger('click')
click_link 'No one'
find(".js-allowed-to-push").trigger('click')
click_link 'Developers + Masters'
end
visit project_protected_branches_path(project)
within form do
page.within(".js-allowed-to-merge") do
expect(page.find(".dropdown-toggle-text")).to have_content("1 role, 0 users, 0 groups")
end
page.within(".js-allowed-to-push") do
expect(page.find(".dropdown-toggle-text")).to have_content("1 role, 0 users, 0 groups")
end
end
end
end
end
context 'logged in as admin' do
before do
sign_in(admin)
<<<<<<< HEAD
end
describe "explicit protected branches" do
......@@ -126,7 +110,7 @@ feature 'Protected Branches', :js do
it "displays the last commit on the matching branch if it exists" do
commit = create(:commit, project: project)
project.repository.add_branch(user, 'some-branch', commit.id)
project.repository.add_branch(admin, 'some-branch', commit.id)
visit project_protected_branches_path(project)
set_protected_branch_name('some-branch')
......@@ -162,8 +146,8 @@ feature 'Protected Branches', :js do
end
it "displays the number of matching branches" do
project.repository.add_branch(user, 'production-stable', 'master')
project.repository.add_branch(user, 'staging-stable', 'master')
project.repository.add_branch(admin, 'production-stable', 'master')
project.repository.add_branch(admin, 'staging-stable', 'master')
visit project_protected_branches_path(project)
set_protected_branch_name('*-stable')
......@@ -175,9 +159,9 @@ feature 'Protected Branches', :js do
end
it "displays all the branches matching the wildcard" do
project.repository.add_branch(user, 'production-stable', 'master')
project.repository.add_branch(user, 'staging-stable', 'master')
project.repository.add_branch(user, 'development', 'master')
project.repository.add_branch(admin, 'production-stable', 'master')
project.repository.add_branch(admin, 'staging-stable', 'master')
project.repository.add_branch(admin, 'development', 'master')
visit project_protected_branches_path(project)
set_protected_branch_name('*-stable')
......@@ -257,116 +241,14 @@ feature 'Protected Branches', :js do
"this branch:")
expect(page).to have_content(/(Team Awesome|Team B) and (Team Awesome|Team B)/)
end
=======
end
describe "explicit protected branches" do
it "allows creating explicit protected branches" do
visit project_protected_branches_path(project)
set_protected_branch_name('some-branch')
click_on "Protect"
within(".protected-branches-list") { expect(page).to have_content('some-branch') }
expect(ProtectedBranch.count).to eq(1)
expect(ProtectedBranch.last.name).to eq('some-branch')
end
it "displays the last commit on the matching branch if it exists" do
commit = create(:commit, project: project)
project.repository.add_branch(admin, 'some-branch', commit.id)
visit project_protected_branches_path(project)
set_protected_branch_name('some-branch')
click_on "Protect"
within(".protected-branches-list") { expect(page).to have_content(commit.id[0..7]) }
end
it "displays an error message if the named branch does not exist" do
visit project_protected_branches_path(project)
set_protected_branch_name('some-branch')
click_on "Protect"
within(".protected-branches-list") { expect(page).to have_content('branch was removed') }
end
end
describe "wildcard protected branches" do
it "allows creating protected branches with a wildcard" do
visit project_protected_branches_path(project)
set_protected_branch_name('*-stable')
click_on "Protect"
within(".protected-branches-list") { expect(page).to have_content('*-stable') }
expect(ProtectedBranch.count).to eq(1)
expect(ProtectedBranch.last.name).to eq('*-stable')
end
it "displays the number of matching branches" do
project.repository.add_branch(admin, 'production-stable', 'master')
project.repository.add_branch(admin, 'staging-stable', 'master')
visit project_protected_branches_path(project)
set_protected_branch_name('*-stable')
click_on "Protect"
within(".protected-branches-list") { expect(page).to have_content("2 matching branches") }
end
it "displays all the branches matching the wildcard" do
project.repository.add_branch(admin, 'production-stable', 'master')
project.repository.add_branch(admin, 'staging-stable', 'master')
project.repository.add_branch(admin, 'development', 'master')
visit project_protected_branches_path(project)
set_protected_branch_name('*-stable')
click_on "Protect"
visit project_protected_branches_path(project)
click_on "2 matching branches"
within(".protected-branches-list") do
expect(page).to have_content("production-stable")
expect(page).to have_content("staging-stable")
expect(page).not_to have_content("development")
>>>>>>> ce/master
end
end
end
describe "access control" do
include_examples "protected branches > access control > CE"
end
end
def set_protected_branch_name(branch_name)
find(".js-protected-branch-select").trigger('click')
find(".dropdown-input-field").set(branch_name)
click_on("Create wildcard #{branch_name}")
end
<<<<<<< HEAD
def set_protected_branch_name(branch_name)
find(".js-protected-branch-select").trigger('click')
find(".dropdown-input-field").set(branch_name)
click_on("Create wildcard #{branch_name}")
=======
describe "saved defaults" do
it "keeps the allowed to merge and push dropdowns defaults based on the previous selection" do
visit project_protected_branches_path(project)
set_protected_branch_name('some-branch')
find(".js-allowed-to-merge").trigger('click')
click_link 'No one'
find(".js-allowed-to-push").trigger('click')
click_link 'Developers + Masters'
visit project_protected_branches_path(project)
page.within(".js-allowed-to-merge") do
expect(page.find(".dropdown-toggle-text")).to have_content("No one")
end
page.within(".js-allowed-to-push") do
expect(page.find(".dropdown-toggle-text")).to have_content("Developers + Masters")
end
end
>>>>>>> ce/master
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