Move part of file creation logic into separate context
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
parent
f2eb668348
commit
f61850e6e3
|
@ -17,4 +17,3 @@ class BaseContext
|
||||||
abilities.allowed?(object, action, subject)
|
abilities.allowed?(object, action, subject)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
module Files
|
||||||
|
class BaseContext < ::BaseContext
|
||||||
|
attr_reader :ref, :path
|
||||||
|
|
||||||
|
def initialize(project, user, params, ref, path = nil)
|
||||||
|
@project, @current_user, @params = project, user, params.dup
|
||||||
|
@ref = ref
|
||||||
|
@path = path
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def error(message)
|
||||||
|
{
|
||||||
|
error: message,
|
||||||
|
status: :error
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def success
|
||||||
|
{
|
||||||
|
error: '',
|
||||||
|
status: :success
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def repository
|
||||||
|
project.repository
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,50 @@
|
||||||
|
module Files
|
||||||
|
class CreateContext < BaseContext
|
||||||
|
def execute
|
||||||
|
allowed = if project.protected_branch?(ref)
|
||||||
|
can?(current_user, :push_code_to_protected_branches, project)
|
||||||
|
else
|
||||||
|
can?(current_user, :push_code, project)
|
||||||
|
end
|
||||||
|
|
||||||
|
unless allowed
|
||||||
|
return error("You are not allowed to create file in this branch")
|
||||||
|
end
|
||||||
|
|
||||||
|
unless repository.branch_names.include?(ref)
|
||||||
|
return error("You can only create files if you are on top of a branch")
|
||||||
|
end
|
||||||
|
|
||||||
|
file_name = params[:file_name]
|
||||||
|
|
||||||
|
unless file_name =~ Gitlab::Regex.path_regex
|
||||||
|
return error("Your changes could not be commited, because file name contains not allowed characters")
|
||||||
|
end
|
||||||
|
|
||||||
|
file_path = if path.blank?
|
||||||
|
file_name
|
||||||
|
else
|
||||||
|
File.join(path, file_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
blob = repository.blob_at(ref, file_path)
|
||||||
|
|
||||||
|
if blob
|
||||||
|
return error("Your changes could not be commited, because file with such name exists")
|
||||||
|
end
|
||||||
|
|
||||||
|
new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, path)
|
||||||
|
created_successfully = new_file_action.commit!(
|
||||||
|
params[:content],
|
||||||
|
params[:commit_message],
|
||||||
|
file_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
if created_successfully
|
||||||
|
success
|
||||||
|
else
|
||||||
|
error("Your changes could not be commited, because the file has been changed")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -6,60 +6,18 @@ class Projects::NewTreeController < Projects::ApplicationController
|
||||||
before_filter :authorize_code_access!
|
before_filter :authorize_code_access!
|
||||||
before_filter :require_non_empty_project
|
before_filter :require_non_empty_project
|
||||||
|
|
||||||
before_filter :create_requirements, only: [:show, :update]
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
file_name = params[:file_name]
|
result = Files::CreateContext.new(@project, current_user, params, @ref, @path).execute
|
||||||
|
|
||||||
unless file_name =~ Gitlab::Regex.path_regex
|
if result[:status] == :success
|
||||||
flash[:notice] = "Your changes could not be commited, because file name contains not allowed characters"
|
flash[:notice] = "Your changes have been successfully commited"
|
||||||
render :show and return
|
redirect_to project_blob_path(@project, File.join(@id, params[:file_name]))
|
||||||
end
|
|
||||||
|
|
||||||
file_path = if @path.blank?
|
|
||||||
file_name
|
|
||||||
else
|
|
||||||
File.join(@path, file_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
blob = @repository.blob_at(@commit.id, file_path)
|
|
||||||
|
|
||||||
if blob
|
|
||||||
flash[:notice] = "Your changes could not be commited, because file with such name exists"
|
|
||||||
render :show and return
|
|
||||||
end
|
|
||||||
|
|
||||||
new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, @project, @ref, @path)
|
|
||||||
updated_successfully = new_file_action.commit!(
|
|
||||||
params[:content],
|
|
||||||
params[:commit_message],
|
|
||||||
file_name,
|
|
||||||
)
|
|
||||||
|
|
||||||
if updated_successfully
|
|
||||||
redirect_to project_blob_path(@project, File.join(@id, params[:file_name])), notice: "Your changes have been successfully commited"
|
|
||||||
else
|
else
|
||||||
flash[:notice] = "Your changes could not be commited, because the file has been changed"
|
flash[:alert] = result[:error]
|
||||||
render :show
|
render :show
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def create_requirements
|
|
||||||
allowed = if project.protected_branch? @ref
|
|
||||||
can?(current_user, :push_code_to_protected_branches, project)
|
|
||||||
else
|
|
||||||
can?(current_user, :push_code, project)
|
|
||||||
end
|
|
||||||
|
|
||||||
return access_denied! unless allowed
|
|
||||||
|
|
||||||
unless @repository.branch_names.include?(@ref)
|
|
||||||
redirect_to project_blob_path(@project, @id), notice: "You can only create files if you are on top of a branch"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
.controls
|
.controls
|
||||||
%span.monospace= @path[-1] == "/" ? @path : @path + "/"
|
%span.monospace= @path[-1] == "/" ? @path : @path + "/"
|
||||||
|
|
||||||
= text_field_tag 'file_name', '', placeholder: "sample.rb", required: true
|
= text_field_tag 'file_name', params[:file_name], placeholder: "sample.rb", required: true
|
||||||
%span
|
%span
|
||||||
|
|
||||||
on
|
on
|
||||||
|
@ -18,13 +18,13 @@
|
||||||
= label_tag 'commit_message', class: "control-label" do
|
= label_tag 'commit_message', class: "control-label" do
|
||||||
Commit message
|
Commit message
|
||||||
.controls
|
.controls
|
||||||
= text_area_tag 'commit_message', '', placeholder: "Added new file", required: true, rows: 3
|
= text_area_tag 'commit_message', params[:commit_message], placeholder: "Added new file", required: true, rows: 3
|
||||||
|
|
||||||
.file-holder
|
.file-holder
|
||||||
.file-title
|
.file-title
|
||||||
%i.icon-file
|
%i.icon-file
|
||||||
.file-content.code
|
.file-content.code
|
||||||
%pre#editor= ""
|
%pre#editor= params[:content]
|
||||||
|
|
||||||
.form-actions
|
.form-actions
|
||||||
= hidden_field_tag 'content', '', id: "file-content"
|
= hidden_field_tag 'content', '', id: "file-content"
|
||||||
|
|
Loading…
Reference in New Issue