-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove paper support; add github module
- Loading branch information
Showing
10 changed files
with
266 additions
and
192 deletions.
There are no files selected for viewing
This file contains 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
88 changes: 88 additions & 0 deletions
88
commons-github/src/main/java/space/bxteam/commons/github/GitCheck.java
This file contains 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,88 @@ | ||
package space.bxteam.commons.github; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.URI; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.Duration; | ||
|
||
/** | ||
* A utility class to check for the latest release of a GitHub repository using the GitHub API. | ||
* | ||
* @see GitCheckResult | ||
* @see GitRelease | ||
* @see GitRepository | ||
* @see GitTag | ||
* @author NONPLAYT | ||
*/ | ||
public class GitCheck { | ||
private static final String GITHUB_API_URL = "https://api.github.com/repos"; | ||
private static final Duration CACHE_DURATION = Duration.ofHours(1); | ||
|
||
private GitCheckResult cachedResult; | ||
private Instant cacheTimestamp; | ||
|
||
/** | ||
* Checks the latest release of the given GitHub repository and compares it with the current tag. | ||
* If the result is cached and still valid, returns the cached result. | ||
* | ||
* @param repository the GitHub repository to check | ||
* @param currentTag the current version tag to compare against | ||
* @return a GitCheckResult indicating if the current version is up-to-date and the latest release details | ||
*/ | ||
public GitCheckResult checkRelease(GitRepository repository, GitTag currentTag) { | ||
if (isCacheValid()) { | ||
return cachedResult; | ||
} | ||
|
||
try { | ||
HttpClient client = HttpClient.newHttpClient(); | ||
String url = GITHUB_API_URL + "/" + repository.getOwner() + "/" + repository.getProject() + "/releases"; | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(url)) | ||
.build(); | ||
|
||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
if (response.statusCode() == 200) { | ||
JsonParser parser = new JsonParser(); | ||
JsonArray releases = parser.parse(response.body()).getAsJsonArray(); | ||
if (releases.size() > 0) { | ||
JsonObject latestRelease = releases.get(0).getAsJsonObject(); | ||
String latestTag = latestRelease.get("tag_name").getAsString(); | ||
String pageUrl = latestRelease.get("html_url").getAsString(); | ||
String publishedAt = latestRelease.get("published_at").getAsString(); | ||
|
||
boolean isUpToDate = latestTag.equals(currentTag.getTag()); | ||
GitRelease release = isUpToDate ? null : new GitRelease(GitTag.of(latestTag), pageUrl, publishedAt); | ||
|
||
cachedResult = new GitCheckResult(isUpToDate, release); | ||
cacheTimestamp = Instant.now(); | ||
|
||
return cachedResult; | ||
} | ||
} | ||
} catch (IOException | InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
cachedResult = new GitCheckResult(true, null); | ||
cacheTimestamp = Instant.now(); | ||
return cachedResult; | ||
} | ||
|
||
/** | ||
* Checks if the cached result is still valid based on the defined cache duration. | ||
* | ||
* @return true if the cached result is still valid, false otherwise | ||
*/ | ||
private boolean isCacheValid() { | ||
return cachedResult != null && cacheTimestamp != null && Duration.between(cacheTimestamp, Instant.now()).compareTo(CACHE_DURATION) <= 0; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
commons-github/src/main/java/space/bxteam/commons/github/GitCheckResult.java
This file contains 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,39 @@ | ||
package space.bxteam.commons.github; | ||
|
||
/** | ||
* Represents the result of a release check, indicating whether the current version | ||
* is up-to-date and providing information about the latest release if it is not. | ||
*/ | ||
public class GitCheckResult { | ||
private final boolean upToDate; | ||
private final GitRelease latestRelease; | ||
|
||
/** | ||
* Constructs a GitCheckResult instance. | ||
* | ||
* @param upToDate whether the current version is up-to-date | ||
* @param latestRelease the latest release information, or null if up-to-date | ||
*/ | ||
public GitCheckResult(boolean upToDate, GitRelease latestRelease) { | ||
this.upToDate = upToDate; | ||
this.latestRelease = latestRelease; | ||
} | ||
|
||
/** | ||
* Checks if the current version is up-to-date. | ||
* | ||
* @return true if the current version is up-to-date, false otherwise | ||
*/ | ||
public boolean isUpToDate() { | ||
return upToDate; | ||
} | ||
|
||
/** | ||
* Gets the latest release information if the current version is not up-to-date. | ||
* | ||
* @return the latest GitRelease, or null if up-to-date | ||
*/ | ||
public GitRelease getLatestRelease() { | ||
return latestRelease; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
commons-github/src/main/java/space/bxteam/commons/github/GitRelease.java
This file contains 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,51 @@ | ||
package space.bxteam.commons.github; | ||
|
||
/** | ||
* Represents a GitHub release, containing information about the release tag, | ||
* URL of the release page, and the release date. | ||
*/ | ||
public class GitRelease { | ||
private final GitTag tag; | ||
private final String pageUrl; | ||
private final String publishedAt; | ||
|
||
/** | ||
* Constructs a GitRelease instance. | ||
* | ||
* @param tag the tag associated with this release | ||
* @param pageUrl the URL of the release page | ||
* @param publishedAt the date the release was published | ||
*/ | ||
public GitRelease(GitTag tag, String pageUrl, String publishedAt) { | ||
this.tag = tag; | ||
this.pageUrl = pageUrl; | ||
this.publishedAt = publishedAt; | ||
} | ||
|
||
/** | ||
* Gets the tag associated with this release. | ||
* | ||
* @return the GitTag associated with this release | ||
*/ | ||
public GitTag getTag() { | ||
return tag; | ||
} | ||
|
||
/** | ||
* Gets the URL of the release page. | ||
* | ||
* @return the URL of the release page | ||
*/ | ||
public String getPageUrl() { | ||
return pageUrl; | ||
} | ||
|
||
/** | ||
* Gets the date when the release was published. | ||
* | ||
* @return the date the release was published | ||
*/ | ||
public String getPublishedAt() { | ||
return publishedAt; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
commons-github/src/main/java/space/bxteam/commons/github/GitRepository.java
This file contains 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,49 @@ | ||
package space.bxteam.commons.github; | ||
|
||
/** | ||
* Represents a GitHub repository identified by its owner and project name. | ||
*/ | ||
public class GitRepository { | ||
private final String owner; | ||
private final String project; | ||
|
||
/** | ||
* Private constructor to create a GitRepository instance. | ||
* | ||
* @param owner the owner of the GitHub repository | ||
* @param project the name of the GitHub project | ||
*/ | ||
private GitRepository(String owner, String project) { | ||
this.owner = owner; | ||
this.project = project; | ||
} | ||
|
||
/** | ||
* Creates a new instance of GitRepository. | ||
* | ||
* @param owner the owner of the GitHub repository | ||
* @param project the name of the GitHub project | ||
* @return a new GitRepository instance | ||
*/ | ||
public static GitRepository of(String owner, String project) { | ||
return new GitRepository(owner, project); | ||
} | ||
|
||
/** | ||
* Gets the owner of the repository. | ||
* | ||
* @return the owner of the repository | ||
*/ | ||
public String getOwner() { | ||
return owner; | ||
} | ||
|
||
/** | ||
* Gets the name of the repository project. | ||
* | ||
* @return the name of the repository project | ||
*/ | ||
public String getProject() { | ||
return project; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
commons-github/src/main/java/space/bxteam/commons/github/GitTag.java
This file contains 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,36 @@ | ||
package space.bxteam.commons.github; | ||
|
||
/** | ||
* Represents a Git tag, typically used to mark a specific release. | ||
*/ | ||
public class GitTag { | ||
private final String tag; | ||
|
||
/** | ||
* Private constructor to create a GitTag instance. | ||
* | ||
* @param tag the name of the tag | ||
*/ | ||
private GitTag(String tag) { | ||
this.tag = tag; | ||
} | ||
|
||
/** | ||
* Creates a new instance of GitTag. | ||
* | ||
* @param tag the name of the tag | ||
* @return a new GitTag instance | ||
*/ | ||
public static GitTag of(String tag) { | ||
return new GitTag(tag); | ||
} | ||
|
||
/** | ||
* Gets the name of the tag. | ||
* | ||
* @return the name of the tag | ||
*/ | ||
public String getTag() { | ||
return tag; | ||
} | ||
} |
Oops, something went wrong.