Inject automatically BotLikeExtractorImpl

Do not require to instantiate explicitly the extractor for bot-like
comments. Bind the proper implementation in the Guice module and
instantiate it automatically with Guice when needed.

Change-Id: Ib88bf8809607ee63b43613b937b0c531dc89b0f8
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/analytics/AnalyticsConfig.scala b/src/main/scala/com/googlesource/gerrit/plugins/analytics/AnalyticsConfig.scala
index 108b271..8035a43 100644
--- a/src/main/scala/com/googlesource/gerrit/plugins/analytics/AnalyticsConfig.scala
+++ b/src/main/scala/com/googlesource/gerrit/plugins/analytics/AnalyticsConfig.scala
@@ -26,5 +26,7 @@
   val Contributors = "contributors"
   val BotlikeFilenameRegexp = "botlike-filename-regexp"
 
-  lazy val botlikeFilenameRegexps: List[String] = pluginConfig.getStringList(Contributors, null, BotlikeFilenameRegexp).toList
+  lazy val botlikeFilenameRegexps: List[String] = pluginConfigBotLikeFilenameRegexp
+
+  private lazy val pluginConfigBotLikeFilenameRegexp = pluginConfig.getStringList(Contributors, null, BotlikeFilenameRegexp).toList
 }
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/analytics/Contributors.scala b/src/main/scala/com/googlesource/gerrit/plugins/analytics/Contributors.scala
index 1bda651..932e04c 100644
--- a/src/main/scala/com/googlesource/gerrit/plugins/analytics/Contributors.scala
+++ b/src/main/scala/com/googlesource/gerrit/plugins/analytics/Contributors.scala
@@ -78,7 +78,7 @@
 
   override protected def run =
     gsonFmt.format(executor.get(projectRes, beginDate, endDate,
-      granularity.getOrElse(AggregationStrategy.EMAIL), extractBranches, extractIssues, botLikeRegexps), stdout)
+      granularity.getOrElse(AggregationStrategy.EMAIL), extractBranches, extractIssues), stdout)
 
 }
 
