|
16 | 16 |
|
17 | 17 | package com.github.introfog.gitwave.model;
|
18 | 18 |
|
| 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 | + |
19 | 30 | 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 | + |
20 | 34 | private UpdateChecker() {
|
21 | 35 | // Do nothing
|
22 | 36 | }
|
23 | 37 |
|
24 | 38 | 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(); |
26 | 88 | }
|
27 | 89 | }
|
0 commit comments