Commit 4f3da9a5 authored by Robert Speicher's avatar Robert Speicher

Merge branch '219340-add-protected-branch-check-to-codeowners-validator' into 'master'

Confirm protected branch before running checks

Closes #219340

See merge request gitlab-org/gitlab!33310
parents d4524750 31c852ea
...@@ -10,6 +10,7 @@ module Gitlab ...@@ -10,6 +10,7 @@ module Gitlab
end end
def execute def execute
return unless @project.branch_requires_code_owner_approval?(@branch_name)
return if loader.entries.blank? return if loader.entries.blank?
assemble_error_msg_for_codeowner_matches assemble_error_msg_for_codeowner_matches
......
...@@ -74,6 +74,11 @@ describe Gitlab::Checks::DiffCheck do ...@@ -74,6 +74,11 @@ describe Gitlab::Checks::DiffCheck do
subject.send(:validate_code_owners).call(["docs/CODEOWNERS", "README"]) subject.send(:validate_code_owners).call(["docs/CODEOWNERS", "README"])
end end
before do
expect(project).to receive(:branch_requires_code_owner_approval?)
.at_least(:once).and_return(true)
end
it_behaves_like "returns codeowners validation message" it_behaves_like "returns codeowners validation message"
end end
......
...@@ -37,18 +37,46 @@ describe Gitlab::CodeOwners::Validator do ...@@ -37,18 +37,46 @@ describe Gitlab::CodeOwners::Validator do
allow(project.repository).to receive(:code_owners_blob).and_return(codeowner_blob) allow(project.repository).to receive(:code_owners_blob).and_return(codeowner_blob)
end end
shared_examples_for "finds no errors" do
it "returns nil" do
expect(subject.execute).to be_nil
end
end
describe "#execute" do describe "#execute" do
context "when paths match entries in the codeowners file" do context "when the branch does not require code owner approval" do
it "returns an error message" do before do
expect(subject.execute).to include("Pushes to protected branches") expect(project).to receive(:branch_requires_code_owner_approval?)
.and_return(false)
end
context "when paths match entries in the codeowners file" do
it_behaves_like "finds no errors"
end
context "when paths do not match entries in the codeowners file" do
let(:paths) { "not/a/matching/path" }
it_behaves_like "finds no errors"
end end
end end
context "when paths do not match entries in the codeowners file" do context "when the branch requires code owner approval" do
let(:paths) { "not/a/matching/path" } before do
expect(project).to receive(:branch_requires_code_owner_approval?)
.and_return(true)
end
context "when paths match entries in the codeowners file" do
it "returns an error message" do
expect(subject.execute).to include("Pushes to protected branches")
end
end
context "when paths do not match entries in the codeowners file" do
let(:paths) { "not/a/matching/path" }
it "returns nil" do it_behaves_like "finds no errors"
expect(subject.execute).to be_nil
end end
end end
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