|
| 1 | +const baseUrl = 'https://sentry.io/api/0/projects/sentry-sdks/sentry-react-native'; |
| 2 | + |
| 3 | +const RETRY_COUNT = 600; |
| 4 | +const RETRY_INTERVAL = 1000; |
| 5 | +const requestHeaders = { 'Authorization': `Bearer ${sentryAuthToken}` } |
| 6 | + |
| 7 | +function sleep(ms) { |
| 8 | + // TODO reach out to Maestro & GrallJS via GitHub issues. |
| 9 | + // return new Promise(resolve => setTimeout(resolve, ms)); |
| 10 | + // Instead, we need to do a busy wait. |
| 11 | + const until = Date.now() + ms; |
| 12 | + while (Date.now() < until) { |
| 13 | + // console.log(`Sleeping for ${until - Date.now()} ms`); |
| 14 | + try { |
| 15 | + http.get('http://127.0.0.1:1'); |
| 16 | + } catch (e) { |
| 17 | + // Ignore |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function fetchFromSentry(url) { |
| 23 | + console.log(`Fetching ${url}`); |
| 24 | + let retries = 0; |
| 25 | + const shouldRetry = (response) => { |
| 26 | + switch (response.status) { |
| 27 | + case 200: |
| 28 | + return false; |
| 29 | + case 403: |
| 30 | + throw new Error(`Could not fetch ${url}: ${response.status} | ${response.body}`); |
| 31 | + default: |
| 32 | + if (retries++ < RETRY_COUNT) { |
| 33 | + console.log(`Request failed (HTTP ${response.status}), retrying: ${retries}/${RETRY_COUNT}`); |
| 34 | + return true; |
| 35 | + } |
| 36 | + throw new Error(`Could not fetch ${url} within retry limit: ${response.status} | ${response.body}`); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + while (true) { |
| 41 | + const response = http.get(url, { headers: requestHeaders }) |
| 42 | + if (!shouldRetry(response)) { |
| 43 | + console.log(`Received HTTP ${response.status}: body length ${response.body.length}`); |
| 44 | + return response.body; |
| 45 | + } |
| 46 | + sleep(RETRY_INTERVAL); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +function setOutput(data) { |
| 51 | + for (const [key, value] of Object.entries(data)) { |
| 52 | + console.log(`Setting output.${key} = '${value}'`); |
| 53 | + output[key] = value; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Note: "fetch", "id", "eventId", etc. are script inputs, see for example assertEventIdIVisible.yml |
| 58 | +switch (fetch) { |
| 59 | + case 'event': { |
| 60 | + const data = json(fetchFromSentry(`${baseUrl}/events/${id}/json/`)); |
| 61 | + setOutput({ eventId: data.event_id }); |
| 62 | + break; |
| 63 | + } |
| 64 | + case 'replay': { |
| 65 | + const event = json(fetchFromSentry(`${baseUrl}/events/${eventId}/json/`)); |
| 66 | + const replayId = event._dsc.replay_id.replace(/\-/g, ''); |
| 67 | + const replay = json(fetchFromSentry(`${baseUrl}/replays/${replayId}/`)); |
| 68 | + const segment = fetchFromSentry(`${baseUrl}/replays/${replayId}/videos/0/`); |
| 69 | + |
| 70 | + setOutput({ |
| 71 | + replayId: replay.data.id, |
| 72 | + replayDuration: replay.data.duration, |
| 73 | + replaySegments: replay.data.count_segments, |
| 74 | + replayCodec: segment.slice(4, 12) |
| 75 | + }); |
| 76 | + break; |
| 77 | + } |
| 78 | + default: |
| 79 | + throw new Error(`Unknown "fetch" value: '${fetch}'`); |
| 80 | +} |
0 commit comments