-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUploadSourceBundleMojo.java
More file actions
398 lines (342 loc) · 15.2 KB
/
UploadSourceBundleMojo.java
File metadata and controls
398 lines (342 loc) · 15.2 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package io.sentry;
import static io.sentry.config.PluginConfig.*;
import io.sentry.cli.SentryCliRunner;
import io.sentry.telemetry.SentryTelemetryService;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Mojo(name = "uploadSourceBundle", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
public class UploadSourceBundleMojo extends AbstractMojo {
private static Logger logger = LoggerFactory.getLogger(UploadSourceBundleMojo.class);
@Parameter(property = "sentry.cli.debug", defaultValue = DEFAULT_DEBUG_SENTRY_CLI_STRING)
private boolean debugSentryCli;
@Parameter(property = "sentry.cli.path")
private @Nullable String sentryCliExecutablePath;
@Parameter(property = "sentry.org")
private @Nullable String org;
@Parameter(property = "sentry.project")
private @Nullable String project;
@Parameter(property = "sentry.url")
private @Nullable String url;
@Parameter(property = "sentry.authToken")
private @Nullable String authToken;
@SuppressWarnings("NullAway")
@Parameter(property = "project.build.directory")
private @NotNull File outputDirectory;
@SuppressWarnings("NullAway")
@Parameter(defaultValue = "${project}", readonly = true)
private @NotNull MavenProject mavenProject;
@SuppressWarnings("NullAway")
@Parameter(defaultValue = "${session}", readonly = true)
private @NotNull MavenSession mavenSession;
@Parameter(property = "additionalSourceDirsForSourceContext")
private final @NotNull List<String> additionalSourceDirsForSourceContext = new ArrayList<>();
@Parameter(defaultValue = DEFAULT_SKIP_STRING)
private boolean skip;
@Parameter(defaultValue = DEFAULT_SKIP_SOURCE_BUNDLE_STRING)
private boolean skipSourceBundle;
@Parameter(defaultValue = DEFAULT_IGNORE_SOURCE_BUNDLE_UPLOAD_FAILURE_STRING)
private boolean ignoreSourceBundleUploadFailure;
@Parameter(defaultValue = DEFAULT_REPRODUCIBLE_BUNDLE_ID_STRING)
private boolean reproducibleBundleId;
@SuppressWarnings("NullAway")
@Component
private @NotNull BuildPluginManager pluginManager;
@Override
public void execute() throws MojoExecutionException {
final @Nullable String sentrySkipSourceUpload = System.getenv("SENTRY_SKIP_SOURCE_UPLOAD");
if (skip || skipSourceBundle || "true".equalsIgnoreCase(sentrySkipSourceUpload)) {
logger.info("Upload Source Bundle skipped");
return;
}
final @NotNull File collectedSourcesTargetDir = new File(sentryBuildDir(), "collected-sources");
final @NotNull File sourceBundleTargetDir = new File(sentryBuildDir(), "source-bundle");
final @NotNull SentryCliRunner cliRunner =
new SentryCliRunner(
debugSentryCli, sentryCliExecutablePath, mavenProject, mavenSession, pluginManager);
collectSources(collectedSourcesTargetDir);
final @NotNull String bundleId;
if (reproducibleBundleId) {
bundleId = generateDeterministicBundleId(collectedSourcesTargetDir);
} else {
bundleId = UUID.randomUUID().toString();
}
createDebugMetaPropertiesFile(bundleId);
bundleSources(cliRunner, bundleId, collectedSourcesTargetDir, sourceBundleTargetDir);
uploadSourceBundle(cliRunner, sourceBundleTargetDir);
}
private void collectSources(@NotNull File outputDir) {
final @Nullable ISpan span = SentryTelemetryService.getInstance().startTask("collectSources");
logger.debug("Collecting files from source directories");
if (!outputDir.exists()) {
outputDir.mkdirs();
}
final @Nullable List<String> sourceRootsPaths = mavenProject.getCompileSourceRoots();
final @NotNull List<String> sourceDirsPaths =
sourceRootsPaths == null ? new ArrayList<>() : new ArrayList<>(sourceRootsPaths);
sourceDirsPaths.removeIf(Objects::isNull);
sourceDirsPaths.addAll(additionalSourceDirsForSourceContext);
if (sourceDirsPaths.isEmpty()) {
logger.info("No source directories to collect and bundle");
} else {
logger.debug(
"Copying files from the following directories to {} for inclusion in source bundle: {}",
outputDir,
String.join(",", sourceDirsPaths));
}
int count = 0;
for (final @NotNull String sourceDirPath : sourceDirsPaths) {
try {
final @NotNull File sourceDir = new File(sourceDirPath);
final @NotNull Path sourceDirAbsolutePath = sourceDir.toPath().toAbsolutePath().normalize();
if (!sourceDir.exists()) {
logger.error(
"Not collecting sources in {}: directory does not exist", sourceDirAbsolutePath);
continue;
}
if (!sourceDir.isDirectory()) {
logger.error("Not collecting sources in {}: not a directory", sourceDirAbsolutePath);
continue;
}
logger.debug("Collecting sources in {}", sourceDirPath);
try (final @NotNull Stream<Path> stream = Files.walk(sourceDirAbsolutePath)) {
stream.forEach(
(sourcePath) -> {
final @NotNull Path relativePath = sourceDirAbsolutePath.relativize(sourcePath);
final @NotNull Path destinationPath = outputDir.toPath().resolve(relativePath);
if (sourcePath.toFile().isFile()) {
try {
Files.createDirectories(destinationPath.getParent());
Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
logger.error(
"Failed to copy file from {} to {}", sourcePath, destinationPath, e);
}
}
});
}
count++;
} catch (Throwable t) {
logger.error("Failed to collect sources in {}", sourceDirPath, t);
SentryTelemetryService.getInstance().captureError(t, "bundleSources " + sourceDirPath);
}
}
logger.info("Collected sources from {} source directories", count);
SentryTelemetryService.getInstance().endTask(span);
}
private @NotNull File sentryBuildDir() {
return new File(outputDirectory, "sentry");
}
/**
* Generates a deterministic bundle ID based on the MD5 hash of all collected source files. This
* ensures reproducible builds produce the same bundle ID when the source files are identical.
*
* @param collectedSourcesDir the directory containing the collected source files
* @return a UUID v4 string derived from the hash of the source files
*/
private @NotNull String generateDeterministicBundleId(final @NotNull File collectedSourcesDir) {
final @Nullable ISpan span =
SentryTelemetryService.getInstance().startTask("generateDeterministicBundleId");
try {
final @NotNull MessageDigest digest = MessageDigest.getInstance("MD5");
if (collectedSourcesDir.exists() && collectedSourcesDir.isDirectory()) {
try (final @NotNull Stream<Path> stream = Files.walk(collectedSourcesDir.toPath())) {
final @NotNull List<Path> sortedFiles =
stream
.filter(Files::isRegularFile)
.sorted(
Comparator.comparing(
p ->
collectedSourcesDir
.toPath()
.relativize(p)
.toString()
.replace('\\', '/')))
.collect(Collectors.toList());
for (final @NotNull Path file : sortedFiles) {
final @NotNull String relativePath =
collectedSourcesDir.toPath().relativize(file).toString().replace('\\', '/');
digest.update(relativePath.getBytes(StandardCharsets.UTF_8));
// Include the file content in the hash
final byte[] fileBytes = Files.readAllBytes(file);
digest.update(fileBytes);
}
}
}
final byte[] hashBytes = digest.digest();
return bytesToUuid(hashBytes);
} catch (NoSuchAlgorithmException e) {
logger.warn("MD5 algorithm not available, falling back to random UUID", e);
return UUID.randomUUID().toString();
} catch (IOException e) {
logger.warn(
"Failed to read source files for bundle ID generation, falling back to random UUID", e);
SentryTelemetryService.getInstance().captureError(e, "generateDeterministicBundleId");
return UUID.randomUUID().toString();
} catch (Throwable t) {
logger.warn("Failed to generate deterministic bundle ID, falling back to random UUID", t);
SentryTelemetryService.getInstance().captureError(t, "generateDeterministicBundleId");
return UUID.randomUUID().toString();
} finally {
SentryTelemetryService.getInstance().endTask(span);
}
}
/**
* Converts 16 bytes into a UUID v4 string format (RFC 4122).
*
* @param hashBytes the hash bytes (exactly 16 bytes expected from MD5)
* @return a UUID string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
*/
private @NotNull String bytesToUuid(final byte[] hashBytes) {
// Set version 4 (bits 12-15 of time_hi_and_version to 0100)
hashBytes[6] = (byte) ((hashBytes[6] & 0x0F) | 0x40);
// Set variant to RFC 4122 (bits 6-7 of clock_seq_hi_and_reserved to 10)
hashBytes[8] = (byte) ((hashBytes[8] & 0x3F) | 0x80);
final @NotNull ByteBuffer buffer = ByteBuffer.wrap(hashBytes);
return new UUID(buffer.getLong(), buffer.getLong()).toString();
}
private void bundleSources(
final @NotNull SentryCliRunner cliRunner,
final @NotNull String bundleId,
final @NotNull File collectedSourcesDir,
final @NotNull File sourceBundleTargetDir)
throws MojoExecutionException {
final @Nullable ISpan span = SentryTelemetryService.getInstance().startTask("bundleSources");
try {
if (!sourceBundleTargetDir.exists()) {
sourceBundleTargetDir.mkdirs();
}
logger.debug(
"Bundling collected sources located in {}", collectedSourcesDir.getAbsolutePath());
final @NotNull List<String> bundleSourcesCommand = new ArrayList<>();
if (debugSentryCli) {
bundleSourcesCommand.add("--log-level=debug");
}
final @NotNull List<String> tracingArgs = SentryTelemetryService.getInstance().traceCli();
for (final @NotNull String tracingArg : tracingArgs) {
bundleSourcesCommand.add(tracingArg);
}
if (url != null) {
bundleSourcesCommand.add("--url=" + url);
}
if (authToken != null) {
bundleSourcesCommand.add("--auth-token=" + authToken);
}
bundleSourcesCommand.add("debug-files");
bundleSourcesCommand.add("bundle-jvm");
bundleSourcesCommand.add(
"--output=" + cliRunner.escape(sourceBundleTargetDir.getAbsolutePath()));
bundleSourcesCommand.add("--debug-id=" + bundleId);
if (org != null) {
bundleSourcesCommand.add("--org=" + org);
}
if (project != null) {
bundleSourcesCommand.add("--project=" + project);
}
bundleSourcesCommand.add(cliRunner.escape(collectedSourcesDir.getAbsolutePath()));
cliRunner.runSentryCli(String.join(" ", bundleSourcesCommand), true);
} catch (Throwable t) {
SentryTelemetryService.getInstance().captureError(t, "bundleSources");
throw t;
} finally {
SentryTelemetryService.getInstance().endTask(span);
}
}
private void uploadSourceBundle(
final @NotNull SentryCliRunner cliRunner, final @NotNull File sourceBundleTargetDir)
throws MojoExecutionException {
final @Nullable ISpan span =
SentryTelemetryService.getInstance().startTask("uploadSourceBundle");
try {
final @NotNull List<String> command = new ArrayList<>();
if (debugSentryCli) {
command.add("--log-level=debug");
}
final @NotNull List<String> tracingArgs = SentryTelemetryService.getInstance().traceCli();
for (final @NotNull String tracingArg : tracingArgs) {
command.add(tracingArg);
}
if (url != null) {
command.add("--url=" + url);
}
if (authToken != null) {
command.add("--auth-token=" + authToken);
}
command.add("debug-files");
command.add("upload");
command.add("--type=jvm");
if (org != null) {
command.add("--org=" + org);
}
if (project != null) {
command.add("--project=" + project);
}
command.add(cliRunner.escape(sourceBundleTargetDir.getAbsolutePath()));
cliRunner.runSentryCli(String.join(" ", command), true);
} catch (Throwable t) {
SentryTelemetryService.getInstance().captureError(t, "uploadSourceBundle");
if (ignoreSourceBundleUploadFailure) {
logger.warn("Source bundle upload failed, ignored by configuration");
} else {
throw t;
}
} finally {
SentryTelemetryService.getInstance().endTask(span);
}
}
private void createDebugMetaPropertiesFile(final @NotNull String bundleId)
throws MojoExecutionException {
final @Nullable ISpan span =
SentryTelemetryService.getInstance().startTask("createDebugMetaPropertiesFile");
final @NotNull File sentryBuildDir = new File(sentryBuildDir(), "properties");
if (!sentryBuildDir.exists()) {
sentryBuildDir.mkdirs();
}
final @NotNull File debugMetaFile = new File(sentryBuildDir, "sentry-debug-meta.properties");
try (final @NotNull BufferedWriter fileWriter =
Files.newBufferedWriter(debugMetaFile.toPath(), StandardCharsets.UTF_8)) {
// Write properties without timestamp comment for reproducible builds
// Properties are written in sorted order for consistency
fileWriter.write("# Generated by sentry-maven-plugin");
fileWriter.newLine();
fileWriter.write("io.sentry.build-tool=maven");
fileWriter.newLine();
fileWriter.write("io.sentry.bundle-ids=" + bundleId);
fileWriter.newLine();
final @NotNull Resource resource = new Resource();
resource.setDirectory(sentryBuildDir.getPath());
resource.setFiltering(false);
mavenProject.addResource(resource);
} catch (IOException e) {
SentryTelemetryService.getInstance().captureError(e, "createDebugMetaPropertiesFile");
throw new MojoExecutionException("Error creating file " + debugMetaFile, e);
} catch (Throwable t) {
SentryTelemetryService.getInstance().captureError(t, "createDebugMetaPropertiesFile");
throw t;
} finally {
SentryTelemetryService.getInstance().endTask(span);
}
}
}