feat(Settings): add helpers to get version number of bot
This commit is contained in:
1 parent
e239be6c9a
commit
2b08351f6d
3 files changed
+85
-2
No files matched your search
@@ -170,11 +170,17 @@ public class WebModule {
|
||||
*
|
||||
* @return the response as a JSONArray
|
||||
*/
|
||||
public JSONArray getAsArray() throws JSONException, IOException {
|
||||
public JSONArray getAsArray() {
|
||||
if (response == null)
|
||||
throw new RuntimeException("Request has not been sent");
|
||||
|
||||
return new JSONArray(response.body().string());
|
||||
try {
|
||||
ResponseBody body = response.body();
|
||||
return new JSONArray(body.string());
|
||||
} catch (IOException | NullPointerException e) {
|
||||
LogHandler.log(e);
|
||||
return new JSONArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,11 +6,15 @@ package netneutrality.utils;
|
||||
* https://discord.gg/qUa6zNFZsd
|
||||
*/
|
||||
|
||||
import netneutrality.http.WebModule;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -69,4 +73,47 @@ public class Settings {
|
||||
return property;
|
||||
}
|
||||
|
||||
public String getMavenVersion() {
|
||||
String version = null;
|
||||
|
||||
// try to load from maven properties first
|
||||
try {
|
||||
Properties p = new Properties();
|
||||
InputStream is = getClass().getResourceAsStream("/META-INF/maven/me.bobert/netneutrality/pom.properties");
|
||||
if (is != null) {
|
||||
p.load(is);
|
||||
version = p.getProperty("version", "");
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
// fallback to using Java API
|
||||
if (version == null) {
|
||||
Package aPackage = getClass().getPackage();
|
||||
if (aPackage != null) {
|
||||
version = aPackage.getImplementationVersion();
|
||||
if (version == null) {
|
||||
version = aPackage.getSpecificationVersion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (version == null) {
|
||||
version = "0.0.0";
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getGitVersion() {
|
||||
String gitlabHost = "https://git.bobert.me";
|
||||
String gitlabProjectId = "22";
|
||||
String latestReleaseUrl = String.format("%s/api/v4/projects/%s/releases/", gitlabHost, gitlabProjectId);
|
||||
|
||||
JSONArray json = new WebModule(latestReleaseUrl).request().getAsArray();
|
||||
if (json.isNull(0)) {
|
||||
return "0.0.0";
|
||||
}
|
||||
|
||||
return json.getJSONObject(0).getString("tag_name");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package netneutrality.utils;
|
||||
|
||||
/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
* https://discord.gg/qUa6zNFZsd
|
||||
*/
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SettingsTest {
|
||||
|
||||
@Test
|
||||
public void getMavenVersion() {
|
||||
String version = Settings.getInstance().getMavenVersion();
|
||||
System.out.println(version);
|
||||
|
||||
assertNotNull(version);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGitVersion() {
|
||||
String version = Settings.getInstance().getGitVersion();
|
||||
System.out.println(version);
|
||||
|
||||
assertNotNull(version);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user