Commit 458e4def authored by Mark Chao's avatar Mark Chao

Allow fetching owners for MR

parent e3890a26
......@@ -50,5 +50,11 @@ module EE
def participant_approvers
approval_needed? ? approvers_left : []
end
def code_owners
strong_memoize(:code_owners) do
::Gitlab::CodeOwners.for_merge_request(self).freeze
end
end
end
end
......@@ -12,5 +12,20 @@ module Gitlab
User.none
end
end
# @param merge_request [MergeRequest]
# @param merge_request_diff [MergeRequestDiff]
# Find code owners at a particular MergeRequestDiff.
# Assumed to be the most recent one if not provided.
def self.for_merge_request(merge_request, merge_request_diff: nil)
return [] if merge_request.source_project.nil? || merge_request.source_branch.nil?
return [] unless merge_request.target_project.feature_available?(:code_owners)
Loader.new(
merge_request.target_project,
merge_request.target_branch,
merge_request.modified_paths(past_merge_request_diff: merge_request_diff)
).members.where_not_in(merge_request.author).to_a
end
end
end
......@@ -7,18 +7,22 @@ describe Gitlab::CodeOwners do
let!(:code_owner) { create(:user, username: 'owner-1') }
let(:project) { create(:project, :repository) }
let(:blob) do
project.repository.blob_at(TestEnv::BRANCH_SHA['with-codeowners'], 'docs/CODEOWNERS')
end
let(:codeowner_content) { "docs/CODEOWNERS @owner-1" }
let(:codeowner_blob) { fake_blob(path: 'CODEOWNERS', data: codeowner_content) }
let(:codeowner_blob_ref) { fake_blob(path: 'CODEOWNERS', data: codeowner_content) }
before do
project.add_developer(code_owner)
allow(project.repository).to receive(:code_owners_blob).and_return(codeowner_blob)
allow(project.repository).to receive(:code_owners_blob)
.with(ref: codeowner_lookup_ref)
.and_return(codeowner_blob)
end
describe '.for_blob' do
let(:branch) { TestEnv::BRANCH_SHA['with-codeowners'] }
let(:blob) { project.repository.blob_at(branch, 'docs/CODEOWNERS') }
let(:codeowner_lookup_ref) { branch }
context 'when the feature is available' do
before do
stub_licensed_features(code_owners: true)
......@@ -39,4 +43,59 @@ describe Gitlab::CodeOwners do
end
end
end
describe '.for_merge_request' do
let(:codeowner_lookup_ref) { merge_request.target_branch }
let(:merge_request) do
build(
:merge_request,
source_project: project,
source_branch: 'feature',
target_project: project,
target_branch: 'with-codeowners'
)
end
context 'when the feature is available' do
before do
stub_licensed_features(code_owners: true)
end
it 'returns owners for merge request' do
expect(merge_request).to receive(:modified_paths).with(past_merge_request_diff: nil).and_return(['docs/CODEOWNERS'])
expect(described_class.for_merge_request(merge_request)).to eq([code_owner])
end
context 'when owner is merge request author' do
let(:merge_request) { build(:merge_request, target_project: project, author: code_owner) }
it 'excludes author' do
expect(merge_request).to receive(:modified_paths).with(past_merge_request_diff: nil).and_return(['docs/CODEOWNERS'])
expect(described_class.for_merge_request(merge_request)).to eq([])
end
end
context 'when merge_request_diff is specified' do
let(:merge_request_diff) { double(:merge_request_diff) }
it 'returns owners at the specified ref' do
expect(merge_request).to receive(:modified_paths).with(past_merge_request_diff: merge_request_diff).and_return(['docs/CODEOWNERS'])
expect(described_class.for_merge_request(merge_request, merge_request_diff: merge_request_diff)).to eq([code_owner])
end
end
end
context 'when the feature is not available' do
before do
stub_licensed_features(code_owners: false)
end
it 'returns no users' do
expect(described_class.for_merge_request(merge_request)).to eq([])
end
end
end
end
......@@ -14,6 +14,18 @@ describe MergeRequest do
it { is_expected.to have_many(:approved_by_users) }
end
describe '#code_owners' do
subject(:merge_request) { build(:merge_request) }
let(:owners) { [double(:owner)] }
it 'returns code owners, frozen' do
allow(::Gitlab::CodeOwners).to receive(:for_merge_request).with(subject).and_return(owners)
expect(subject.code_owners).to eq(owners)
expect(subject.code_owners).to be_frozen
end
end
describe '#approvals_before_merge' do
where(:license_value, :db_value, :expected) do
true | 5 | 5
......
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