@@ -134,28 +134,28 @@
     Response.ok(
       new GsonStreamedResult[UserActivitySummary](gson,
         executor.get(projectRes, beginDate, endDate,
-          granularity.getOrElse(AggregationStrategy.EMAIL), extractBranches, extractIssues, botLikeRegexps)))
+          granularity.getOrElse(AggregationStrategy.EMAIL), extractBranches, extractIssues)))
 }
 
 class ContributorsService @Inject()(repoManager: GitRepositoryManager,
                                     projectCache:ProjectCache,
                                     histogram: UserActivityHistogram,
-                                    gsonFmt: GsonFormatter) {
+                                    gsonFmt: GsonFormatter,
+                                    botLikeExtractor: BotLikeExtractor) {
   import RichBoolean._
 
   import scala.collection.JavaConverters._
 
   def get(projectRes: ProjectResource, startDate: Option[Long], stopDate: Option[Long],
-          aggregationStrategy: AggregationStrategy, extractBranches: Boolean, extractIssues: Boolean, botLikeIdentifiers: List[String])
+          aggregationStrategy: AggregationStrategy, extractBranches: Boolean, extractIssues: Boolean)
   : TraversableOnce[UserActivitySummary] = {
     val nameKey = projectRes.getNameKey
     val commentLinks: List[CommentLinkInfo] = extractIssues.option {
       projectCache.get(nameKey).getCommentLinks.asScala
     }.toList.flatten
 
-
     ManagedResource.use(repoManager.openRepository(projectRes.getNameKey)) { repo =>
-      val stats = new Statistics(repo, new BotLikeExtractorImpl(botLikeIdentifiers), commentLinks)
+      val stats = new Statistics(repo, botLikeExtractor, commentLinks)
       val branchesExtractor = extractBranches.option(new BranchesExtractor(repo))
 
       histogram.get(repo, new AggregatedHistogramFilterByDates(startDate, stopDate, branchesExtractor, aggregationStrategy))
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/analytics/Module.scala b/src/main/scala/com/googlesource/gerrit/plugins/analytics/Module.scala
index 935a198..ca8167b 100644
--- a/src/main/scala/com/googlesource/gerrit/plugins/analytics/Module.scala
+++ b/src/main/scala/com/googlesource/gerrit/plugins/analytics/Module.scala
@@ -17,10 +17,13 @@
 import com.google.gerrit.extensions.restapi.RestApiModule
 import com.google.gerrit.server.project.ProjectResource.PROJECT_KIND
 import com.google.inject.AbstractModule
+import com.googlesource.gerrit.plugins.analytics.common.{BotLikeExtractor, BotLikeExtractorImpl}
 
 class Module extends AbstractModule {
 
   override protected def configure() {
+    bind(classOf[BotLikeExtractor]).to(classOf[BotLikeExtractorImpl])
+
     install(new RestApiModule() {
       override protected def configure() = {
         get(PROJECT_KIND, "contributors").to(classOf[ContributorsResource])
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractor.scala b/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractor.scala
index 16a8bf2..ac67647 100644
--- a/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractor.scala
+++ b/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractor.scala
@@ -14,13 +14,16 @@
 
 package com.googlesource.gerrit.plugins.analytics.common
 
+import com.google.inject.Inject
+import com.googlesource.gerrit.plugins.analytics.AnalyticsConfig
+
 import scala.util.matching.Regex
 
 trait BotLikeExtractor {
   def isBotLike(files: Set[String]): Boolean
 }
 
-class BotLikeExtractorImpl(botLikeIdentifiers: List[String]) extends BotLikeExtractor {
-  private val botRegexps = new Regex(botLikeIdentifiers.mkString("|"))
-  override def isBotLike(files: Set[String]): Boolean = botLikeIdentifiers.nonEmpty && files.forall(botRegexps.findFirstIn(_).isDefined)
+class BotLikeExtractorImpl @Inject() (val analyticsConfig: AnalyticsConfig) extends BotLikeExtractor {
+  private val botRegexps = new Regex(analyticsConfig.botlikeFilenameRegexps.mkString("|"))
+  override def isBotLike(files: Set[String]): Boolean = analyticsConfig.botlikeFilenameRegexps.nonEmpty && files.forall(botRegexps.findFirstIn(_).isDefined)
 }
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractorImplSpec.scala b/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractorImplSpec.scala
index 2893a31..dedab3b 100644
--- a/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractorImplSpec.scala
+++ b/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/BotLikeExtractorImplSpec.scala
@@ -14,6 +14,7 @@
 
 package com.googlesource.gerrit.plugins.analytics.common
 
+import com.googlesource.gerrit.plugins.analytics.AnalyticsConfig
 import com.googlesource.gerrit.plugins.analytics.test.GitTestCase
 import org.scalatest.{FlatSpec, Matchers}
 
@@ -22,7 +23,7 @@
   behavior of "isBotLike"
 
   it should "return true when all files match bot-like identifiers" in {
-    val extractor = new BotLikeExtractorImpl(List(""".+\.xml"""))
+    val extractor = newBotLikeExtractorImpl(List(""".+\.xml"""))
 
     extractor.isBotLike(Set(
       "some/path/AFile.xml",
@@ -32,7 +33,7 @@
   }
 
   it should "return false when at least one file does not match bot-like identifiers" in {
-    val extractor = new BotLikeExtractorImpl(List(""".+\.xml"""))
+    val extractor = newBotLikeExtractorImpl(List(""".+\.xml"""))
 
     extractor.isBotLike(Set(
       "some/path/AFile.xml",
@@ -42,10 +43,14 @@
   }
 
   it should "return false when no bot-like identifiers have been provided" in {
-    val extractor = new BotLikeExtractorImpl(List.empty)
+    val extractor = newBotLikeExtractorImpl(List.empty)
 
     extractor.isBotLike(Set("some/path/anyFile")).shouldBe(false)
 
   }
 
+  private def newBotLikeExtractorImpl(botLikeRegexps: List[String]) = new BotLikeExtractorImpl(new AnalyticsConfig(null, null) {
+    override lazy val botlikeFilenameRegexps = botLikeRegexps
+  })
+
 }