Commit 99860899 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '207927-validate-actor-user-against-codeowners' into 'master'

Resolve "Validate actor/user against CODEOWNERS"

Closes #207927

See merge request gitlab-org/gitlab!26027
parents 72737c03 149d4428
---
title: Validate actor against CODEOWNERS entries
merge_request:
author:
type: fixed
...@@ -23,7 +23,10 @@ module EE ...@@ -23,7 +23,10 @@ module EE
lambda do |paths| lambda do |paths|
loader = ::Gitlab::CodeOwners::Loader.new(project, branch_name, paths) loader = ::Gitlab::CodeOwners::Loader.new(project, branch_name, paths)
assemble_error_msg_for_codeowner_matches(loader) if loader.entries.any? return if loader.entries.blank?
return if loader.members.include?(change_access.user_access.user)
assemble_error_msg_for_codeowner_matches(loader)
end end
end end
......
...@@ -8,8 +8,12 @@ module Gitlab ...@@ -8,8 +8,12 @@ module Gitlab
@extractor = extractor @extractor = extractor
end end
# Generate a list of all project members who are mentioned in the
# CODEOWNERS file, and load them to the matching entry.
#
def load_to(entries) def load_to(entries)
members = project.members_among(users) members = project.members_among(users)
entries.each do |entry| entries.each do |entry|
entry.add_matching_users_from(members) entry.add_matching_users_from(members)
end end
......
...@@ -36,21 +36,35 @@ describe Gitlab::Checks::DiffCheck do ...@@ -36,21 +36,35 @@ describe Gitlab::Checks::DiffCheck do
end end
before do before do
project.add_developer(code_owner)
allow(project.repository).to receive(:code_owners_blob) allow(project.repository).to receive(:code_owners_blob)
.with(ref: codeowner_lookup_ref) .with(ref: codeowner_lookup_ref)
.and_return(codeowner_blob) .and_return(codeowner_blob)
end end
context "the MR contains a matching file path" do context "the MR contains a matching file path" do
it "return an error message" do let(:validation_result) do
expect(subject.send(:validate_code_owners) subject.send(:validate_code_owners).call(["docs/CODEOWNERS", "README"])
.call(["docs/CODEOWNERS", "README"])).not_to be_nil end
context "and the user is not listed as a code owner" do
it "returns an error message" do
expect(validation_result).to include("Pushes to protected branches")
end
end
context "and the user is listed as a code owner" do
# `user` is set as the owner of the incoming change by the shared
# context found in 'push rules checks context'
let(:codeowner_content) { "* @#{user.username}" }
it "returns nil" do
expect(validation_result).to be_nil
end
end end
end end
context "the MR doesn't contain a matching file path" do context "the MR doesn't contain a matching file path" do
it "doesn't raise an exception" do it "returns nil" do
expect(subject.send(:validate_code_owners) expect(subject.send(:validate_code_owners)
.call(["docs/SAFE_FILE_NAME", "README"])).to be_nil .call(["docs/SAFE_FILE_NAME", "README"])).to be_nil
end end
......
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