Commit ca4c8032 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'rename-sentry-module' into 'master'

Rename Sentry::Client to ErrorTracking::SentryClient

See merge request gitlab-org/gitlab!55506
parents df99d858 40401150
......@@ -382,7 +382,6 @@ Performance/DeleteSuffix:
- 'app/workers/concerns/application_worker.rb'
- 'ee/app/models/geo/upload_registry.rb'
- 'ee/app/workers/geo/file_download_dispatch_worker/attachment_job_finder.rb'
- 'lib/sentry/client/issue.rb'
# Offense count: 13
# Cop supports --auto-correct.
......@@ -1042,4 +1041,3 @@ Style/StringLiteralsInInterpolation:
# IgnoredMethods: respond_to, define_method
Style/SymbolProc:
Enabled: false
......@@ -77,7 +77,7 @@ module ErrorTracking
def sentry_client
strong_memoize(:sentry_client) do
Sentry::Client.new(api_url, token)
ErrorTracking::SentryClient.new(api_url, token)
end
end
......@@ -168,13 +168,13 @@ module ErrorTracking
def handle_exceptions
yield
rescue Sentry::Client::Error => e
rescue ErrorTracking::SentryClient::Error => e
{ error: e.message, error_type: SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE }
rescue Sentry::Client::MissingKeysError => e
rescue ErrorTracking::SentryClient::MissingKeysError => e
{ error: e.message, error_type: SENTRY_API_ERROR_TYPE_MISSING_KEYS }
rescue Sentry::Client::ResponseInvalidSizeError => e
rescue ErrorTracking::SentryClient::ResponseInvalidSizeError => e
{ error: e.message, error_type: SENTRY_API_ERROR_INVALID_SIZE }
rescue Sentry::Client::BadRequestError => e
rescue ErrorTracking::SentryClient::BadRequestError => e
{ error: e.message, error_type: SENTRY_API_ERROR_TYPE_BAD_REQUEST }
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e)
......
......@@ -26,7 +26,7 @@ class ErrorTrackingIssueLinkWorker # rubocop:disable Scalability/IdempotentWorke
logger.info("Linking Sentry issue #{sentry_issue_id} to GitLab issue #{issue.id}")
sentry_client.create_issue_link(integration_id, sentry_issue_id, issue)
rescue Sentry::Client::Error => e
rescue ErrorTracking::SentryClient::Error => e
logger.info("Failed to link Sentry issue #{sentry_issue_id} to GitLab issue #{issue.id} with error: #{e.message}")
end
end
......@@ -63,7 +63,7 @@ class ErrorTrackingIssueLinkWorker # rubocop:disable Scalability/IdempotentWorke
sentry_client
.repos(organization_slug)
.find { |repo| repo.project_id == issue.project_id && repo.status == 'active' }
rescue Sentry::Client::Error => e
rescue ErrorTracking::SentryClient::Error => e
logger.info("Unable to retrieve Sentry repo for organization #{organization_slug}, id #{sentry_issue_id}, with error: #{e.message}")
nil
......
# frozen_string_literal: true
module Sentry
class Client
include Sentry::Client::Event
include Sentry::Client::Projects
include Sentry::Client::Issue
include Sentry::Client::Repo
include Sentry::Client::IssueLink
module ErrorTracking
class SentryClient
include SentryClient::Event
include SentryClient::Projects
include SentryClient::Issue
include SentryClient::Repo
include SentryClient::IssueLink
Error = Class.new(StandardError)
MissingKeysError = Class.new(StandardError)
......@@ -21,7 +21,7 @@ module Sentry
private
def api_urls
@api_urls ||= Sentry::ApiUrls.new(@url)
@api_urls ||= SentryClient::ApiUrls.new(@url)
end
def handle_mapping_exceptions(&block)
......@@ -94,7 +94,7 @@ module Sentry
end
def raise_error(message)
raise Client::Error, message
raise SentryClient::Error, message
end
end
end
# frozen_string_literal: true
module ErrorTracking
class SentryClient
class ApiUrls
def initialize(url_base)
@uri = URI(url_base).freeze
end
def issues_url
with_path(File.join(@uri.path, '/issues/'))
end
def issue_url(issue_id)
with_path("/api/0/issues/#{escape(issue_id)}/")
end
def projects_url
with_path('/api/0/projects/')
end
def issue_latest_event_url(issue_id)
with_path("/api/0/issues/#{escape(issue_id)}/events/latest/")
end
private
def with_path(new_path)
new_uri = @uri.dup
# Sentry API returns 404 if there are extra slashes in the URL
new_uri.path = new_path.squeeze('/')
new_uri
end
def escape(param)
CGI.escape(param.to_s)
end
end
end
end
# frozen_string_literal: true
module Sentry
class Client
module ErrorTracking
class SentryClient
module Event
def issue_latest_event(issue_id:)
latest_event = http_get(api_urls.issue_latest_event_url(issue_id))[:body]
......
# frozen_string_literal: true
module Sentry
class Client
module ErrorTracking
class SentryClient
module Issue
BadRequestError = Class.new(StandardError)
ResponseInvalidSizeError = Class.new(StandardError)
......@@ -49,7 +49,7 @@ module Sentry
{
issues: response[:body],
pagination: Sentry::PaginationParser.parse(response[:headers])
pagination: SentryClient::PaginationParser.parse(response[:headers])
}
end
......@@ -113,7 +113,7 @@ module Sentry
uri = URI(url)
uri.path.squeeze!('/')
# Remove trailing slash
uri = uri.to_s.gsub(/\/\z/, '')
uri = uri.to_s.delete_suffix('/')
uri
end
......
# frozen_string_literal: true
module Sentry
class Client
module ErrorTracking
class SentryClient
module IssueLink
# Creates a link in Sentry corresponding to the provided
# Sentry issue and GitLab issue
......
# frozen_string_literal: true
module ErrorTracking
class SentryClient
module PaginationParser
PATTERN = /rel=\"(?<direction>\w+)\";\sresults=\"(?<results>\w+)\";\scursor=\"(?<cursor>.+)\"/.freeze
def self.parse(headers)
links = headers['link'].to_s.split(',')
links.map { |link| parse_link(link) }.compact.to_h
end
def self.parse_link(link)
match = link.match(PATTERN)
return unless match
return if match['results'] != "true"
[match['direction'], { 'cursor' => match['cursor'] }]
end
private_class_method :parse_link
end
end
end
# frozen_string_literal: true
module Sentry
class Client
module ErrorTracking
class SentryClient
module Projects
def projects
projects = get_projects
......
# frozen_string_literal: true
module Sentry
class Client
module ErrorTracking
class SentryClient
module Repo
def repos(organization_slug)
repos_url = repos_api_url(organization_slug)
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
class DetailedError
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
class Error
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
class ErrorCollection
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
class ErrorEvent
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
class Project
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
class Repo
......
# frozen_string_literal: true
# This should be in the ErrorTracking namespace. For more details, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
module Gitlab
module ErrorTracking
module StackTraceHighlightDecorator
......
# frozen_string_literal: true
module Sentry
class ApiUrls
def initialize(url_base)
@uri = URI(url_base).freeze
end
def issues_url
with_path(File.join(@uri.path, '/issues/'))
end
def issue_url(issue_id)
with_path("/api/0/issues/#{escape(issue_id)}/")
end
def projects_url
with_path('/api/0/projects/')
end
def issue_latest_event_url(issue_id)
with_path("/api/0/issues/#{escape(issue_id)}/events/latest/")
end
private
def with_path(new_path)
new_uri = @uri.dup
# Sentry API returns 404 if there are extra slashes in the URL
new_uri.path = new_path.squeeze('/')
new_uri
end
def escape(param)
CGI.escape(param.to_s)
end
end
end
# frozen_string_literal: true
module Sentry
module PaginationParser
PATTERN = /rel=\"(?<direction>\w+)\";\sresults=\"(?<results>\w+)\";\scursor=\"(?<cursor>.+)\"/.freeze
def self.parse(headers)
links = headers['link'].to_s.split(',')
links.map { |link| parse_link(link) }.compact.to_h
end
def self.parse_link(link)
match = link.match(PATTERN)
return unless match
return if match['results'] != "true"
[match['direction'], { 'cursor' => match['cursor'] }]
end
private_class_method :parse_link
end
end
......@@ -2,13 +2,13 @@
require 'spec_helper'
RSpec.describe Sentry::ApiUrls do
RSpec.describe ErrorTracking::SentryClient::ApiUrls do
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/' }
let(:token) { 'test-token' }
let(:issue_id) { '123456' }
let(:issue_id_with_reserved_chars) { '123$%' }
let(:escaped_issue_id) { '123%24%25' }
let(:api_urls) { Sentry::ApiUrls.new(sentry_url) }
let(:api_urls) { described_class.new(sentry_url) }
# Sentry API returns 404 if there are extra slashes in the URL!
shared_examples 'correct url with extra slashes' do
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Sentry::Client do
RSpec.describe ErrorTracking::SentryClient do
include SentryClientHelpers
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Sentry::Client::IssueLink do
RSpec.describe ErrorTracking::SentryClient::IssueLink do
include SentryClientHelpers
let_it_be(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
......
......@@ -2,12 +2,12 @@
require 'spec_helper'
RSpec.describe Sentry::Client::Issue do
RSpec.describe ErrorTracking::SentryClient::Issue do
include SentryClientHelpers
let(:token) { 'test-token' }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0' }
let(:client) { Sentry::Client.new(sentry_url, token) }
let(:client) { ErrorTracking::SentryClient.new(sentry_url, token) }
let(:issue_id) { 11 }
describe '#list_issues' do
......@@ -136,7 +136,7 @@ RSpec.describe Sentry::Client::Issue do
subject { client.list_issues(issue_status: issue_status, limit: limit, sort: 'fish') }
it 'throws an error' do
expect { subject }.to raise_error(Sentry::Client::BadRequestError, 'Invalid value for sort param')
expect { subject }.to raise_error(ErrorTracking::SentryClient::BadRequestError, 'Invalid value for sort param')
end
end
......@@ -164,7 +164,7 @@ RSpec.describe Sentry::Client::Issue do
end
it 'raises exception' do
expect { subject }.to raise_error(Sentry::Client::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
......@@ -173,7 +173,7 @@ RSpec.describe Sentry::Client::Issue do
deep_size = double('Gitlab::Utils::DeepSize', valid?: false)
allow(Gitlab::Utils::DeepSize).to receive(:new).with(sentry_api_response).and_return(deep_size)
expect { subject }.to raise_error(Sentry::Client::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
......
......@@ -2,7 +2,7 @@
require 'fast_spec_helper'
RSpec.describe Sentry::PaginationParser do
RSpec.describe ErrorTracking::SentryClient::PaginationParser do
describe '.parse' do
subject { described_class.parse(headers) }
......
......@@ -2,12 +2,12 @@
require 'spec_helper'
RSpec.describe Sentry::Client::Projects do
RSpec.describe ErrorTracking::SentryClient::Projects do
include SentryClientHelpers
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:client) { Sentry::Client.new(sentry_url, token) }
let(:client) { ErrorTracking::SentryClient.new(sentry_url, token) }
let(:projects_sample_response) do
Gitlab::Utils.deep_indifferent_access(
Gitlab::Json.parse(fixture_file('sentry/list_projects_sample_response.json'))
......@@ -44,7 +44,7 @@ RSpec.describe Sentry::Client::Projects do
end
it 'raises exception' do
expect { subject }.to raise_error(Sentry::Client::MissingKeysError, 'Sentry API response is missing keys. key not found: "slug"')
expect { subject }.to raise_error(ErrorTracking::SentryClient::MissingKeysError, 'Sentry API response is missing keys. key not found: "slug"')
end
end
......
......@@ -2,12 +2,12 @@
require 'spec_helper'
RSpec.describe Sentry::Client::Repo do
RSpec.describe ErrorTracking::SentryClient::Repo do
include SentryClientHelpers
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:client) { Sentry::Client.new(sentry_url, token) }
let(:client) { ErrorTracking::SentryClient.new(sentry_url, token) }
let(:repos_sample_response) { Gitlab::Json.parse(fixture_file('sentry/repos_sample_response.json')) }
describe '#repos' do
......
......@@ -2,11 +2,11 @@
require 'spec_helper'
RSpec.describe Sentry::Client do
RSpec.describe ErrorTracking::SentryClient do
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
subject { Sentry::Client.new(sentry_url, token) }
subject { described_class.new(sentry_url, token) }
it { is_expected.to respond_to :projects }
it { is_expected.to respond_to :list_issues }
......
......@@ -111,7 +111,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
describe '#sentry_client' do
it 'returns sentry client' do
expect(subject.sentry_client).to be_a(Sentry::Client)
expect(subject.sentry_client).to be_a(ErrorTracking::SentryClient)
end
end
......@@ -152,7 +152,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
end
end
context 'when sentry client raises Sentry::Client::Error' do
context 'when sentry client raises ErrorTracking::SentryClient::Error' do
let(:sentry_client) { spy(:sentry_client) }
before do
......@@ -160,7 +160,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts)
.and_raise(Sentry::Client::Error, 'error message')
.and_raise(ErrorTracking::SentryClient::Error, 'error message')
end
it 'returns error' do
......@@ -171,7 +171,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
end
end
context 'when sentry client raises Sentry::Client::MissingKeysError' do
context 'when sentry client raises ErrorTracking::SentryClient::MissingKeysError' do
let(:sentry_client) { spy(:sentry_client) }
before do
......@@ -179,7 +179,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts)
.and_raise(Sentry::Client::MissingKeysError, 'Sentry API response is missing keys. key not found: "id"')
.and_raise(ErrorTracking::SentryClient::MissingKeysError, 'Sentry API response is missing keys. key not found: "id"')
end
it 'returns error' do
......@@ -190,7 +190,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
end
end
context 'when sentry client raises Sentry::Client::ResponseInvalidSizeError' do
context 'when sentry client raises ErrorTracking::SentryClient::ResponseInvalidSizeError' do
let(:sentry_client) { spy(:sentry_client) }
let(:error_msg) {"Sentry API response is too big. Limit is #{Gitlab::Utils::DeepSize.human_default_max_size}."}
......@@ -199,7 +199,7 @@ RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts)
.and_raise(Sentry::Client::ResponseInvalidSizeError, error_msg)
.and_raise(ErrorTracking::SentryClient::ResponseInvalidSizeError, error_msg)
end
it 'returns error' do
......
......@@ -9,7 +9,7 @@ RSpec.shared_context 'sentry error tracking context feature' do
let_it_be(:issue_response) { Gitlab::Json.parse(issue_response_body) }
let_it_be(:event_response_body) { fixture_file('sentry/issue_latest_event_sample_response.json') }
let_it_be(:event_response) { Gitlab::Json.parse(event_response_body) }
let(:sentry_api_urls) { Sentry::ApiUrls.new(project_error_tracking_settings.api_url) }
let(:sentry_api_urls) { ErrorTracking::SentryClient::ApiUrls.new(project_error_tracking_settings.api_url) }
let(:issue_id) { issue_response['id'] }
let(:issue_seen) { 1.year.ago.utc }
let(:formatted_issue_seen) { issue_seen.strftime("%Y-%m-%d %-l:%M:%S%p %Z") }
......
......@@ -26,7 +26,7 @@ RSpec.shared_examples 'no Sentry redirects' do |http_method|
end
it 'does not follow redirects' do
expect { subject }.to raise_exception(Sentry::Client::Error, 'Sentry response status code: 302')
expect { subject }.to raise_exception(ErrorTracking::SentryClient::Error, 'Sentry response status code: 302')
expect(redirect_req_stub).to have_been_requested
expect(redirected_req_stub).not_to have_been_requested
end
......@@ -53,7 +53,7 @@ RSpec.shared_examples 'maps Sentry exceptions' do |http_method|
it do
expect { subject }
.to raise_exception(Sentry::Client::Error, message)
.to raise_exception(ErrorTracking::SentryClient::Error, message)
end
end
end
......
......@@ -20,7 +20,7 @@ RSpec.describe ErrorTrackingIssueLinkWorker do
describe '#perform' do
it 'creates a link between an issue and a Sentry issue in Sentry' do
expect_next_instance_of(Sentry::Client) do |client|
expect_next_instance_of(ErrorTracking::SentryClient) do |client|
expect(client).to receive(:repos).with('sentry-org').and_return([repo])
expect(client)
.to receive(:create_issue_link)
......@@ -33,8 +33,8 @@ RSpec.describe ErrorTrackingIssueLinkWorker do
shared_examples_for 'makes no external API requests' do
it 'takes no action' do
expect_any_instance_of(Sentry::Client).not_to receive(:repos)
expect_any_instance_of(Sentry::Client).not_to receive(:create_issue_link)
expect_any_instance_of(ErrorTracking::SentryClient).not_to receive(:repos)
expect_any_instance_of(ErrorTracking::SentryClient).not_to receive(:create_issue_link)
expect(subject).to be nil
end
......@@ -42,7 +42,7 @@ RSpec.describe ErrorTrackingIssueLinkWorker do
shared_examples_for 'attempts to create a link via plugin' do
it 'takes no action' do
expect_next_instance_of(Sentry::Client) do |client|
expect_next_instance_of(ErrorTracking::SentryClient) do |client|
expect(client).to receive(:repos).with('sentry-org').and_return([repo])
expect(client)
.to receive(:create_issue_link)
......@@ -98,8 +98,8 @@ RSpec.describe ErrorTrackingIssueLinkWorker do
context 'when Sentry repos request errors' do
it 'falls back to creating a link via plugin' do
expect_next_instance_of(Sentry::Client) do |client|
expect(client).to receive(:repos).with('sentry-org').and_raise(Sentry::Client::Error)
expect_next_instance_of(ErrorTracking::SentryClient) do |client|
expect(client).to receive(:repos).with('sentry-org').and_raise(ErrorTracking::SentryClient::Error)
expect(client)
.to receive(:create_issue_link)
.with(nil, sentry_issue.sentry_issue_identifier, issue)
......
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