提交 ca196453 编写于 作者: D Dmitriy Zaporozhets

Merge branch 'feature/api_create_file' of /home/git/repositories/gitlab/gitlabhq

......@@ -368,4 +368,19 @@ 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
\ No newline at end of file
+ `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
......@@ -39,5 +39,6 @@ module API
mount DeployKeys
mount ProjectHooks
mount Services
mount Files
end
end
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
......@@ -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
......
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
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册