Commit 98fdd617 authored by Albert Salim's avatar Albert Salim

Move code owner rule validation into EE

- `Gitlab::CodeOwners::Validator` is an EE module
so anything depending on this needs to be in EE
- Inline `#codeowners_check_error`
- Use getter methods for instance vars in module
parent 59122560
# frozen_string_literal: true
module CheckCodeownerRules
extend ActiveSupport::Concern
def codeowners_check_error(project, branch_name, paths)
::Gitlab::CodeOwners::Validator.new(project, branch_name, paths).execute
end
end
......@@ -9,7 +9,6 @@ class Projects::BlobController < Projects::ApplicationController
include ActionView::Helpers::SanitizeHelper
include RedirectsForMissingPathOnTree
include SourcegraphDecorator
include CheckCodeownerRules
prepend_before_action :authenticate_user!, only: [:edit]
......@@ -29,7 +28,6 @@ class Projects::BlobController < Projects::ApplicationController
before_action :editor_variables, except: [:show, :preview, :diff]
before_action :validate_diff_params, only: :diff
before_action :set_last_commit_sha, only: [:edit, :update]
before_action :validate_codeowner_rules, only: [:create, :update]
before_action only: :show do
push_frontend_feature_flag(:code_navigation, @project)
......@@ -118,18 +116,7 @@ class Projects::BlobController < Projects::ApplicationController
private
def validate_codeowner_rules
return if params[:file_path].blank?
codeowners_error = codeowners_check_error(@project, @branch_name, params[:file_path])
if codeowners_error.present?
flash.now[:alert] = codeowners_error
view = params[:action] == 'update' ? :edit : :new
render view
end
end
attr_reader :branch_name
def blob
@blob ||= @repository.blob_at(@commit.id, @path)
......@@ -270,3 +257,5 @@ class Projects::BlobController < Projects::ApplicationController
params.permit(:full, :since, :to, :bottom, :unfold, :offset, :indent)
end
end
Projects::BlobController.prepend_if_ee('EE::Projects::BlobController')
# frozen_string_literal: true
module EE
module Projects
module BlobController
extend ActiveSupport::Concern
prepended do
before_action :validate_codeowner_rules, only: [:create, :update]
end
private
def validate_codeowner_rules
return if params[:file_path].blank?
codeowners_error = codeowners_check_error(project, branch_name, params[:file_path])
if codeowners_error.present?
flash.now[:alert] = codeowners_error
view = params[:action] == 'update' ? :edit : :new
render view
end
end
def codeowners_check_error(project, branch_name, paths)
::Gitlab::CodeOwners::Validator.new(project, branch_name, paths).execute
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Projects::BlobController do
include ProjectForksHelper
let(:project) { create(:project, :public, :repository) }
shared_examples "file matches a codeowners rule" do
let(:error_msg) { "Example error msg" }
it "renders to the edit page with an error msg" do
default_params[:file_path] = "CHANGELOG"
expect_next_instance_of(Gitlab::CodeOwners::Validator) do |validator|
expect(validator).to receive(:execute).and_return(error_msg)
end
subject
expect(flash[:alert]).to eq(error_msg)
expect(response).to render_template(expected_view)
end
end
describe 'POST create' do
let(:user) { create(:user) }
let(:default_params) do
{
namespace_id: project.namespace,
project_id: project,
id: 'master',
branch_name: 'master',
file_name: 'CHANGELOG',
content: 'Added changes',
commit_message: 'Create CHANGELOG'
}
end
before do
project.add_developer(user)
sign_in(user)
end
it 'redirects to blob' do
post :create, params: default_params
expect(response).to be_ok
end
it_behaves_like "file matches a codeowners rule" do
subject { post :create, params: default_params }
let(:expected_view) { :new }
end
end
describe 'PUT update' do
let(:user) { create(:user) }
let(:default_params) do
{
namespace_id: project.namespace,
project_id: project,
id: 'master/CHANGELOG',
branch_name: 'master',
content: 'Added changes',
commit_message: 'Update CHANGELOG'
}
end
def blob_after_edit_path
project_blob_path(project, 'master/CHANGELOG')
end
before do
project.add_maintainer(user)
sign_in(user)
end
it_behaves_like "file matches a codeowners rule" do
subject { put :update, params: default_params }
let(:expected_view) { :edit }
end
end
end
......@@ -250,56 +250,6 @@ describe Projects::BlobController do
end
end
shared_examples "file matches a codeowners rule" do
let(:error_msg) { "Example error msg" }
it "renders to the edit page with an error msg" do
default_params[:file_path] = "CHANGELOG"
expect_next_instance_of(Gitlab::CodeOwners::Validator) do |validator|
expect(validator).to receive(:execute).and_return(error_msg)
end
subject
expect(flash[:alert]).to eq(error_msg)
expect(response).to render_template(expected_view)
end
end
describe 'POST create' do
let(:user) { create(:user) }
let(:default_params) do
{
namespace_id: project.namespace,
project_id: project,
id: 'master',
branch_name: 'master',
file_name: 'CHANGELOG',
content: 'Added changes',
commit_message: 'Create CHANGELOG'
}
end
before do
project.add_developer(user)
sign_in(user)
end
it 'redirects to blob' do
post :create, params: default_params
expect(response).to be_ok
end
it_behaves_like "file matches a codeowners rule" do
subject { post :create, params: default_params }
let(:expected_view) { :new }
end
end
describe 'PUT update' do
let(:user) { create(:user) }
let(:default_params) do
......@@ -329,12 +279,6 @@ describe Projects::BlobController do
expect(response).to redirect_to(blob_after_edit_path)
end
it_behaves_like "file matches a codeowners rule" do
subject { put :update, params: default_params }
let(:expected_view) { :edit }
end
context '?from_merge_request_iid' do
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
let(:mr_params) { default_params.merge(from_merge_request_iid: merge_request.iid) }
......
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