-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathsentryApi.js
More file actions
88 lines (81 loc) · 2.89 KB
/
sentryApi.js
File metadata and controls
88 lines (81 loc) · 2.89 KB
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
const baseUrl = 'https://sentry.io/api/0/projects/sentry-sdks/sentry-react-native';
const RETRY_COUNT = 600;
const RETRY_INTERVAL = 1000;
const requestHeaders = { 'Authorization': `Bearer ${sentryAuthToken}` }
function sleep(ms) {
// TODO reach out to Maestro & GrallJS via GitHub issues.
// return new Promise(resolve => setTimeout(resolve, ms));
// Instead, we need to do a busy wait.
const until = Date.now() + ms;
while (Date.now() < until) {
// console.log(`Sleeping for ${until - Date.now()} ms`);
try {
http.get('http://127.0.0.1:1');
} catch (e) {
// Ignore
}
}
}
function fetchFromSentry(url) {
console.log(`Fetching ${url}`);
let retries = 0;
const shouldRetry = (response) => {
switch (response.status) {
case 200:
return false;
case 403:
throw new Error(`Could not fetch ${url}: ${response.status} | ${response.body}`);
default:
if (retries++ < RETRY_COUNT) {
console.log(`Request failed (HTTP ${response.status}), retrying: ${retries}/${RETRY_COUNT}`);
return true;
}
throw new Error(`Could not fetch ${url} within retry limit: ${response.status} | ${response.body}`);
}
}
while (true) {
const response = http.get(url, { headers: requestHeaders })
if (!shouldRetry(response)) {
console.log(`Received HTTP ${response.status}: body length ${response.body.length}`);
return response.body;
}
sleep(RETRY_INTERVAL);
}
};
function setOutput(data) {
for (const [key, value] of Object.entries(data)) {
console.log(`Setting output.${key} = '${value}'`);
output[key] = value;
}
}
// Note: "fetch", "id", "eventId", etc. are script inputs, see for example assertEventIdIVisible.yml
switch (fetch) {
case 'event': {
const data = json(fetchFromSentry(`${baseUrl}/events/${id}/json/`));
setOutput({ eventId: data.event_id });
break;
}
case 'replay': {
// The replay_id is set by the SDK on the event before sending (in
// contexts.replay.replay_id or _dsc.replay_id). It should be present
// when the event is fetched from the API.
const event = json(fetchFromSentry(`${baseUrl}/events/${eventId}/json/`));
const rawReplayId = (event.contexts && event.contexts.replay && event.contexts.replay.replay_id)
|| (event._dsc && event._dsc.replay_id);
if (!rawReplayId) {
throw new Error('replay_id not available on the event');
}
const replayId = rawReplayId.replace(/\-/g, '');
const replay = json(fetchFromSentry(`${baseUrl}/replays/${replayId}/`));
const segment = fetchFromSentry(`${baseUrl}/replays/${replayId}/videos/0/`);
setOutput({
replayId: replay.data.id,
replayDuration: replay.data.duration,
replaySegments: replay.data.count_segments,
replayCodec: segment.slice(4, 12)
});
break;
}
default:
throw new Error(`Unknown "fetch" value: '${fetch}'`);
}