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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
ca196453
Commit
ca196453
authored
Nov 09, 2013
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/api_create_file' of /home/git/repositories/gitlab/gitlabhq
parents
ea3680ad
f64dd1b4
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
106 additions
and
2 deletions
+106
-2
doc/api/repositories.md
doc/api/repositories.md
+16
-1
lib/api/api.rb
lib/api/api.rb
+1
-0
lib/api/files.rb
lib/api/files.rb
+41
-0
lib/gitlab/satellite/files/new_file_action.rb
lib/gitlab/satellite/files/new_file_action.rb
+1
-1
spec/requests/api/files_spec.rb
spec/requests/api/files_spec.rb
+46
-0
spec/support/test_env.rb
spec/support/test_env.rb
+1
-0
No files found.
doc/api/repositories.md
View file @
ca196453
...
...
@@ -369,3 +369,18 @@ GET /projects/:id/repository/archive
Parameters:
+
`id`
(required) - The ID of a project
+
`sha`
(optional) - The commit sha to download defaults to the tip of the default branch
## Create new file in repository
```
POST /projects/:id/repository/files
```
Parameters:
+
`file_name`
(required) - The name of new file. Ex. class.rb
+
`file_path`
(optiona) - The path to new file. Ex. lib/
+
`branch_name`
(required) - The name of branch
+
`content`
(required) - File content
+
`commit_message`
(required) - Commit message
lib/api/api.rb
View file @
ca196453
...
...
@@ -39,5 +39,6 @@ module API
mount
DeployKeys
mount
ProjectHooks
mount
Services
mount
Files
end
end
lib/api/files.rb
0 → 100644
View file @
ca196453
module
API
# Projects API
class
Files
<
Grape
::
API
before
{
authenticate!
}
before
{
authorize!
:push_code
,
user_project
}
resource
:projects
do
# Create new file in repository
#
# Parameters:
# file_name (required) - The name of new file. Ex. class.rb
# file_path (optiona) - The path to new file. Ex. lib/
# branch_name (required) - The name of branch
# content (required) - File content
# commit_message (required) - Commit message
#
# Example Request:
# POST /projects/:id/repository/files
post
":id/repository/files"
do
required_attributes!
[
:file_name
,
:branch_name
,
:content
]
attrs
=
attributes_for_keys
[
:file_name
,
:file_path
,
:branch_name
,
:content
]
branch_name
=
attrs
.
delete
(
:branch_name
)
file_path
=
attrs
.
delete
(
:file_path
)
result
=
::
Files
::
CreateContext
.
new
(
user_project
,
current_user
,
attrs
,
branch_name
,
file_path
).
execute
if
result
[
:status
]
==
:success
status
(
201
)
{
file_name:
attrs
[
:file_name
],
file_path:
file_path
,
branch_name:
branch_name
}
else
render_api_error!
(
result
[
:error
],
400
)
end
end
end
end
end
lib/gitlab/satellite/files/new_file_action.rb
View file @
ca196453
...
...
@@ -17,7 +17,7 @@ module Gitlab
repo
.
git
.
checkout
({
raise:
true
,
timeout:
true
,
b:
true
},
ref
,
"origin/
#{
ref
}
"
)
# update the file in the satellite's working dir
file_path_in_satellite
=
File
.
join
(
repo
.
working_dir
,
file_path
,
file_name
)
file_path_in_satellite
=
File
.
join
(
repo
.
working_dir
,
file_path
||
''
,
file_name
)
File
.
open
(
file_path_in_satellite
,
'w'
)
{
|
f
|
f
.
write
(
content
)
}
# add new file
...
...
spec/requests/api/files_spec.rb
0 → 100644
View file @
ca196453
require
'spec_helper'
describe
API
::
API
do
include
ApiHelpers
before
(
:each
)
{
ActiveRecord
::
Base
.
observers
.
enable
(
:user_observer
)
}
after
(
:each
)
{
ActiveRecord
::
Base
.
observers
.
disable
(
:user_observer
)
}
let
(
:user
)
{
create
(
:user
)
}
let!
(
:project
)
{
create
(
:project_with_code
,
namespace:
user
.
namespace
)
}
before
{
project
.
team
<<
[
user
,
:developer
]
}
describe
"POST /projects/:id/repository/files"
do
it
"should create a new file in project repo"
do
Gitlab
::
Satellite
::
NewFileAction
.
any_instance
.
stub
(
commit!:
true
,
)
post
api
(
"/projects/
#{
project
.
id
}
/repository/files"
,
user
),
valid_params
response
.
status
.
should
==
201
json_response
[
'file_name'
].
should
==
'newfile.rb'
end
it
"should return a 400 bad request if no params given"
do
post
api
(
"/projects/
#{
project
.
id
}
/repository/files"
,
user
)
response
.
status
.
should
==
400
end
it
"should return a 400 if satellite fails to create file"
do
Gitlab
::
Satellite
::
NewFileAction
.
any_instance
.
stub
(
commit!:
false
,
)
post
api
(
"/projects/
#{
project
.
id
}
/repository/files"
,
user
),
valid_params
response
.
status
.
should
==
400
end
end
def
valid_params
{
file_name:
'newfile.rb'
,
branch_name:
'master'
,
content:
'puts 8'
,
commit_message:
'Added newfile'
}
end
end
spec/support/test_env.rb
View file @
ca196453
...
...
@@ -45,6 +45,7 @@ module TestEnv
def
disable_mailer
NotificationService
.
any_instance
.
stub
(
mailer:
double
.
as_null_object
)
end
def
enable_mailer
NotificationService
.
any_instance
.
unstub
(
:mailer
)
end
...
...
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