Commit 1535ea91 authored by Kerri Miller's avatar Kerri Miller Committed by charlie ablett

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

parent 1998e450
# 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 ...@@ -9,6 +9,7 @@ class Projects::BlobController < Projects::ApplicationController
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
include RedirectsForMissingPathOnTree include RedirectsForMissingPathOnTree
include SourcegraphDecorator include SourcegraphDecorator
include CheckCodeownerRules
prepend_before_action :authenticate_user!, only: [:edit] prepend_before_action :authenticate_user!, only: [:edit]
...@@ -28,6 +29,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -28,6 +29,7 @@ class Projects::BlobController < Projects::ApplicationController
before_action :editor_variables, except: [:show, :preview, :diff] before_action :editor_variables, except: [:show, :preview, :diff]
before_action :validate_diff_params, only: :diff before_action :validate_diff_params, only: :diff
before_action :set_last_commit_sha, only: [:edit, :update] before_action :set_last_commit_sha, only: [:edit, :update]
before_action :validate_codeowner_rules, only: [:create, :update]
before_action only: :show do before_action only: :show do
push_frontend_feature_flag(:code_navigation, @project) push_frontend_feature_flag(:code_navigation, @project)
...@@ -71,6 +73,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -71,6 +73,7 @@ class Projects::BlobController < Projects::ApplicationController
def update def update
@path = params[:file_path] if params[:file_path].present? @path = params[:file_path] if params[:file_path].present?
create_commit(Files::UpdateService, success_path: -> { after_edit_path }, create_commit(Files::UpdateService, success_path: -> { after_edit_path },
failure_view: :edit, failure_view: :edit,
failure_path: project_blob_path(@project, @id)) failure_path: project_blob_path(@project, @id))
...@@ -115,6 +118,19 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -115,6 +118,19 @@ class Projects::BlobController < Projects::ApplicationController
private 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 def blob
@blob ||= @repository.blob_at(@commit.id, @path) @blob ||= @repository.blob_at(@commit.id, @path)
......
...@@ -250,6 +250,56 @@ describe Projects::BlobController do ...@@ -250,6 +250,56 @@ describe Projects::BlobController do
end end
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 describe 'PUT update' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:default_params) do let(:default_params) do
...@@ -279,6 +329,12 @@ describe Projects::BlobController do ...@@ -279,6 +329,12 @@ describe Projects::BlobController do
expect(response).to redirect_to(blob_after_edit_path) expect(response).to redirect_to(blob_after_edit_path)
end 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 context '?from_merge_request_iid' do
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) } 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) } 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