Commit 0b67606af6b339b6d3a84ffd981bf7d850e3b93f
1 parent
c28786ec
Exists in
master
and in
4 other branches
New API: create file in repo
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
3 changed files
with
43 additions
and
1 deletions
Show diff stats
lib/api/api.rb
... | ... | @@ -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 | 17 | repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") |
18 | 18 | |
19 | 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 | 21 | File.open(file_path_in_satellite, 'w') { |f| f.write(content) } |
22 | 22 | |
23 | 23 | # add new file | ... | ... |