Commit c77d957b365bd023b1dc73b5f5111b4fe262bfbe
1 parent
75303241
Exists in
master
and in
4 other branches
API: Edit file in repository
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
3 changed files
with
88 additions
and
8 deletions
Show diff stats
doc/api/repositories.md
| @@ -384,3 +384,16 @@ Parameters: | @@ -384,3 +384,16 @@ Parameters: | ||
| 384 | + `branch_name` (required) - The name of branch | 384 | + `branch_name` (required) - The name of branch |
| 385 | + `content` (required) - File content | 385 | + `content` (required) - File content |
| 386 | + `commit_message` (required) - Commit message | 386 | + `commit_message` (required) - Commit message |
| 387 | + | ||
| 388 | +## Update existing file in repository | ||
| 389 | + | ||
| 390 | +``` | ||
| 391 | +PUT /projects/:id/repository/files | ||
| 392 | +``` | ||
| 393 | + | ||
| 394 | +Parameters: | ||
| 395 | + | ||
| 396 | ++ `file_path` (required) - Full path to file. Ex. lib/class.rb | ||
| 397 | ++ `branch_name` (required) - The name of branch | ||
| 398 | ++ `content` (required) - New file content | ||
| 399 | ++ `commit_message` (required) - Commit message |
lib/api/files.rb
| @@ -16,9 +16,10 @@ module API | @@ -16,9 +16,10 @@ module API | ||
| 16 | # | 16 | # |
| 17 | # Example Request: | 17 | # Example Request: |
| 18 | # POST /projects/:id/repository/files | 18 | # POST /projects/:id/repository/files |
| 19 | + # | ||
| 19 | post ":id/repository/files" do | 20 | post ":id/repository/files" do |
| 20 | - required_attributes! [:file_name, :branch_name, :content] | ||
| 21 | - attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content] | 21 | + required_attributes! [:file_name, :branch_name, :content, :commit_message] |
| 22 | + attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content, :commit_message] | ||
| 22 | branch_name = attrs.delete(:branch_name) | 23 | branch_name = attrs.delete(:branch_name) |
| 23 | file_path = attrs.delete(:file_path) | 24 | file_path = attrs.delete(:file_path) |
| 24 | result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute | 25 | result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute |
| @@ -35,6 +36,37 @@ module API | @@ -35,6 +36,37 @@ module API | ||
| 35 | render_api_error!(result[:error], 400) | 36 | render_api_error!(result[:error], 400) |
| 36 | end | 37 | end |
| 37 | end | 38 | end |
| 39 | + | ||
| 40 | + # Update existing file in repository | ||
| 41 | + # | ||
| 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/ | ||
| 45 | + # branch_name (required) - The name of branch | ||
| 46 | + # content (required) - File content | ||
| 47 | + # commit_message (required) - Commit message | ||
| 48 | + # | ||
| 49 | + # Example Request: | ||
| 50 | + # PUT /projects/:id/repository/files | ||
| 51 | + # | ||
| 52 | + put ":id/repository/files" do | ||
| 53 | + required_attributes! [:file_path, :branch_name, :content, :commit_message] | ||
| 54 | + attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message] | ||
| 55 | + branch_name = attrs.delete(:branch_name) | ||
| 56 | + file_path = attrs.delete(:file_path) | ||
| 57 | + result = ::Files::UpdateContext.new(user_project, current_user, attrs, branch_name, file_path).execute | ||
| 58 | + | ||
| 59 | + if result[:status] == :success | ||
| 60 | + status(200) | ||
| 61 | + | ||
| 62 | + { | ||
| 63 | + file_path: file_path, | ||
| 64 | + branch_name: branch_name | ||
| 65 | + } | ||
| 66 | + else | ||
| 67 | + render_api_error!(result[:error], 400) | ||
| 68 | + end | ||
| 69 | + end | ||
| 38 | end | 70 | end |
| 39 | end | 71 | end |
| 40 | end | 72 | end |
spec/requests/api/files_spec.rb
| @@ -10,6 +10,15 @@ describe API::API do | @@ -10,6 +10,15 @@ describe API::API do | ||
| 10 | before { project.team << [user, :developer] } | 10 | before { project.team << [user, :developer] } |
| 11 | 11 | ||
| 12 | describe "POST /projects/:id/repository/files" do | 12 | describe "POST /projects/:id/repository/files" do |
| 13 | + let(:valid_params) { | ||
| 14 | + { | ||
| 15 | + file_name: 'newfile.rb', | ||
| 16 | + branch_name: 'master', | ||
| 17 | + content: 'puts 8', | ||
| 18 | + commit_message: 'Added newfile' | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + | ||
| 13 | it "should create a new file in project repo" do | 22 | it "should create a new file in project repo" do |
| 14 | Gitlab::Satellite::NewFileAction.any_instance.stub( | 23 | Gitlab::Satellite::NewFileAction.any_instance.stub( |
| 15 | commit!: true, | 24 | commit!: true, |
| @@ -35,12 +44,38 @@ describe API::API do | @@ -35,12 +44,38 @@ describe API::API do | ||
| 35 | end | 44 | end |
| 36 | end | 45 | end |
| 37 | 46 | ||
| 38 | - def valid_params | ||
| 39 | - { | ||
| 40 | - file_name: 'newfile.rb', | ||
| 41 | - branch_name: 'master', | ||
| 42 | - content: 'puts 8', | ||
| 43 | - commit_message: 'Added newfile' | 47 | + describe "PUT /projects/:id/repository/files" do |
| 48 | + let(:valid_params) { | ||
| 49 | + { | ||
| 50 | + file_path: 'spec/spec_helper.rb', | ||
| 51 | + branch_name: 'master', | ||
| 52 | + content: 'puts 8', | ||
| 53 | + commit_message: 'Changed file' | ||
| 54 | + } | ||
| 44 | } | 55 | } |
| 56 | + | ||
| 57 | + it "should update existing file in project repo" do | ||
| 58 | + Gitlab::Satellite::EditFileAction.any_instance.stub( | ||
| 59 | + commit!: true, | ||
| 60 | + ) | ||
| 61 | + | ||
| 62 | + put api("/projects/#{project.id}/repository/files", user), valid_params | ||
| 63 | + response.status.should == 200 | ||
| 64 | + json_response['file_path'].should == 'spec/spec_helper.rb' | ||
| 65 | + end | ||
| 66 | + | ||
| 67 | + it "should return a 400 bad request if no params given" do | ||
| 68 | + put api("/projects/#{project.id}/repository/files", user) | ||
| 69 | + response.status.should == 400 | ||
| 70 | + end | ||
| 71 | + | ||
| 72 | + it "should return a 400 if satellite fails to create file" do | ||
| 73 | + Gitlab::Satellite::EditFileAction.any_instance.stub( | ||
| 74 | + commit!: false, | ||
| 75 | + ) | ||
| 76 | + | ||
| 77 | + put api("/projects/#{project.id}/repository/files", user), valid_params | ||
| 78 | + response.status.should == 400 | ||
| 79 | + end | ||
| 45 | end | 80 | end |
| 46 | end | 81 | end |