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

Merge branch 'fix/loading_markdown_images' into 'develop'

Fix markdown image resolve regex.

See merge request !91
parents 8ae70e72 26ac14f5
......@@ -10,7 +10,7 @@ data class Issue(
@SerializedName("id") val id: Long,
@SerializedName("iid") val iid: Long,
@SerializedName("state") val state: IssueState,
@SerializedName("description") val description: String?,
@SerializedName("description") val description: String,
@SerializedName("author") val author: Author,
@SerializedName("milestone") val milestone: Milestone?,
@SerializedName("project_id") val projectId: Long,
......
......@@ -22,7 +22,7 @@ data class MergeRequest(
@SerializedName("assignee") val assignee: User?,
@SerializedName("source_project_id") val sourceProjectId: Int,
@SerializedName("target_project_id") val targetProjectId: Int,
@SerializedName("description") val description: String?,
@SerializedName("description") val description: String,
@SerializedName("work_in_progress") val workInProgress: Boolean,
@SerializedName("milestone") val milestone: Milestone?,
@SerializedName("merge_when_pipeline_succeeds") val mergeWhenPipelineSucceeds: Boolean,
......
......@@ -8,20 +8,13 @@ import javax.inject.Inject
*/
class MarkDownUrlResolver @Inject constructor() {
private val regex = Regex("^!\\[.+]\\(/uploads/.+/.+\\.\\w{3,4}\\)$")
// ![CragHag](/uploads/69c4ef83b86c66eb3f147915d26c427e/CragHag.png) - before attach
// ![2018-09-03_15.26.43](/uploads/c1fc914375a3d975f12bb6d54d1ee8c8/2018-09-03_15.26.43.jpg)
// https://gitlab.com/terrakok/gitlab-client/uploads/b4048510da2ba117cdc793007066bc25/CragHag.png link to download
// https://gitlab.com/CraggyHaggy/GandastBot/uploads/9475c799f5e9ad5cd1a8ce28ce652ff9/citadel.jpg
private val regex = Regex("!\\[[^]]+]\\(/uploads/[^)]+\\)")
fun resolve(body: String, project: Project): String {
return if (regex.matches(body)) {
StringBuilder(body)
.insert(body.indexOf("/uploads/"), project.pathWithNamespace)
.toString()
} else {
body
val builderBody = StringBuilder(body)
regex.findAll(body).forEach {
builderBody.insert(builderBody.indexOf("/uploads/", it.range.start), project.pathWithNamespace)
}
return builderBody.toString()
}
}
\ No newline at end of file
......@@ -126,8 +126,19 @@ class IssueRepository @Inject constructor(
fun getIssue(
projectId: Long,
issueId: Long
) = api
.getIssue(projectId, issueId)
) = Single
.zip(
api.getProject(projectId),
api.getIssue(projectId, issueId),
BiFunction<Project, Issue, Issue> { project, issue ->
val resolved = markDownUrlResolver.resolve(issue.description, project)
if (resolved != issue.description) {
issue.copy(description = resolved)
} else {
issue
}
}
)
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
......
......@@ -140,8 +140,19 @@ class MergeRequestRepository @Inject constructor(
fun getMergeRequest(
projectId: Long,
mergeRequestId: Long
) = api
.getMergeRequest(projectId, mergeRequestId)
) = Single
.zip(
api.getProject(projectId),
api.getMergeRequest(projectId, mergeRequestId),
BiFunction<Project, MergeRequest, MergeRequest> { project, mr ->
val resolved = markDownUrlResolver.resolve(mr.description, project)
if (resolved != mr.description) {
mr.copy(description = resolved)
} else {
mr
}
}
)
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
......
......@@ -42,7 +42,7 @@ class IssueInfoPresenter @Inject constructor(
.getIssue(projectId, issueId)
.flatMap { issue ->
mdConverter
.markdownToSpannable(issue.description ?: "")
.markdownToSpannable(issue.description)
.map { Pair(issue, it) }
},
projectInteractor.getProject(projectId),
......
......@@ -42,7 +42,7 @@ class MergeRequestInfoPresenter @Inject constructor(
.getMergeRequest(projectId, mrId)
.flatMap { mr ->
mdConverter
.markdownToSpannable(mr.description ?: "")
.markdownToSpannable(mr.description)
.map { Pair(mr, it) }
},
projectInteractor.getProject(projectId),
......
package ru.terrakok.gitlabclient
import org.junit.Assert
import org.junit.Test
import ru.terrakok.gitlabclient.entity.Project
import ru.terrakok.gitlabclient.entity.Visibility
import ru.terrakok.gitlabclient.model.data.server.MarkDownUrlResolver
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 25.10.18.
*/
class MarkDownUrlResolverTest {
private val markDownUrlResolver = MarkDownUrlResolver()
private val project = Project(
1, null, "", Visibility.PUBLIC, "", "", "", null, null, "GitFox", "", "",
"terrakok/gitlab-client", true, 1, true, true, true, true, true, null, null, 1, null, null, false, null, true,
1, 1, null, true, null, true, true, true, null
)
@Test
fun check_markdown_without_image() {
val body = "no image body"
val expected = "no image body"
val resolved = markDownUrlResolver.resolve(body, project)
Assert.assertEquals(expected, resolved)
}
@Test
fun check_markdown_only_image() {
val body = "![CragHag](/uploads/69c4ef83b86c66eb3f147915d26c427e/CragHag.png)"
val expected = "![CragHag](terrakok/gitlab-client/uploads/69c4ef83b86c66eb3f147915d26c427e/CragHag.png)"
val resolved = markDownUrlResolver.resolve(body, project)
Assert.assertEquals(expected, resolved)
}
@Test
fun check_markdown_with_text_and_images() {
val body = "* **Issue**\n" +
"* Web:\n" +
"\n" +
"![Screen_Shot_2018-09-24_at_23.34.07](/uploads/6e1a6bbed7cba2219624d48f2eef731c/Screen_Shot_2018-09-24_at_23.34.07.png)\n" +
"\n" +
"![Screen_Shot_2018-09-24_at_23.34.53](/uploads/a7474cab52c1b53dcddb7a3ce6ac9af3/Screen_Shot_2018-09-24_at_23.34.53.png)\n" +
"\n" +
"* Mobile:\n" +
"\n" +
"![device-2018-09-24-234043](/uploads/caf09f19e9cef4f5dab9cfd01c4b66d6/device-2018-09-24-234043.png)\n" +
"\n" +
"![device-2018-09-24-234018](/uploads/1f4b871a039e197c9c59b6d2cb89f1e7/device-2018-09-24-234018.png)"
val expected = "* **Issue**\n" +
"* Web:\n" +
"\n" +
"![Screen_Shot_2018-09-24_at_23.34.07](terrakok/gitlab-client/uploads/6e1a6bbed7cba2219624d48f2eef731c/Screen_Shot_2018-09-24_at_23.34.07.png)\n" +
"\n" +
"![Screen_Shot_2018-09-24_at_23.34.53](terrakok/gitlab-client/uploads/a7474cab52c1b53dcddb7a3ce6ac9af3/Screen_Shot_2018-09-24_at_23.34.53.png)\n" +
"\n" +
"* Mobile:\n" +
"\n" +
"![device-2018-09-24-234043](terrakok/gitlab-client/uploads/caf09f19e9cef4f5dab9cfd01c4b66d6/device-2018-09-24-234043.png)\n" +
"\n" +
"![device-2018-09-24-234018](terrakok/gitlab-client/uploads/1f4b871a039e197c9c59b6d2cb89f1e7/device-2018-09-24-234018.png)"
val resolved = markDownUrlResolver.resolve(body, project)
Assert.assertEquals(expected, resolved)
}
@Test
fun check_markdown_with_images_in_table() {
val body = "## What does this MR do?\n" +
"\n" +
"Make vertical margin of page titles symmetric and aligned to grid.\n" +
"\n" +
"## Screenshots\n" +
"\n" +
"| Before | After |\n" +
"| --- | --- |\n" +
"| ![Screen_Shot_2018-09-24_at_18.08.23](/uploads/01237180208137f436d5f2a561ab1d74/Screen_Shot_2018-09-24_at_18.08.23.png) | ![Screen_Shot_2018-09-24_at_18.54.10](/uploads/1a29578cdac9a266888709f4f9b607fa/Screen_Shot_2018-09-24_at_18.54.10.png) |\n" +
"\n" +
"## What are the relevant issue numbers?\n" +
"\n" +
"https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/7433#note_103849055\n" +
"\n" +
"## Does this MR meet the acceptance criteria?\n" +
"\n" +
"- [ ] [Changelog entry](https://docs.gitlab.com/ee/development/changelog.html) added, if necessary\n" +
"- [ ] [Documentation created/updated](https://docs.gitlab.com/ee/development/documentation/index.html#contributing-to-docs)\n" +
"- [ ] [Tests added for this feature/bug](https://docs.gitlab.com/ee/development/testing_guide/index.html)\n" +
"- [ ] Conforms to the [code review guidelines](https://docs.gitlab.com/ee/development/code_review.html)\n" +
"- [ ] Conforms to the [merge request performance guidelines](https://docs.gitlab.com/ee/development/merge_request_performance_guidelines.html)\n" +
"- [ ] Conforms to the [style guides](https://gitlab.com/gitlab-org/gitlab-ee/blob/master/CONTRIBUTING.md#style-guides)\n" +
"- [ ] Conforms to the [database guides](https://docs.gitlab.com/ee/development/README.html#databases-guides)"
val expected = "## What does this MR do?\n" +
"\n" +
"Make vertical margin of page titles symmetric and aligned to grid.\n" +
"\n" +
"## Screenshots\n" +
"\n" +
"| Before | After |\n" +
"| --- | --- |\n" +
"| ![Screen_Shot_2018-09-24_at_18.08.23](terrakok/gitlab-client/uploads/01237180208137f436d5f2a561ab1d74/Screen_Shot_2018-09-24_at_18.08.23.png) | ![Screen_Shot_2018-09-24_at_18.54.10](terrakok/gitlab-client/uploads/1a29578cdac9a266888709f4f9b607fa/Screen_Shot_2018-09-24_at_18.54.10.png) |\n" +
"\n" +
"## What are the relevant issue numbers?\n" +
"\n" +
"https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/7433#note_103849055\n" +
"\n" +
"## Does this MR meet the acceptance criteria?\n" +
"\n" +
"- [ ] [Changelog entry](https://docs.gitlab.com/ee/development/changelog.html) added, if necessary\n" +
"- [ ] [Documentation created/updated](https://docs.gitlab.com/ee/development/documentation/index.html#contributing-to-docs)\n" +
"- [ ] [Tests added for this feature/bug](https://docs.gitlab.com/ee/development/testing_guide/index.html)\n" +
"- [ ] Conforms to the [code review guidelines](https://docs.gitlab.com/ee/development/code_review.html)\n" +
"- [ ] Conforms to the [merge request performance guidelines](https://docs.gitlab.com/ee/development/merge_request_performance_guidelines.html)\n" +
"- [ ] Conforms to the [style guides](https://gitlab.com/gitlab-org/gitlab-ee/blob/master/CONTRIBUTING.md#style-guides)\n" +
"- [ ] Conforms to the [database guides](https://docs.gitlab.com/ee/development/README.html#databases-guides)"
val resolved = markDownUrlResolver.resolve(body, project)
Assert.assertEquals(expected, resolved)
}
}
\ No newline at end of file
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