-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublishToFillTask.java
More file actions
387 lines (346 loc) · 14.9 KB
/
PublishToFillTask.java
File metadata and controls
387 lines (346 loc) · 14.9 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
/*
* Copyright 2024 PaperMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.papermc.fill.gradle.task;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.hash.Hashing;
import io.papermc.fill.gradle.FillExtension;
import io.papermc.fill.model.Checksums;
import io.papermc.fill.model.Commit;
import io.papermc.fill.model.Download;
import io.papermc.fill.model.request.PublishRequest;
import io.papermc.fill.model.response.v3.BuildResponse;
import io.papermc.fill.model.response.v3.VersionResponse;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import javax.inject.Inject;
import io.papermc.fill.model.response.v3.VersionsResponse;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.UntrackedTask;
import org.jetbrains.annotations.VisibleForTesting;
import org.jspecify.annotations.NullMarked;
@NullMarked
@UntrackedTask(because = "PublishToFillTask should always run when requested")
public abstract class PublishToFillTask extends DefaultTask implements AutoCloseable {
public static final String NAME = "publishToFill";
private static final String USER_AGENT = "Fill (Gradle Plugin)";
private final HttpClient httpClient = HttpClient.newBuilder()
.build();
public PublishToFillTask() {
this.setGroup("fill");
this.setDescription("Publish to Fill");
}
@Nested
public abstract Property<FillExtension> getExtension();
@Inject
public abstract ProjectLayout getProjectLayout();
private void withGit(final Consumer<Git> consumer) {
final File settingsDir = this.getProjectLayout().getSettingsDirectory().getAsFile();
try (final Git git = Git.open(settingsDir)) {
consumer.accept(git);
} catch (final IOException e) {
throw new GradleException("Failed to open git repository", e);
}
}
@TaskAction
public void run() {
this.withGit(this::runWithGit);
}
private void runWithGit(final Git git) {
final FillExtension extension = this.getExtension().get();
final String project = extension.getProject().get();
final String familyId = extension.getVersionFamily().get();
final String versionId = extension.getVersion().get();
final FillExtension.Build build = extension.getBuild();
final int buildId = build.getId().get();
final String timeString = this.getExtension().get().getBuildTimestamp().getOrNull();
final Instant time;
if (timeString != null) {
try {
time = Instant.parse(timeString);
} catch (final DateTimeParseException e) {
throw new GradleException("Failed to parse build timestamp: " + timeString, e);
}
} else {
time = Instant.now();
}
final List<Commit> commits = this.gatherCommits(git, extension);
final UUID id = UUID.randomUUID();
final List<HttpRequest> requests = new ArrayList<>();
try {
final Map<String, Download> downloads = new HashMap<>();
for (final FillExtension.Download download : build.getDownloads()) {
final String key = download.getName();
final String name = download.getNameResolver().get().name(project, familyId, versionId, buildId);
final Path path = download.getFile().get().getAsFile().toPath();
final byte[] content = Files.readAllBytes(path);
final String sha256 = Hashing.sha256().hashBytes(content).toString();
final int size = (int) Files.size(path);
downloads.put(key, new Download(name, new Checksums(sha256), size));
final HttpRequest.Builder builder = HttpRequest.newBuilder();
builder.header("User-Agent", USER_AGENT);
builder.header("Content-Type", "multipart/form-data; boundary=boundary");
builder.uri(URI.create(extension.getApiUrl().get() + "/upload"));
final List<byte[]> requestParts = new ArrayList<>();
requestParts.add(("--boundary\r\nContent-Disposition: form-data; name=\"request\"\r\nContent-Type: application/json\r\n\r\n{\"id\":\"" + id + "\"}\r\n").getBytes(StandardCharsets.UTF_8));
requestParts.add(("--boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + name + "\"\r\n\r\n").getBytes(StandardCharsets.UTF_8));
requestParts.add(content);
requestParts.add(("\r\n--boundary").getBytes(StandardCharsets.UTF_8));
builder.POST(HttpRequest.BodyPublishers.ofByteArrays(requestParts));
if (extension.getApiToken().isPresent()) {
builder.header("Authorization", extension.getApiToken().get());
} else {
throw new GradleException("API token is not present");
}
requests.add(builder.build());
}
for (final HttpRequest request : requests) {
try {
final HttpResponse<String> response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new GradleException("Failed to post data to the API: " + response.statusCode() + ": " + response.body());
}
} catch (final Exception e) {
throw new GradleException("Failed to post data to the API", e);
}
}
final PublishRequest request = new PublishRequest(
id,
project,
familyId,
versionId,
buildId,
time,
build.getChannel().get(),
commits.reversed(),
downloads
);
try {
final HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(URI.create(extension.getApiUrl().get() + "/publish"))
.header("Content-Type", "application/json")
.header("User-Agent", USER_AGENT);
builder.POST(HttpRequest.BodyPublishers.ofString(MapperHolder.MAPPER.writeValueAsString(request)));
if (extension.getApiToken().isPresent()) {
builder.headers("Authorization", extension.getApiToken().get());
} else {
throw new GradleException("Api token is not present.");
}
final HttpRequest post = builder.build();
final HttpResponse<String> response = this.httpClient.send(post, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 201) {
throw new GradleException("Failed to post data to the API: " + response.statusCode() + ": " + response.body());
}
} catch (final Exception e) {
throw new GradleException("Failed to post data to the API: " + e.getMessage(), e);
}
} catch (final JsonProcessingException e) {
throw new GradleException("Failed to serialize json", e);
} catch (final IOException e) {
throw new GradleException("Failed to read file", e);
}
}
private List<Commit> gatherCommits(Git git, FillExtension extension) {
final List<Commit> commits = new ArrayList<>();
try (final RevWalk revWalk = new RevWalk(git.getRepository())) {
final RevCommit currentCommit = revWalk.parseCommit(git.getRepository().exactRef(Constants.HEAD).getObjectId());
revWalk.markStart(currentCommit);
final List<BuildResponse> builds = this.fetchPreviousBuilds(extension);
if (!builds.isEmpty()) {
if (hasLatestBuildWithEmptyCommits(builds)) {
return commits;
}
// not every build might have commits, we have to find the last one that did have some
BuildResponse lastBuildWithCommits = null;
for (final BuildResponse build : builds) {
if (!build.commits().isEmpty()) {
lastBuildWithCommits = build;
break;
}
}
if (lastBuildWithCommits != null) {
final Commit lastCommit = lastBuildWithCommits.commits().getFirst();
if (!this.tryMarkPreviousBuildCommit(git, revWalk, currentCommit, lastCommit)) {
return commits;
}
}
}
for (final RevCommit commit : revWalk) {
commits.add(new Commit(
commit.getName(),
commit.getAuthorIdent().getWhenAsInstant(),
commit.getFullMessage()
));
}
} catch (final IOException e) {
throw new GradleException("Failed to get commit data", e);
}
return commits;
}
@VisibleForTesting
static boolean hasLatestBuildWithEmptyCommits(final List<BuildResponse> builds) {
return !builds.isEmpty() && builds.getFirst().commits().isEmpty();
}
private boolean tryMarkPreviousBuildCommit(final Git git, final RevWalk revWalk, final RevCommit currentCommit, final Commit lastCommit) throws IOException {
final ObjectId lastBuildObjectId = git.getRepository().resolve(lastCommit.sha());
if (lastBuildObjectId == null) {
this.logEmptyChangelogWarning(
lastCommit.sha(),
currentCommit.getName(),
"previous build commit is not present in the local repository"
);
return false;
}
try (final RevWalk ancestryWalk = new RevWalk(git.getRepository())) {
final RevCommit previousBuildCommit = ancestryWalk.parseCommit(lastBuildObjectId);
final RevCommit currentBuildCommit = ancestryWalk.parseCommit(currentCommit);
if (!ancestryWalk.isMergedInto(previousBuildCommit, currentBuildCommit)) {
this.logEmptyChangelogWarning(
lastCommit.sha(),
currentCommit.getName(),
"previous build commit is not an ancestor of the current HEAD"
);
return false;
}
} catch (final MissingObjectException e) {
this.logEmptyChangelogWarning(
lastCommit.sha(),
currentCommit.getName(),
"previous build commit could not be loaded from the local repository"
);
return false;
}
revWalk.markUninteresting(revWalk.parseCommit(lastBuildObjectId));
return true;
}
private void logEmptyChangelogWarning(final String previousCommit, final String currentCommit, final String reason) {
this.getLogger().warn(
"Unable to compute changelog: {} (previous build commit: {}, current HEAD: {}). Publishing with an empty changelog.",
reason,
previousCommit,
currentCommit
);
}
private List<BuildResponse> fetchPreviousBuilds(final FillExtension extension) {
final String currentVersion = extension.getVersion().get();
final VersionsResponse versions = this.getVersions(extension);
// Check if the current version already has builds
for (final VersionResponse version : versions.versions()) {
if (version.version().id().equals(currentVersion) && !version.builds().isEmpty()) {
return this.fetchCurrentVersionBuilds(extension, currentVersion);
}
}
// For new versions without builds, fall back to finding the last version with builds
return this.fetchLastVersionBuilds(extension, versions);
}
private List<BuildResponse> fetchCurrentVersionBuilds(final FillExtension extension, final String version) {
return this.getBuilds(extension, version);
}
private List<BuildResponse> fetchLastVersionBuilds(final FillExtension extension, final VersionsResponse versions) {
for (final VersionResponse version : versions.versions()) {
if (!version.builds().isEmpty()) {
return this.getBuilds(extension, version.version().id());
}
}
return List.of();
}
private VersionsResponse getVersions(final FillExtension extension) {
final String url = String.format(
"%s/v3/projects/%s/versions",
extension.getApiUrl().get(),
extension.getProject().get()
);
try {
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("User-Agent", USER_AGENT)
.build();
final HttpResponse<String> response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
final int statusCode = response.statusCode();
if (statusCode == 200) {
final String json = response.body();
return MapperHolder.MAPPER.readValue(json, VersionsResponse.class);
} else {
throw new IOException("Unexpected response status: " + statusCode);
}
} catch (final IOException | InterruptedException e) {
throw new GradleException("Failed to fetch latest build data for version " + extension.getVersion().get() + ": " + e.getMessage(), e);
}
}
private List<BuildResponse> getBuilds(final FillExtension extension, final String version) {
final String url = String.format(
"%s/v3/projects/%s/versions/%s/builds",
extension.getApiUrl().get(),
extension.getProject().get(),
version
);
try {
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("User-Agent", USER_AGENT)
.build();
final HttpResponse<String> response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
final int statusCode = response.statusCode();
if (statusCode == 200) {
final String json = response.body();
@SuppressWarnings("Convert2Diamond")
final TypeReference<List<BuildResponse>> type = new TypeReference<List<BuildResponse>>() {};
return MapperHolder.MAPPER.readValue(json, type);
} else {
throw new IOException("Unexpected response status: " + statusCode);
}
} catch (final IOException | InterruptedException e) {
throw new GradleException("Failed to fetch latest build data for version " + extension.getVersion().get() + ": " + e.getMessage(), e);
}
}
@Override
public void close() {
this.httpClient.close();
}
@VisibleForTesting
public static final class MapperHolder {
public static final ObjectMapper MAPPER = new ObjectMapper()
.registerModule(new JavaTimeModule());
}
}