Commit 5e2b6890 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '33337-fix-insights-only-projects-for-project' into 'master'

Make sure project insights stick on its own

Closes #33337

See merge request gitlab-org/gitlab!18082
parents 31720873 a0d9ea8a
---
title: Make sure project insights stick on its own
merge_request: 18082
author:
type: fixed
......@@ -31,6 +31,8 @@ module Gitlab
# Returns an Active Record relation of issuables.
def find
return unless entity_args
relation = finder
.new(current_user, finder_args)
.execute
......@@ -69,23 +71,24 @@ module Gitlab
state: query[:issuable_state] || 'opened',
label_name: query[:filter_labels],
sort: 'created_asc',
created_after: created_after_argument,
projects: finder_projects
}.merge(entity_arg)
created_after: created_after_argument
}.merge(entity_args)
end
def entity_arg
case entity
when ::Project
if finder_projects
{} # We just rely on projects argument
def entity_args
strong_memoize(:entity_args) do
case entity
when ::Project
if finder_projects
{ project_id: entity.id } if finder_projects.exists?(entity.id) # rubocop: disable CodeReuse/ActiveRecord
else
{ project_id: entity.id }
end
when ::Namespace
{ group_id: entity.id, projects: finder_projects }
else
{ project_id: entity.id }
raise InvalidEntityError, "Entity class `#{entity.class}` is not supported. Supported classes are Project and Namespace!"
end
when ::Namespace
{ group_id: entity.id }
else
raise InvalidEntityError, "Entity class `#{entity.class}` is not supported. Supported classes are Project and Namespace!"
end
end
......
......@@ -170,9 +170,14 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
context 'when `projects.only` are specified by two ids' do
let(:projects) { { only: [project.id, other_project.id] } }
it 'returns issuables for all projects' do
expect(subject.to_a)
.to eq([issuable0, issuable1, issuable2, issuable3, issuable4])
it 'returns issuables for all valid projects' do
expected = [issuable0, issuable1, issuable2, issuable3, issuable4]
if entity.id == project.id
expected.shift(2) # Those are from other_project
end
expect(subject.to_a).to eq(expected)
end
end
......@@ -185,10 +190,10 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
end
context 'when `projects.only` are specified by bad id and good id' do
let(:projects) { { only: [0, other_project.id] } }
let(:projects) { { only: [0, project.id] } }
it 'returns issuables for good project' do
expect(subject.to_a).to eq([issuable0, issuable1])
expect(subject.to_a).to eq([issuable2, issuable3, issuable4])
end
end
......@@ -203,9 +208,22 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
context 'when `projects.only` are specified by project full path and id' do
let(:projects) { { only: [project.id, other_project.full_path] } }
it 'returns issuables for all projects' do
expect(subject.to_a)
.to eq([issuable0, issuable1, issuable2, issuable3, issuable4])
it 'returns issuables for all valid projects' do
expected = [issuable0, issuable1, issuable2, issuable3, issuable4]
if entity.id == project.id
expected.shift(2) # Those are from other_project
end
expect(subject.to_a).to eq(expected)
end
end
context 'when `projects.only` are specified by duplicated projects' do
let(:projects) { { only: [project.id, project.full_path] } }
it 'returns issuables for that project without duplicated issuables' do
expect(subject.to_a).to eq([issuable2, issuable3, issuable4])
end
end
......@@ -216,6 +234,14 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
expect(subject.to_a).to be_empty
end
end
context 'when `projects.only` are specified by unrelated project' do
let(:projects) { { only: [create(:project, :public).id] } }
it 'returns nothing' do
expect(subject.to_a).to be_empty
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