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 ...@@ -31,6 +31,8 @@ module Gitlab
# Returns an Active Record relation of issuables. # Returns an Active Record relation of issuables.
def find def find
return unless entity_args
relation = finder relation = finder
.new(current_user, finder_args) .new(current_user, finder_args)
.execute .execute
...@@ -69,25 +71,26 @@ module Gitlab ...@@ -69,25 +71,26 @@ module Gitlab
state: query[:issuable_state] || 'opened', state: query[:issuable_state] || 'opened',
label_name: query[:filter_labels], label_name: query[:filter_labels],
sort: 'created_asc', sort: 'created_asc',
created_after: created_after_argument, created_after: created_after_argument
projects: finder_projects }.merge(entity_args)
}.merge(entity_arg)
end end
def entity_arg def entity_args
strong_memoize(:entity_args) do
case entity case entity
when ::Project when ::Project
if finder_projects if finder_projects
{} # We just rely on projects argument { project_id: entity.id } if finder_projects.exists?(entity.id) # rubocop: disable CodeReuse/ActiveRecord
else else
{ project_id: entity.id } { project_id: entity.id }
end end
when ::Namespace when ::Namespace
{ group_id: entity.id } { group_id: entity.id, projects: finder_projects }
else else
raise InvalidEntityError, "Entity class `#{entity.class}` is not supported. Supported classes are Project and Namespace!" raise InvalidEntityError, "Entity class `#{entity.class}` is not supported. Supported classes are Project and Namespace!"
end end
end end
end
def created_after_argument def created_after_argument
return unless query.key?(:group_by) return unless query.key?(:group_by)
......
...@@ -170,9 +170,14 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -170,9 +170,14 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
context 'when `projects.only` are specified by two ids' do context 'when `projects.only` are specified by two ids' do
let(:projects) { { only: [project.id, other_project.id] } } let(:projects) { { only: [project.id, other_project.id] } }
it 'returns issuables for all projects' do it 'returns issuables for all valid projects' do
expect(subject.to_a) expected = [issuable0, issuable1, issuable2, issuable3, issuable4]
.to eq([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
end end
...@@ -185,10 +190,10 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -185,10 +190,10 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
end end
context 'when `projects.only` are specified by bad id and good id' do 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 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
end end
...@@ -203,9 +208,22 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -203,9 +208,22 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
context 'when `projects.only` are specified by project full path and id' do context 'when `projects.only` are specified by project full path and id' do
let(:projects) { { only: [project.id, other_project.full_path] } } let(:projects) { { only: [project.id, other_project.full_path] } }
it 'returns issuables for all projects' do it 'returns issuables for all valid projects' do
expect(subject.to_a) expected = [issuable0, issuable1, issuable2, issuable3, issuable4]
.to eq([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
end end
...@@ -216,6 +234,14 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -216,6 +234,14 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
expect(subject.to_a).to be_empty expect(subject.to_a).to be_empty
end end
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
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