-
-
Notifications
You must be signed in to change notification settings - Fork 38
Fix version sorting by using SemVer instead of publishedDate #168
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
Changes from all commits
23c9023
a216ac0
84d6140
ebb9ed8
e14c55e
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,8 +6,16 @@ import ca.cgagnier.wlednativeandroid.model.Asset | |||||
| import ca.cgagnier.wlednativeandroid.model.Repository | ||||||
| import ca.cgagnier.wlednativeandroid.model.Version | ||||||
| import ca.cgagnier.wlednativeandroid.model.VersionWithAssets | ||||||
| import com.vdurmont.semver4j.Semver | ||||||
| import javax.inject.Inject | ||||||
|
|
||||||
| /** | ||||||
| * nightly tag is not supported at the moment. Exclude it from results. | ||||||
| * TODO: Add support for nightly tags. This will need special handling since the tag itself never | ||||||
| * changes. Probably need a new Branch option for it too. | ||||||
| */ | ||||||
| private const val IGNORED_TAG = "nightly" | ||||||
|
|
||||||
| class VersionWithAssetsRepository @Inject constructor( | ||||||
| private val database: DevicesDatabase, | ||||||
| private val repositoryDao: RepositoryDao, | ||||||
|
|
@@ -76,12 +84,41 @@ class VersionWithAssetsRepository @Inject constructor( | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| suspend fun getLatestStableVersionWithAssets(repositoryId: Long): VersionWithAssets? = | ||||||
| versionDao.getLatestStableVersionWithAssets(repositoryId) | ||||||
| suspend fun getLatestStableVersionWithAssets(repositoryId: Long): VersionWithAssets? { | ||||||
| val latestVersion = getLatestVersion( | ||||||
| versionDao.getVersionsByRepository(repositoryId).filter { !it.isPrerelease && it.tagName != IGNORED_TAG }, | ||||||
| ) | ||||||
| return latestVersion?.let { versionDao.getVersionByTagName(repositoryId, it.tagName) } | ||||||
| } | ||||||
|
|
||||||
| suspend fun getLatestBetaVersionWithAssets(repositoryId: Long): VersionWithAssets? = | ||||||
| versionDao.getLatestBetaVersionWithAssets(repositoryId) | ||||||
| suspend fun getLatestBetaVersionWithAssets(repositoryId: Long): VersionWithAssets? { | ||||||
| val latestVersion = getLatestVersion( | ||||||
| versionDao.getVersionsByRepository(repositoryId).filter { it.tagName != IGNORED_TAG }, | ||||||
| ) | ||||||
| return latestVersion?.let { versionDao.getVersionByTagName(repositoryId, it.tagName) } | ||||||
| } | ||||||
|
|
||||||
| suspend fun getVersionByTag(repositoryId: Long, tagName: String): VersionWithAssets? = | ||||||
| versionDao.getVersionByTagName(repositoryId, tagName) | ||||||
|
|
||||||
| companion object { | ||||||
| val semVerComparator = | ||||||
|
Contributor
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. The
Suggested change
|
||||||
| Comparator<Pair<Version, Semver?>> { v1, v2 -> | ||||||
| val semver1 = v1.second | ||||||
| val semver2 = v2.second | ||||||
|
|
||||||
| when { | ||||||
| semver1 != null && semver2 != null -> semver1.compareTo(semver2) | ||||||
| semver1 != null -> 1 | ||||||
| semver2 != null -> -1 | ||||||
| else -> v1.first.publishedDate.compareTo(v2.first.publishedDate) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fun getLatestVersion(versions: List<Version>): Version? = versions.map { | ||||||
|
Contributor
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. The
Suggested change
|
||||||
| it to runCatching { | ||||||
| Semver(it.tagName, Semver.SemverType.LOOSE) | ||||||
| }.getOrNull() | ||||||
| }.maxWithOrNull(semVerComparator)?.first | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package ca.cgagnier.wlednativeandroid.repository | ||
|
|
||
| import ca.cgagnier.wlednativeandroid.model.Version | ||
| import ca.cgagnier.wlednativeandroid.model.VersionWithAssets | ||
|
Moustachauve marked this conversation as resolved.
|
||
| import com.vdurmont.semver4j.Semver | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class VersionWithAssetsRepositoryTest { | ||
|
|
||
| @Test | ||
| fun semVerComparator_sortsCorrectly() { | ||
| val versions = listOf( | ||
| createVersion("0.14.0", "2023-01-01T00:00:00Z"), | ||
| createVersion("0.15.0", "2023-06-01T00:00:00Z"), | ||
| createVersion("0.15.5", "2023-07-01T00:00:00Z"), | ||
| createVersion("16.0.0", "2022-01-01T00:00:00Z"), // Older date, newer semver | ||
| createVersion("invalid-tag", "2024-01-01T00:00:00Z"), // Fallback to date | ||
| createVersion("invalid-old", "2023-12-01T00:00:00Z"), // Fallback to date | ||
| ).shuffled() | ||
|
|
||
| val parsedVersions = versions.map { | ||
| it to runCatching { | ||
| Semver(it.tagName, Semver.SemverType.LOOSE) | ||
| }.getOrNull() | ||
| } | ||
| val sorted = parsedVersions.sortedWith(VersionWithAssetsRepository.semVerComparator).map { it.first } | ||
|
|
||
| // Invalid semver tags are sorted by date and placed before valid semver tags | ||
| assertEquals("invalid-old", sorted[0].tagName) | ||
| assertEquals("invalid-tag", sorted[1].tagName) | ||
| // Valid semver tags are sorted by semver | ||
| assertEquals("0.14.0", sorted[2].tagName) | ||
| assertEquals("0.15.0", sorted[3].tagName) | ||
| assertEquals("0.15.5", sorted[4].tagName) | ||
| assertEquals("16.0.0", sorted[5].tagName) | ||
|
|
||
| val latest = VersionWithAssetsRepository.getLatestVersion(versions) | ||
| assertEquals("16.0.0", latest?.tagName) | ||
| } | ||
|
|
||
| private fun createVersion(tagName: String, publishedDate: String): Version = Version( | ||
| id = 0, | ||
| repositoryId = 0, | ||
| tagName = tagName, | ||
| name = tagName, | ||
| description = "", | ||
| isPrerelease = false, | ||
| publishedDate = publishedDate, | ||
| htmlUrl = "", | ||
| ) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.