summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Botelho <tiagonbotelho@hotmail.com>2018-07-10 09:42:38 +0100
committerTiago Botelho <tiagonbotelho@hotmail.com>2018-07-18 09:25:00 +0100
commit65e5cf0ace9cb57b474656633d2bee8bbebc940f (patch)
treebbab55702f6b759c750f38e56bf475485acc0ff7
parent21a511e47e448250802b1777b43589c9168e0638 (diff)
downloadgitlab-ce-27456-improve-feedback-when-dev-cannot-push-to-empty-repo.tar.gz
Improves remote message when a developer is unable to push in an empty repository27456-improve-feedback-when-dev-cannot-push-to-empty-repo
-rw-r--r--changelogs/unreleased/27456-improve-feedback-when-dev-cannot-push-to-empty-repo.yml5
-rw-r--r--lib/gitlab/checks/change_access.rb25
-rw-r--r--spec/lib/gitlab/checks/change_access_spec.rb10
3 files changed, 39 insertions, 1 deletions
diff --git a/changelogs/unreleased/27456-improve-feedback-when-dev-cannot-push-to-empty-repo.yml b/changelogs/unreleased/27456-improve-feedback-when-dev-cannot-push-to-empty-repo.yml
new file mode 100644
index 00000000000..55d82c4ee5d
--- /dev/null
+++ b/changelogs/unreleased/27456-improve-feedback-when-dev-cannot-push-to-empty-repo.yml
@@ -0,0 +1,5 @@
+---
+title: Improve feedback when a developer is unable to push to an empty repository
+merge_request: 20519
+author:
+type: changed
diff --git a/lib/gitlab/checks/change_access.rb b/lib/gitlab/checks/change_access.rb
index f76a6fb5f17..7a4224e5bbe 100644
--- a/lib/gitlab/checks/change_access.rb
+++ b/lib/gitlab/checks/change_access.rb
@@ -93,7 +93,7 @@ module Gitlab
end
else
unless user_access.can_push_to_branch?(branch_name)
- raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:push_protected_branch]
+ raise GitAccess::UnauthorizedError, push_to_protected_branch_rejected_message
end
end
end
@@ -140,6 +140,29 @@ module Gitlab
private
+ def push_to_protected_branch_rejected_message
+ if project.empty_repo?
+ empty_project_push_message
+ else
+ ERROR_MESSAGES[:push_protected_branch]
+ end
+ end
+
+ def empty_project_push_message
+ <<~MESSAGE
+
+ A default branch (e.g. master) does not yet exist for #{project.full_path}
+ Ask a project Owner or Maintainer to create a default branch:
+
+ #{project_members_url}
+
+ MESSAGE
+ end
+
+ def project_members_url
+ Gitlab::Routing.url_helpers.project_project_members_url(project)
+ end
+
def should_run_commit_validations?
commit_check.validate_lfs_file_locks?
end
diff --git a/spec/lib/gitlab/checks/change_access_spec.rb b/spec/lib/gitlab/checks/change_access_spec.rb
index 1cb8143a9e9..7d1bc5e4227 100644
--- a/spec/lib/gitlab/checks/change_access_spec.rb
+++ b/spec/lib/gitlab/checks/change_access_spec.rb
@@ -132,6 +132,16 @@ describe Gitlab::Checks::ChangeAccess do
expect { subject.exec }.to raise_error(Gitlab::GitAccess::UnauthorizedError, 'You are not allowed to push code to protected branches on this project.')
end
+ 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)
+
+ expect { subject.exec }.to raise_error(Gitlab::GitAccess::UnauthorizedError, /Ask a project Owner or Maintainer to create a default branch/)
+ end
+ end
+
context 'branch deletion' do
let(:newrev) { '0000000000000000000000000000000000000000' }
let(:ref) { 'refs/heads/feature' }