Skip to content
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

Bump Version, Changelog, New Update Checks And New Tests #55

Merged
merged 3 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v0.4.1 - Snapshot
* Added custom blossom injector dependency
* Updated dependencies
* Moved github update check util
* Added new update check utils
* Updated .idea files
* Removed simplecoreapi properties to use injected variables
* Added tests for every update checker (available at the moment)

Im-Fran marked this conversation as resolved.
Show resolved Hide resolved
## v0.4.0 - Snapshot
* Added Velocity Support
* Now we use the ILogger util to allow the usage of slf4j and JUL
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'org.jetbrains.dokka' version '1.7.10'
}

def projectVersion = (System.getenv("VERSION") ?: '0.4.0-SNAPSHOT').replaceFirst("v", "").replace('/', '')
def projectVersion = (System.getenv("VERSION") ?: '0.4.1-SNAPSHOT').replaceFirst("v", "").replace('/', '')

group 'xyz.theprogramsrc'
version projectVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import java.time.format.DateTimeFormatter
/**
* Representation of the GitHub Update Checker
* @param logger The logger to use, it must be an instance of [ILogger]
* @param repo The repository to check
* @param repo The repository to check. The format should be <Holder>/<Repository>, for example TheProgramSrc/SimpleCoreAPI
* @param currentVersion the current version (tag name) of the product
Im-Fran marked this conversation as resolved.
Show resolved Hide resolved
* @param latestReleaseTag The tag name of the latest release. (Defaults to "latest")
*/
class GitHubUpdateChecker(val logger: ILogger, val repo: String, val currentVersion: String, val latestReleaseTag: String = "latest") {
class GitHubUpdateChecker(val logger: ILogger, val repo: String, val currentVersion: String, val latestReleaseTag: String = "latest"): UpdateChecker {

private var lastCheck = 0L
private var lastCheckResult = false
private var lastRequest = 0L
private var latestData = JsonObject()
private val requestedData = mutableMapOf<String, Pair<JsonObject, Long>>()
private val current = if(currentVersion.startsWith("v")) currentVersion else "v$currentVersion"

/**
* Checks if there is an update available and prints
* a message if there is one asking the end user to
* update the product.
*/
fun checkWithPrint() {
val latestData = getLatestReleaseData()
val latestVersion = latestData.get("tag_name").asString
override fun checkWithPrint() {
val latestData = getReleaseData()
val latestVersion = latestData.get("version").asString
if(checkForUpdates()){
logger.info("Please update (from $current to $latestVersion)! Download it now from here: https://github.com/$repo/releases/tag/$latestVersion")
}
Expand All @@ -38,15 +38,13 @@ class GitHubUpdateChecker(val logger: ILogger, val repo: String, val currentVers
* Checks if there is an update available
* @return true if there is an update available, false otherwise
*/
fun checkForUpdates(): Boolean {
override fun checkForUpdates(): Boolean {
val difference = System.currentTimeMillis() - lastCheck
if(difference > 60000 || lastCheck == 0L){
lastCheckResult = try {
val parser = DateTimeFormatter.ISO_INSTANT
val currentData = JsonParser.parseString(URL("https://api.github.com/repos/$repo/releases/tags/$current").readText()).asJsonObject
val currentReleasedAt = Instant.from(parser.parse(currentData.get("published_at").asString))
val latestData = getLatestReleaseData()
val latestReleasedAt = Instant.from(parser.parse(latestData.get("published_at").asString))
val currentReleasedAt = Instant.from(parser.parse(getReleaseData(current).get("published_at").asString))
val latestReleasedAt = Instant.from(parser.parse(getReleaseData(latestReleaseTag).get("published_at").asString))
Instant.from(currentReleasedAt).isBefore(latestReleasedAt)
}catch (e: Exception){
e.printStackTrace()
Expand All @@ -59,16 +57,33 @@ class GitHubUpdateChecker(val logger: ILogger, val repo: String, val currentVers
}

/**
* Gets the information of the latest release
* @return The information of the latest release
* Gets the information of a single release
* Object Sample:
* { "published_at": "2022-07-15T21:51:46.397962Z", "version": "v0.4.1-SNAPSHOT", "url": "https://github.com/TheProgramSrc/SimpleCoreAPI/releases/tag/v0.4.1-SNAPSHOT", "author_url": "https://github.com/Im-Fran" }
* - published_at: Is the date when the version was made public. This date must be able to be parsed by Instant#from
* - version: The version of the latest asset
* - url: The url to the version page (null if not available)
* - author_url: The url to the author profile (null if not available)
*
* @param id the name of the release. (If none specified the latest data is fetched. Defaults to "latest")
* @return The information of the given release name
* @since 0.4.1-SNAPSHOT
*/
fun getLatestReleaseData(): JsonObject {
val difference = System.currentTimeMillis() - lastRequest
if(difference > 60000 || lastRequest == 0L){
latestData = JsonParser.parseString(URL(if(latestReleaseTag != "latest") "https://api.github.com/repos/$repo/releases/tags/$latestReleaseTag" else "https://api.github.com/repos/$repo/releases/latest").readText()).asJsonObject
lastRequest = System.currentTimeMillis()
override fun getReleaseData(id: String): JsonObject {
var cached = requestedData.getOrDefault(id, Pair(JsonObject(), 0L))
val difference = System.currentTimeMillis() - cached.second
if(difference > 60000 || cached.second == 0L){
val json = JsonParser.parseString(URL(if(id != "latest") "https://api.github.com/repos/$repo/releases/tags/$id" else "https://api.github.com/repos/$repo/releases/latest").readText()).asJsonObject
cached = Pair(JsonObject().apply {
addProperty("published_at", json.get("published_at").asString)
addProperty("version", json.get("tag_name").asString)
Im-Fran marked this conversation as resolved.
Show resolved Hide resolved
addProperty("url", json.get("html_url").asString)
addProperty("author_url", json.get("author").asJsonObject.get("html_url").asString)
}, System.currentTimeMillis())
requestedData[id] = cached
}
return latestData
println(cached.first.toString() + " - $id")
return cached.first
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package xyz.theprogramsrc.simplecoreapi.global.utils.update

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import xyz.theprogramsrc.simplecoreapi.global.utils.ILogger
import java.net.URL
import java.time.Instant
import java.time.format.DateTimeFormatter

class SongodaUpdateChecker(val logger: ILogger, val productId: String, val currentVersion: String): UpdateChecker {

private var lastCheck = 0L
private var lastCheckResult = false
private val requestedData = mutableMapOf<String, Pair<JsonObject, Long>>()
private val current = if(currentVersion.startsWith("v")) currentVersion else "v$currentVersion"

/**
* Checks if there is an update available and prints
* a message if there is one asking the end user to
* update the product.
*/
override fun checkWithPrint() {
val latestData = getReleaseData()
val latestVersion = latestData.get("version").asString
if(checkForUpdates()){
logger.info("Please update (from $current to $latestVersion)! Download it now from here: https://songoda.org/marketplace/product/$productId")
}
}

/**
* Checks if there is an update available
* @return true if there is an update available, false otherwise
*/
override fun checkForUpdates(): Boolean {
val difference = System.currentTimeMillis() - lastCheck
if(difference > 60000 || lastCheck == 0L) {
lastCheckResult = try {
val parser = DateTimeFormatter.ISO_INSTANT
val currentReleasedAt = Instant.from(parser.parse(getReleaseData(currentVersion).get("published_at").asString))
val latestReleasedAt = Instant.from(parser.parse(getReleaseData().get("published_at").asString))
currentReleasedAt.isBefore(latestReleasedAt)
}catch (e: Exception) {
e.printStackTrace()
false
}
}

return lastCheckResult
}

/**
* Gets the information of a single release
* Object Sample:
* { "published_at": "2022-07-15T21:51:46.397962Z", "version": "v0.4.1-SNAPSHOT", "url": "https://github.com/TheProgramSrc/SimpleCoreAPI/releases/tag/v0.4.1-SNAPSHOT", "author_url": "https://github.com/Im-Fran" }
* - published_at: Is the date when the version was made public. This date must be able to be parsed by Instant#from
* - version: The version of the latest asset
* - url: The url to the version page (null if not available)
* - author_url: The url to the author profile (null if not available)
*
* @param id the name of the release. (If none specified the latest data is fetched. Defaults to "latest")
* @return The information of the given release name
* @since 0.4.1-SNAPSHOT
*/
override fun getReleaseData(id: String): JsonObject {
var cached = requestedData.getOrDefault(id, Pair(JsonObject(), 0L))
val difference = System.currentTimeMillis() - cached.second
if(difference > 60000 || cached.second == 0L){
val url = if(id == "latest"){
"https://songoda.com/api/v2/products/id/$productId/versions?sort=-created_at&per_page=1"
} else {
"https://songoda.com/api/v2/products/id/$productId/versions?sort=-created_at&per_page=1&filter[version]=$id"
}

val data = JsonParser.parseString(URL(url).readText()).asJsonObject.getAsJsonArray("data")
if(data.isEmpty){
throw RuntimeException("We couldn't find any data for the product with id '$productId' and version '$id'. Please try again later.")
}
val json = data.get(0).asJsonObject

cached = Pair(JsonObject().apply {
addProperty("published_at", DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(json.get("created_at").asLong * 1000L)))
addProperty("version", json.get("version").asString)
addProperty("url", json.get("url").asString)
addProperty("author_url", "https://songoda.com/profiles/${json.get("uploaded_by").asJsonObject.get("name").asString}")
}, System.currentTimeMillis())
requestedData[id] = cached
}
return cached.first
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package xyz.theprogramsrc.simplecoreapi.global.utils.update

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import xyz.theprogramsrc.simplecoreapi.global.utils.ILogger
import java.net.URL
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder

class SpigotUpdateChecker(val logger: ILogger, val resourceId: String, val currentVersion: String): UpdateChecker {

private var lastCheck = 0L
private var lastCheckResult = false
private val requestedData = mutableMapOf<String, Pair<JsonObject, Long>>()
private val current = if(currentVersion.startsWith("v")) currentVersion else "v$currentVersion"

/**
* Checks if there is an update available and prints
* a message if there is one asking the end user to
* update the product.
*/
override fun checkWithPrint() {
val latestData = getReleaseData()
val latestVersion = latestData.get("version").asString
if(checkForUpdates()){
logger.info("Please update (from $current to $latestVersion)! Download it now from here: https://spigotmc.org/resources/$resourceId")
}
}

override fun checkForUpdates(): Boolean {
val difference = System.currentTimeMillis() - lastCheck
if(difference > 60000 || lastCheck == 0L) {
lastCheckResult = try {
val parser = DateTimeFormatter.ISO_INSTANT
val currentReleasedAt = Instant.from(parser.parse(getReleaseData(currentVersion).get("published_at").asString))
val latestReleasedAt = Instant.from(parser.parse(getReleaseData().get("published_at").asString))
currentReleasedAt.isBefore(latestReleasedAt)
}catch (e: Exception) {
e.printStackTrace()
false
}
}

return lastCheckResult
}

/**
* Gets the information of a single release
* Object Sample:
* { "published_at": "2022-07-15T21:51:46.397962Z", "version": "v0.4.1-SNAPSHOT", "url": "https://github.com/TheProgramSrc/SimpleCoreAPI/releases/tag/v0.4.1-SNAPSHOT", "author_url": "https://github.com/Im-Fran" }
* - published_at: Is the date when the version was made public. This date must be able to be parsed by Instant#from
* - version: The version of the latest asset
* - url: The url to the version page (null if not available)
* - author_url: The url to the author profile (null if not available)
*
* @param id the name of the release. (If none specified the latest data is fetched. Defaults to "latest")
* @return The information of the given release name
* @since 0.4.1-SNAPSHOT
*/
override fun getReleaseData(id: String): JsonObject {
var cached = requestedData.getOrDefault(id, Pair(JsonObject(), 0L))
val difference = System.currentTimeMillis() - cached.second
if(difference > 60000 || cached.second == 0L){
val json = if(id == "latest"){
JsonParser.parseString(URL("https://api.spiget.org/v2/resources/$resourceId/versions/latest").readText()).asJsonObject
} else {
var page = 1
var data: JsonObject? = null
while(data == null) {
val versions = JsonParser.parseString(URL("http://api.spiget.org/v2/resources/$resourceId/versions?size=50&page=$page").readText()).asJsonArray
if(versions.isEmpty) throw RuntimeException("Couldn't find any version for the given id: $id! Make sure you're using a valid version")
data = versions.firstOrNull {
it.asJsonObject.get("name").asString == id
}?.asJsonObject
page++
}
data
}

cached = Pair(JsonObject().apply {
addProperty("published_at", DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(json.get("releaseDate").asLong * 1000L)))
addProperty("version", json.get("name").asString)
}, System.currentTimeMillis())
requestedData[id] = cached
}
return cached.first
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package xyz.theprogramsrc.simplecoreapi.global.utils.update

import com.google.gson.JsonObject

interface UpdateChecker {

/**
* Checks if there is an update available and prints
* a message if there is one asking the end user to
* update the product.
*/
fun checkWithPrint()

/**
* Checks if there is an update available
* @return true if there is an update available, false otherwise
*/
fun checkForUpdates(): Boolean

/**
* Gets the information of a single release
* Object Sample:
* { "published_at": "2022-07-15T21:51:46.397962Z", "version": "v0.4.1-SNAPSHOT", "url": "https://github.com/TheProgramSrc/SimpleCoreAPI/releases/tag/v0.4.1-SNAPSHOT", "author_url": "https://github.com/Im-Fran" }
* - published_at: Is the date when the version was made public. This date must be able to be parsed by Instant#from
* - version: The version of the latest asset
* - url: The url to the version page (null if not available)
* - author_url: The url to the author profile (null if not available)
*
* @param id the name of the release. (If none specified the latest data is fetched. Defaults to "latest")
* @return The information of the given release name
* @since 0.4.1-SNAPSHOT
*/
fun getReleaseData(id: String = "latest"): JsonObject
}

This file was deleted.

Loading