diff options
-rw-r--r-- | doc/api/repositories.md | 12 | ||||
-rw-r--r-- | lib/api/files.rb | 34 | ||||
-rw-r--r-- | spec/requests/api/files_spec.rb | 34 |
3 files changed, 77 insertions, 3 deletions
diff --git a/doc/api/repositories.md b/doc/api/repositories.md index 9ec6ba74125..af760795d00 100644 --- a/doc/api/repositories.md +++ b/doc/api/repositories.md @@ -397,3 +397,15 @@ Parameters: + `branch_name` (required) - The name of branch + `content` (required) - New file content + `commit_message` (required) - Commit message + +## Delete existing file in repository + +``` +DELETE /projects/:id/repository/files +``` + +Parameters: + ++ `file_path` (required) - Full path to file. Ex. lib/class.rb ++ `branch_name` (required) - The name of branch ++ `commit_message` (required) - Commit message diff --git a/lib/api/files.rb b/lib/api/files.rb index e467b0f8e9a..588c27d5692 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -40,8 +40,7 @@ module API # Update existing file in repository # # Parameters: - # file_name (required) - The name of new file. Ex. class.rb - # file_path (optional) - The path to new file. Ex. lib/ + # file_path (optional) - The path to file. Ex. lib/class.rb # branch_name (required) - The name of branch # content (required) - File content # commit_message (required) - Commit message @@ -67,7 +66,36 @@ module API render_api_error!(result[:error], 400) end end + + # Delete existing file in repository + # + # Parameters: + # file_path (optional) - The path to file. Ex. lib/class.rb + # branch_name (required) - The name of branch + # content (required) - File content + # commit_message (required) - Commit message + # + # Example Request: + # DELETE /projects/:id/repository/files + # + delete ":id/repository/files" do + required_attributes! [:file_path, :branch_name, :commit_message] + attrs = attributes_for_keys [:file_path, :branch_name, :commit_message] + branch_name = attrs.delete(:branch_name) + file_path = attrs.delete(:file_path) + result = ::Files::DeleteContext.new(user_project, current_user, attrs, branch_name, file_path).execute + + if result[:status] == :success + status(200) + + { + file_path: file_path, + branch_name: branch_name + } + else + render_api_error!(result[:error], 400) + end + end end end end - diff --git a/spec/requests/api/files_spec.rb b/spec/requests/api/files_spec.rb index 345f2bae65a..0e2a48689ac 100644 --- a/spec/requests/api/files_spec.rb +++ b/spec/requests/api/files_spec.rb @@ -78,4 +78,38 @@ describe API::API do response.status.should == 400 end end + + describe "DELETE /projects/:id/repository/files" do + let(:valid_params) { + { + file_path: 'spec/spec_helper.rb', + branch_name: 'master', + commit_message: 'Changed file' + } + } + + it "should delete existing file in project repo" do + Gitlab::Satellite::DeleteFileAction.any_instance.stub( + commit!: true, + ) + + delete api("/projects/#{project.id}/repository/files", user), valid_params + response.status.should == 200 + json_response['file_path'].should == 'spec/spec_helper.rb' + end + + it "should return a 400 bad request if no params given" do + delete 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::DeleteFileAction.any_instance.stub( + commit!: false, + ) + + delete api("/projects/#{project.id}/repository/files", user), valid_params + response.status.should == 400 + end + end end |