Skip to content

Commit 590bd19

Browse files
committed
Final part of check update feature: backend part
1 parent 80bd5d0 commit 590bd19

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/main/java/com/github/introfog/gitwave/model/AppConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public final class AppConstants {
2121
public static final String LINK_TO_GIT_CONTRIBUTING_FILE = "https://github.com/introfog/GitWave/blob/master/CONTRIBUTING.md";
2222
public static final String LINK_TO_GIT_REPO = "https://github.com/introfog/GitWave";
2323
public static final String LINK_TO_GIT_RELEASES = "https://github.com/introfog/GitWave/releases";
24+
public static final String GIT_HUB_REPO_OWNER = "introfog";
25+
public static final String APP_NAME = "GitWave";
2426
public static final String PATH_TO_LOGO = "/logo.png";
2527
// TODO make pretty logo
2628

src/main/java/com/github/introfog/gitwave/model/UpdateChecker.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,74 @@
1616

1717
package com.github.introfog.gitwave.model;
1818

19+
import java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.net.HttpURLConnection;
24+
import java.net.URL;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
1930
public final class UpdateChecker {
31+
private static final Pattern TAG_NAME_PATTERN = Pattern.compile("\"name\":\\s?\"([^\"]+)\"");
32+
private static final Logger LOGGER = LoggerFactory.getLogger(UpdateChecker.class);
33+
2034
private UpdateChecker() {
2135
// Do nothing
2236
}
2337

2438
public static boolean isNewReleaseAvailable() {
25-
return true;
39+
// TODO MINOR fetch and check update in the separate thread, to quicker open app.
40+
final String latestTag = fetchLatestTag(AppConstants.GIT_HUB_REPO_OWNER, AppConstants.APP_NAME);
41+
LOGGER.info("Fetch '{}' tag from GitHub repo.", latestTag);
42+
return latestTag != null && !AppConstants.VERSION.equals(latestTag);
43+
}
44+
45+
public static String fetchLatestTag(String owner, String repo) {
46+
final String apiUrl = "https://api.github.com/repos/" + owner + "/" + repo + "/tags";
47+
48+
try {
49+
HttpURLConnection connection = (HttpURLConnection) new URL(apiUrl).openConnection();
50+
connection.setConnectTimeout(5000);
51+
connection.setReadTimeout(5000);
52+
connection.setRequestMethod("GET");
53+
connection.setRequestProperty("Accept", "application/vnd.github.v3+json");
54+
connection.setRequestProperty("User-Agent", "GitWave/" + AppConstants.VERSION + " (Java; en-US)");
55+
connection.connect();
56+
57+
if (connection.getResponseCode() != 200) {
58+
LOGGER.warn("Failed to fetch tags from GitHub API: code='{}', body='{}'",
59+
connection.getResponseCode(), connection.getResponseMessage());
60+
return null;
61+
}
62+
63+
String responseStr = inputStreamToString(connection.getInputStream());
64+
connection.disconnect();
65+
// Use here regex on purpose, to not increase size of the app by adding huge library for one request.
66+
Matcher matcher = TAG_NAME_PATTERN.matcher(responseStr);
67+
68+
if (matcher.find()) {
69+
return matcher.group(1);
70+
}
71+
return null;
72+
73+
} catch (Exception e) {
74+
LOGGER.warn("Error fetching latest version from GitHub", e);
75+
return null;
76+
}
77+
}
78+
79+
private static String inputStreamToString(InputStream inputStream) throws IOException {
80+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
81+
StringBuilder result = new StringBuilder();
82+
String line;
83+
while ((line = reader.readLine()) != null) {
84+
result.append(line);
85+
}
86+
reader.close();
87+
return result.toString();
2688
}
2789
}

0 commit comments

Comments
 (0)