From 0b67606af6b339b6d3a84ffd981bf7d850e3b93f Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 7 Nov 2013 18:53:09 +0200 Subject: [PATCH] New API: create file in repo Signed-off-by: Dmitriy Zaporozhets --- lib/api/api.rb | 1 + lib/api/files.rb | 41 +++++++++++++++++++ lib/gitlab/satellite/files/new_file_action.rb | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/api/files.rb diff --git a/lib/api/api.rb b/lib/api/api.rb index 4db81f42b4c..45efcc8cd47 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -39,5 +39,6 @@ module API mount DeployKeys mount ProjectHooks mount Services + mount Files end end diff --git a/lib/api/files.rb b/lib/api/files.rb new file mode 100644 index 00000000000..5918b6e5ca0 --- /dev/null +++ b/lib/api/files.rb @@ -0,0 +1,41 @@ +module API + # Projects API + class Files < Grape::API + before { authenticate! } + before { authorize! :push_code, user_project } + + resource :projects do + # Create new file in repository + # + # Parameters: + # file_name (required) - The name of new file. Ex. class.rb + # file_path (optiona) - The path to new file. Ex. lib/ + # branch_name (required) - The name of branch + # content (required) - File content + # commit_message (required) - Commit message + # + # Example Request: + # POST /projects/:id/repository/files + post ":id/repository/files" do + required_attributes! [:file_name, :branch_name, :content] + attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content] + branch_name = attrs.delete(:branch_name) + file_path = attrs.delete(:file_path) + result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute + + if result[:status] == :success + status(201) + + { + file_name: attrs[:file_name], + file_path: file_path, + branch_name: branch_name + } + else + render_api_error!(result[:error], 400) + end + end + end + end +end + diff --git a/lib/gitlab/satellite/files/new_file_action.rb b/lib/gitlab/satellite/files/new_file_action.rb index 9fe5a38eb80..70a5ea219de 100644 --- a/lib/gitlab/satellite/files/new_file_action.rb +++ b/lib/gitlab/satellite/files/new_file_action.rb @@ -17,7 +17,7 @@ module Gitlab repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") # update the file in the satellite's working dir - file_path_in_satellite = File.join(repo.working_dir, file_path, file_name) + file_path_in_satellite = File.join(repo.working_dir, file_path || '', file_name) File.open(file_path_in_satellite, 'w') { |f| f.write(content) } # add new file