Commit f61850e6e3521073f49f1cfe429b7fe49f3b44f0

Authored by Dmitriy Zaporozhets
1 parent f2eb6683

Move part of file creation logic into separate context

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/contexts/base_context.rb
@@ -17,4 +17,3 @@ class BaseContext @@ -17,4 +17,3 @@ class BaseContext
17 abilities.allowed?(object, action, subject) 17 abilities.allowed?(object, action, subject)
18 end 18 end
19 end 19 end
20 -  
app/contexts/files/base_context.rb 0 → 100644
@@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
  1 +module Files
  2 + class BaseContext < ::BaseContext
  3 + attr_reader :ref, :path
  4 +
  5 + def initialize(project, user, params, ref, path = nil)
  6 + @project, @current_user, @params = project, user, params.dup
  7 + @ref = ref
  8 + @path = path
  9 + end
  10 +
  11 + private
  12 +
  13 + def error(message)
  14 + {
  15 + error: message,
  16 + status: :error
  17 + }
  18 + end
  19 +
  20 + def success
  21 + {
  22 + error: '',
  23 + status: :success
  24 + }
  25 + end
  26 +
  27 + def repository
  28 + project.repository
  29 + end
  30 + end
  31 +end
app/contexts/files/create_context.rb 0 → 100644
@@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
  1 +module Files
  2 + class CreateContext < BaseContext
  3 + def execute
  4 + allowed = if project.protected_branch?(ref)
  5 + can?(current_user, :push_code_to_protected_branches, project)
  6 + else
  7 + can?(current_user, :push_code, project)
  8 + end
  9 +
  10 + unless allowed
  11 + return error("You are not allowed to create file in this branch")
  12 + end
  13 +
  14 + unless repository.branch_names.include?(ref)
  15 + return error("You can only create files if you are on top of a branch")
  16 + end
  17 +
  18 + file_name = params[:file_name]
  19 +
  20 + unless file_name =~ Gitlab::Regex.path_regex
  21 + return error("Your changes could not be commited, because file name contains not allowed characters")
  22 + end
  23 +
  24 + file_path = if path.blank?
  25 + file_name
  26 + else
  27 + File.join(path, file_name)
  28 + end
  29 +
  30 + blob = repository.blob_at(ref, file_path)
  31 +
  32 + if blob
  33 + return error("Your changes could not be commited, because file with such name exists")
  34 + end
  35 +
  36 + new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, path)
  37 + created_successfully = new_file_action.commit!(
  38 + params[:content],
  39 + params[:commit_message],
  40 + file_name,
  41 + )
  42 +
  43 + if created_successfully
  44 + success
  45 + else
  46 + error("Your changes could not be commited, because the file has been changed")
  47 + end
  48 + end
  49 + end
  50 +end
app/controllers/projects/new_tree_controller.rb
@@ -6,60 +6,18 @@ class Projects::NewTreeController &lt; Projects::ApplicationController @@ -6,60 +6,18 @@ class Projects::NewTreeController &lt; Projects::ApplicationController
6 before_filter :authorize_code_access! 6 before_filter :authorize_code_access!
7 before_filter :require_non_empty_project 7 before_filter :require_non_empty_project
8 8
9 - before_filter :create_requirements, only: [:show, :update]  
10 -  
11 def show 9 def show
12 end 10 end
13 11
14 def update 12 def update
15 - file_name = params[:file_name]  
16 -  
17 - unless file_name =~ Gitlab::Regex.path_regex  
18 - flash[:notice] = "Your changes could not be commited, because file name contains not allowed characters"  
19 - render :show and return  
20 - end  
21 -  
22 - file_path = if @path.blank?  
23 - file_name  
24 - else  
25 - File.join(@path, file_name)  
26 - end  
27 -  
28 - blob = @repository.blob_at(@commit.id, file_path)  
29 -  
30 - if blob  
31 - flash[:notice] = "Your changes could not be commited, because file with such name exists"  
32 - render :show and return  
33 - end 13 + result = Files::CreateContext.new(@project, current_user, params, @ref, @path).execute
34 14
35 - new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, @project, @ref, @path)  
36 - updated_successfully = new_file_action.commit!(  
37 - params[:content],  
38 - params[:commit_message],  
39 - file_name,  
40 - )  
41 -  
42 - if updated_successfully  
43 - redirect_to project_blob_path(@project, File.join(@id, params[:file_name])), notice: "Your changes have been successfully commited" 15 + if result[:status] == :success
  16 + flash[:notice] = "Your changes have been successfully commited"
  17 + redirect_to project_blob_path(@project, File.join(@id, params[:file_name]))
44 else 18 else
45 - flash[:notice] = "Your changes could not be commited, because the file has been changed" 19 + flash[:alert] = result[:error]
46 render :show 20 render :show
47 end 21 end
48 end 22 end
49 -  
50 - private  
51 -  
52 - def create_requirements  
53 - allowed = if project.protected_branch? @ref  
54 - can?(current_user, :push_code_to_protected_branches, project)  
55 - else  
56 - can?(current_user, :push_code, project)  
57 - end  
58 -  
59 - return access_denied! unless allowed  
60 -  
61 - unless @repository.branch_names.include?(@ref)  
62 - redirect_to project_blob_path(@project, @id), notice: "You can only create files if you are on top of a branch"  
63 - end  
64 - end  
65 end 23 end
app/views/projects/new_tree/show.html.haml
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 .controls 8 .controls
9 %span.monospace= @path[-1] == "/" ? @path : @path + "/" 9 %span.monospace= @path[-1] == "/" ? @path : @path + "/"
10 &nbsp; 10 &nbsp;
11 - = text_field_tag 'file_name', '', placeholder: "sample.rb", required: true 11 + = text_field_tag 'file_name', params[:file_name], placeholder: "sample.rb", required: true
12 %span 12 %span
13 &nbsp; 13 &nbsp;
14 on 14 on
@@ -18,13 +18,13 @@ @@ -18,13 +18,13 @@
18 = label_tag 'commit_message', class: "control-label" do 18 = label_tag 'commit_message', class: "control-label" do
19 Commit message 19 Commit message
20 .controls 20 .controls
21 - = text_area_tag 'commit_message', '', placeholder: "Added new file", required: true, rows: 3 21 + = text_area_tag 'commit_message', params[:commit_message], placeholder: "Added new file", required: true, rows: 3
22 22
23 .file-holder 23 .file-holder
24 .file-title 24 .file-title
25 %i.icon-file 25 %i.icon-file
26 .file-content.code 26 .file-content.code
27 - %pre#editor= "" 27 + %pre#editor= params[:content]
28 28
29 .form-actions 29 .form-actions
30 = hidden_field_tag 'content', '', id: "file-content" 30 = hidden_field_tag 'content', '', id: "file-content"