Skip to content

Commit 272497d

Browse files
committed
GH-24: Support Maven projects as well
Fixes: #24 Currently, the current milestone is extraction only from the `version` from `gradle.properties`. * Handle `pom.xml` similar way when we fail to find `gradle.properties`
1 parent 1b712d4 commit 272497d

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import reactor.core.publisher.Mono
2727
*/
2828
interface BackportService {
2929
/**
30-
* Gets the milestone number. Typically the title is found from gradle.properites and
30+
* Gets the milestone number. Typically, the title is found from gradle.properites, pom.xml and
3131
* then that is used to find a milestone number by its title.
3232
*/
3333
fun findMilestoneNumber(branchRef: BranchRef) : Mono<Int>

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ package io.spring.github.event
1818

1919
import io.spring.github.api.*
2020
import org.springframework.stereotype.Component
21+
import org.w3c.dom.Document
2122
import reactor.core.publisher.Flux
2223
import reactor.core.publisher.Mono
2324
import java.util.*
25+
import javax.xml.parsers.DocumentBuilderFactory
26+
import javax.xml.xpath.XPath
27+
import javax.xml.xpath.XPathConstants
28+
import javax.xml.xpath.XPathFactory
2429

2530
/**
2631
* @author Rob Winch
@@ -56,13 +61,28 @@ class DefaultBackportService(val github: GitHubApi) : BackportService {
5661

5762
private fun findMilestoneTitle(ref: BranchRef): Mono<String> {
5863
return this.github.findFile(ref, "gradle.properties")
59-
.map { input ->
64+
.map { file ->
6065
val p = Properties()
61-
p.load(input)
62-
val version = p.getProperty("version")
63-
version.replace(".BUILD-SNAPSHOT", "").replace("-SNAPSHOT", "")
66+
p.load(file)
67+
p.getProperty("version")
6468
}
65-
.switchIfEmpty(Mono.error { IllegalStateException("Cannot find file gradle.properties for $ref") })
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
82+
}
83+
})
84+
.map { it.replace(".BUILD-SNAPSHOT", "").replace("-SNAPSHOT", "") }
85+
.switchIfEmpty(Mono.error { IllegalStateException("Cannot find 'gradle.properties' or 'pom.xml' for $ref") })
6686
}
6787

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

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,29 @@ class DefaultBackportServiceTests {
102102
@Test
103103
fun findMilestoneNumberWhenFindFileEmptyThenError() {
104104
whenever(github.findFile(branchRef, "gradle.properties")).thenReturn(Mono.empty())
105+
whenever(github.findFile(branchRef, "pom.xml")).thenReturn(Mono.empty())
105106

106107
StepVerifier.create(backport.findMilestoneNumber(branchRef))
107-
.verifyErrorSatisfies {e -> assertThat(e).hasMessage("Cannot find file gradle.properties for BranchRef(repository=RepositoryRef(fullName=rwinch/test), ref=1.0.x)")}
108+
.verifyErrorSatisfies {e -> assertThat(e).hasMessage("Cannot find 'gradle.properties' or 'pom.xml' for BranchRef(repository=RepositoryRef(fullName=rwinch/test), ref=1.0.x)")}
109+
}
110+
111+
@Test
112+
fun findMavenMilestoneNumber() {
113+
whenever(github.findFile(branchRef, "gradle.properties")).thenReturn(Mono.empty())
114+
whenever(github.findFile(branchRef, "pom.xml"))
115+
.thenReturn(Mono.just("""
116+
<?xml version="1.0" encoding="UTF-8"?>
117+
<project>
118+
<properties>
119+
<revision>1.1.0-SNAPSHOT</revision>
120+
</properties>
121+
</project>
122+
""".trimIndent().byteInputStream(Charset.defaultCharset())))
123+
whenever(github.findMilestoneNumberByTitle(repositoryRef, "1.1.0")).thenReturn(Mono.just(1))
124+
125+
StepVerifier.create(backport.findMilestoneNumber(branchRef))
126+
.expectNext(1)
127+
.verifyComplete()
108128
}
109129

110130
@Test

0 commit comments

Comments
 (0)