Allow filtering by projects prefix

Limit the results to those projects that start with the specified prefix.
See Gerrit REST API:
https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html

Change-Id: Ib00fa6c15b7e30b0dd0ca3ed68b4eb4a5e110363
diff --git a/README.md b/README.md
index 65d1059..0147b34 100644
--- a/README.md
+++ b/README.md
@@ -18,6 +18,7 @@
 - since, until, aggregate are the same defined in Gerrit Analytics plugin
     see: https://gerrit.googlesource.com/plugins/analytics/+/master/README.md
 - -u --url Gerrit server URL with the analytics plugins installed
+- -p --prefix (*optional*) Projects prefix. Limit the results to those projects that start with the specified prefix.
 - -e --elasticIndex specify as <index>/<type> to be loaded in Elastic Search
     if not provided no ES export will be performed
 - -o --out folder location for storing the output as JSON files
diff --git a/src/main/scala/com/gerritforge/analytics/job/Main.scala b/src/main/scala/com/gerritforge/analytics/job/Main.scala
index 5a658ef..8c7953f 100644
--- a/src/main/scala/com/gerritforge/analytics/job/Main.scala
+++ b/src/main/scala/com/gerritforge/analytics/job/Main.scala
@@ -27,6 +27,9 @@
     opt[String]('u', "url") optional() action { (x, c) =>
       c.copy(baseUrl = x)
     } text "gerrit url"
+    opt[String]('p', "prefix") optional() action { (p, c) =>
+      c.copy(prefix = Some(p))
+    } text "projects prefix"
     opt[String]('o', "out") optional() action { (x, c) =>
       c.copy(outputDir = x)
     } text "output directory"
@@ -68,7 +71,7 @@
   def run()(implicit config: GerritEndpointConfig, spark: SparkSession): DataFrame = {
     import spark.sqlContext.implicits._ // toDF
     val sc = spark.sparkContext
-    val projects = sc.parallelize(GerritProjects(Source.fromURL(s"${config.baseUrl}/projects/")))
+    val projects = sc.parallelize(GerritProjects(Source.fromURL(config.gerritProjectsUrl)))
     val aliasesDF = getAliasDF(config.emailAlias)
 
     projects
diff --git a/src/main/scala/com/gerritforge/analytics/model/GerritEndpointConfig.scala b/src/main/scala/com/gerritforge/analytics/model/GerritEndpointConfig.scala
index a32abd0..b7bc647 100644
--- a/src/main/scala/com/gerritforge/analytics/model/GerritEndpointConfig.scala
+++ b/src/main/scala/com/gerritforge/analytics/model/GerritEndpointConfig.scala
@@ -15,6 +15,7 @@
 package com.gerritforge.analytics.model
 
 case class GerritEndpointConfig(baseUrl: String = "",
+                                prefix: Option[String] = None,
                                 outputDir: String = s"${System.getProperty("java.io.tmp")}/analytics-${System.nanoTime()}",
                                 elasticIndex: Option[String] = None,
                                 since: Option[String] = None,
@@ -22,6 +23,8 @@
                                 aggregate: Option[String] = None,
                                 emailAlias: Option[String] = None) {
 
+  val gerritProjectsUrl: String = s"${baseUrl}/projects/" + prefix.fold("")("?p=" + _)
+
   def queryOpt(opt: (String, Option[String])): Option[String] = {
     opt match {
       case (name: String, value: Option[String]) => value.map(name + "=" + _)
diff --git a/src/test/scala/com/gerritforge/analytics/model/GerritEndpointConfigTest.scala b/src/test/scala/com/gerritforge/analytics/model/GerritEndpointConfigTest.scala
new file mode 100644
index 0000000..780461e
--- /dev/null
+++ b/src/test/scala/com/gerritforge/analytics/model/GerritEndpointConfigTest.scala
@@ -0,0 +1,18 @@
+package com.gerritforge.analytics.model
+
+import org.scalatest.{FlatSpec, Matchers}
+
+class GerritEndpointConfigTest extends FlatSpec with Matchers {
+
+  "gerritProjectsUrl" should "contain prefix when available" in {
+    val prefix = "prefixMustBeThere"
+    val conf = GerritEndpointConfig(baseUrl = "testBaseUrl", prefix = Some(prefix))
+    conf.gerritProjectsUrl shouldBe s"testBaseUrl/projects/?p=$prefix"
+  }
+
+  it should "not contain prefix when not available" in {
+    val conf = GerritEndpointConfig(baseUrl = "testBaseUrl", prefix = None)
+    conf.gerritProjectsUrl shouldBe s"testBaseUrl/projects/"
+  }
+
+}