BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
c36418aa
Commit
c36418aa
authored
Mar 02, 2017
by
Robert Speicher
Committed by
Felipe Artur
Mar 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge branch 'dm-fix-api-create-file-on-empty-repo' into 'master'
Fix creating a file in an empty repo using the API Closes #28626 See merge request !9632
parent
fb274568
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
37 additions
and
40 deletions
+37
-40
repository.rb
app/models/repository.rb
+2
-0
base_service.rb
app/services/files/base_service.rb
+6
-10
git_operation_service.rb
app/services/git_operation_service.rb
+7
-30
dm-fix-api-create-file-on-empty-repo.yml
...elogs/unreleased/dm-fix-api-create-file-on-empty-repo.yml
+4
-0
projects.rb
spec/factories/projects.rb
+4
-0
files_spec.rb
spec/requests/api/files_spec.rb
+14
-0
No files found.
app/models/repository.rb
View file @
c36418aa
...
...
@@ -1075,6 +1075,8 @@ class Repository
end
def
with_repo_branch_commit
(
start_repository
,
start_branch_name
)
return
yield
(
nil
)
if
start_repository
.
empty_repo?
branch_name_or_sha
=
if
start_repository
==
self
start_branch_name
...
...
app/services/files/base_service.rb
View file @
c36418aa
...
...
@@ -58,16 +58,12 @@ module Files
raise_error
(
"You are not allowed to push into this branch"
)
end
unless
project
.
empty_repo?
unless
@start_project
.
repository
.
branch_exists?
(
@start_branch
)
raise_error
(
'You can only create or edit files when you are on a branch'
)
end
if
different_branch?
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
if
!
@start_project
.
empty_repo?
&&
!
@start_project
.
repository
.
branch_exists?
(
@start_branch
)
raise
ValidationError
,
'You can only create or edit files when you are on a branch'
end
if
!
project
.
empty_repo?
&&
different_branch?
&&
repository
.
branch_exists?
(
@branch_name
)
raise
ValidationError
,
"A branch called
#{
@branch_name
}
already exists. Switch to that branch in order to make changes"
end
end
...
...
app/services/git_operation_service.rb
View file @
c36418aa
...
...
@@ -56,12 +56,16 @@ class GitOperationService
start_project:
repository
.
project
,
&
block
)
check_with_branch_arguments!
(
branch_name
,
start_branch_name
,
start_project
)
start_repository
=
start_project
.
repository
start_branch_name
=
nil
if
start_repository
.
empty_repo?
if
start_branch_name
&&
!
start_repository
.
branch_exists?
(
start_branch_name
)
raise
ArgumentError
,
"Cannot find branch
#{
start_branch_name
}
in
#{
start_repository
.
path_with_namespace
}
"
end
update_branch_with_hooks
(
branch_name
)
do
repository
.
with_repo_branch_commit
(
start_
project
.
repository
,
start_repository
,
start_branch_name
||
branch_name
,
&
block
)
end
...
...
@@ -149,31 +153,4 @@ class GitOperationService
repository
.
raw_repository
.
autocrlf
=
:input
end
end
def
check_with_branch_arguments!
(
branch_name
,
start_branch_name
,
start_project
)
return
if
repository
.
branch_exists?
(
branch_name
)
if
repository
.
project
!=
start_project
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
changelogs/unreleased/dm-fix-api-create-file-on-empty-repo.yml
0 → 100644
View file @
c36418aa
---
title
:
Fix creating a file in an empty repo using the API
merge_request
:
9632
author
:
spec/factories/projects.rb
View file @
c36418aa
...
...
@@ -39,6 +39,10 @@ FactoryGirl.define do
trait
:empty_repo
do
after
(
:create
)
do
|
project
|
project
.
create_repository
# We delete hooks so that gitlab-shell will not try to authenticate with
# an API that isn't running
FileUtils
.
rm_r
(
File
.
join
(
project
.
repository_storage_path
,
"
#{
project
.
path_with_namespace
}
.git"
,
'hooks'
))
end
end
...
...
spec/requests/api/files_spec.rb
View file @
c36418aa
...
...
@@ -147,6 +147,20 @@ describe API::Files, api: true do
expect
(
last_commit
.
author_name
).
to
eq
(
author_name
)
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
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