forked from DataDog/datadog-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.ts
341 lines (300 loc) · 11.5 KB
/
upload.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import chalk from 'chalk'
import {Command, Option} from 'clipanion'
import {ApiKeyValidator, newApiKeyValidator} from '../../helpers/apikey'
import {getBaseSourcemapIntakeUrl} from '../../helpers/base-intake-url'
import {doWithMaxConcurrency} from '../../helpers/concurrency'
import {InvalidConfigurationError} from '../../helpers/errors'
import {getRepositoryData, newSimpleGit, RepositoryData} from '../../helpers/git/format-git-sourcemaps-data'
import {RequestBuilder} from '../../helpers/interfaces'
import {getMetricsLogger, MetricsLogger} from '../../helpers/metrics'
import {upload, UploadStatus} from '../../helpers/upload'
import {getRequestBuilder, resolveConfigFromFileAndEnvironment} from '../../helpers/utils'
import * as validation from '../../helpers/validation'
import {checkAPIKeyOverride} from '../../helpers/validation'
import {version} from '../../helpers/version'
import {RNPlatform, RNSourcemap, RN_SUPPORTED_PLATFORMS} from './interfaces'
import {
renderCommandInfo,
renderConfigurationError,
renderFailedSourcesContentRemovalError,
renderFailedUpload,
renderGitDataNotAttachedWarning,
renderGitWarning,
renderRemoveSourcesContentWarning,
renderRetriedUpload,
renderSourcesNotFoundWarning,
renderSuccessfulCommand,
renderUpload,
} from './renderer'
import {getBundleName} from './utils'
import {InvalidPayload, validatePayload} from './validation'
export class UploadCommand extends Command {
public static paths = [['react-native', 'upload']]
public static usage = Command.Usage({
category: 'RUM',
description: 'Upload React Native sourcemaps to Datadog.',
details: `
This command will upload React Native sourcemaps and their corresponding JavaScript bundle to Datadog in order to un-minify front-end stack traces received by Datadog.\n
See README for details.
`,
examples: [
[
'Upload ios sourcemaps',
'datadog-ci react-native upload --platform ios --service com.company.app --bundle ./main.jsbundle --sourcemap ./main.jsbundle.map --release-version 1.23.4 --build-version 1234',
],
[
'Upload android sourcemaps',
'datadog-ci react-native upload --platform android --service com.company.app --bundle ./index.android.bundle --sourcemap ./index.android.bundle.map --release-version 1.23.4 --build-version 1234',
],
],
})
private buildVersion = Option.String('--build-version')
private bundle = Option.String('--bundle')
private configPath = Option.String('--config')
private disableGit = Option.Boolean('--disable-git')
private dryRun = Option.Boolean('--dry-run', false)
private maxConcurrency = Option.String('--max-concurrency', '20', {validator: validation.isInteger()})
private platform?: RNPlatform = Option.String('--platform')
private projectPath = Option.String('--project-path', process.cwd() || '')
private releaseVersion = Option.String('--release-version')
private removeSourcesContent = Option.Boolean('--remove-sources-content', false)
private repositoryURL = Option.String('--repository-url')
private service = Option.String('--service')
private sourcemap = Option.String('--sourcemap')
private cliVersion = version
private config: Record<string, string> = {
datadogSite: 'datadoghq.com',
}
public async execute() {
if (!this.releaseVersion) {
this.context.stderr.write('Missing release version\n')
return 1
}
if (!this.buildVersion) {
this.context.stderr.write('Missing build version\n')
return 1
}
if (!this.service) {
this.context.stderr.write('Missing service\n')
return 1
}
if (!this.platform) {
this.context.stderr.write('Missing platform\n')
return 1
}
if (!RN_SUPPORTED_PLATFORMS.includes(this.platform)) {
this.context.stderr.write(
`Platform ${this.platform} is not supported.\nSupported platforms are ios and android.\n`
)
return 1
}
if (!this.sourcemap) {
this.context.stderr.write('Missing sourcemap file path\n')
return 1
}
const bundleName = getBundleName(this.bundle, this.platform)
this.context.stdout.write(
renderCommandInfo(
this.bundle,
this.sourcemap,
this.platform,
this.releaseVersion,
this.service,
this.maxConcurrency,
this.dryRun,
this.projectPath,
this.buildVersion,
bundleName
)
)
this.config = await resolveConfigFromFileAndEnvironment(
this.config,
{
apiKey: process.env.DATADOG_API_KEY,
datadogSite: process.env.DATADOG_SITE,
},
{
configPath: this.configPath,
defaultConfigPaths: ['datadog-ci.json', '../datadog-ci.json'],
configFromFileCallback: (configFromFile: any) => {
checkAPIKeyOverride(process.env.DATADOG_API_KEY, configFromFile.apiKey, this.context.stdout)
},
}
)
const metricsLogger = getMetricsLogger({
apiKey: this.config.apiKey,
datadogSite: this.config.datadogSite,
defaultTags: [
`version:${this.releaseVersion}`,
`build:${this.buildVersion}`,
`service:${this.service}`,
`cli_version:${this.cliVersion}`,
'react-native:true',
`platform:${this.platform}`,
],
prefix: 'datadog.ci.sourcemaps.upload.',
})
const apiKeyValidator = newApiKeyValidator({
apiKey: this.config.apiKey,
datadogSite: this.config.datadogSite,
metricsLogger: metricsLogger.logger,
})
const useGit = this.disableGit === undefined || !this.disableGit
const initialTime = Date.now()
const payloads = await this.getPayloadsToUpload(useGit, bundleName)
const requestBuilder = this.getRequestBuilder()
const uploadMultipart = this.upload(requestBuilder, metricsLogger, apiKeyValidator)
try {
const results = await doWithMaxConcurrency(this.maxConcurrency, payloads, uploadMultipart)
const totalTime = (Date.now() - initialTime) / 1000
this.context.stdout.write(renderSuccessfulCommand(results, totalTime, this.dryRun))
metricsLogger.logger.gauge('duration', totalTime)
return results.some((result) => result !== UploadStatus.Success) ? 1 : 0
} catch (error) {
if (error instanceof InvalidConfigurationError) {
this.context.stdout.write(renderConfigurationError(error))
return 1
}
// Otherwise unknown error, let's propagate the exception
throw error
} finally {
try {
await metricsLogger.flush()
} catch (err) {
this.context.stdout.write(`WARN: ${err}\n`)
}
}
}
// Fills the 'repository' field of each payload with data gathered using git.
private addRepositoryDataToPayloads = async (payloads: RNSourcemap[]) => {
try {
const repositoryData = await getRepositoryData(await newSimpleGit(), this.repositoryURL)
payloads.forEach((payload) => {
const repositoryPayload = this.getRepositoryPayload(repositoryData, payload.sourcemapPath)
payload.addRepositoryData({
gitCommitSha: repositoryData.hash,
gitRepositoryPayload: repositoryPayload,
gitRepositoryURL: repositoryData.remote,
})
})
} catch (e) {
this.context.stdout.write(renderGitWarning(e))
}
}
// Looks for the sourcemaps on disk and returns the associated payloads.
private getMatchingRNSourcemapFiles = (bundleName: string): RNSourcemap[] => [
new RNSourcemap(bundleName, this.sourcemap!),
]
private getPayloadsToUpload = async (useGit: boolean, bundleName: string): Promise<RNSourcemap[]> => {
const payloads = this.getMatchingRNSourcemapFiles(bundleName)
if (!useGit) {
return payloads
}
await this.addRepositoryDataToPayloads(payloads)
return payloads
}
// GetRepositoryPayload generates the repository payload for a specific sourcemap.
// It specifically looks for the list of tracked files that are associated to the source paths
// declared inside the sourcemap.
private getRepositoryPayload = (repositoryData: RepositoryData, sourcemapPath: string): string | undefined => {
const onSourcesNotFound = () => {
this.context.stdout.write(renderSourcesNotFoundWarning(sourcemapPath))
}
let repositoryPayload: string | undefined
try {
const files = repositoryData.trackedFilesMatcher.matchSourcemap(sourcemapPath, onSourcesNotFound)
if (files) {
repositoryPayload = JSON.stringify({
data: [
{
files,
hash: repositoryData.hash,
repository_url: repositoryData.remote,
},
],
// Make sure to update the version if the format of the JSON payloads changes in any way.
version: 1,
})
}
return repositoryPayload
} catch (error) {
this.context.stdout.write(renderGitDataNotAttachedWarning(sourcemapPath, error.message))
return undefined
}
}
private getRequestBuilder(): RequestBuilder {
if (!this.config.apiKey) {
throw new InvalidConfigurationError(`Missing ${chalk.bold('DATADOG_API_KEY')} in your environment.`)
}
return getRequestBuilder({
apiKey: this.config.apiKey,
baseUrl: getBaseSourcemapIntakeUrl(this.config.datadogSite),
headers: new Map([
['DD-EVP-ORIGIN', 'datadog-ci react-native'],
['DD-EVP-ORIGIN-VERSION', this.cliVersion],
]),
overrideUrl: 'api/v2/srcmap',
})
}
private upload(
requestBuilder: RequestBuilder,
metricsLogger: MetricsLogger,
apiKeyValidator: ApiKeyValidator
): (sourcemap: RNSourcemap) => Promise<UploadStatus> {
return async (sourcemap: RNSourcemap) => {
try {
validatePayload(sourcemap)
} catch (error) {
if (error instanceof InvalidPayload) {
this.context.stdout.write(renderFailedUpload(sourcemap, error.message))
metricsLogger.logger.increment('skipped_sourcemap', 1, [`reason:${error.reason}`])
} else {
this.context.stdout.write(
renderFailedUpload(
sourcemap,
`Skipping sourcemap ${sourcemap.sourcemapPath} because of error: ${error.message}`
)
)
metricsLogger.logger.increment('skipped_sourcemap', 1, ['reason:unknown'])
}
return UploadStatus.Skipped
}
if (this.removeSourcesContent) {
try {
this.context.stdout.write(renderRemoveSourcesContentWarning())
sourcemap.removeSourcesContentFromSourceMap()
} catch (error) {
this.context.stdout.write(renderFailedSourcesContentRemovalError(sourcemap, error.message))
}
}
const payload = sourcemap.asMultipartPayload(
this.cliVersion,
this.service!,
this.releaseVersion!,
this.projectPath,
this.platform!,
this.buildVersion!
)
if (this.dryRun) {
this.context.stdout.write(`[DRYRUN] ${renderUpload(sourcemap)}`)
return UploadStatus.Success
}
return upload(requestBuilder)(payload, {
apiKeyValidator,
onError: (e) => {
this.context.stdout.write(renderFailedUpload(sourcemap, e.message))
metricsLogger.logger.increment('failed', 1)
},
onRetry: (e, attempts) => {
this.context.stdout.write(renderRetriedUpload(sourcemap, e.message, attempts))
metricsLogger.logger.increment('retries', 1)
},
onUpload: () => {
this.context.stdout.write(renderUpload(sourcemap))
},
retries: 5,
useGzip: true,
})
}
}
}