Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MAKEFLAGS += --always-make --warn-undefined-variables
SHELL=/bin/bash -u

SHELL=/bin/bash
.SHELLFLAGS = -eu -c
red=\033[31m
nc=\033[0m
SSH_USER ?= $${USER}
Expand All @@ -11,9 +11,16 @@ help: ## Show this help text

all: format check

gradleProperties=$$HOME/.gradle/gradle.properties

setup: ## Installs pre-commit as a pre-push git hook (requires pre-commit to be installed)
set -e
uv tool install pre-commit
pre-commit install --hook-type pre-push
test -f $(gradleProperties) || touch $(gradleProperties)
# Check that required gradle properties are set
grep -q "triplea_github_username" $(gradleProperties)
grep -q "triplea_github_access_token" $(gradleProperties)

print-versions: ## Prints versions of system dependencies (EG: java, docker)
@echo -e "\n$(red)### Versions used by Gradle ###$(nc)"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Support Server


Create file (or append to): `~/.gradle/gradle.properties`, the following:

```
triplea_github_username=CHANGE_ME
triplea_github_access.token=CHANGE_ME
```

## Tech Overview

- Java 21
Expand Down
11 changes: 2 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ spotless {
}

val quarkusPlatformVersion = "3.34.6"
val feignVersion = "13.6"
val gsonVersion = "2.12.1"
val junitVersion = "5.13.4"
val mockitoVersion = "5.19.0"
val tripleaVersion = "2.7.15281"
val tripleaVersion = "2.7.15496"

dependencies {
implementation(enforcedPlatform("io.quarkus.platform:quarkus-bom:$quarkusPlatformVersion"))
Expand All @@ -99,18 +98,12 @@ dependencies {
implementation("com.google.code.findbugs:jsr305:3.0.2") // @Nonnull

// TripleA shared libraries
implementation("triplea:domain-data:$tripleaVersion")
implementation("triplea:java-extras:$tripleaVersion")
implementation("triplea:lobby-client:$tripleaVersion")
implementation("triplea:websocket-client:$tripleaVersion")
implementation("triplea:lobby-client-data:${tripleaVersion}")

// Test dependencies
testImplementation(enforcedPlatform("io.quarkus.platform:quarkus-bom:$quarkusPlatformVersion"))
testImplementation("io.quarkus:quarkus-junit5") // @QuarkusTest + Dev Services

testImplementation("io.github.openfeign:feign-core:$feignVersion")
testImplementation("io.github.openfeign:feign-gson:$feignVersion")
testImplementation("triplea:feign-common:$tripleaVersion")

testImplementation("com.github.database-rider:rider-junit5:1.44.0")
testImplementation("com.github.npathai:hamcrest-optional:2.0.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.triplea.java.StringUtils;

/** Represents request data to create a github issue. */
@ToString
Expand All @@ -20,11 +19,26 @@ public class CreateIssueRequest {

public String getTitle() {
final int maxLength = 125;
return title == null ? null : StringUtils.truncate(title, maxLength);
return title == null ? null : truncate(title, maxLength);
}

public String getBody() {
final int maxLength = 65536;
return body == null ? null : StringUtils.truncate(body, maxLength);
return body == null ? null : truncate(body, maxLength);
}

private static String truncate(final String stringToTruncate, final int maxLength) {
if (maxLength < "...".length()) {
throw new IllegalArgumentException(
String.format(
"Illegal max length for truncate requested: %s, must be at least 3, the length of the ellipsis",
maxLength));
}
final String s = stringToTruncate == null ? "" : stringToTruncate;
if (s.length() <= maxLength) {
return s;
}
final String ellipsis = "...";
return s.substring(0, Math.max(0, maxLength - ellipsis.length())) + ellipsis;
}
}
39 changes: 39 additions & 0 deletions src/main/java/org/triplea/io/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.triplea.io.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

/** A collection of useful methods related to files. */
@UtilityClass
@Slf4j
public final class FileUtils {

/**
* Utility to delete file specified by the given path. This method handles any needed logging if
* the delete fails.
*/
public static void delete(final Path pathToDelete) {
try {
Files.delete(pathToDelete);
} catch (final IOException e) {
log.error("Failed to delete file: {}, {}", pathToDelete.toAbsolutePath(), e.getMessage(), e);
}
}

/**
* Creates a temp file, logs and returns an empty optional if there is a problem creating the temp
* file.
*/
public static Optional<Path> createTempFile() {
try {
return Optional.of(Files.createTempFile("triplea-temp-file", ".temp"));
} catch (final IOException e) {
log.error("Failed to create temp file: {}", e.getMessage(), e);
return Optional.empty();
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/triplea/java/function/ThrowingFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.triplea.java.function;

/**
* A function that accepts one argument, produces a result, and may throw a checked exception.
*
* @param <T> The type of the input to the function.
* @param <R> The type of the result of the function.
* @param <E> The type of exception that may be thrown by the function.
*/
@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Throwable> {
/**
* Applies the function to the given argument.
*
* @param value The function argument.
* @return The function result.
* @throws E If an error occurs while applying the function.
*/
R apply(T value) throws E;
}
11 changes: 6 additions & 5 deletions src/main/java/org/triplea/maps/MapsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import java.util.List;
import java.util.function.Supplier;
import org.jdbi.v3.core.Jdbi;
import org.triplea.http.client.maps.listing.MapDownloadItem;
import org.triplea.http.client.maps.listing.MapsClient;
import org.triplea.http.client.ServerPaths;
import org.triplea.http.client.lobby.maps.listing.MapDownloadItem;
import org.triplea.http.client.lobby.maps.listing.MapListingResponse;
import org.triplea.maps.listing.MapsListingModule;

@Path("/")
Expand All @@ -35,8 +36,8 @@ void init() {
* maps listing', the intended audience is any game user searching for available to download maps.
*/
@GET
@Path(MapsClient.MAPS_LISTING_PATH)
public List<MapDownloadItem> fetchAvailableMaps() {
return downloadListingSupplier.get();
@Path(ServerPaths.MAPS_LISTING_PATH)
public MapListingResponse fetchAvailableMaps() {
return MapListingResponse.builder().maps(downloadListingSupplier.get()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
import lombok.extern.slf4j.Slf4j;
import org.triplea.http.client.github.GithubClient;
import org.triplea.http.client.github.MapRepoListing;
import org.triplea.java.Interruptibles;

// import org.triplea.http.client.github.GithubApiClient;
// import org.triplea.http.client.github.MapRepoListing;

/**
* Task that runs a map indexing pass on all maps. The indexing will update database to reflect the
Expand Down Expand Up @@ -73,7 +69,12 @@ public void run() {
IndexingResult result = index(listing);
log.info("Indexing map: {}", listing.getUri());
mapIndexDao.recordIndexingStatus(listing, result);
Interruptibles.sleep(indexingTaskDelaySeconds * 1000L);
try {
Thread.sleep(indexingTaskDelaySeconds * 1000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("System aborted, process terminated while sleeping");
}
}

log.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand All @@ -12,8 +15,7 @@
import lombok.AccessLevel;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.triplea.io.ContentDownloader;
import org.triplea.io.FileUtils;
import org.triplea.io.utils.FileUtils;
import org.triplea.java.function.ThrowingFunction;

/**
Expand All @@ -24,11 +26,23 @@
*/
@Slf4j
public class DownloadSizeFetcher implements Function<URI, Optional<Long>> {
private static final HttpClient HTTP_CLIENT =
HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();

@SuppressWarnings("resource")
@Setter(value = AccessLevel.PACKAGE, onMethod_ = @VisibleForTesting)
private ThrowingFunction<URI, InputStream, IOException> downloadFunction =
(uri -> new ContentDownloader(uri).getStream());
uri -> {
try {
HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
HttpResponse<InputStream> response =
HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofInputStream());
return response.body();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Download interrupted", e);
}
};

@Override
public Optional<Long> apply(URI uri) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.triplea.maps.indexing.tasks;

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.util.Optional;
import java.util.function.Function;
import org.triplea.http.client.github.MapRepoListing;
import org.triplea.io.ContentDownloader;

/**
* A function where if given a map repo listing will find the 'description.html' file in that repo
Expand All @@ -13,6 +16,8 @@
*/
public class MapDescriptionReader implements Function<MapRepoListing, String> {
private static final int DESCRIPTION_COLUMN_DATABASE_MAX_LENGTH = 3000;
private static final HttpClient HTTP_CLIENT =
HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();

@Override
public String apply(final MapRepoListing mapRepoListing) {
Expand All @@ -34,8 +39,18 @@ public String apply(final MapRepoListing mapRepoListing) {
}

private Optional<String> downloadDescription(final MapRepoListing mapRepoListing) {
final String descriptionUri =
mapRepoListing.getUri().toString() + "/blob/master/description.html?raw=true";
return ContentDownloader.downloadAsString(URI.create(descriptionUri));
final URI descriptionUri =
URI.create(mapRepoListing.getUri().toString() + "/blob/master/description.html?raw=true");
try {
HttpRequest request = HttpRequest.newBuilder(descriptionUri).GET().build();
HttpResponse<String> response =
HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode() == 200 ? Optional.of(response.body()) : Optional.empty();
} catch (IOException e) {
return Optional.empty();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Optional.empty();
}
}
}
24 changes: 21 additions & 3 deletions src/main/java/org/triplea/maps/indexing/tasks/MapNameReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.triplea.maps.indexing.tasks;

import com.google.common.annotations.VisibleForTesting;
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.util.Map;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -10,17 +14,31 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.triplea.http.client.github.MapRepoListing;
import org.triplea.io.ContentDownloader;
import org.triplea.yaml.YamlReader;

@Slf4j
@Builder
public class MapNameReader implements Function<MapRepoListing, Optional<String>> {
/* Function to download content as a string and log an info message if not found. */
private static final HttpClient HTTP_CLIENT =
HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();

/* Function to download content as a string, returns null if the download fails or returns a non-200 status. */
@Setter(value = AccessLevel.PACKAGE, onMethod_ = @VisibleForTesting)
@Builder.Default
private Function<URI, String> downloadFunction =
uri -> ContentDownloader.downloadAsString(uri).orElse(null);
uri -> {
try {
HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
HttpResponse<String> response =
HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode() == 200 ? response.body() : null;
} catch (IOException e) {
return null;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
};

public static URI computeMapYamlLocation(MapRepoListing mapRepoListing) {
return URI.create(mapRepoListing.getUri().toString() + "/blob/master/map.yml?raw=true");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/triplea/maps/listing/MapListingDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import lombok.AllArgsConstructor;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.mapper.reflect.BeanMapper;
import org.triplea.http.client.maps.listing.MapDownloadItem;
import org.triplea.http.client.maps.listing.MapTag;
import org.triplea.http.client.lobby.maps.listing.MapDownloadItem;
import org.triplea.http.client.lobby.maps.listing.MapTag;

@AllArgsConstructor
public class MapListingDao {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/triplea/maps/listing/MapListingRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lombok.Getter;
import lombok.Setter;
import org.jdbi.v3.core.mapper.reflect.JdbiConstructor;
import org.triplea.http.client.maps.listing.MapDownloadItem;
import org.triplea.http.client.maps.listing.MapTag;
import org.triplea.http.client.lobby.maps.listing.MapDownloadItem;
import org.triplea.http.client.lobby.maps.listing.MapTag;

@Setter
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import org.jdbi.v3.core.Jdbi;
import org.triplea.http.client.maps.listing.MapDownloadItem;
import org.triplea.http.client.lobby.maps.listing.MapDownloadItem;

@AllArgsConstructor
public class MapsListingModule implements Supplier<List<MapDownloadItem>> {
Expand Down
Loading
Loading