1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# frozen_string_literal: true
module ErrorTracking
class SentryIssuesService
def initialize(url, token)
@url = URI(url)
@token = token
end
def execute(limit: 20, issue_status: 'unresolved')
external_url = extract_external_url
issues = get_issues(limit, issue_status)
errors = map_to_errors(issues)
[external_url, errors]
end
private
# http://HOST/api/0/projects/ORG/PROJECT
# ->
# http://HOST/ORG/PROJECT
def extract_external_url
@url.to_s.sub('api/0/projects/', '')
end
def get_issues(limit, issue_status)
issues_url = "#{@url}/issues/"
sentry_query = {
query: "is:#{issue_status}",
limit: limit
}
# "query=is:unresolved&limit=#{limit}&sort=date&statsPeriod=24h&shortIdLookup=1"
resp = Gitlab::HTTP.get(issues_url,
query: sentry_query,
headers: {
'Authorization' => "Bearer #{@token}"
})
if resp.code == 200
resp.as_json
else
# TODO: Handle non 200 status (error)
[]
end
end
def map_to_errors(issues)
issues.map do |issue|
map_to_error(issue)
end
end
def map_to_error(issue)
project = issue.fetch('project')
metadata = issue.fetch('metadata')
count = issue.fetch('count')
frequency = issue.fetch('stats').fetch('24h')
count, frequency = fake_freq(frequency)
ErrorTracking::Error.new(
id: issue.fetch('id'),
first_seen: issue.fetch('firstSeen'),
last_seen: issue.fetch('lastSeen'),
title: issue.fetch('title'),
type: issue.fetch('type'),
user_count: issue.fetch('userCount'),
count: count,
message: metadata.fetch('value', nil),
culprit: issue.fetch('culprit'),
external_url: issue.fetch('permalink'),
short_id: issue.fetch('shortId'),
status: issue.fetch('status'),
frequency: frequency,
project_id: project.fetch('id'),
project_name: project.fetch('name'),
project_slug: project.fetch('slug'),
)
end
def fake_freq(frequency)
faked = frequency.map do |(time, count)|
[time, count == 0 ? 10 + rand(50) : count]
end
count = faked.reduce(0) { |sum, (_, c)| sum + c }
[count, faked]
end
end
end
|