2019-12-18 12:07:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-04 15:11:19 +00:00
|
|
|
module ErrorTracking
|
|
|
|
class SentryClient
|
2019-12-18 12:07:48 +00:00
|
|
|
module Issue
|
2020-01-09 09:07:51 +00:00
|
|
|
BadRequestError = Class.new(StandardError)
|
2020-03-31 18:07:42 +00:00
|
|
|
ResponseInvalidSizeError = Class.new(StandardError)
|
2020-01-09 09:07:51 +00:00
|
|
|
|
|
|
|
SENTRY_API_SORT_VALUE_MAP = {
|
|
|
|
# <accepted_by_client> => <accepted_by_sentry_api>
|
|
|
|
'frequency' => 'freq',
|
|
|
|
'first_seen' => 'new',
|
|
|
|
'last_seen' => nil
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def list_issues(**keyword_args)
|
2020-10-04 09:09:22 +00:00
|
|
|
response = get_issues(**keyword_args)
|
2020-01-09 09:07:51 +00:00
|
|
|
|
|
|
|
issues = response[:issues]
|
|
|
|
pagination = response[:pagination]
|
|
|
|
|
|
|
|
validate_size(issues)
|
|
|
|
|
|
|
|
handle_mapping_exceptions do
|
|
|
|
{
|
|
|
|
issues: map_to_errors(issues),
|
|
|
|
pagination: pagination
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-18 12:07:48 +00:00
|
|
|
def issue_details(issue_id:)
|
|
|
|
issue = get_issue(issue_id: issue_id)
|
|
|
|
|
|
|
|
map_to_detailed_error(issue)
|
|
|
|
end
|
|
|
|
|
2020-01-09 21:07:48 +00:00
|
|
|
def update_issue(issue_id:, params:)
|
2020-01-14 15:07:55 +00:00
|
|
|
http_put(api_urls.issue_url(issue_id), params)[:body]
|
2020-01-09 21:07:48 +00:00
|
|
|
end
|
|
|
|
|
2019-12-18 12:07:48 +00:00
|
|
|
private
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
def get_issues(**keyword_args)
|
|
|
|
response = http_get(
|
2020-01-14 15:07:55 +00:00
|
|
|
api_urls.issues_url,
|
2020-10-04 09:09:22 +00:00
|
|
|
query: list_issue_sentry_query(**keyword_args)
|
2020-01-09 09:07:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
{
|
|
|
|
issues: response[:body],
|
2021-03-04 15:11:19 +00:00
|
|
|
pagination: SentryClient::PaginationParser.parse(response[:headers])
|
2020-01-09 09:07:51 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
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'
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
query: "is:#{issue_status} #{search_term}".strip,
|
|
|
|
limit: limit,
|
|
|
|
sort: SENTRY_API_SORT_VALUE_MAP[sort],
|
|
|
|
cursor: cursor
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_size(issues)
|
|
|
|
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}."
|
|
|
|
end
|
|
|
|
|
2019-12-18 12:07:48 +00:00
|
|
|
def get_issue(issue_id:)
|
2020-01-14 15:07:55 +00:00
|
|
|
http_get(api_urls.issue_url(issue_id))[:body]
|
2019-12-18 12:07:48 +00:00
|
|
|
end
|
|
|
|
|
2020-03-10 12:08:16 +00:00
|
|
|
def parse_gitlab_issue(issue)
|
|
|
|
parse_issue_annotations(issue) || parse_plugin_issue(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_issue_annotations(issue)
|
|
|
|
issue
|
|
|
|
.fetch('annotations', [])
|
|
|
|
.reject(&:blank?)
|
|
|
|
.map { |annotation| Nokogiri.make(annotation) }
|
|
|
|
.find { |html| html['href']&.starts_with?(Gitlab.config.gitlab.url) }
|
|
|
|
.try(:[], 'href')
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_plugin_issue(issue)
|
|
|
|
plugin_issues = issue.fetch('pluginIssues', nil)
|
2019-12-18 12:07:48 +00:00
|
|
|
return unless plugin_issues
|
|
|
|
|
|
|
|
gitlab_plugin = plugin_issues.detect { |item| item['id'] == 'gitlab' }
|
|
|
|
return unless gitlab_plugin
|
|
|
|
|
|
|
|
gitlab_plugin.dig('issue', 'url')
|
|
|
|
end
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
def issue_url(id)
|
|
|
|
parse_sentry_url("#{url}/issues/#{id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_url
|
|
|
|
parse_sentry_url(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_sentry_url(api_url)
|
|
|
|
url = ErrorTracking::ProjectErrorTrackingSetting.extract_sentry_external_url(api_url)
|
|
|
|
|
|
|
|
uri = URI(url)
|
|
|
|
uri.path.squeeze!('/')
|
|
|
|
# Remove trailing slash
|
2021-04-19 15:09:08 +00:00
|
|
|
uri.to_s.delete_suffix('/')
|
2020-01-09 09:07:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def map_to_errors(issues)
|
|
|
|
issues.map(&method(:map_to_error))
|
|
|
|
end
|
|
|
|
|
|
|
|
def map_to_error(issue)
|
|
|
|
Gitlab::ErrorTracking::Error.new(
|
|
|
|
id: issue.fetch('id'),
|
|
|
|
first_seen: issue.fetch('firstSeen', nil),
|
|
|
|
last_seen: issue.fetch('lastSeen', nil),
|
|
|
|
title: issue.fetch('title', nil),
|
|
|
|
type: issue.fetch('type', nil),
|
|
|
|
user_count: issue.fetch('userCount', nil),
|
|
|
|
count: issue.fetch('count', nil),
|
|
|
|
message: issue.dig('metadata', 'value'),
|
|
|
|
culprit: issue.fetch('culprit', nil),
|
|
|
|
external_url: issue_url(issue.fetch('id')),
|
|
|
|
short_id: issue.fetch('shortId', nil),
|
|
|
|
status: issue.fetch('status', nil),
|
|
|
|
frequency: issue.dig('stats', '24h'),
|
|
|
|
project_id: issue.dig('project', 'id'),
|
|
|
|
project_name: issue.dig('project', 'name'),
|
|
|
|
project_slug: issue.dig('project', 'slug')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-18 12:07:48 +00:00
|
|
|
def map_to_detailed_error(issue)
|
2020-01-10 18:07:43 +00:00
|
|
|
Gitlab::ErrorTracking::DetailedError.new({
|
2019-12-18 12:07:48 +00:00
|
|
|
id: issue.fetch('id'),
|
|
|
|
first_seen: issue.fetch('firstSeen', nil),
|
|
|
|
last_seen: issue.fetch('lastSeen', nil),
|
2019-12-20 15:07:34 +00:00
|
|
|
tags: extract_tags(issue),
|
2019-12-18 12:07:48 +00:00
|
|
|
title: issue.fetch('title', nil),
|
|
|
|
type: issue.fetch('type', nil),
|
|
|
|
user_count: issue.fetch('userCount', nil),
|
|
|
|
count: issue.fetch('count', nil),
|
|
|
|
message: issue.dig('metadata', 'value'),
|
|
|
|
culprit: issue.fetch('culprit', nil),
|
|
|
|
external_url: issue_url(issue.fetch('id')),
|
|
|
|
external_base_url: project_url,
|
|
|
|
short_id: issue.fetch('shortId', nil),
|
|
|
|
status: issue.fetch('status', nil),
|
|
|
|
frequency: issue.dig('stats', '24h'),
|
2020-03-10 12:08:16 +00:00
|
|
|
gitlab_issue: parse_gitlab_issue(issue),
|
2019-12-18 12:07:48 +00:00
|
|
|
project_id: issue.dig('project', 'id'),
|
|
|
|
project_name: issue.dig('project', 'name'),
|
|
|
|
project_slug: issue.dig('project', 'slug'),
|
|
|
|
first_release_last_commit: issue.dig('firstRelease', 'lastCommit'),
|
|
|
|
first_release_short_version: issue.dig('firstRelease', 'shortVersion'),
|
2020-01-10 18:07:43 +00:00
|
|
|
first_release_version: issue.dig('firstRelease', 'version'),
|
|
|
|
last_release_last_commit: issue.dig('lastRelease', 'lastCommit'),
|
2020-07-02 15:09:08 +00:00
|
|
|
last_release_short_version: issue.dig('lastRelease', 'shortVersion'),
|
|
|
|
last_release_version: issue.dig('lastRelease', 'version')
|
2020-01-10 18:07:43 +00:00
|
|
|
})
|
2019-12-18 12:07:48 +00:00
|
|
|
end
|
2019-12-20 15:07:34 +00:00
|
|
|
|
|
|
|
def extract_tags(issue)
|
|
|
|
{
|
|
|
|
level: issue.fetch('level', nil),
|
|
|
|
logger: issue.fetch('logger', nil)
|
|
|
|
}
|
|
|
|
end
|
2019-12-18 12:07:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|