Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
80543e0a
Commit
80543e0a
authored
Mar 01, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix creating a file in an empty repo using the API
parent
981c730f
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
50 additions
and
44 deletions
+50
-44
app/controllers/concerns/creates_commit.rb
app/controllers/concerns/creates_commit.rb
+1
-6
app/models/repository.rb
app/models/repository.rb
+2
-0
app/services/files/base_service.rb
app/services/files/base_service.rb
+6
-10
app/services/git_operation_service.rb
app/services/git_operation_service.rb
+9
-28
changelogs/unreleased/dm-fix-api-create-file-on-empty-repo.yml
...elogs/unreleased/dm-fix-api-create-file-on-empty-repo.yml
+4
-0
spec/requests/api/files_spec.rb
spec/requests/api/files_spec.rb
+14
-0
spec/requests/api/v3/files_spec.rb
spec/requests/api/v3/files_spec.rb
+14
-0
No files found.
app/controllers/concerns/creates_commit.rb
View file @
80543e0a
...
@@ -4,7 +4,7 @@ module CreatesCommit
...
@@ -4,7 +4,7 @@ module CreatesCommit
def
create_commit
(
service
,
success_path
:,
failure_path
:,
failure_view:
nil
,
success_notice:
nil
)
def
create_commit
(
service
,
success_path
:,
failure_path
:,
failure_view:
nil
,
success_notice:
nil
)
set_commit_variables
set_commit_variables
start_branch
=
@mr_target_branch
unless
initial_commit?
start_branch
=
@mr_target_branch
commit_params
=
@commit_params
.
merge
(
commit_params
=
@commit_params
.
merge
(
start_project:
@mr_target_project
,
start_project:
@mr_target_project
,
start_branch:
start_branch
,
start_branch:
start_branch
,
...
@@ -117,11 +117,6 @@ module CreatesCommit
...
@@ -117,11 +117,6 @@ module CreatesCommit
@mr_source_branch
=
guess_mr_source_branch
@mr_source_branch
=
guess_mr_source_branch
end
end
def
initial_commit?
@mr_target_branch
.
nil?
||
!
@mr_target_project
.
repository
.
branch_exists?
(
@mr_target_branch
)
end
def
guess_mr_source_branch
def
guess_mr_source_branch
# XXX: Happens when viewing a commit without a branch. In this case,
# XXX: Happens when viewing a commit without a branch. In this case,
# @target_branch would be the default branch for @mr_source_project,
# @target_branch would be the default branch for @mr_source_project,
...
...
app/models/repository.rb
View file @
80543e0a
...
@@ -995,6 +995,8 @@ class Repository
...
@@ -995,6 +995,8 @@ class Repository
end
end
def
with_repo_branch_commit
(
start_repository
,
start_branch_name
)
def
with_repo_branch_commit
(
start_repository
,
start_branch_name
)
return
yield
(
nil
)
if
start_repository
.
empty_repo?
branch_name_or_sha
=
branch_name_or_sha
=
if
start_repository
==
self
if
start_repository
==
self
start_branch_name
start_branch_name
...
...
app/services/files/base_service.rb
View file @
80543e0a
...
@@ -58,16 +58,12 @@ module Files
...
@@ -58,16 +58,12 @@ module Files
raise_error
(
"You are not allowed to push into this branch"
)
raise_error
(
"You are not allowed to push into this branch"
)
end
end
unless
project
.
empty_repo?
if
!
@start_project
.
empty_repo?
&&
!
@start_project
.
repository
.
branch_exists?
(
@start_branch
)
unless
@start_project
.
repository
.
branch_exists?
(
@start_branch
)
raise
ValidationError
,
'You can only create or edit files when you are on a branch'
raise_error
(
'You can only create or edit files when you are on a branch'
)
end
end
if
!
project
.
empty_repo?
&&
different_branch?
&&
repository
.
branch_exists?
(
@branch_name
)
if
different_branch?
raise
ValidationError
,
'Branch with such name already exists. You need to switch to this branch in order to make changes'
if
repository
.
branch_exists?
(
@target_branch
)
raise_error
(
'Branch with such name already exists. You need to switch to this branch in order to make changes'
)
end
end
end
end
end
end
...
...
app/services/git_operation_service.rb
View file @
80543e0a
...
@@ -56,13 +56,14 @@ class GitOperationService
...
@@ -56,13 +56,14 @@ class GitOperationService
start_project:
repository
.
project
,
start_project:
repository
.
project
,
&
block
)
&
block
)
check_with_branch_arguments!
(
start_branch
||=
branch_name
branch_name
,
start_branch_name
,
start_project
)
verify_start_branch_exists!
(
start_project
.
repository
,
start_branch_name
)
update_branch_with_hooks
(
branch_name
)
do
update_branch_with_hooks
(
branch_name
)
do
repository
.
with_repo_branch_commit
(
repository
.
with_repo_branch_commit
(
start_project
.
repository
,
start_project
.
repository
,
start_branch_name
||
branch_name
,
start_branch_name
,
&
block
)
&
block
)
end
end
end
end
...
@@ -150,30 +151,10 @@ class GitOperationService
...
@@ -150,30 +151,10 @@ class GitOperationService
end
end
end
end
def
check_with_branch_arguments!
(
def
verify_start_branch_exists!
(
start_repository
,
start_branch_name
)
branch_name
,
start_branch_name
,
start_project
)
return
if
start_repository
.
empty_repo?
return
if
repository
.
branch_exists?
(
branch_name
)
return
if
start_repository
.
branch_exists?
(
start_branch_name
)
if
repository
.
project
!=
start_project
raise
ArgumentError
,
"Cannot find branch
#{
start_branch_name
}
in
#{
start_repository
.
path_with_namespace
}
"
unless
start_branch_name
raise
ArgumentError
,
'Should also pass :start_branch_name if'
+
' :start_project is different from current project'
end
unless
start_project
.
repository
.
branch_exists?
(
start_branch_name
)
raise
ArgumentError
,
"Cannot find branch
#{
branch_name
}
nor"
\
"
#{
start_branch_name
}
from"
\
"
#{
start_project
.
path_with_namespace
}
"
end
elsif
start_branch_name
unless
repository
.
branch_exists?
(
start_branch_name
)
raise
ArgumentError
,
"Cannot find branch
#{
branch_name
}
nor"
\
"
#{
start_branch_name
}
from"
\
"
#{
repository
.
project
.
path_with_namespace
}
"
end
end
end
end
end
end
changelogs/unreleased/dm-fix-api-create-file-on-empty-repo.yml
0 → 100644
View file @
80543e0a
---
title
:
Fix creating a file in an empty repo using the API
merge_request
:
9632
author
:
spec/requests/api/files_spec.rb
View file @
80543e0a
...
@@ -147,6 +147,20 @@ describe API::Files, api: true do
...
@@ -147,6 +147,20 @@ describe API::Files, api: true do
expect
(
last_commit
.
author_name
).
to
eq
(
author_name
)
expect
(
last_commit
.
author_name
).
to
eq
(
author_name
)
end
end
end
end
context
'when the repo is empty'
do
let!
(
:project
)
{
create
(
:project_empty_repo
,
namespace:
user
.
namespace
)
}
it
"creates a new file in project repo"
do
post
api
(
"/projects/
#{
project
.
id
}
/repository/files"
,
user
),
valid_params
expect
(
response
).
to
have_http_status
(
201
)
expect
(
json_response
[
'file_path'
]).
to
eq
(
'newfile.rb'
)
last_commit
=
project
.
repository
.
commit
.
raw
expect
(
last_commit
.
author_email
).
to
eq
(
user
.
email
)
expect
(
last_commit
.
author_name
).
to
eq
(
user
.
name
)
end
end
end
end
describe
"PUT /projects/:id/repository/files"
do
describe
"PUT /projects/:id/repository/files"
do
...
...
spec/requests/api/v3/files_spec.rb
View file @
80543e0a
...
@@ -148,6 +148,20 @@ describe API::V3::Files, api: true do
...
@@ -148,6 +148,20 @@ describe API::V3::Files, api: true do
expect
(
last_commit
.
author_name
).
to
eq
(
author_name
)
expect
(
last_commit
.
author_name
).
to
eq
(
author_name
)
end
end
end
end
context
'when the repo is empty'
do
let!
(
:project
)
{
create
(
:project_empty_repo
,
namespace:
user
.
namespace
)
}
it
"creates a new file in project repo"
do
post
api
(
"/projects/
#{
project
.
id
}
/repository/files"
,
user
),
valid_params
expect
(
response
).
to
have_http_status
(
201
)
expect
(
json_response
[
'file_path'
]).
to
eq
(
'newfile.rb'
)
last_commit
=
project
.
repository
.
commit
.
raw
expect
(
last_commit
.
author_email
).
to
eq
(
user
.
email
)
expect
(
last_commit
.
author_name
).
to
eq
(
user
.
name
)
end
end
end
end
describe
"PUT /projects/:id/repository/files"
do
describe
"PUT /projects/:id/repository/files"
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment