Commit 9772cefc43d1b9ea8ad526a33e20706361857b0f

Authored by Dmitriy Zaporozhets
2 parents b0f30006 a185cce5

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

doc/api/repositories.md
... ... @@ -397,3 +397,15 @@ Parameters:
397 397 + `branch_name` (required) - The name of branch
398 398 + `content` (required) - New file content
399 399 + `commit_message` (required) - Commit message
  400 +
  401 +## Delete existing file in repository
  402 +
  403 +```
  404 +DELETE /projects/:id/repository/files
  405 +```
  406 +
  407 +Parameters:
  408 +
  409 ++ `file_path` (required) - Full path to file. Ex. lib/class.rb
  410 ++ `branch_name` (required) - The name of branch
  411 ++ `commit_message` (required) - Commit message
... ...
lib/api/files.rb
... ... @@ -40,8 +40,7 @@ module API
40 40 # Update existing file in repository
41 41 #
42 42 # Parameters:
43   - # file_name (required) - The name of new file. Ex. class.rb
44   - # file_path (optional) - The path to new file. Ex. lib/
  43 + # file_path (optional) - The path to file. Ex. lib/class.rb
45 44 # branch_name (required) - The name of branch
46 45 # content (required) - File content
47 46 # commit_message (required) - Commit message
... ... @@ -67,7 +66,36 @@ module API
67 66 render_api_error!(result[:error], 400)
68 67 end
69 68 end
  69 +
  70 + # Delete existing file in repository
  71 + #
  72 + # Parameters:
  73 + # file_path (optional) - The path to file. Ex. lib/class.rb
  74 + # branch_name (required) - The name of branch
  75 + # content (required) - File content
  76 + # commit_message (required) - Commit message
  77 + #
  78 + # Example Request:
  79 + # DELETE /projects/:id/repository/files
  80 + #
  81 + delete ":id/repository/files" do
  82 + required_attributes! [:file_path, :branch_name, :commit_message]
  83 + attrs = attributes_for_keys [:file_path, :branch_name, :commit_message]
  84 + branch_name = attrs.delete(:branch_name)
  85 + file_path = attrs.delete(:file_path)
  86 + result = ::Files::DeleteContext.new(user_project, current_user, attrs, branch_name, file_path).execute
  87 +
  88 + if result[:status] == :success
  89 + status(200)
  90 +
  91 + {
  92 + file_path: file_path,
  93 + branch_name: branch_name
  94 + }
  95 + else
  96 + render_api_error!(result[:error], 400)
  97 + end
  98 + end
70 99 end
71 100 end
72 101 end
73   -
... ...
spec/requests/api/files_spec.rb
... ... @@ -78,4 +78,38 @@ describe API::API do
78 78 response.status.should == 400
79 79 end
80 80 end
  81 +
  82 + describe "DELETE /projects/:id/repository/files" do
  83 + let(:valid_params) {
  84 + {
  85 + file_path: 'spec/spec_helper.rb',
  86 + branch_name: 'master',
  87 + commit_message: 'Changed file'
  88 + }
  89 + }
  90 +
  91 + it "should delete existing file in project repo" do
  92 + Gitlab::Satellite::DeleteFileAction.any_instance.stub(
  93 + commit!: true,
  94 + )
  95 +
  96 + delete api("/projects/#{project.id}/repository/files", user), valid_params
  97 + response.status.should == 200
  98 + json_response['file_path'].should == 'spec/spec_helper.rb'
  99 + end
  100 +
  101 + it "should return a 400 bad request if no params given" do
  102 + delete api("/projects/#{project.id}/repository/files", user)
  103 + response.status.should == 400
  104 + end
  105 +
  106 + it "should return a 400 if satellite fails to create file" do
  107 + Gitlab::Satellite::DeleteFileAction.any_instance.stub(
  108 + commit!: false,
  109 + )
  110 +
  111 + delete api("/projects/#{project.id}/repository/files", user), valid_params
  112 + response.status.should == 400
  113 + end
  114 + end
81 115 end
... ...