[db] Add URL as mirror if repo was added with unofficial mirror

This commit is contained in:
Torsten Grote 2023-10-25 13:31:52 -03:00 committed by Hans-Christoph Steiner
parent 66844a17a7
commit b1a0403960
2 changed files with 32 additions and 6 deletions

View File

@ -85,7 +85,7 @@ public data class AddRepoError(
}
public sealed class FetchResult {
public object IsNewRepository : FetchResult()
public data class IsNewRepository(internal val addUrl: String) : FetchResult()
public data class IsNewMirror(
internal val existingRepoId: Long,
internal val newMirrorUrl: String,
@ -211,7 +211,7 @@ internal class RepoAdder(
val cert = repo.certificate ?: error("Certificate was null")
val existingRepo = repositoryDao.getRepository(cert)
return if (existingRepo == null) {
FetchResult.IsNewRepository
FetchResult.IsNewRepository(url)
} else {
val existingMirror = if (existingRepo.address.trimEnd('/') == url) {
url
@ -258,8 +258,17 @@ internal class RepoAdder(
username = repo.username,
password = repo.password,
)
val repoId = repositoryDao.insert(newRepo)
repositoryDao.getRepository(repoId) ?: error("New repository not found in DB")
db.runInTransaction<Repository> {
val repoId = repositoryDao.insert(newRepo)
// add user mirror, if URL is not the repo address and not a known mirror
if (fetchResult.addUrl != repo.address.trimEnd('/') &&
repo.mirrors.find { fetchResult.addUrl == it.url.trimEnd('/') } == null
) {
val userMirrors = listOf(fetchResult.addUrl)
repositoryDao.updateUserMirrors(repoId, userMirrors)
}
repositoryDao.getRepository(repoId) ?: error("New repository not found in DB")
}
}
is FetchResult.IsNewMirror -> {

View File

@ -136,6 +136,7 @@ internal class RepoAdderTest {
@Test
fun testAddingMinRepo() = runTest {
val url = "https://example.org/repo/"
val urlTrimmed = url.trimEnd('/')
val repoName = TestDataMinV2.repo.name.getBestLocale(localeList)
expectDownloadOfMinRepo(url)
@ -143,11 +144,16 @@ internal class RepoAdderTest {
// repo not in DB
every { repoDao.getRepository(any<String>()) } returns null
expectMinRepoPreview(repoName, FetchResult.IsNewRepository) {
expectMinRepoPreview(repoName, FetchResult.IsNewRepository(urlTrimmed)) {
repoAdder.fetchRepository(url = url, proxy = null)
}
val newRepo: Repository = mockk()
val txnSlot = slot<Callable<Repository>>()
every { db.runInTransaction(capture(txnSlot)) } answers {
assertTrue(txnSlot.isCaptured)
txnSlot.captured.call()
}
every {
repoDao.insert(match<NewRepository> {
// Note that we are not using the url the user used to add the repo,
@ -158,6 +164,7 @@ internal class RepoAdderTest {
})
} returns 42L
every { repoDao.getRepository(42L) } returns newRepo
every { repoDao.updateUserMirrors(42L, listOf(urlTrimmed)) } just Runs
repoAdder.addRepoState.test {
assertIs<Fetching>(awaitItem()) // still Fetching from last call
@ -170,6 +177,10 @@ internal class RepoAdderTest {
assertIs<Added>(addedState)
assertEquals(newRepo, addedState.repo)
}
verify {
repoDao.updateUserMirrors(42L, listOf(urlTrimmed))
}
}
@Test
@ -772,11 +783,16 @@ internal class RepoAdderTest {
// repo not in DB
every { repoDao.getRepository(any<String>()) } returns null
expectMinRepoPreview(repoName, FetchResult.IsNewRepository) {
expectMinRepoPreview(repoName, FetchResult.IsNewRepository(urlTrimmed)) {
repoAdder.fetchRepository(url = url, proxy = null)
}
val newRepo: Repository = mockk()
val txnSlot = slot<Callable<Repository>>()
every { db.runInTransaction(capture(txnSlot)) } answers {
assertTrue(txnSlot.isCaptured)
txnSlot.captured.call()
}
every {
repoDao.insert(match<NewRepository> {
// Note that we are not using the url the user used to add the repo,
@ -787,6 +803,7 @@ internal class RepoAdderTest {
it.username == username && it.password == password // this is the important bit
})
} returns 42L
every { repoDao.updateUserMirrors(42L, listOf(urlTrimmed)) } just Runs
every { repoDao.getRepository(42L) } returns newRepo
repoAdder.addRepoState.test {