11package 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 ;
37import com .lezurex .githubversionchecker .exceptions .RepoNotFoundException ;
48
59import javax .net .ssl .HttpsURLConnection ;
10+ import java .io .BufferedReader ;
611import java .io .IOException ;
7- import java .net . HttpURLConnection ;
12+ import java .io . InputStreamReader ;
813import java .net .URL ;
914
15+ import static java .net .HttpURLConnection .HTTP_OK ;
16+
1017public 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}
0 commit comments