Commit 32d7332d2c9fe21db74073e65d1317c11dc2c861

Authored by Dmitriy Zaporozhets
1 parent bd20ec1a

API: delete file from repository

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing 2 changed files with 43 additions and 3 deletions   Show diff stats
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   -
... ...