Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use search api for maven property. #395

Merged
merged 9 commits into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions library/src/main/scala/giter8/MavenHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import org.apache.http.impl.client.DefaultHttpClient

// http://hc.apache.org/httpcomponents-client-4.2.x/httpclient/apidocs/
trait MavenHelper {
def fromMaven(org: String, name: String)(process: (String, NodeSeq) => VersionE): VersionE = {
val loc = s"https://repo1.maven.org/maven2/${org.replace('.', '/')}/$name/maven-metadata.xml"
def fromMaven(org: String, name: String, getVersions: Boolean)(process: (String, NodeSeq) => VersionE): VersionE = {
val search = "https://search.maven.org/solrsearch/select"
val loc = s"""$search?q=g:%22$org%22+AND+a:%22$name%22&rows=10&wt=xml${if (getVersions) "&core=gav" else ""}"""

withHttp(loc) { response =>
val status = response.getStatusLine
status.getStatusCode match {
Expand Down
22 changes: 10 additions & 12 deletions library/src/main/scala/maven.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,28 @@ object Maven extends JavaTokenParsers with MavenHelper {
private def latestVersion(
org: String,
name: String
): VersionE = fromMaven(org, name)(findLatestVersion)
): VersionE = fromMaven(org, name, false)(findLatestVersion)

private def latestStableVersion(
org: String,
name: String
): VersionE = fromMaven(org, name)(findLatestStableVersion)
): VersionE = fromMaven(org, name, true)(findLatestStableVersion)

private[giter8] def findLatestVersion(loc: String, elem: NodeSeq): VersionE = {
(elem \ "versioning" \ "latest").headOption
.map(_.text)
(elem \ "result" \ "doc" \ "str")
.collectFirst { case x if x.attribute("name").map(_.text) == Some("latestVersion") => x.text }
.toRight(s"Found metadata at $loc but can't extract latest version")
}

private[giter8] def findLatestStableVersion(loc: String, elem: NodeSeq)(
implicit svo: Ordering[VersionNumber]): VersionE = {
(elem \ "versioning" \ "latest").headOption.map(_.text) match {
case Some(VersionNumber.Stable(version)) => Right(version.toString)
case _ =>
val versions = (elem \ "versioning" \ "versions" \ "version").map(_.text)
val validVersions = versions.collect {
case VersionNumber.Stable(version) => version
}
validVersions.sorted.headOption.map(_.toString).toRight(s"Could not find latest stable version at $loc")
val versions = (elem \ "result" \ "doc" \ "str").collect {
case x if x.attribute("name").map(_.text) == Some("v") => x.text
}
val validVersions = versions.collect {
case VersionNumber.Stable(version) => version
}
validVersions.sorted.headOption.map(_.toString).toRight(s"Could not find latest stable version at $loc")
}

def lsIsGone(artifact: String): VersionE =
Expand Down
226 changes: 158 additions & 68 deletions library/src/test/scala/giter8/MavenTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,98 +23,188 @@ final class MavenTest extends FlatSpec with Matchers with EitherValues {

"Maven" should "resolve the latest version when found" in {
val xml =
<metadata>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<versioning>
<latest>2.12.1</latest>
<release>2.12.1</release>
</versioning>
</metadata>
<response>
<result name="response" numFound="1" start="0">
<doc>
<str name="g">org.scala-lang</str>
<str name="id">org.scala-lang:scala-library</str>
<str name="latestVersion">2.13.0-M5</str>
</doc>
</result>
</response>

val loc = "https://repo1.maven.org/maven2/org/scala-lang/scala-libary/maven-metadata.xml"
Maven.findLatestVersion(loc, xml).right.value should be("2.12.1")
val loc =
"https://search.maven.org/solrsearch/select?q=g:%22org.scala-lang%22+AND+a:%22scala-library%22&rows=20&wt=xml"
Maven.findLatestVersion(loc, xml).right.value should be("2.13.0-M5")
}

it should "return an error when the latest version is not found" in {
val xml =
<metadata>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</metadata>
<response>
<result name="response" numFound="1" start="0">
<doc>
<str name="g">org.scala-lang</str>
<str name="id">org.scala-lang:scala-library</str>
</doc>
</result>
</response>

val loc = "https://repo1.maven.org/maven2/org/scala-lang/scala-libary/maven-metadata.xml"
val loc =
"https://search.maven.org/solrsearch/select?q=g:%22org.scala-lang%22+AND+a:%22scala-library%22&rows=20&wt=xml"
Maven.findLatestVersion(loc, xml).left.value should be(
s"Found metadata at $loc but can't extract latest version"
)
}

it should "resolve the latest stable version when the latest version is not stable" in {
val xml =
<metadata>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.12</artifactId>
<versioning>
<latest>3.2.0-SNAP4</latest>
<release>3.2.0-SNAP4</release>
<versions>
<version>3.0.0</version>
<version>3.0.1</version>
<version>3.2.0-SNAP1</version>
<version>3.2.0-SNAP2</version>
<version>3.2.0-SNAP3</version>
<version>3.2.0-SNAP4</version>
</versions>
<lastUpdated>20170217001002</lastUpdated>
</versioning>
</metadata>
<response>
<result name="response" numFound="21" start="0">
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.0.6-SNAP2</str>
<str name="v">3.0.6-SNAP2</str>
</doc>
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.0.6-SNAP1</str>
<str name="v">3.0.6-SNAP1</str>
</doc>
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.0.5-M1</str>
<str name="v">3.0.5-M1</str>
</doc>
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.0.5</str>
<str name="v">3.0.5</str>
</doc>
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.2.0-SNAP10</str>
<str name="v">3.2.0-SNAP10</str>
</doc>
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.1.0-SNAP6</str>
<str name="v">3.1.0-SNAP6</str>
</doc>
<doc>
<str name="a">scalatest_2.12</str>
<str name="g">org.scalatest</str>
<str name="id">org.scalatest:scalatest_2.12:3.0.4</str>
<str name="v">3.0.4</str>
</doc>
</result>
</response>

val loc = "https://repo1.maven.org/maven2/org/scalatest/scalatest_2.12/maven-metadata.xml"
Maven.findLatestStableVersion(loc, xml).right.value should be("3.0.1")
val loc =
"https://search.maven.org/solrsearch/select?q=g:%22org.scalatest%22+AND+a:%22scalatest_2.12%22&rows=20&wt=xml&core=gav"
Maven.findLatestStableVersion(loc, xml).right.value should be("3.0.5")
}

it should "return the latest version if the latest version is stable" in {
val xml =
<metadata>
<groupId>org.scalacheck</groupId>
<artifactId>scalacheck_2.12</artifactId>
<versioning>
<latest>1.13.5</latest>
<release>1.13.5</release>
<versions>
<version>1.11.6</version>
<version>1.12.6</version>
<version>1.13.4</version>
<version>1.13.5</version>
</versions>
<lastUpdated>20170313115803</lastUpdated>
</versioning>
</metadata>
<response>
<result name="response" numFound="5" start="0">
<doc>
<str name="a">scalacheck_2.12</str>
<str name="g">org.scalacheck</str>
<str name="id">org.scalacheck:scalacheck_2.12:1.14.0</str>
<str name="v">1.14.0</str>
</doc>
<doc>
<str name="a">scalacheck_2.12</str>
<str name="g">org.scalacheck</str>
<str name="id">org.scalacheck:scalacheck_2.12:1.13.5</str>
<str name="v">1.13.5</str>
</doc>
<doc>
<str name="a">scalacheck_2.12</str>
<str name="g">org.scalacheck</str>
<str name="id">org.scalacheck:scalacheck_2.12:1.12.6</str>
<str name="v">1.12.6</str>
</doc>
<doc>
<str name="a">scalacheck_2.12</str>
<str name="g">org.scalacheck</str>
<str name="id">org.scalacheck:scalacheck_2.12:1.13.4</str>
<str name="v">1.13.4</str>
</doc>
<doc>
<str name="a">scalacheck_2.12</str>
<str name="g">org.scalacheck</str>
<str name="id">org.scalacheck:scalacheck_2.12:1.11.6</str>
<str name="p">jar</str>
<str name="v">1.11.6</str>
</doc>
</result>
</response>

val loc = "https://repo1.maven.org/maven2/org/scalacheck/scalacheck_2.12/maven-metadata.xml"
Maven.findLatestStableVersion(loc, xml).right.value should be("1.13.5")
val loc =
"https://search.maven.org/solrsearch/select?q=g:%22org.scalacheck%22+AND+a:%22scalacheck_2.12%22&core=gav&rows=20&wt=xml"
Maven.findLatestStableVersion(loc, xml).right.value should be("1.14.0")
}

it should "return an error if the latest stable version is not found" in {
val xml =
<metadata>
<groupId>org.scalaz</groupId>
<artifactId>scalaz-concurrent_2.12</artifactId>
<versioning>
<latest>7.3.0-M10</latest>
<release>7.3.0-M10</release>
<versions>
<version>7.3.0-M6</version>
<version>7.3.0-M7</version>
<version>7.3.0-M8</version>
<version>7.3.0-M9</version>
<version>7.3.0-M10</version>
</versions>
<lastUpdated>20170319035300</lastUpdated>
</versioning>
</metadata>
<response>
<result name="response" numFound="47" start="0">
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M24</str>
<str name="v">7.3.0-M24</str>
</doc>
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M23</str>
<str name="v">7.3.0-M23</str>
</doc>
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M22</str>
<str name="v">7.3.0-M22</str>
</doc>
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M21</str>
<str name="v">7.3.0-M21</str>
</doc>
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M20</str>
<str name="v">7.3.0-M20</str>
</doc>
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M19</str>
<str name="v">7.3.0-M19</str>
</doc>
<doc>
<str name="a">scalaz-concurrent_2.12</str>
<str name="g">org.scalaz</str>
<str name="id">org.scalaz:scalaz-concurrent_2.12:7.3.0-M18</str>
<long name="timestamp">1509498138000</long>
<str name="v">7.3.0-M18</str>
</doc>
</result>
</response>

val loc = "https://repo1.maven.org/maven2/org/scalaz/scalaz-concurrent_2.12/maven-metadata.xml"
val loc =
"https://search.maven.org/solrsearch/select?q=g:%22org.scalaz%22+AND+a:%22scalaz-concurrent_2.12%22&rows=20&wt=xml&core=gav"
Maven.findLatestStableVersion(loc, xml).left.value should be(
s"Could not find latest stable version at $loc"
)
Expand Down