Handle repos without commits Catch and handle exception raised when a repo doesn't have any commit. Latest versions of gitective-core have a better handling of this case, hence the version bump up. Change-Id: I2743b5dbed46875e20cea56c828b07ee869a5dcf
diff --git a/build.sbt b/build.sbt index 211b55b..8cd77dc 100644 --- a/build.sbt +++ b/build.sbt
@@ -11,7 +11,7 @@ scalaVersion := "2.11.8", libraryDependencies ++= Seq( - "io.fabric8" % "gitective-core" % "0.9.19" + "io.fabric8" % "gitective-core" % "0.9.37" exclude("org.eclipse.jgit", "org.eclipse.jgit"), "com.google.inject" % "guice" % "3.0" % Provided, "com.google.gerrit" % "gerrit-plugin-api" % gerritApiVersion % Provided withSources(),
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogram.scala b/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogram.scala index 5b5a70b..ba73fcb 100644 --- a/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogram.scala +++ b/src/main/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogram.scala
@@ -14,17 +14,25 @@ package com.googlesource.gerrit.plugins.analytics.common +import com.google.gerrit.extensions.restapi.PreconditionFailedException import com.google.inject.Singleton import org.eclipse.jgit.lib.Repository import org.gitective.core.CommitFinder -import org.gitective.core.stat.CommitHistogramFilter @Singleton class UserActivityHistogram { def get(repo: Repository, filter: AbstractCommitHistogramFilter) = { val finder = new CommitFinder(repo) - finder.setFilter(filter).find - val histogram = filter.getHistogram - histogram.getAggregatedUserActivity + + try { + finder.setFilter(filter).find + val histogram = filter.getHistogram + histogram.getAggregatedUserActivity + } catch { + // 'find' throws an IllegalArgumentException when the conditions to walk through the commits tree are not met, + // i.e: an empty repository doesn't have the starting commit. + case _: IllegalArgumentException => Array.empty[AggregatedUserCommitActivity] + case e: Exception => throw new PreconditionFailedException(s"Cannot find commits: ${e.getMessage}").initCause(e) + } } } \ No newline at end of file
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogramTest.scala b/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogramTest.scala new file mode 100644 index 0000000..282d29f --- /dev/null +++ b/src/test/scala/com/googlesource/gerrit/plugins/analytics/common/UserActivityHistogramTest.scala
@@ -0,0 +1,37 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.analytics.common + +import com.googlesource.gerrit.plugins.analytics.common.AggregationStrategy.EMAIL_YEAR +import com.googlesource.gerrit.plugins.analytics.test.GitTestCase +import org.eclipse.jgit.internal.storage.file.FileRepository +import org.scalatest.{FlatSpec, Matchers} + +class UserActivityHistogramTest extends FlatSpec with Matchers with GitTestCase { + + "UserActivityHistogram" should "return no activities" in { + val repo = new FileRepository(testRepo) + val filter = new AggregatedHistogramFilterByDates(aggregationStrategy = EMAIL_YEAR) + new UserActivityHistogram().get(repo, filter) should have size 0 + } + + it should "aggregate to one activity" in { + val repo = new FileRepository(testRepo) + add("test.txt", "content") + val filter = new AggregatedHistogramFilterByDates(aggregationStrategy = EMAIL_YEAR) + new UserActivityHistogram().get(repo, filter) should have size 1 + } + +}