Commit 79ccefcb authored by Andy Soiron's avatar Andy Soiron

Merge branch 'pl-error-tracking-limit' into 'master'

Resolve all RuboCop offenses for Sentry Client code and specs

See merge request gitlab-org/gitlab!84190
parents 27b28c1a 3872c7f1
...@@ -256,7 +256,6 @@ Layout/FirstHashElementIndentation: ...@@ -256,7 +256,6 @@ Layout/FirstHashElementIndentation:
- 'lib/banzai/filter/commit_trailers_filter.rb' - 'lib/banzai/filter/commit_trailers_filter.rb'
- 'lib/banzai/filter/playable_link_filter.rb' - 'lib/banzai/filter/playable_link_filter.rb'
- 'lib/banzai/reference_parser/user_parser.rb' - 'lib/banzai/reference_parser/user_parser.rb'
- 'lib/error_tracking/sentry_client/issue.rb'
- 'lib/gitlab/analytics/cycle_analytics/aggregated/records_fetcher.rb' - 'lib/gitlab/analytics/cycle_analytics/aggregated/records_fetcher.rb'
- 'lib/gitlab/analytics/cycle_analytics/records_fetcher.rb' - 'lib/gitlab/analytics/cycle_analytics/records_fetcher.rb'
- 'lib/gitlab/asciidoc.rb' - 'lib/gitlab/asciidoc.rb'
......
...@@ -437,7 +437,6 @@ RSpec/VerifiedDoubles: ...@@ -437,7 +437,6 @@ RSpec/VerifiedDoubles:
- spec/lib/error_tracking/collector/dsn_spec.rb - spec/lib/error_tracking/collector/dsn_spec.rb
- spec/lib/error_tracking/collector/sentry_auth_parser_spec.rb - spec/lib/error_tracking/collector/sentry_auth_parser_spec.rb
- spec/lib/error_tracking/collector/sentry_request_parser_spec.rb - spec/lib/error_tracking/collector/sentry_request_parser_spec.rb
- spec/lib/error_tracking/sentry_client/issue_spec.rb
- spec/lib/extracts_path_spec.rb - spec/lib/extracts_path_spec.rb
- spec/lib/feature_spec.rb - spec/lib/feature_spec.rb
- spec/lib/gitaly/server_spec.rb - spec/lib/gitaly/server_spec.rb
......
...@@ -59,10 +59,8 @@ module ErrorTracking ...@@ -59,10 +59,8 @@ module ErrorTracking
end end
end end
def http_request def http_request(&block)
response = handle_request_exceptions do response = handle_request_exceptions(&block)
yield
end
handle_response(response) handle_response(response)
end end
...@@ -86,9 +84,7 @@ module ErrorTracking ...@@ -86,9 +84,7 @@ module ErrorTracking
end end
def handle_response(response) def handle_response(response)
unless response.code.between?(200, 204) raise_error "Sentry response status code: #{response.code}" unless response.code.between?(200, 204)
raise_error "Sentry response status code: #{response.code}"
end
{ body: response.parsed_response, headers: response.headers } { body: response.parsed_response, headers: response.headers }
end end
......
...@@ -15,14 +15,14 @@ module ErrorTracking ...@@ -15,14 +15,14 @@ module ErrorTracking
stack_trace = parse_stack_trace(event) stack_trace = parse_stack_trace(event)
Gitlab::ErrorTracking::ErrorEvent.new( Gitlab::ErrorTracking::ErrorEvent.new(
issue_id: event.dig('groupID'), issue_id: event['groupID'],
date_received: event.dig('dateReceived'), date_received: event['dateReceived'],
stack_trace_entries: stack_trace stack_trace_entries: stack_trace
) )
end end
def parse_stack_trace(event) def parse_stack_trace(event)
exception_entry = event.dig('entries')&.detect { |h| h['type'] == 'exception' } exception_entry = event['entries']&.detect { |h| h['type'] == 'exception' }
return [] unless exception_entry return [] unless exception_entry
exception_values = exception_entry.dig('data', 'values') exception_values = exception_entry.dig('data', 'values')
......
...@@ -54,9 +54,7 @@ module ErrorTracking ...@@ -54,9 +54,7 @@ module ErrorTracking
end end
def list_issue_sentry_query(issue_status:, limit:, sort: nil, search_term: '', cursor: nil) def list_issue_sentry_query(issue_status:, limit:, sort: nil, search_term: '', cursor: nil)
unless SENTRY_API_SORT_VALUE_MAP.key?(sort) raise BadRequestError, 'Invalid value for sort param' unless SENTRY_API_SORT_VALUE_MAP.key?(sort)
raise BadRequestError, 'Invalid value for sort param'
end
{ {
query: "is:#{issue_status} #{search_term}".strip, query: "is:#{issue_status} #{search_term}".strip,
...@@ -69,7 +67,8 @@ module ErrorTracking ...@@ -69,7 +67,8 @@ module ErrorTracking
def validate_size(issues) def validate_size(issues)
return if Gitlab::Utils::DeepSize.new(issues).valid? return if Gitlab::Utils::DeepSize.new(issues).valid?
raise ResponseInvalidSizeError, "Sentry API response is too big. Limit is #{Gitlab::Utils::DeepSize.human_default_max_size}." message = "Sentry API response is too big. Limit is #{Gitlab::Utils::DeepSize.human_default_max_size}."
raise ResponseInvalidSizeError, message
end end
def get_issue(issue_id:) def get_issue(issue_id:)
...@@ -117,7 +116,7 @@ module ErrorTracking ...@@ -117,7 +116,7 @@ module ErrorTracking
end end
def map_to_errors(issues) def map_to_errors(issues)
issues.map(&method(:map_to_error)) issues.map { map_to_error(_1) }
end end
def map_to_error(issue) def map_to_error(issue)
...@@ -142,7 +141,7 @@ module ErrorTracking ...@@ -142,7 +141,7 @@ module ErrorTracking
end end
def map_to_detailed_error(issue) def map_to_detailed_error(issue)
Gitlab::ErrorTracking::DetailedError.new({ Gitlab::ErrorTracking::DetailedError.new(
id: issue.fetch('id'), id: issue.fetch('id'),
first_seen: issue.fetch('firstSeen', nil), first_seen: issue.fetch('firstSeen', nil),
last_seen: issue.fetch('lastSeen', nil), last_seen: issue.fetch('lastSeen', nil),
...@@ -169,7 +168,7 @@ module ErrorTracking ...@@ -169,7 +168,7 @@ module ErrorTracking
last_release_short_version: issue.dig('lastRelease', 'shortVersion'), last_release_short_version: issue.dig('lastRelease', 'shortVersion'),
last_release_version: issue.dig('lastRelease', 'version'), last_release_version: issue.dig('lastRelease', 'version'),
integrated: false integrated: false
}) )
end end
def extract_tags(issue) def extract_tags(issue)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module ErrorTracking module ErrorTracking
class SentryClient class SentryClient
module PaginationParser module PaginationParser
PATTERN = /rel=\"(?<direction>\w+)\";\sresults=\"(?<results>\w+)\";\scursor=\"(?<cursor>.+)\"/.freeze PATTERN = /rel="(?<direction>\w+)";\sresults="(?<results>\w+)";\scursor="(?<cursor>.+)"/.freeze
def self.parse(headers) def self.parse(headers)
links = headers['link'].to_s.split(',') links = headers['link'].to_s.split(',')
......
...@@ -18,7 +18,7 @@ module ErrorTracking ...@@ -18,7 +18,7 @@ module ErrorTracking
end end
def map_to_projects(projects) def map_to_projects(projects)
projects.map(&method(:map_to_project)) projects.map { map_to_project(_1) }
end end
def map_to_project(project) def map_to_project(project)
...@@ -28,7 +28,7 @@ module ErrorTracking ...@@ -28,7 +28,7 @@ module ErrorTracking
id: project.fetch('id', nil), id: project.fetch('id', nil),
name: project.fetch('name'), name: project.fetch('name'),
slug: project.fetch('slug'), slug: project.fetch('slug'),
status: project.dig('status'), status: project['status'],
organization_name: organization.fetch('name'), organization_name: organization.fetch('name'),
organization_id: organization.fetch('id', nil), organization_id: organization.fetch('id', nil),
organization_slug: organization.fetch('slug') organization_slug: organization.fetch('slug')
......
...@@ -23,7 +23,7 @@ module ErrorTracking ...@@ -23,7 +23,7 @@ module ErrorTracking
end end
def map_to_repos(repos) def map_to_repos(repos)
repos.map(&method(:map_to_repo)) repos.map { map_to_repo(_1) }
end end
def map_to_repo(repo) def map_to_repo(repo)
......
...@@ -13,7 +13,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -13,7 +13,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
describe '#list_issues' do describe '#list_issues' do
shared_examples 'issues have correct return type' do |klass| shared_examples 'issues have correct return type' do |klass|
it "returns objects of type #{klass}" do it "returns objects of type #{klass}" do
expect(subject[:issues]).to all( be_a(klass) ) expect(subject[:issues]).to all(be_a(klass))
end end
end end
...@@ -41,10 +41,18 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -41,10 +41,18 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
let(:cursor) { nil } let(:cursor) { nil }
let(:sort) { 'last_seen' } let(:sort) { 'last_seen' }
let(:sentry_api_response) { issues_sample_response } let(:sentry_api_response) { issues_sample_response }
let(:sentry_request_url) { sentry_url + '/issues/?limit=20&query=is:unresolved' } let(:sentry_request_url) { "#{sentry_url}/issues/?limit=20&query=is:unresolved" }
let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) } let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }
subject { client.list_issues(issue_status: issue_status, limit: limit, search_term: search_term, sort: sort, cursor: cursor) } subject do
client.list_issues(
issue_status: issue_status,
limit: limit,
search_term: search_term,
sort: sort,
cursor: cursor
)
end
it_behaves_like 'calls sentry api' it_behaves_like 'calls sentry api'
...@@ -52,7 +60,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -52,7 +60,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
it_behaves_like 'issues have correct length', 3 it_behaves_like 'issues have correct length', 3
shared_examples 'has correct external_url' do shared_examples 'has correct external_url' do
context 'external_url' do describe '#external_url' do
it 'is constructed correctly' do it 'is constructed correctly' do
expect(subject[:issues][0].external_url).to eq('https://sentrytest.gitlab.com/sentry-org/sentry-project/issues/11') expect(subject[:issues][0].external_url).to eq('https://sentrytest.gitlab.com/sentry-org/sentry-project/issues/11')
end end
...@@ -62,7 +70,8 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -62,7 +70,8 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
context 'when response has a pagination info' do context 'when response has a pagination info' do
let(:headers) do let(:headers) do
{ {
link: '<https://sentrytest.gitlab.com>; rel="previous"; results="true"; cursor="1573556671000:0:1", <https://sentrytest.gitlab.com>; rel="next"; results="true"; cursor="1572959139000:0:0"' link: '<https://sentrytest.gitlab.com>; rel="previous"; results="true"; cursor="1573556671000:0:1",' \
'<https://sentrytest.gitlab.com>; rel="next"; results="true"; cursor="1572959139000:0:0"'
} }
end end
...@@ -76,7 +85,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -76,7 +85,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
end end
end end
context 'error object created from sentry response' do context 'when error object created from sentry response' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:error_object, :sentry_response) do where(:error_object, :sentry_response) do
...@@ -104,13 +113,13 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -104,13 +113,13 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
it_behaves_like 'has correct external_url' it_behaves_like 'has correct external_url'
end end
context 'redirects' do context 'with redirects' do
let(:sentry_api_url) { sentry_url + '/issues/?limit=20&query=is:unresolved' } let(:sentry_api_url) { "#{sentry_url}/issues/?limit=20&query=is:unresolved" }
it_behaves_like 'no Sentry redirects' it_behaves_like 'no Sentry redirects'
end end
context 'requests with sort parameter in sentry api' do context 'with sort parameter in sentry api' do
let(:sentry_request_url) do let(:sentry_request_url) do
'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/' \ 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/' \
'issues/?limit=20&query=is:unresolved&sort=freq' 'issues/?limit=20&query=is:unresolved&sort=freq'
...@@ -140,7 +149,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -140,7 +149,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
end end
end end
context 'Older sentry versions where keys are not present' do context 'with older sentry versions where keys are not present' do
let(:sentry_api_response) do let(:sentry_api_response) do
issues_sample_response[0...1].map do |issue| issues_sample_response[0...1].map do |issue|
issue[:project].delete(:id) issue[:project].delete(:id)
...@@ -156,7 +165,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -156,7 +165,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
it_behaves_like 'has correct external_url' it_behaves_like 'has correct external_url'
end end
context 'essential keys missing in API response' do context 'when essential keys are missing in API response' do
let(:sentry_api_response) do let(:sentry_api_response) do
issues_sample_response[0...1].map do |issue| issues_sample_response[0...1].map do |issue|
issue.except(:id) issue.except(:id)
...@@ -164,16 +173,18 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -164,16 +173,18 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
end end
it 'raises exception' do it 'raises exception' do
expect { subject }.to raise_error(ErrorTracking::SentryClient::MissingKeysError, 'Sentry API response is missing keys. key not found: "id"') expect { subject }.to raise_error(ErrorTracking::SentryClient::MissingKeysError,
'Sentry API response is missing keys. key not found: "id"')
end end
end end
context 'sentry api response too large' do context 'when sentry api response is too large' do
it 'raises exception' do it 'raises exception' do
deep_size = double('Gitlab::Utils::DeepSize', valid?: false) deep_size = instance_double(Gitlab::Utils::DeepSize, valid?: false)
allow(Gitlab::Utils::DeepSize).to receive(:new).with(sentry_api_response).and_return(deep_size) allow(Gitlab::Utils::DeepSize).to receive(:new).with(sentry_api_response).and_return(deep_size)
expect { subject }.to raise_error(ErrorTracking::SentryClient::ResponseInvalidSizeError, 'Sentry API response is too big. Limit is 1 MB.') expect { subject }.to raise_error(ErrorTracking::SentryClient::ResponseInvalidSizeError,
'Sentry API response is too big. Limit is 1 MB.')
end end
end end
...@@ -212,7 +223,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -212,7 +223,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
subject { client.issue_details(issue_id: issue_id) } subject { client.issue_details(issue_id: issue_id) }
context 'error object created from sentry response' do context 'with error object created from sentry response' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:error_object, :sentry_response) do where(:error_object, :sentry_response) do
...@@ -298,17 +309,16 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -298,17 +309,16 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
describe '#update_issue' do describe '#update_issue' do
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0' } let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0' }
let(:sentry_request_url) { "#{sentry_url}/issues/#{issue_id}/" } let(:sentry_request_url) { "#{sentry_url}/issues/#{issue_id}/" }
before do
stub_sentry_request(sentry_request_url, :put)
end
let(:params) do let(:params) do
{ {
status: 'resolved' status: 'resolved'
} }
end end
before do
stub_sentry_request(sentry_request_url, :put)
end
subject { client.update_issue(issue_id: issue_id, params: params) } subject { client.update_issue(issue_id: issue_id, params: params) }
it_behaves_like 'calls sentry api' do it_behaves_like 'calls sentry api' do
...@@ -319,7 +329,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do ...@@ -319,7 +329,7 @@ RSpec.describe ErrorTracking::SentryClient::Issue do
expect(subject).to be_truthy expect(subject).to be_truthy
end end
context 'error encountered' do context 'when error is encountered' do
let(:error) { StandardError.new('error') } let(:error) { StandardError.new('error') }
before do before do
......
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