Commit c47a5851 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Fix getting project readme file.

parent 844966bc
......@@ -39,5 +39,6 @@ data class Project(
@SerializedName("shared_with_groups") val sharedWithGroups: List<SharedWithGroup>?,
@SerializedName("only_allow_merge_if_pipeline_succeeds") val onlyAllowMergeIfPipelineSucceeds: Boolean,
@SerializedName("only_allow_merge_if_all_discussions_are_resolved") val onlyAllowMergeIfAllDiscussionsAreResolved: Boolean,
@SerializedName("request_access_enabled") val requestAccessEnabled: Boolean
@SerializedName("request_access_enabled") val requestAccessEnabled: Boolean,
@SerializedName("readme_url") val readmeUrl: String?
)
......@@ -2,6 +2,7 @@ package ru.terrakok.gitlabclient.model.interactor.project
import io.reactivex.Single
import ru.terrakok.gitlabclient.entity.OrderBy
import ru.terrakok.gitlabclient.entity.Project
import ru.terrakok.gitlabclient.model.repository.project.ProjectRepository
import ru.terrakok.gitlabclient.model.repository.tools.Base64Tools
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
......@@ -42,28 +43,22 @@ class ProjectInteractor @Inject constructor(
fun getProject(id: Long) = projectRepository.getProject(id)
/**
* Returns [Single] with the project readme file decoded as [String]
* at the specified project and branch. The project readme is searched ignoring case.
*
* @param id project id.
* @param branchName project branch name to search readme.
* @return [Single] with readme decoded as [String].
* @throws NoSuchElementException if project readme with name [README_FILE_NAME] not found.
*/
fun getProjectReadme(id: Long, branchName: String) =
projectRepository.getRepositoryTree(projectId = id, branchName = branchName)
.map { treeNodes ->
treeNodes.first { it.name.contains(README_FILE_NAME, true) }
}
.flatMap { treeNode ->
projectRepository.getFile(id, treeNode.name, branchName)
.observeOn(schedulers.computation())
.map { file -> base64Tools.decode(file.content) }
.observeOn(schedulers.ui())
fun getProjectReadme(project: Project) =
Single
.defer {
if (project.readmeUrl != null) {
val readmePath = project.readmeUrl.substringAfter(
"${project.webUrl}/blob/${project.defaultBranch}/"
)
projectRepository.getFile(project.id, readmePath, project.defaultBranch)
} else {
Single.error(ReadmeNotFound())
}
}
.observeOn(schedulers.computation())
.map { file -> base64Tools.decode(file.content) }
.observeOn(schedulers.ui())
companion object {
private const val README_FILE_NAME = "readme.md"
}
class ReadmeNotFound : Exception()
}
\ No newline at end of file
......@@ -30,10 +30,10 @@ class ProjectInfoPresenter @Inject constructor(
.getProject(projectId)
.flatMap { project ->
projectInteractor
.getProjectReadme(project.id, project.defaultBranch)
.getProjectReadme(project)
.onErrorResumeNext { throwable ->
when (throwable) {
is NoSuchElementException -> Single.just("")
is ProjectInteractor.ReadmeNotFound -> Single.just("")
else -> Single.error(throwable)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment