Skip to content

Commit 9f78e81

Browse files
committed
Fix DefaultBackportService.findMilestoneTitle for errors
The `this.github.findFile()` produces a `Mono.error` when file not found instead of `Mono.empty()` * Fix `DefaultBackportService.findMilestoneTitle()` to use `onErrorResume()` instead of `switchIfEmpty()` * Adjust `DefaultBackportServiceTests` mocks to emit `Mono.error(HttpClientErrorException(HttpStatus.NOT_FOUND))` instead of `Mono.empty()` to simulate real HTTP result for `findFile` GH API
1 parent ac3476f commit 9f78e81

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/main/kotlin/io/spring/github/event/DefaultBackportService.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,25 @@ class DefaultBackportService(val github: GitHubApi) : BackportService {
6666
p.load(file)
6767
p.getProperty("version")
6868
}
69-
.switchIfEmpty(Mono.defer {
70-
this.github.findFile(ref, "pom.xml")
71-
.map { file ->
72-
val builderFactory = DocumentBuilderFactory.newInstance()
73-
val builder = builderFactory.newDocumentBuilder()
74-
val xmlDocument: Document = builder.parse(file)
75-
76-
val xPath: XPath = XPathFactory.newInstance().newXPath()
77-
var version = xPath.compile("/project/properties/revision").evaluate(xmlDocument)
78-
if (version == null) {
79-
version = xPath.compile("/project/version").evaluate(xmlDocument)
80-
}
81-
version
69+
.onErrorResume { _ ->
70+
Mono.defer {
71+
this.github.findFile(ref, "pom.xml")
72+
.map { file ->
73+
val builderFactory = DocumentBuilderFactory.newInstance()
74+
val builder = builderFactory.newDocumentBuilder()
75+
val xmlDocument: Document = builder.parse(file)
76+
77+
val xPath: XPath = XPathFactory.newInstance().newXPath()
78+
var version = xPath.compile("/project/properties/revision").evaluate(xmlDocument)
79+
if (version == null) {
80+
version = xPath.compile("/project/version").evaluate(xmlDocument)
81+
}
82+
version
83+
}
8284
}
83-
})
85+
}
8486
.map { it.replace(".BUILD-SNAPSHOT", "").replace("-SNAPSHOT", "") }
85-
.switchIfEmpty(Mono.error { IllegalStateException("Cannot find 'gradle.properties' or 'pom.xml' for $ref") })
87+
.onErrorResume { e -> Mono.error { IllegalStateException("Cannot find 'gradle.properties' or 'pom.xml' for $ref", e) }}
8688
}
8789

8890
private fun findMilestoneNumberByTitle(repositoryRef: RepositoryRef, title: String): Mono<Int> {

src/test/kotlin/io/spring/github/DefaultBackportServiceTests.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import io.spring.github.event.IssueEvent
2323
import io.spring.github.event.PushEvent
2424
import org.assertj.core.api.Assertions.assertThat
2525
import org.junit.jupiter.api.Test
26+
import org.springframework.http.HttpStatus
27+
import org.springframework.web.client.HttpClientErrorException
2628
import reactor.core.publisher.Flux
2729
import reactor.core.publisher.Mono
2830
import reactor.test.StepVerifier
@@ -101,16 +103,20 @@ class DefaultBackportServiceTests {
101103

102104
@Test
103105
fun findMilestoneNumberWhenFindFileEmptyThenError() {
104-
whenever(github.findFile(branchRef, "gradle.properties")).thenReturn(Mono.empty())
105-
whenever(github.findFile(branchRef, "pom.xml")).thenReturn(Mono.empty())
106+
whenever(github.findFile(branchRef, "gradle.properties"))
107+
.thenReturn(Mono.error(HttpClientErrorException(HttpStatus.NOT_FOUND, "'gradle.properties' file not found")))
108+
109+
whenever(github.findFile(branchRef, "pom.xml"))
110+
.thenReturn(Mono.error(HttpClientErrorException(HttpStatus.NOT_FOUND, "'pom.xml' file not found")))
106111

107112
StepVerifier.create(backport.findMilestoneNumber(branchRef))
108113
.verifyErrorSatisfies {e -> assertThat(e).hasMessage("Cannot find 'gradle.properties' or 'pom.xml' for BranchRef(repository=RepositoryRef(fullName=rwinch/test), ref=1.0.x)")}
109114
}
110115

111116
@Test
112117
fun findMavenMilestoneNumber() {
113-
whenever(github.findFile(branchRef, "gradle.properties")).thenReturn(Mono.empty())
118+
whenever(github.findFile(branchRef, "gradle.properties"))
119+
.thenReturn(Mono.error(HttpClientErrorException(HttpStatus.NOT_FOUND, "'gradle.properties' file not found")))
114120
whenever(github.findFile(branchRef, "pom.xml"))
115121
.thenReturn(Mono.just("""
116122
<?xml version="1.0" encoding="UTF-8"?>

0 commit comments

Comments
 (0)