Repository navigation
Expand file tree
/
Copy pathcheckExists.ts
More file actions
64 lines (57 loc) · 2.15 KB
/
Copy pathcheckExists.ts
File metadata and controls
64 lines (57 loc) · 2.15 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
import {getApiUrl} from '@datadog/datadog-ci-base/helpers/api'
import {datadogRoute} from '@datadog/datadog-ci-base/helpers/request/datadog-route'
import {getRequestBuilder} from '@datadog/datadog-ci-base/helpers/utils'
// The check_exists endpoint accepts at most 1000 debug IDs per request.
const MAX_DEBUG_IDS_PER_REQUEST = 1000
// Best effort: a failed or slow check uploads everything anyway, so fail fast.
const CHECK_EXISTS_TIMEOUT_MS = 1000
interface CheckExistsResponse {
data?: {
attributes?: {
// Untrusted wire values: callers only treat an explicit `true` as existing.
results?: Record<string, unknown>
}
}
}
/**
* Query the check_exists endpoint for the debug IDs that already
* exist in Datadog. Returns a map of debug ID to existence. Throws on request
* failure or malformed response — callers should fail open and upload everything.
*/
export const checkExistingDebugIds = async (
apiKey: string,
datadogSite: string,
cliVersion: string,
debugIds: string[]
): Promise<Record<string, unknown>> => {
const uniqueIds = [...new Set(debugIds)]
const results: Record<string, unknown> = {}
if (uniqueIds.length === 0) {
return results
}
const requestBuilder = getRequestBuilder({
apiKey,
baseUrl: getApiUrl(datadogSite),
headers: new Map([
['Content-Type', 'application/vnd.api+json'],
['DD-EVP-ORIGIN', 'datadog-ci_sourcemaps'],
['DD-EVP-ORIGIN-VERSION', cliVersion],
]),
})
for (let i = 0; i < uniqueIds.length; i += MAX_DEBUG_IDS_PER_REQUEST) {
const chunk = uniqueIds.slice(i, i + MAX_DEBUG_IDS_PER_REQUEST)
const response = await requestBuilder({
method: 'POST',
url: datadogRoute('/api/unstable/sourcemaps/check_exists'),
// The endpoint speaks JSON:API on the wire.
data: {data: {type: 'check_exists', attributes: {debug_ids: chunk}}},
timeout: CHECK_EXISTS_TIMEOUT_MS,
})
const chunkResults = (response.data as CheckExistsResponse).data?.attributes?.results
if (chunkResults === undefined) {
throw new Error('Invalid check_exists response: missing results')
}
Object.assign(results, chunkResults)
}
return results
}