|
15 | 15 | import java.io.IOException; |
16 | 16 |
|
17 | 17 | import com.github.zafarkhaja.semver.Version; |
18 | | -import com.google.gson.JsonElement; |
19 | 18 | import com.google.gson.JsonObject; |
20 | 19 | import com.google.gson.JsonParser; |
21 | 20 | import okhttp3.*; |
22 | 21 |
|
23 | 22 | import org.web3j.console.utils.CliVersion; |
24 | | -import org.web3j.console.utils.OSUtils; |
25 | 23 |
|
26 | 24 | import static org.web3j.console.config.ConfigManager.config; |
27 | 25 |
|
28 | 26 | public class Updater { |
29 | | - private static final String DEFAULT_UPDATE_URL = |
30 | | - "https://internal.services.web3labs.com/api/epirus/versions/latest"; |
| 27 | + private static final String GITHUB_API_URL = |
| 28 | + "https://api.github.com/repos/hyperledger/web3j-cli/releases/latest"; |
31 | 29 |
|
32 | 30 | public static void promptIfUpdateAvailable() throws IOException { |
33 | | - String version = CliVersion.getVersion(); |
34 | | - if (config.getLatestVersion() != null |
35 | | - && Version.valueOf(config.getLatestVersion()).greaterThan(Version.valueOf(version)) |
| 31 | + String version = CliVersion.getVersion(); // Get current version from CLI |
| 32 | + String latestVersion = getLatestVersionFromGitHub(); // Fetch latest version from GitHub |
| 33 | + |
| 34 | + if (latestVersion != null |
| 35 | + && Version.valueOf(latestVersion).greaterThan(Version.valueOf(version)) |
36 | 36 | && !version.contains("SNAPSHOT")) { |
37 | 37 | System.out.println( |
38 | 38 | String.format( |
39 | 39 | "Your current Web3j version is: " |
40 | 40 | + version |
41 | 41 | + ". The latest Version is: " |
42 | | - + config.getLatestVersion() |
| 42 | + + latestVersion |
43 | 43 | + ". To update, run: %s", |
44 | 44 | config.getUpdatePrompt())); |
45 | 45 | } |
46 | 46 | } |
47 | 47 |
|
48 | 48 | public static void onlineUpdateCheck() { |
49 | | - onlineUpdateCheck(DEFAULT_UPDATE_URL); |
| 49 | + try { |
| 50 | + promptIfUpdateAvailable(); |
| 51 | + } catch (IOException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
50 | 54 | } |
51 | 55 |
|
52 | | - public static void onlineUpdateCheck(String updateUrl) { |
| 56 | + public static String getLatestVersionFromGitHub() { |
53 | 57 | OkHttpClient client = new OkHttpClient(); |
| 58 | + Request request = new Request.Builder().url(GITHUB_API_URL).get().build(); |
54 | 59 |
|
55 | | - RequestBody updateBody = |
56 | | - new MultipartBody.Builder() |
57 | | - .setType(MultipartBody.FORM) |
58 | | - .addFormDataPart("os", OSUtils.determineOS().toString()) |
59 | | - .addFormDataPart("clientId", config.getClientId()) |
60 | | - .addFormDataPart("data", "update_check") |
61 | | - .build(); |
62 | | - |
63 | | - Request updateCheckRequest = new Request.Builder().url(updateUrl).post(updateBody).build(); |
64 | | - |
65 | | - try { |
66 | | - Response sendRawResponse = client.newCall(updateCheckRequest).execute(); |
67 | | - JsonElement element; |
68 | | - ResponseBody body; |
69 | | - if (sendRawResponse.code() == 200 |
70 | | - && (body = sendRawResponse.body()) != null |
71 | | - && (element = JsonParser.parseString(body.string())) != null |
72 | | - && element.isJsonObject()) { |
73 | | - JsonObject rootObj = element.getAsJsonObject().get("latest").getAsJsonObject(); |
74 | | - String latestVersion = rootObj.get("version").getAsString(); |
75 | | - if (!latestVersion.equals(CliVersion.getVersion())) { |
76 | | - config.setLatestVersion(latestVersion); |
77 | | - config.setUpdatePrompt( |
78 | | - rootObj.get( |
79 | | - OSUtils.determineOS() == OSUtils.OS.WINDOWS |
80 | | - ? "install_win" |
81 | | - : "install_unix") |
82 | | - .getAsString()); |
| 60 | + try (Response response = client.newCall(request).execute()) { |
| 61 | + if (response.isSuccessful()) { |
| 62 | + ResponseBody body = response.body(); |
| 63 | + if (body != null) { |
| 64 | + JsonObject jsonObject = JsonParser.parseString(body.string()).getAsJsonObject(); |
| 65 | + String tagName = jsonObject.get("tag_name").getAsString(); |
| 66 | + return tagName.replace("v", ""); |
83 | 67 | } |
84 | 68 | } |
85 | | - } catch (Exception ignored) { |
| 69 | + } catch (IOException e) { |
| 70 | + e.printStackTrace(); |
86 | 71 | } |
| 72 | + return null; |
87 | 73 | } |
88 | 74 | } |
0 commit comments