Commit 9e84bb8b authored by charlie ablett's avatar charlie ablett

Merge branch...

Merge branch '217830-insert-gitlab-codeowners-validationservice-into-file-webide-merge-routine' into 'master'

Apply codeowner validation when creating/updating a blob in the UI

See merge request gitlab-org/gitlab!32341
parents 6cfe09c4 1535ea91
# 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,6 +9,7 @@ class Projects::BlobController < Projects::ApplicationController
include ActionView::Helpers::SanitizeHelper
include RedirectsForMissingPathOnTree
include SourcegraphDecorator
include CheckCodeownerRules
prepend_before_action :authenticate_user!, only: [:edit]
......@@ -28,6 +29,7 @@ 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)
......@@ -71,6 +73,7 @@ class Projects::BlobController < Projects::ApplicationController
def update
@path = params[:file_path] if params[:file_path].present?
create_commit(Files::UpdateService, success_path: -> { after_edit_path },
failure_view: :edit,
failure_path: project_blob_path(@project, @id))
......@@ -115,6 +118,19 @@ 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
def blob
@blob ||= @repository.blob_at(@commit.id, @path)
......
......@@ -250,6 +250,56 @@ 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
......@@ -279,6 +329,12 @@ 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