Skip to content

Commit 08e4d98

Browse files
committed
Create master branch first if project is repository-less
1 parent 319dfd6 commit 08e4d98

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

app/controllers/projects/branches_controller.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class Projects::BranchesController < Projects::ApplicationController
22
include ActionView::Helpers::SanitizeHelper
33
include SortingHelper
4+
include ProjectsHelper
5+
46
# Authorize
5-
before_action :require_non_empty_project
7+
before_action :require_non_empty_project, except: :create
68
before_action :authorize_download_code!
79
before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
810

@@ -32,6 +34,8 @@ def create
3234
branch_name = sanitize(strip_tags(params[:branch_name]))
3335
branch_name = Addressable::URI.unescape(branch_name)
3436

37+
is_redirect_to_autodeploy_needed = project.empty_repo? && project.deployment_services.present?
38+
3539
result = CreateBranchService.new(project, current_user).
3640
execute(branch_name, ref)
3741

@@ -42,8 +46,16 @@ def create
4246

4347
if result[:status] == :success
4448
@branch = result[:branch]
45-
redirect_to namespace_project_tree_path(@project.namespace, @project,
46-
@branch.name)
49+
50+
if is_redirect_to_autodeploy_needed
51+
redirect_to(
52+
url_to_autodeploy_setup(project, branch_name),
53+
notice: "Branch \"#{sanitize(branch_name)}\" was created. To set up auto deploy, \
54+
choose a GitLab CI Yaml template and commit your changes. #{view_context.link_to_autodeploy_doc}".html_safe)
55+
else
56+
redirect_to namespace_project_tree_path(@project.namespace, @project,
57+
@branch.name)
58+
end
4759
else
4860
@error = result[:message]
4961
render action: 'new'
@@ -76,7 +88,7 @@ def ref
7688
ref_escaped = sanitize(strip_tags(params[:ref]))
7789
Addressable::URI.unescape(ref_escaped)
7890
else
79-
@project.default_branch
91+
@project.default_branch || 'master'
8092
end
8193
end
8294
end

app/helpers/projects_helper.rb

+16
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def project_feature_access_select(field)
150150
).html_safe
151151
end
152152

153+
def link_to_autodeploy_doc
154+
link_to 'About auto deploy', help_page_path('ci/autodeploy/index'), target: '_blank'
155+
end
156+
153157
private
154158

155159
def repo_children_classes(field)
@@ -268,6 +272,18 @@ def add_special_file_path(project, file_name:, commit_message: nil, target_branc
268272
)
269273
end
270274

275+
def url_to_autodeploy_setup(project, branch_name)
276+
namespace_project_new_blob_path(
277+
project.namespace,
278+
project,
279+
branch_name,
280+
file_name: '.gitlab-ci.yml',
281+
commit_message: 'Set up auto deploy',
282+
target_branch: branch_name,
283+
context: 'autodeploy'
284+
)
285+
end
286+
271287
def add_koding_stack_path(project)
272288
namespace_project_new_blob_path(
273289
project.namespace,

app/services/create_branch_service.rb

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class CreateBranchService < BaseService
22
def execute(branch_name, ref)
3+
create_master_branch if project.empty_repo?
4+
35
result = ValidateNewBranchService.new(project, current_user)
46
.execute(branch_name)
57

@@ -19,4 +21,16 @@ def execute(branch_name, ref)
1921
def success(branch)
2022
super().merge(branch: branch)
2123
end
24+
25+
private
26+
27+
def create_master_branch
28+
project.repository.commit_file(
29+
current_user,
30+
'/README.md',
31+
'',
32+
message: 'Add README.md',
33+
branch_name: 'master',
34+
update: false)
35+
end
2236
end

spec/controllers/projects/branches_controller_spec.rb

+44-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
describe "created from the new branch button on issues" do
7070
let(:branch) { "1-feature-branch" }
71-
let!(:issue) { create(:issue, project: project) }
71+
let(:issue) { create(:issue, project: project) }
7272

7373
before do
7474
sign_in(user)
@@ -95,6 +95,49 @@
9595
issue_iid: issue.iid
9696
end
9797

98+
context 'repository-less project' do
99+
let(:project) { create :empty_project }
100+
101+
it 'redirects to newly created branch' do
102+
result = { status: :success, branch: double(name: branch) }
103+
104+
expect_any_instance_of(CreateBranchService).to receive(:execute).and_return(result)
105+
expect(SystemNoteService).to receive(:new_issue_branch).and_return(true)
106+
107+
post :create,
108+
namespace_id: project.namespace.to_param,
109+
project_id: project.to_param,
110+
branch_name: branch,
111+
issue_iid: issue.iid
112+
113+
expect(response).to redirect_to namespace_project_tree_path(project.namespace, project, branch)
114+
end
115+
116+
it 'redirects to autodeploy setup page' do
117+
result = { status: :success, branch: double(name: branch) }
118+
119+
project.create_kubernetes_service(
120+
active: true,
121+
properties: {
122+
namespace: project.path,
123+
api_url: 'https://kubernetes.example.com',
124+
token: 'a' * 40,
125+
}
126+
)
127+
128+
expect_any_instance_of(CreateBranchService).to receive(:execute).and_return(result)
129+
expect(SystemNoteService).to receive(:new_issue_branch).and_return(true)
130+
131+
post :create,
132+
namespace_id: project.namespace.to_param,
133+
project_id: project.to_param,
134+
branch_name: branch,
135+
issue_iid: issue.iid
136+
137+
expect(response.location).to include(namespace_project_new_blob_path(project.namespace, project, branch))
138+
end
139+
end
140+
98141
context 'without issue feature access' do
99142
before do
100143
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)

0 commit comments

Comments
 (0)