From 333097d7652a863f1d328e4c6b86be5e3b570bbf Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Thu, 7 Mar 2019 09:09:37 +0800 Subject: [PATCH 1/2] Allow protected branch creation for empty project --- lib/gitlab/checks/branch_check.rb | 2 +- spec/lib/gitlab/checks/branch_check_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/checks/branch_check.rb b/lib/gitlab/checks/branch_check.rb index bd305ace0a0..75b337a14cc 100644 --- a/lib/gitlab/checks/branch_check.rb +++ b/lib/gitlab/checks/branch_check.rb @@ -46,7 +46,7 @@ module Gitlab end end - if creation? && protected_branch_creation_enabled? + if creation? && protected_branch_creation_enabled? && !project.empty_repo? protected_branch_creation_checks elsif deletion? protected_branch_deletion_checks diff --git a/spec/lib/gitlab/checks/branch_check_spec.rb b/spec/lib/gitlab/checks/branch_check_spec.rb index f99fc639dbd..1a672f9d80a 100644 --- a/spec/lib/gitlab/checks/branch_check_spec.rb +++ b/spec/lib/gitlab/checks/branch_check_spec.rb @@ -116,6 +116,21 @@ describe Gitlab::Checks::BranchCheck do .and_return(['branch']) end + context "when repo is empty" do + let(:project) { create(:project, :empty_repo) } + let(:ref) { 'refs/heads/master' } + + before do + allow(user_access) + .to receive(:can_push_to_branch?) + .and_return(true) + end + + it 'allows branch creation' do + expect { subject.validate! }.not_to raise_error + end + end + context "newrev isn't in any protected branches" do before do allow(ProtectedBranch) From 461e3979691ade5bac7b882ac2c14b4d3d229676 Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Thu, 7 Mar 2019 16:51:58 +0800 Subject: [PATCH 2/2] Move empty_repo check on its own --- lib/gitlab/checks/branch_check.rb | 4 ++- spec/lib/gitlab/checks/branch_check_spec.rb | 39 +++++++++++---------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/gitlab/checks/branch_check.rb b/lib/gitlab/checks/branch_check.rb index 75b337a14cc..ad926739752 100644 --- a/lib/gitlab/checks/branch_check.rb +++ b/lib/gitlab/checks/branch_check.rb @@ -46,7 +46,9 @@ module Gitlab end end - if creation? && protected_branch_creation_enabled? && !project.empty_repo? + if project.empty_repo? + protected_branch_push_checks + elsif creation? && protected_branch_creation_enabled? protected_branch_creation_checks elsif deletion? protected_branch_deletion_checks diff --git a/spec/lib/gitlab/checks/branch_check_spec.rb b/spec/lib/gitlab/checks/branch_check_spec.rb index 1a672f9d80a..12beeecd470 100644 --- a/spec/lib/gitlab/checks/branch_check_spec.rb +++ b/spec/lib/gitlab/checks/branch_check_spec.rb @@ -48,10 +48,28 @@ describe Gitlab::Checks::BranchCheck do context 'when project repository is empty' do let(:project) { create(:project) } - it 'raises an error if the user is not allowed to push to protected branches' do - expect(user_access).to receive(:can_push_to_branch?).and_return(false) + context 'user is not allowed to push to protected branches' do + before do + allow(user_access) + .to receive(:can_push_to_branch?) + .and_return(false) + end - expect { subject.validate! }.to raise_error(Gitlab::GitAccess::UnauthorizedError, /Ask a project Owner or Maintainer to create a default branch/) + it 'raises an error' do + expect { subject.validate! }.to raise_error(Gitlab::GitAccess::UnauthorizedError, /Ask a project Owner or Maintainer to create a default branch/) + end + end + + context 'user is allowed to push to protected branches' do + before do + allow(user_access) + .to receive(:can_push_to_branch?) + .and_return(true) + end + + it 'allows branch creation' do + expect { subject.validate! }.not_to raise_error + end end end @@ -116,21 +134,6 @@ describe Gitlab::Checks::BranchCheck do .and_return(['branch']) end - context "when repo is empty" do - let(:project) { create(:project, :empty_repo) } - let(:ref) { 'refs/heads/master' } - - before do - allow(user_access) - .to receive(:can_push_to_branch?) - .and_return(true) - end - - it 'allows branch creation' do - expect { subject.validate! }.not_to raise_error - end - end - context "newrev isn't in any protected branches" do before do allow(ProtectedBranch)