Commit 160bd862bede180faf66de7e5b09300347c24279
1 parent
f61850e6
Exists in
master
and in
4 other branches
Refactoring for EditTree and NewTree controllers and contexts
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
6 changed files
with
65 additions
and
52 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,38 @@ |
| 1 | +module Files | |
| 2 | + class UpdateContext < 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 push into 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 | + blob = repository.blob_at(ref, path) | |
| 19 | + | |
| 20 | + unless blob | |
| 21 | + return error("You can only edit text files") | |
| 22 | + end | |
| 23 | + | |
| 24 | + new_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path) | |
| 25 | + created_successfully = new_file_action.commit!( | |
| 26 | + params[:content], | |
| 27 | + params[:commit_message], | |
| 28 | + params[:last_commit] | |
| 29 | + ) | |
| 30 | + | |
| 31 | + if created_successfully | |
| 32 | + success | |
| 33 | + else | |
| 34 | + error("Your changes could not be commited, because the file has been changed") | |
| 35 | + end | |
| 36 | + end | |
| 37 | + end | |
| 38 | +end | ... | ... |
app/controllers/projects/application_controller.rb
| ... | ... | @@ -23,4 +23,10 @@ class Projects::ApplicationController < ApplicationController |
| 23 | 23 | 'public_projects' |
| 24 | 24 | end |
| 25 | 25 | end |
| 26 | + | |
| 27 | + def require_branch_head | |
| 28 | + unless @repository.branch_names.include?(@ref) | |
| 29 | + redirect_to project_tree_path(@project, @ref), notice: "This action is not allowed unless you are on top of a branch" | |
| 30 | + end | |
| 31 | + end | |
| 26 | 32 | end | ... | ... |
app/controllers/projects/edit_tree_controller.rb
| 1 | -# Controller for edit a repository's file | |
| 2 | -class Projects::EditTreeController < Projects::ApplicationController | |
| 3 | - include ExtractsPath | |
| 4 | - | |
| 5 | - # Authorize | |
| 6 | - before_filter :authorize_read_project! | |
| 7 | - before_filter :authorize_code_access! | |
| 8 | - before_filter :require_non_empty_project | |
| 9 | - | |
| 10 | - before_filter :edit_requirements, only: [:show, :update] | |
| 1 | +class Projects::EditTreeController < Projects::BaseTreeController | |
| 2 | + before_filter :require_branch_head | |
| 3 | + before_filter :blob | |
| 11 | 4 | |
| 12 | 5 | def show |
| 13 | 6 | @last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha |
| 14 | 7 | end |
| 15 | 8 | |
| 16 | 9 | def update |
| 17 | - edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) | |
| 18 | - updated_successfully = edit_file_action.commit!( | |
| 19 | - params[:content], | |
| 20 | - params[:commit_message], | |
| 21 | - params[:last_commit] | |
| 22 | - ) | |
| 10 | + result = Files::UpdateContext.new(@project, current_user, params, @ref, @path).execute | |
| 23 | 11 | |
| 24 | - if updated_successfully | |
| 25 | - redirect_to project_blob_path(@project, @id), notice: "Your changes have been successfully commited" | |
| 12 | + if result[:status] == :success | |
| 13 | + flash[:notice] = "Your changes have been successfully commited" | |
| 14 | + redirect_to project_blob_path(@project, @id) | |
| 26 | 15 | else |
| 27 | - flash[:notice] = "Your changes could not be commited, because the file has been changed" | |
| 16 | + flash[:alert] = result[:error] | |
| 28 | 17 | render :show |
| 29 | 18 | end |
| 30 | 19 | end |
| 31 | 20 | |
| 32 | 21 | private |
| 33 | 22 | |
| 34 | - def edit_requirements | |
| 35 | - @blob = @repository.blob_at(@commit.id, @path) | |
| 36 | - | |
| 37 | - unless @blob | |
| 38 | - redirect_to project_blob_path(@project, @id), notice: "You can only edit text files" | |
| 39 | - end | |
| 40 | - | |
| 41 | - allowed = if project.protected_branch? @ref | |
| 42 | - can?(current_user, :push_code_to_protected_branches, project) | |
| 43 | - else | |
| 44 | - can?(current_user, :push_code, project) | |
| 45 | - end | |
| 46 | - | |
| 47 | - return access_denied! unless allowed | |
| 48 | - | |
| 49 | - unless @repository.branch_names.include?(@ref) | |
| 50 | - redirect_to project_blob_path(@project, @id), notice: "You can only edit this file if you are on top of a branch" | |
| 51 | - end | |
| 23 | + def blob | |
| 24 | + @blob ||= @repository.blob_at(@commit.id, @path) | |
| 52 | 25 | end |
| 53 | 26 | end | ... | ... |
app/controllers/projects/new_tree_controller.rb
| 1 | -class Projects::NewTreeController < Projects::ApplicationController | |
| 2 | - include ExtractsPath | |
| 3 | - | |
| 4 | - # Authorize | |
| 5 | - before_filter :authorize_read_project! | |
| 6 | - before_filter :authorize_code_access! | |
| 7 | - before_filter :require_non_empty_project | |
| 1 | +class Projects::NewTreeController < Projects::BaseTreeController | |
| 2 | + before_filter :require_branch_head | |
| 8 | 3 | |
| 9 | 4 | def show |
| 10 | 5 | end | ... | ... |
app/controllers/projects/tree_controller.rb
| 1 | 1 | # Controller for viewing a repository's file structure |
| 2 | -class Projects::TreeController < Projects::ApplicationController | |
| 3 | - include ExtractsPath | |
| 4 | - | |
| 5 | - # Authorize | |
| 6 | - before_filter :authorize_read_project! | |
| 7 | - before_filter :authorize_code_access! | |
| 8 | - before_filter :require_non_empty_project | |
| 9 | - | |
| 2 | +class Projects::TreeController < Projects::BaseTreeController | |
| 10 | 3 | def show |
| 11 | 4 | return not_found! if tree.entries.empty? |
| 12 | 5 | ... | ... |