-
-
Notifications
You must be signed in to change notification settings - Fork 0
GH-48 Introduce simple update checker, using Modrinth api. #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vLuckyyy
wants to merge
5
commits into
master
Choose a base branch
from
add-updater-by-modrinth-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−0
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugins { | ||
`commons-java-17` | ||
} | ||
|
||
dependencies { | ||
implementation(project(":eternalcode-commons-updater")) | ||
} |
17 changes: 17 additions & 0 deletions
17
...updater-example/src/main/java/com/eternalcode/commons/updater/example/ExampleChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.eternalcode.commons.updater.example; | ||
|
||
import com.eternalcode.commons.updater.UpdateResult; | ||
|
||
public class ExampleChecker { | ||
public static void main(String[] args) { | ||
ExampleUpdateService service = new ExampleUpdateService(); | ||
|
||
UpdateResult modrinthResult = service.checkModrinth("EternalCombat", "2.2.0"); | ||
System.out.println("Modrinth update available: " + modrinthResult.isUpdateAvailable()); | ||
if (modrinthResult.isUpdateAvailable()) { | ||
System.out.println("Latest: " + modrinthResult.latestVersion()); | ||
System.out.println("Download: " + modrinthResult.downloadUrl()); | ||
System.out.println("Release page: " + modrinthResult.releaseUrl()); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...r-example/src/main/java/com/eternalcode/commons/updater/example/ExampleUpdateService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.eternalcode.commons.updater.example; | ||
|
||
import com.eternalcode.commons.updater.UpdateResult; | ||
import com.eternalcode.commons.updater.Version; | ||
import com.eternalcode.commons.updater.impl.ModrinthUpdateChecker; | ||
|
||
public final class ExampleUpdateService { | ||
private final ModrinthUpdateChecker modrinthChecker = new ModrinthUpdateChecker(); | ||
|
||
public UpdateResult checkModrinth(String projectId, String currentVersion) { | ||
return modrinthChecker.check(projectId, new Version(currentVersion)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
plugins { | ||
`commons-java-17` | ||
`commons-publish` | ||
`commons-repositories` | ||
`commons-java-unit-test` | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
6 changes: 6 additions & 0 deletions
6
eternalcode-commons-updater/src/main/java/com/eternalcode/commons/updater/UpdateChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.eternalcode.commons.updater; | ||
|
||
public interface UpdateChecker { | ||
|
||
UpdateResult check(String projectId, Version currentVersion); | ||
} |
8 changes: 8 additions & 0 deletions
8
eternalcode-commons-updater/src/main/java/com/eternalcode/commons/updater/UpdateResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.eternalcode.commons.updater; | ||
|
||
public record UpdateResult(Version currentVersion, Version latestVersion, String downloadUrl, String releaseUrl) { | ||
|
||
public boolean isUpdateAvailable() { | ||
return this.latestVersion.isNewerThan(this.currentVersion); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
eternalcode-commons-updater/src/main/java/com/eternalcode/commons/updater/Version.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.eternalcode.commons.updater; | ||
|
||
public class Version implements Comparable<Version> { | ||
|
||
private final String value; | ||
|
||
public Version(String version) { | ||
this.value = version.trim(); | ||
} | ||
|
||
@Override | ||
public int compareTo(Version version) { | ||
return this.value.compareTo(version.value); | ||
} | ||
|
||
public boolean isNewerThan(Version version) { | ||
return this.value.compareTo(version.value) > 0; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
return object instanceof Version other && value.equals(other.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.value; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...ons-updater/src/main/java/com/eternalcode/commons/updater/impl/ModrinthUpdateChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.eternalcode.commons.updater.impl; | ||
|
||
import com.eternalcode.commons.updater.UpdateChecker; | ||
import com.eternalcode.commons.updater.UpdateResult; | ||
import com.eternalcode.commons.updater.Version; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
|
||
public final class ModrinthUpdateChecker implements UpdateChecker { | ||
|
||
private static final String API_BASE_URL = "https://api.modrinth.com/v2"; | ||
private static final String MODRINTH_BASE_URL = "https://modrinth.com/mod"; | ||
private static final String USER_AGENT = "UpdateChecker/1.0"; | ||
private static final Duration TIMEOUT = Duration.ofSeconds(10); | ||
|
||
private final HttpClient client; | ||
|
||
public ModrinthUpdateChecker() { | ||
this.client = HttpClient.newBuilder().connectTimeout(TIMEOUT).build(); | ||
} | ||
|
||
@Override | ||
public UpdateResult check(String projectId, Version currentVersion) { | ||
if (projectId == null || projectId.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Project ID cannot be null or empty"); | ||
} | ||
|
||
try { | ||
String url = API_BASE_URL + "/project/" + projectId + "/version"; | ||
|
||
HttpRequest request = | ||
HttpRequest.newBuilder().uri(URI.create(url)).header("User-Agent", USER_AGENT).timeout(TIMEOUT).build(); | ||
|
||
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
if (response.statusCode() != 200) { | ||
return createEmptyResult(currentVersion); | ||
} | ||
|
||
String json = response.body(); | ||
if (json == null || json.trim().isEmpty()) { | ||
return createEmptyResult(currentVersion); | ||
} | ||
|
||
Optional<String> versionNumber = extractJsonValue(json, "version_number"); | ||
Optional<String> downloadUrl = extractJsonValue(json, "url"); | ||
|
||
if (versionNumber.isEmpty()) { | ||
return createEmptyResult(currentVersion); | ||
} | ||
|
||
String releaseUrl = MODRINTH_BASE_URL + "/" + projectId + "/version/" + versionNumber.get(); | ||
Version latestVersion = new Version(versionNumber.get()); | ||
|
||
return new UpdateResult(currentVersion, latestVersion, downloadUrl.orElse(null), releaseUrl); | ||
} | ||
catch (Exception exception) { | ||
throw new RuntimeException("Failed to check Modrinth updates for project: " + projectId, exception); | ||
} | ||
vLuckyyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private UpdateResult createEmptyResult(Version currentVersion) { | ||
return new UpdateResult(currentVersion, currentVersion, null, null); | ||
} | ||
|
||
private Optional<String> extractJsonValue(String json, String key) { | ||
if (json == null || key == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
String pattern = "\"" + key + "\":\""; | ||
int start = json.indexOf(pattern); | ||
|
||
if (start == -1) { | ||
return Optional.empty(); | ||
} | ||
|
||
start += pattern.length(); | ||
int end = json.indexOf("\"", start); | ||
|
||
if (end == -1) { | ||
return Optional.empty(); | ||
} | ||
|
||
String value = json.substring(start, end); | ||
return value.isEmpty() ? Optional.empty() : Optional.of(value); | ||
} | ||
vLuckyyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.