Commit 67fa45e7 authored by Rémy Coutable's avatar Rémy Coutable

Allow issues/merge_requests as an issuable_type in Insights config

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent abda9897
---
title: Allow issues/merge_requests as an issuable_type in Insights configuration
merge_request: 26061
author:
type: added
...@@ -29,6 +29,10 @@ module Gitlab ...@@ -29,6 +29,10 @@ module Gitlab
@projects = projects @projects = projects
end end
def issuable_type
@issuable_type ||= query[:issuable_type]&.to_s&.singularize&.to_sym
end
# Returns an Active Record relation of issuables. # Returns an Active Record relation of issuables.
def find def find
return unless entity_args return unless entity_args
...@@ -59,8 +63,6 @@ module Gitlab ...@@ -59,8 +63,6 @@ module Gitlab
attr_reader :entity, :current_user, :query, :projects attr_reader :entity, :current_user, :query, :projects
def finder def finder
issuable_type = query[:issuable_type]&.to_sym
FINDERS[issuable_type] || FINDERS[issuable_type] ||
raise(InvalidIssuableTypeError, "Invalid `:issuable_type` option: `#{query[:issuable_type]}`. Allowed values are #{FINDERS.keys}!") raise(InvalidIssuableTypeError, "Invalid `:issuable_type` option: `#{query[:issuable_type]}`. Allowed values are #{FINDERS.keys}!")
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Insights::Finders::IssuableFinder do RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
using RSpec::Parameterized::TableSyntax
around do |example| around do |example|
Timecop.freeze(Time.utc(2019, 3, 5)) { example.run } Timecop.freeze(Time.utc(2019, 3, 5)) { example.run }
end end
...@@ -14,11 +16,34 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -14,11 +16,34 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
} }
end end
describe '#issuable_type' do
subject { described_class.new(build(:project), nil, query: { issuable_type: issuable_type_in_query }).issuable_type }
where(:issuable_type_in_query, :expected_issuable_type) do
'issue' | :issue
'issues' | :issue
'merge_request' | :merge_request
'merge_requests' | :merge_request
end
with_them do
it { is_expected.to eq(expected_issuable_type) }
end
end
describe '#find' do describe '#find' do
def find(entity, query:, projects: {}) def find(entity, query:, projects: {})
described_class.new(entity, nil, query: query, projects: projects).find described_class.new(entity, nil, query: query, projects: projects).find
end end
it 'calls issuable_type' do
finder = described_class.new(build(:project), nil, query: { issuable_type: 'issue' })
expect(finder).to receive(:issuable_type).and_call_original
finder.find
end
it 'raises an error for an invalid :issuable_type option' do it 'raises an error for an invalid :issuable_type option' do
expect do expect do
find(build(:project), query: { issuable_type: 'foo' }) find(build(:project), query: { issuable_type: 'foo' })
...@@ -54,11 +79,11 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -54,11 +79,11 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
let(:label_create) { create(label_type, label_entity_association_key => entity, name: 'Create') } let(:label_create) { create(label_type, label_entity_association_key => entity, name: 'Create') }
let(:label_quality) { create(label_type, label_entity_association_key => entity, name: 'Quality') } let(:label_quality) { create(label_type, label_entity_association_key => entity, name: 'Quality') }
let(:extra_issuable_attrs) { [{}, {}, {}, {}, {}, {}] } let(:extra_issuable_attrs) { [{}, {}, {}, {}, {}, {}] }
let!(:issuable0) { create(:"labeled_#{issuable_type}", :opened, created_at: Time.utc(2018, 1, 1), project_association_key => project, **extra_issuable_attrs[0]) } let!(:issuable0) { create(:"labeled_#{issuable_type.singularize}", :opened, created_at: Time.utc(2018, 1, 1), project_association_key => project, **extra_issuable_attrs[0]) }
let!(:issuable1) { create(:"labeled_#{issuable_type}", :opened, created_at: Time.utc(2018, 2, 1), labels: [label_bug, label_manage], project_association_key => project, **extra_issuable_attrs[1]) } let!(:issuable1) { create(:"labeled_#{issuable_type.singularize}", :opened, created_at: Time.utc(2018, 2, 1), labels: [label_bug, label_manage], project_association_key => project, **extra_issuable_attrs[1]) }
let!(:issuable2) { create(:"labeled_#{issuable_type}", :opened, created_at: Time.utc(2019, 2, 6), labels: [label_bug, label_plan], project_association_key => project, **extra_issuable_attrs[2]) } let!(:issuable2) { create(:"labeled_#{issuable_type.singularize}", :opened, created_at: Time.utc(2019, 2, 6), labels: [label_bug, label_plan], project_association_key => project, **extra_issuable_attrs[2]) }
let!(:issuable3) { create(:"labeled_#{issuable_type}", :opened, created_at: Time.utc(2019, 2, 20), labels: [label_bug, label_create], project_association_key => project, **extra_issuable_attrs[3]) } let!(:issuable3) { create(:"labeled_#{issuable_type.singularize}", :opened, created_at: Time.utc(2019, 2, 20), labels: [label_bug, label_create], project_association_key => project, **extra_issuable_attrs[3]) }
let!(:issuable4) { create(:"labeled_#{issuable_type}", :opened, created_at: Time.utc(2019, 3, 5), labels: [label_bug, label_quality], project_association_key => project, **extra_issuable_attrs[4]) } let!(:issuable4) { create(:"labeled_#{issuable_type.singularize}", :opened, created_at: Time.utc(2019, 3, 5), labels: [label_bug, label_quality], project_association_key => project, **extra_issuable_attrs[4]) }
let(:query) do let(:query) do
base_query.merge( base_query.merge(
issuable_type: issuable_type, issuable_type: issuable_type,
...@@ -71,7 +96,7 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do ...@@ -71,7 +96,7 @@ RSpec.describe Gitlab::Insights::Finders::IssuableFinder do
it 'avoids N + 1 queries' do it 'avoids N + 1 queries' do
control_queries = ActiveRecord::QueryRecorder.new { subject.map { |issuable| issuable.labels.map(&:title) } } control_queries = ActiveRecord::QueryRecorder.new { subject.map { |issuable| issuable.labels.map(&:title) } }
create(:"labeled_#{issuable_type}", :opened, created_at: Time.utc(2019, 3, 5), labels: [label_bug], project_association_key => project, **extra_issuable_attrs[5]) create(:"labeled_#{issuable_type.singularize}", :opened, created_at: Time.utc(2019, 3, 5), labels: [label_bug], project_association_key => project, **extra_issuable_attrs[5])
expect do expect do
find(entity, query: query).map { |issuable| issuable.labels.map(&:title) } find(entity, query: query).map { |issuable| issuable.labels.map(&:title) }
......
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