Skip to content

Commit

Permalink
🔖 prepare release v1.0.1
Browse files Browse the repository at this point in the history
And some formatting and deprecations
  • Loading branch information
Lezurex committed Dec 1, 2024
1 parent 461a4a5 commit 81108de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.lezurex'
version '1.0.0'
version '1.0.1'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import static java.net.HttpURLConnection.HTTP_OK;
Expand All @@ -33,20 +35,23 @@ public class GithubVersionChecker {
* @param currentVersion The current version running
* @param includePreReleases Whether pre releases should be tested (default: false)
*/
public GithubVersionChecker(String username, String repo, ReleaseVersion currentVersion, boolean includePreReleases) {
public GithubVersionChecker(String username, String repo, ReleaseVersion currentVersion,
boolean includePreReleases) {
this.username = username;
this.repo = repo;
this.currentVersion = currentVersion;
this.includePreReleases = includePreReleases;

try {
URL url = new URL(String.format("https://api.github.com/repos/%s/%s", this.username, this.repo));
URL url = new URI(
String.format("https://api.github.com/repos/%s/%s", this.username, this.repo))
.toURL();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() != HTTP_OK) {
throw new RepoNotFoundException(this.username, this.repo);
}
} catch (IOException e) {
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
Expand All @@ -65,24 +70,27 @@ public CheckResult check() {
if (this.includePreReleases)
queryURL = "https://api.github.com/repos/%s/%s/releases?per_page=1";
try {
URL url = new URL(String.format(queryURL, this.username, this.repo));
URL url = new URI(String.format(queryURL, this.username, this.repo)).toURL();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() != HTTP_OK) throw new NoReleaseFoundException(this.username, this.repo);
if (con.getResponseCode() != HTTP_OK)
throw new NoReleaseFoundException(this.username, this.repo);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

JsonObject releaseData;
if (this.includePreReleases) {
JsonArray jsonArray = JsonParser.parseReader(in).getAsJsonArray();
if (jsonArray.size() == 0) throw new NoReleaseFoundException(this.username, this.repo);
if (jsonArray.size() == 0)
throw new NoReleaseFoundException(this.username, this.repo);
releaseData = jsonArray.get(0).getAsJsonObject();
} else
releaseData = JsonParser.parseReader(in).getAsJsonObject();
in.close();
con.disconnect();

ReleaseVersion githubVersion = new ReleaseVersion(releaseData.get("tag_name").getAsString());
ReleaseVersion githubVersion =
new ReleaseVersion(releaseData.get("tag_name").getAsString());
String pageLink = releaseData.get("html_url").getAsString();
switch (this.currentVersion.compareTo(githubVersion)) {
case -1:
Expand All @@ -95,7 +103,7 @@ public CheckResult check() {
return null;
}

} catch (IOException e) {
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@ class GithubVersionCheckerTest {
void setupValidRepo() {
ReleaseVersion releaseVersion = new ReleaseVersion("0.1.1");
assertDoesNotThrow(() -> {
GithubVersionChecker versionChecker = new GithubVersionChecker("Lezurex", "github-version-checker", releaseVersion);
new GithubVersionChecker("Lezurex", "github-version-checker", releaseVersion);
}, "Repo validation failed!");
}

@Test
@DisplayName("Setup with invalid repo")
void setupInvalidRepo() {
ReleaseVersion releaseVersion = new ReleaseVersion("0.1.1");
assertThrows(RepoNotFoundException.class, () -> new GithubVersionChecker("Lezurex", "doesnotexist", releaseVersion), "Repo validation di not fail!");
assertThrows(RepoNotFoundException.class,
() -> new GithubVersionChecker("Lezurex", "doesnotexist", releaseVersion),
"Repo validation di not fail!");
}

@Test
@DisplayName("Check with outdated version")
void checkOutdated() {
ReleaseVersion releaseVersion = new ReleaseVersion("0.0.1");
GithubVersionChecker githubVersionChecker = new GithubVersionChecker("VoxCrafterLP", "JumpRace", releaseVersion);
GithubVersionChecker githubVersionChecker =
new GithubVersionChecker("VoxCrafterLP", "JumpRace", releaseVersion);
CheckResult checkResult = githubVersionChecker.check();
assertEquals(VersionState.OUTDATED, checkResult.getVersionState(), "Version state is not outdated!");
assertTrue(checkResult.getPageLink().endsWith(checkResult.getVersion().toString()), "End of release URL does not match version!");
assertEquals(VersionState.OUTDATED, checkResult.getVersionState(),
"Version state is not outdated!");
assertTrue(checkResult.getPageLink().endsWith(checkResult.getVersion().toString()),
"End of release URL does not match version!");
}

}
}

0 comments on commit 81108de

Please sign in to comment.