Commit a516a846 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'feature/edit_file_api' of /home/git/repositories/gitlab/gitlabhq

parents 75303241 986697a9
...@@ -33,11 +33,10 @@ module Files ...@@ -33,11 +33,10 @@ module Files
return error("Your changes could not be commited, because file with such name exists") return error("Your changes could not be commited, because file with such name exists")
end end
new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, path) new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, file_path)
created_successfully = new_file_action.commit!( created_successfully = new_file_action.commit!(
params[:content], params[:content],
params[:commit_message], params[:commit_message]
file_name,
) )
if created_successfully if created_successfully
......
...@@ -24,8 +24,7 @@ module Files ...@@ -24,8 +24,7 @@ module Files
new_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path) new_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path)
created_successfully = new_file_action.commit!( created_successfully = new_file_action.commit!(
params[:content], params[:content],
params[:commit_message], params[:commit_message]
params[:last_commit]
) )
if created_successfully if created_successfully
......
...@@ -384,3 +384,16 @@ Parameters: ...@@ -384,3 +384,16 @@ Parameters:
+ `branch_name` (required) - The name of branch + `branch_name` (required) - The name of branch
+ `content` (required) - File content + `content` (required) - File content
+ `commit_message` (required) - Commit message + `commit_message` (required) - Commit message
## Update existing file in repository
```
PUT /projects/:id/repository/files
```
Parameters:
+ `file_path` (required) - Full path to file. Ex. lib/class.rb
+ `branch_name` (required) - The name of branch
+ `content` (required) - New file content
+ `commit_message` (required) - Commit message
...@@ -16,9 +16,10 @@ module API ...@@ -16,9 +16,10 @@ module API
# #
# Example Request: # Example Request:
# POST /projects/:id/repository/files # POST /projects/:id/repository/files
#
post ":id/repository/files" do post ":id/repository/files" do
required_attributes! [:file_name, :branch_name, :content] required_attributes! [:file_name, :branch_name, :content, :commit_message]
attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content] attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content, :commit_message]
branch_name = attrs.delete(:branch_name) branch_name = attrs.delete(:branch_name)
file_path = attrs.delete(:file_path) file_path = attrs.delete(:file_path)
result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute 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
render_api_error!(result[:error], 400) render_api_error!(result[:error], 400)
end end
end end
# Update existing file in repository
#
# Parameters:
# file_name (required) - The name of new file. Ex. class.rb
# file_path (optional) - 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:
# PUT /projects/:id/repository/files
#
put ":id/repository/files" do
required_attributes! [:file_path, :branch_name, :content, :commit_message]
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message]
branch_name = attrs.delete(:branch_name)
file_path = attrs.delete(:file_path)
result = ::Files::UpdateContext.new(user_project, current_user, attrs, branch_name, file_path).execute
if result[:status] == :success
status(200)
{
file_path: file_path,
branch_name: branch_name
}
else
render_api_error!(result[:error], 400)
end
end
end end
end end
end end
......
...@@ -10,9 +10,7 @@ module Gitlab ...@@ -10,9 +10,7 @@ module Gitlab
# Returns false if committing the change fails # Returns false if committing the change fails
# Returns false if pushing from the satellite to Gitolite failed or was rejected # Returns false if pushing from the satellite to Gitolite failed or was rejected
# Returns true otherwise # Returns true otherwise
def commit!(content, commit_message, last_commit) def commit!(content, commit_message)
return false unless can_edit?(last_commit)
in_locked_and_timed_satellite do |repo| in_locked_and_timed_satellite do |repo|
prepare_satellite!(repo) prepare_satellite!(repo)
......
...@@ -8,13 +8,6 @@ module Gitlab ...@@ -8,13 +8,6 @@ module Gitlab
@file_path = file_path @file_path = file_path
@ref = ref @ref = ref
end end
protected
def can_edit?(last_commit)
current_last_commit = Gitlab::Git::Commit.last_for_path(@project.repository, ref, file_path).sha
last_commit == current_last_commit
end
end end
end end
end end
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
# Returns false if committing the change fails # Returns false if committing the change fails
# Returns false if pushing from the satellite to Gitolite failed or was rejected # Returns false if pushing from the satellite to Gitolite failed or was rejected
# Returns true otherwise # Returns true otherwise
def commit!(content, commit_message, file_name) def commit!(content, commit_message)
in_locked_and_timed_satellite do |repo| in_locked_and_timed_satellite do |repo|
prepare_satellite!(repo) prepare_satellite!(repo)
...@@ -17,7 +17,7 @@ module Gitlab ...@@ -17,7 +17,7 @@ module Gitlab
repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}")
# update the file in the satellite's working dir # 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.open(file_path_in_satellite, 'w') { |f| f.write(content) } File.open(file_path_in_satellite, 'w') { |f| f.write(content) }
# add new file # add new file
......
...@@ -10,6 +10,15 @@ describe API::API do ...@@ -10,6 +10,15 @@ describe API::API do
before { project.team << [user, :developer] } before { project.team << [user, :developer] }
describe "POST /projects/:id/repository/files" do describe "POST /projects/:id/repository/files" do
let(:valid_params) {
{
file_name: 'newfile.rb',
branch_name: 'master',
content: 'puts 8',
commit_message: 'Added newfile'
}
}
it "should create a new file in project repo" do it "should create a new file in project repo" do
Gitlab::Satellite::NewFileAction.any_instance.stub( Gitlab::Satellite::NewFileAction.any_instance.stub(
commit!: true, commit!: true,
...@@ -35,12 +44,38 @@ describe API::API do ...@@ -35,12 +44,38 @@ describe API::API do
end end
end end
def valid_params describe "PUT /projects/:id/repository/files" do
{ let(:valid_params) {
file_name: 'newfile.rb', {
branch_name: 'master', file_path: 'spec/spec_helper.rb',
content: 'puts 8', branch_name: 'master',
commit_message: 'Added newfile' content: 'puts 8',
commit_message: 'Changed file'
}
} }
it "should update existing file in project repo" do
Gitlab::Satellite::EditFileAction.any_instance.stub(
commit!: true,
)
put api("/projects/#{project.id}/repository/files", user), valid_params
response.status.should == 200
json_response['file_path'].should == 'spec/spec_helper.rb'
end
it "should return a 400 bad request if no params given" do
put api("/projects/#{project.id}/repository/files", user)
response.status.should == 400
end
it "should return a 400 if satellite fails to create file" do
Gitlab::Satellite::EditFileAction.any_instance.stub(
commit!: false,
)
put api("/projects/#{project.id}/repository/files", user), valid_params
response.status.should == 400
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment