Skip to content
Snippets Groups Projects
Commit 37d9bb8f authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak :zero:
Browse files

Merge branch '347555-type-check-standard-context' into 'master'

Resolve "Type check standard context"

See merge request !88540
parents ff2e880b 938d521e
No related branches found
No related tags found
1 merge request!88540Resolve "Type check standard context"
Pipeline #557574451 passed
---
name: standard_context_type_check
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/88540
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/364265
milestone: '15.1'
type: development
group: group::product intelligence
default_enabled: false
......@@ -7,6 +7,12 @@ class StandardContext
GITLAB_RAILS_SOURCE = 'gitlab-rails'
def initialize(namespace: nil, project: nil, user: nil, **extra)
if Feature.enabled?(:standard_context_type_check)
check_argument_type(:namespace, namespace, [Namespace])
check_argument_type(:project, project, [Project, Integer])
check_argument_type(:user, user, [User, DeployToken])
end
@namespace = namespace
@plan = namespace&.actual_plan_name
@project = project
......@@ -54,6 +60,14 @@ def to_h
def project_id
project.is_a?(Integer) ? project : project&.id
end
def check_argument_type(argument_name, argument_value, allowed_classes)
return if argument_value.nil? || allowed_classes.any? { |allowed_class| argument_value.is_a?(allowed_class) }
exception = "Invalid argument type passed for #{argument_name}." \
" Should be one of #{allowed_classes.map(&:to_s)}"
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(ArgumentError.new(exception))
end
end
end
end
......@@ -92,6 +92,34 @@
end
end
context 'with incorrect argument type' do
context 'when standard_context_type_check FF is disabled' do
before do
stub_feature_flags(standard_context_type_check: false)
end
subject { described_class.new(project: create(:group)) }
it 'does not call `track_and_raise_for_dev_exception`' do
expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)
snowplow_context
end
end
context 'when standard_context_type_check FF is enabled' do
before do
stub_feature_flags(standard_context_type_check: true)
end
subject { described_class.new(project: create(:group)) }
it 'does call `track_and_raise_for_dev_exception`' do
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
snowplow_context
end
end
end
it 'contains user id' do
expect(snowplow_context.to_json[:data].keys).to include(:user_id)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment