Skip to content

Commit 851a582

Browse files
committed
Add version check
Add CheckResult
1 parent 0c915cd commit 851a582

4 files changed

Lines changed: 101 additions & 4 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lezurex.githubversionchecker;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class CheckResult {
7+
8+
private ReleaseVersion version;
9+
private String pageLink;
10+
private VersionState versionState;
11+
12+
public CheckResult(ReleaseVersion version, String pageLink, VersionState versionState) {
13+
this.version = version;
14+
this.pageLink = pageLink;
15+
this.versionState = versionState;
16+
}
17+
}

src/main/java/com/lezurex/githubversionchecker/GithubVersionChecker.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.lezurex.githubversionchecker;
22

3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
import com.lezurex.githubversionchecker.exceptions.NoReleaseFoundException;
37
import com.lezurex.githubversionchecker.exceptions.RepoNotFoundException;
48

59
import javax.net.ssl.HttpsURLConnection;
10+
import java.io.BufferedReader;
611
import java.io.IOException;
7-
import java.net.HttpURLConnection;
12+
import java.io.InputStreamReader;
813
import java.net.URL;
914

15+
import static java.net.HttpURLConnection.HTTP_OK;
16+
1017
public class GithubVersionChecker {
1118

1219
/**
@@ -18,26 +25,79 @@ public class GithubVersionChecker {
1825
*/
1926
private final String repo;
2027
private final ReleaseVersion currentVersion;
28+
private final boolean includePreReleases;
2129

2230
/**
2331
* @param username Username where the targeted repo is located
2432
* @param repo Name of the repository on GitHub
2533
* @param currentVersion The current version running
34+
* @param includePreReleases Whether pre releases should be tested (default: false)
2635
*/
27-
public GithubVersionChecker(String username, String repo, ReleaseVersion currentVersion) {
36+
public GithubVersionChecker(String username, String repo, ReleaseVersion currentVersion, boolean includePreReleases) {
2837
this.username = username;
2938
this.repo = repo;
3039
this.currentVersion = currentVersion;
40+
this.includePreReleases = includePreReleases;
3141

3242
try {
3343
URL url = new URL(String.format("https://api.github.com/repos/%s/%s", this.username, this.repo));
3444
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
3545
con.setRequestMethod("GET");
36-
if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
46+
if (con.getResponseCode() != HTTP_OK) {
3747
throw new RepoNotFoundException(this.username, this.repo);
3848
}
3949
} catch (IOException e) {
4050
e.printStackTrace();
4151
}
4252
}
53+
54+
/**
55+
* @param username Username where the targeted repo is located
56+
* @param repo Name of the repository on GitHub
57+
* @param currentVersion The current version running
58+
*/
59+
public GithubVersionChecker(String username, String repo, ReleaseVersion currentVersion) {
60+
this(username, repo, currentVersion, false);
61+
}
62+
63+
public CheckResult check() {
64+
String queryURL = "https://api.github.com/repos/%s/%s/releases/latest";
65+
if (this.includePreReleases)
66+
queryURL = "https://api.github.com/repos/%s/%s/releases?per_page=1";
67+
try {
68+
URL url = new URL(String.format(queryURL, this.username, this.repo));
69+
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
70+
con.setRequestMethod("GET");
71+
if (con.getResponseCode() != HTTP_OK) throw new NoReleaseFoundException(this.username, this.repo);
72+
73+
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
74+
75+
JsonObject releaseData;
76+
if (this.includePreReleases) {
77+
JsonArray jsonArray = JsonParser.parseReader(in).getAsJsonArray();
78+
if (jsonArray.size() == 0) throw new NoReleaseFoundException(this.username, this.repo);
79+
releaseData = jsonArray.get(0).getAsJsonObject();
80+
} else
81+
releaseData = JsonParser.parseReader(in).getAsJsonObject();
82+
in.close();
83+
con.disconnect();
84+
85+
ReleaseVersion githubVersion = new ReleaseVersion(releaseData.get("tag_name").getAsString());
86+
String pageLink = releaseData.get("html_url").getAsString();
87+
switch (this.currentVersion.compareTo(githubVersion)) {
88+
case -1:
89+
return new CheckResult(githubVersion, pageLink, VersionState.OUTDATED);
90+
case 0:
91+
return new CheckResult(githubVersion, pageLink, VersionState.UP_TO_DATE);
92+
case 1:
93+
return new CheckResult(githubVersion, pageLink, VersionState.NEWER);
94+
default:
95+
return null;
96+
}
97+
98+
} catch (IOException e) {
99+
e.printStackTrace();
100+
}
101+
return null;
102+
}
43103
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.lezurex.githubversionchecker.exceptions;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class NoReleaseFoundException extends RuntimeException {
7+
8+
private final String username;
9+
private final String repo;
10+
11+
public NoReleaseFoundException(String username, String repo) {
12+
this.username = username;
13+
this.repo = repo;
14+
}
15+
16+
@Override
17+
public String getMessage() {
18+
return String.format("Couldn't find an existing release in repo \"%s/%s\"! If you are the maintainer of this application, please create a release!", username, repo);
19+
}
20+
}

src/main/java/com/lezurex/githubversionchecker/exceptions/RepoNotFoundException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public RepoNotFoundException(String username, String repo) {
1515

1616
@Override
1717
public String getMessage() {
18-
return String.format("Repo %s/%s couldn't be found on GitHub! Does it exist and", username, repo);
18+
return String.format("Repo \"%s/%s\" couldn't be found on GitHub! Does it exist and is it public?", username, repo);
1919
}
2020
}

0 commit comments

Comments
 (0)