-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added CheckLibraryVersion class for check library version #142
Changes from 3 commits
4d1060e
68cb13f
cc41f23
86f5fdb
6d07b7a
194616a
d4ce11f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,131 @@ | ||||||
package org.hyperskill.hstest.checker; | ||||||
|
||||||
import com.google.gson.Gson; | ||||||
import com.google.gson.reflect.TypeToken; | ||||||
|
||||||
import java.io.*; | ||||||
import java.lang.reflect.Type; | ||||||
import java.net.HttpURLConnection; | ||||||
import java.net.URL; | ||||||
import java.time.LocalDate; | ||||||
import java.util.Map; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
/** | ||||||
* Checks if the current version of the library is the latest one on GitHub releases page of the library. | ||||||
* If not, throws an exception. | ||||||
*/ | ||||||
public class CheckLibraryVersion { | ||||||
|
||||||
private String VERSION_FILE = "src/main/java/org/hyperskill/hstest/resources/version.txt"; | ||||||
|
||||||
private String currentVersion; | ||||||
private String latestVersion; | ||||||
private boolean isLatestVersion = true; | ||||||
|
||||||
public CheckLibraryVersion() { | ||||||
} | ||||||
|
||||||
/** | ||||||
* Checks if the current version of the library is the latest one on GitHub releases page of the library. | ||||||
* If not, throws an exception. | ||||||
*/ | ||||||
public void checkVersion() { | ||||||
LocalDate lastChecked = null; | ||||||
File lastCheckedFile = new File("LastChecked.txt"); | ||||||
if (lastCheckedFile.exists()) { | ||||||
try { | ||||||
FileReader fileReader = new FileReader(lastCheckedFile); | ||||||
BufferedReader reader = new BufferedReader(fileReader); | ||||||
lastChecked = LocalDate.parse(reader.readLine()); | ||||||
reader.close(); | ||||||
} catch (IOException e) { | ||||||
throw new RuntimeException(e); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this method contains a lot of |
||||||
} | ||||||
} | ||||||
|
||||||
if (lastChecked != null && lastChecked.equals(LocalDate.now())) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return; | ||||||
} | ||||||
|
||||||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(VERSION_FILE); | ||||||
if (inputStream == null) { | ||||||
throw new RuntimeException("Unable to find hs-test library version file"); | ||||||
} | ||||||
|
||||||
try { | ||||||
currentVersion = new BufferedReader(new InputStreamReader(inputStream)).readLine(); | ||||||
} catch (IOException e) { | ||||||
throw new RuntimeException(e); | ||||||
} | ||||||
getLatestHsTestVersionFromGitHub(); | ||||||
|
||||||
if (!currentVersion.equals(latestVersion)) { | ||||||
isLatestVersion = false; | ||||||
} | ||||||
|
||||||
lastChecked = LocalDate.now(); | ||||||
try (FileWriter writer = new FileWriter(lastCheckedFile)) { | ||||||
writer.write(lastChecked.toString()); | ||||||
} catch (IOException e) { | ||||||
throw new RuntimeException(e); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns latest version of the library from GitHub releases page of the library. | ||||||
* @return String latest version of the library | ||||||
*/ | ||||||
private void getLatestHsTestVersionFromGitHub() { | ||||||
HttpURLConnection connection = null; | ||||||
try { | ||||||
URL url = new URL("https://api.github.com/repos/hyperskill/hs-test/releases/latest"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check timeout |
||||||
connection = (HttpURLConnection) url.openConnection(); | ||||||
connection.setRequestMethod("GET"); | ||||||
connection.setRequestProperty("Accept", "application/vnd.github+json"); | ||||||
|
||||||
int responseCode = connection.getResponseCode(); | ||||||
if (responseCode != 200) { | ||||||
latestVersion = currentVersion; | ||||||
return; | ||||||
} | ||||||
} catch (IOException e) { | ||||||
latestVersion = currentVersion; | ||||||
return; | ||||||
} | ||||||
|
||||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { | ||||||
String response = in.lines().collect(Collectors.joining()); | ||||||
|
||||||
Gson gson = new Gson(); | ||||||
Type type = new TypeToken<Map<String, Object>>(){}.getType(); | ||||||
Map<String,Object> map = gson.fromJson(response, type); | ||||||
latestVersion = map.get("tag_name").toString().replace("v", ""); | ||||||
} catch (IOException e) { | ||||||
latestVersion = currentVersion; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns feedback for the user if the current version of the library is not the latest one. | ||||||
* @return String feedback for the user | ||||||
*/ | ||||||
public String getFeedback() { | ||||||
return "\nThe installed hs-test version (" + currentVersion + ") is not the latest version (" + latestVersion + "). " + | ||||||
"Update the library by following the instructions below:\n\n" + | ||||||
"1. Open your project's dependency file build.gradle.\n" + | ||||||
"2. Find the hs-test dependency and change its version to the latest one (" + latestVersion + ").\n" + | ||||||
"3. Sync the dependencies in your development environment or run the following commands in the terminal:\n" + | ||||||
" For Gradle:\n" + | ||||||
" gradle clean build --refresh-dependencies\n\n" + | ||||||
"4. Restart the tests.\n\n"; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns true if the current version of the library is the latest one. | ||||||
* @return Boolean true if the current version of the library is the latest one | ||||||
*/ | ||||||
public Boolean getLatestVersion() { | ||||||
return isLatestVersion; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
10.0.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check what this file doesn't put into submission.