Commit ca196453f000c81d2f476827d718d010a128cf10
Exists in
master
and in
4 other branches
Merge branch 'feature/api_create_file' of /home/git/repositories/gitlab/gitlabhq
Showing
6 changed files
with
106 additions
and
2 deletions
Show diff stats
doc/api/repositories.md
| @@ -368,4 +368,19 @@ GET /projects/:id/repository/archive | @@ -368,4 +368,19 @@ GET /projects/:id/repository/archive | ||
| 368 | 368 | ||
| 369 | Parameters: | 369 | Parameters: |
| 370 | + `id` (required) - The ID of a project | 370 | + `id` (required) - The ID of a project |
| 371 | -+ `sha` (optional) - The commit sha to download defaults to the tip of the default branch | ||
| 372 | \ No newline at end of file | 371 | \ No newline at end of file |
| 372 | ++ `sha` (optional) - The commit sha to download defaults to the tip of the default branch | ||
| 373 | + | ||
| 374 | + | ||
| 375 | +## Create new file in repository | ||
| 376 | + | ||
| 377 | +``` | ||
| 378 | +POST /projects/:id/repository/files | ||
| 379 | +``` | ||
| 380 | + | ||
| 381 | +Parameters: | ||
| 382 | + | ||
| 383 | ++ `file_name` (required) - The name of new file. Ex. class.rb | ||
| 384 | ++ `file_path` (optiona) - The path to new file. Ex. lib/ | ||
| 385 | ++ `branch_name` (required) - The name of branch | ||
| 386 | ++ `content` (required) - File content | ||
| 387 | ++ `commit_message` (required) - Commit message |
lib/api/api.rb
| @@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
| 1 | +module API | ||
| 2 | + # Projects API | ||
| 3 | + class Files < Grape::API | ||
| 4 | + before { authenticate! } | ||
| 5 | + before { authorize! :push_code, user_project } | ||
| 6 | + | ||
| 7 | + resource :projects do | ||
| 8 | + # Create new file in repository | ||
| 9 | + # | ||
| 10 | + # Parameters: | ||
| 11 | + # file_name (required) - The name of new file. Ex. class.rb | ||
| 12 | + # file_path (optiona) - The path to new file. Ex. lib/ | ||
| 13 | + # branch_name (required) - The name of branch | ||
| 14 | + # content (required) - File content | ||
| 15 | + # commit_message (required) - Commit message | ||
| 16 | + # | ||
| 17 | + # Example Request: | ||
| 18 | + # POST /projects/:id/repository/files | ||
| 19 | + 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] | ||
| 22 | + branch_name = attrs.delete(:branch_name) | ||
| 23 | + file_path = attrs.delete(:file_path) | ||
| 24 | + result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute | ||
| 25 | + | ||
| 26 | + if result[:status] == :success | ||
| 27 | + status(201) | ||
| 28 | + | ||
| 29 | + { | ||
| 30 | + file_name: attrs[:file_name], | ||
| 31 | + file_path: file_path, | ||
| 32 | + branch_name: branch_name | ||
| 33 | + } | ||
| 34 | + else | ||
| 35 | + render_api_error!(result[:error], 400) | ||
| 36 | + end | ||
| 37 | + end | ||
| 38 | + end | ||
| 39 | + end | ||
| 40 | +end | ||
| 41 | + |
lib/gitlab/satellite/files/new_file_action.rb
| @@ -17,7 +17,7 @@ module Gitlab | @@ -17,7 +17,7 @@ module Gitlab | ||
| 17 | repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") | 17 | repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") |
| 18 | 18 | ||
| 19 | # update the file in the satellite's working dir | 19 | # update the file in the satellite's working dir |
| 20 | - file_path_in_satellite = File.join(repo.working_dir, file_path, file_name) | 20 | + file_path_in_satellite = File.join(repo.working_dir, file_path || '', file_name) |
| 21 | File.open(file_path_in_satellite, 'w') { |f| f.write(content) } | 21 | File.open(file_path_in_satellite, 'w') { |f| f.write(content) } |
| 22 | 22 | ||
| 23 | # add new file | 23 | # add new file |
| @@ -0,0 +1,46 @@ | @@ -0,0 +1,46 @@ | ||
| 1 | +require 'spec_helper' | ||
| 2 | + | ||
| 3 | +describe API::API do | ||
| 4 | + include ApiHelpers | ||
| 5 | + before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } | ||
| 6 | + after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } | ||
| 7 | + | ||
| 8 | + let(:user) { create(:user) } | ||
| 9 | + let!(:project) { create(:project_with_code, namespace: user.namespace ) } | ||
| 10 | + before { project.team << [user, :developer] } | ||
| 11 | + | ||
| 12 | + describe "POST /projects/:id/repository/files" do | ||
| 13 | + it "should create a new file in project repo" do | ||
| 14 | + Gitlab::Satellite::NewFileAction.any_instance.stub( | ||
| 15 | + commit!: true, | ||
| 16 | + ) | ||
| 17 | + | ||
| 18 | + post api("/projects/#{project.id}/repository/files", user), valid_params | ||
| 19 | + response.status.should == 201 | ||
| 20 | + json_response['file_name'].should == 'newfile.rb' | ||
| 21 | + end | ||
| 22 | + | ||
| 23 | + it "should return a 400 bad request if no params given" do | ||
| 24 | + post api("/projects/#{project.id}/repository/files", user) | ||
| 25 | + response.status.should == 400 | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + it "should return a 400 if satellite fails to create file" do | ||
| 29 | + Gitlab::Satellite::NewFileAction.any_instance.stub( | ||
| 30 | + commit!: false, | ||
| 31 | + ) | ||
| 32 | + | ||
| 33 | + post api("/projects/#{project.id}/repository/files", user), valid_params | ||
| 34 | + response.status.should == 400 | ||
| 35 | + end | ||
| 36 | + end | ||
| 37 | + | ||
| 38 | + def valid_params | ||
| 39 | + { | ||
| 40 | + file_name: 'newfile.rb', | ||
| 41 | + branch_name: 'master', | ||
| 42 | + content: 'puts 8', | ||
| 43 | + commit_message: 'Added newfile' | ||
| 44 | + } | ||
| 45 | + end | ||
| 46 | +end |
spec/support/test_env.rb
| @@ -45,6 +45,7 @@ module TestEnv | @@ -45,6 +45,7 @@ module TestEnv | ||
| 45 | def disable_mailer | 45 | def disable_mailer |
| 46 | NotificationService.any_instance.stub(mailer: double.as_null_object) | 46 | NotificationService.any_instance.stub(mailer: double.as_null_object) |
| 47 | end | 47 | end |
| 48 | + | ||
| 48 | def enable_mailer | 49 | def enable_mailer |
| 49 | NotificationService.any_instance.unstub(:mailer) | 50 | NotificationService.any_instance.unstub(:mailer) |
| 50 | end | 51 | end |