|
| 1 | +package org.hyperskill.hstest.checker; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.reflect.TypeToken; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.lang.reflect.Type; |
| 8 | +import java.net.HttpURLConnection; |
| 9 | +import java.net.SocketTimeoutException; |
| 10 | +import java.net.URL; |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +/** |
| 16 | +* Checks if the current version of the library is the latest one on GitHub releases page of the library. |
| 17 | +* If not, throws an exception. |
| 18 | + */ |
| 19 | +public class CheckLibraryVersion { |
| 20 | + |
| 21 | + private String VERSION_FILE = "src/main/java/org/hyperskill/hstest/resources/version.txt"; |
| 22 | + private String LAST_CHECKED_FILE = "lastCheckedHSTestLibrary.txt"; |
| 23 | + private String GITHUB_API = "https://api.github.com/repos/hyperskill/hs-test/releases/latest"; |
| 24 | + private String currentVersion; |
| 25 | + private String latestVersion; |
| 26 | + public boolean isLatestVersion = true; |
| 27 | + |
| 28 | + public CheckLibraryVersion() { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Checks if the current version of the library is the latest one on GitHub releases page of the library. |
| 33 | + * If not, throws an exception. |
| 34 | + */ |
| 35 | + public void checkVersion() throws IOException { |
| 36 | + LocalDate lastChecked = null; |
| 37 | + String tempDirectoryPath = System.getProperty("java.io.tmpdir"); |
| 38 | + File lastCheckedFile = new File(tempDirectoryPath + File.separator + LAST_CHECKED_FILE); |
| 39 | + if (lastCheckedFile.exists()) { |
| 40 | + try (BufferedReader reader = new BufferedReader(new FileReader(lastCheckedFile))) { |
| 41 | + lastChecked = LocalDate.parse(reader.readLine()); |
| 42 | + } |
| 43 | + } |
| 44 | + if (LocalDate.now().equals(lastChecked)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(VERSION_FILE); |
| 48 | + if (inputStream != null) { |
| 49 | + currentVersion = new BufferedReader(new InputStreamReader(inputStream)).readLine(); |
| 50 | + } else return; |
| 51 | + latestVersion = getLatestHsTestVersionFromGitHub(); |
| 52 | + if (!currentVersion.equals(latestVersion)) { |
| 53 | + isLatestVersion = false; |
| 54 | + } |
| 55 | + lastChecked = LocalDate.now(); |
| 56 | + try (FileWriter writer = new FileWriter(lastCheckedFile)) { |
| 57 | + writer.write(lastChecked.toString()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns latest version of the library from GitHub releases page of the library. |
| 63 | + * @return String latest version of the library |
| 64 | + */ |
| 65 | + private String getLatestHsTestVersionFromGitHub() { |
| 66 | + HttpURLConnection connection = null; |
| 67 | + int responseCode = -1; |
| 68 | + try { |
| 69 | + URL url = new URL(GITHUB_API); |
| 70 | + connection = (HttpURLConnection) url.openConnection(); |
| 71 | + connection.setRequestMethod("GET"); |
| 72 | + connection.setRequestProperty("Accept", "application/vnd.github+json"); |
| 73 | + connection.setConnectTimeout(100); |
| 74 | + connection.setReadTimeout(500); |
| 75 | + responseCode = connection.getResponseCode(); |
| 76 | + } catch (IOException e) { |
| 77 | + return currentVersion; |
| 78 | + } |
| 79 | + if (responseCode != 200) { |
| 80 | + return currentVersion; |
| 81 | + } |
| 82 | + |
| 83 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| 84 | + String response = in.lines().collect(Collectors.joining()); |
| 85 | + Gson gson = new Gson(); |
| 86 | + Type type = new TypeToken<Map<String, Object>>(){}.getType(); |
| 87 | + Map<String,Object> map = gson.fromJson(response, type); |
| 88 | + return map.get("tag_name").toString().replace("v", ""); |
| 89 | + } catch (IOException e) { |
| 90 | + return currentVersion; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns feedback for the user if the current version of the library is not the latest one. |
| 96 | + * @return String feedback for the user |
| 97 | + */ |
| 98 | + public String getFeedback() { |
| 99 | + return "\nThe installed hs-test version (" + currentVersion + ") is not the latest version (" + latestVersion + "). " + |
| 100 | + "Update the library by following the instructions below:\n\n" + |
| 101 | + "1. Open your project's dependency file build.gradle.\n" + |
| 102 | + "2. Find the hs-test dependency and change its version to the latest one (" + latestVersion + ").\n" + |
| 103 | + "3. Sync the dependencies in your development environment or run the following commands in the terminal:\n" + |
| 104 | + " For Gradle:\n" + |
| 105 | + " gradle clean build --refresh-dependencies\n\n" + |
| 106 | + "4. Restart the tests.\n\n"; |
| 107 | + } |
| 108 | +} |
0 commit comments