Fix version check

Refs: #1068
This commit is contained in:
eikek 2021-09-21 22:07:19 +02:00
parent 647e892cfa
commit 193b81bf7d
6 changed files with 148 additions and 19 deletions

View File

@ -247,7 +247,13 @@ object JoexAppImpl {
.withTask(
JobTask.json(
UpdateCheckTask.taskName,
UpdateCheckTask[F](cfg.updateCheck, cfg.sendMail, javaEmil, updateCheck),
UpdateCheckTask[F](
cfg.updateCheck,
cfg.sendMail,
javaEmil,
updateCheck,
ThisVersion.default
),
UpdateCheckTask.onCancel[F]
)
)

View File

@ -24,10 +24,11 @@ object MakeMail {
sendCfg: MailSendConfig,
cfg: UpdateCheckConfig,
smtpCfg: RUserEmail,
latestRelease: UpdateCheck.Release
latestRelease: UpdateCheck.Release,
thisVersion: ThisVersion
): Mail[F] = {
val templateCtx = TemplateCtx(latestRelease)
val templateCtx = TemplateCtx(latestRelease, thisVersion)
val md = templateCtx.render(cfg.body)
val subj = templateCtx.render(cfg.subject)
@ -49,8 +50,8 @@ object MakeMail {
releasedAt: String
)
object TemplateCtx {
def apply(release: UpdateCheck.Release): TemplateCtx =
TemplateCtx(UpdateCheck.currentVersion, release.version, release.published_at)
def apply(release: UpdateCheck.Release, thisVersion: ThisVersion): TemplateCtx =
TemplateCtx(thisVersion.get, release.version, release.published_at)
implicit val yamuscaConverter: ValueConverter[TemplateCtx] =
ValueConverter.deriveConverter[TemplateCtx]

View File

@ -0,0 +1,22 @@
/*
* Copyright 2020 Docspell Contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package docspell.joex.updatecheck
import docspell.joex.BuildInfo
trait ThisVersion {
def get: String
}
object ThisVersion {
def default: ThisVersion =
constant(BuildInfo.version)
def constant(value: String): ThisVersion = new ThisVersion {
override def get: String = value
}
}

View File

@ -8,8 +8,6 @@ package docspell.joex.updatecheck
import cats.effect._
import docspell.joex.BuildInfo
import io.circe.Decoder
import io.circe.generic.semiauto._
import org.http4s.circe.CirceEntityDecoder._
@ -24,9 +22,6 @@ trait UpdateCheck[F[_]] {
object UpdateCheck {
val currentVersion: String =
BuildInfo.version
final case class Release(
html_url: String,
id: Int,
@ -36,13 +31,14 @@ object UpdateCheck {
published_at: String
) {
def version: String = tag_name
def version: String = tag_name.replaceFirst("v", "")
def isCurrent: Boolean = {
val version = BuildInfo.version
version.endsWith("SNAPSHOT") || version == tag_name
/** Checks if `thisVersion` is either a SNAPSHOT version or the same as this release.
*/
def matchesVersion(tv: ThisVersion): Boolean = {
val myVersion = tv.get
myVersion.endsWith("SNAPSHOT") || myVersion == version
}
}
object Release {

View File

@ -45,13 +45,14 @@ object UpdateCheckTask {
cfg: UpdateCheckConfig,
sendCfg: MailSendConfig,
emil: Emil[F],
updateCheck: UpdateCheck[F]
updateCheck: UpdateCheck[F],
thisVersion: ThisVersion
): Task[F, Args, Unit] =
if (cfg.enabled)
Task { ctx =>
for {
_ <- ctx.logger.info(
s"Check for updates. Current version is: ${UpdateCheck.currentVersion}"
s"Check for updates. Current version is: ${thisVersion.get}"
)
_ <- ctx.logger.debug(
s"Get SMTP connection for ${cfg.senderAccount.asString} and ${cfg.smtpId}"
@ -67,7 +68,7 @@ object UpdateCheckTask {
)
else ().pure[F]
_ <-
if (latest.isCurrent && !cfg.testRun)
if (latest.matchesVersion(thisVersion) && !cfg.testRun)
ctx.logger.info(
s"Latest release is ${latest.version}, which is the current one. Everything uptodate."
)
@ -75,7 +76,7 @@ object UpdateCheckTask {
ctx.logger.info(
s"Sending mail about new release: ${latest.tag_name}"
) *> emil(smtpCfg.toMailConfig).send(
MakeMail(sendCfg, cfg, smtpCfg, latest)
MakeMail(sendCfg, cfg, smtpCfg, latest, thisVersion)
)
} yield ()
}

View File

@ -0,0 +1,103 @@
/*
* Copyright 2020 Docspell Contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package docspell.joex.updatecheck
import io.circe.{parser => jsonParser}
import munit._
class UpdateCheckTest extends FunSuite {
val testRelease =
UpdateCheck.Release(
"https://github.com/eikek/docspell/releases/tag/v0.26.0",
99899888,
"v0.26.0",
"Docspell 0.26.0",
"2021-08-28T10:02:01Z",
"2021-08-28T10:30:38Z"
)
test("parse example response") {
val release =
jsonParser
.parse(UpdateCheckTest.exampleResponsePartial)
.flatMap(_.as[UpdateCheck.Release])
.getOrElse(sys.error("Could not parse test response"))
assertEquals(release.version, "0.26.0")
assertEquals(release, testRelease)
}
test("snapshot is matches") {
val thisVersion = ThisVersion.constant("0.24.0-SNAPSHOT")
assert(testRelease.matchesVersion(thisVersion))
}
test("newer version does not match ") {
val thisVersion = ThisVersion.constant("0.25.0")
assert(!testRelease.matchesVersion(thisVersion))
}
test("same version matches") {
val thisVersion = ThisVersion.constant("0.26.0")
assert(testRelease.matchesVersion(thisVersion))
}
}
object UpdateCheckTest {
val exampleResponsePartial = """
|{
| "url": "https://api.github.com/repos/eikek/docspell/releases/99899888",
| "assets_url": "https://api.github.com/repos/eikek/docspell/releases/99899888/assets",
| "upload_url": "https://uploads.github.com/repos/eikek/docspell/releases/99899888/assets{?name,label}",
| "html_url": "https://github.com/eikek/docspell/releases/tag/v0.26.0",
| "id": 99899888,
| "node_id": "MDc6UmVsZWFzZTQ4NjEwNTY2",
| "tag_name": "v0.26.0",
| "target_commitish": "master",
| "name": "Docspell 0.26.0",
| "draft": false,
| "prerelease": false,
| "created_at": "2021-08-28T10:02:01Z",
| "published_at": "2021-08-28T10:30:38Z",
| "assets": [
| {
| "url": "https://api.github.com/repos/eikek/docspell/releases/assets/43494218",
| "id": 43494218,
| "node_id": "MDEyOlJlbGVhc2VBc3NldDQzNDk0MjE4",
| "name": "docspell-joex-0.26.0.zip",
| "label": "",
| "content_type": "application/zip",
| "state": "uploaded",
| "size": 328163415,
| "download_count": 24,
| "created_at": "2021-08-28T10:16:24Z",
| "updated_at": "2021-08-28T10:16:36Z",
| "browser_download_url": "https://github.com/eikek/docspell/releases/download/v0.26.0/docspell-joex-0.26.0.zip"
| },
| {
| "url": "https://api.github.com/repos/eikek/docspell/releases/assets/43494232",
| "id": 43494232,
| "node_id": "MDEyOlJlbGVhc2VBc3NldDQzNDk0MjMy",
| "name": "docspell-joex_0.26.0_all.deb",
| "label": "",
| "content_type": "application/vnd.debian.binary-package",
| "state": "uploaded",
| "size": 337991872,
| "download_count": 8,
| "created_at": "2021-08-28T10:16:37Z",
| "updated_at": "2021-08-28T10:16:53Z",
| "browser_download_url": "https://github.com/eikek/docspell/releases/download/v0.26.0/docspell-joex_0.26.0_all.deb"
| }
| ],
| "tarball_url": "https://api.github.com/repos/eikek/docspell/tarball/v0.26.0",
| "zipball_url": "https://api.github.com/repos/eikek/docspell/zipball/v0.26.0"
|}
|""".stripMargin
}