Skip to content

Commit

Permalink
reduce threshold, use dedicated exception, rename lastCommitMaxAge
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Jan 24, 2025
1 parent 4ff5d19 commit c7bdd22
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/main/resources/default.scala-steward.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Changes to this file are therefore immediately visible to all
// Scala Steward instances.

lastCommitMaxAge = "540 days"
inactivityThreshold = "270 days"

postUpdateHooks = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import org.scalasteward.core.data.{Dependency, DependencyInfo, Repo, RepoData}
import org.scalasteward.core.forge.data.RepoOut
import org.scalasteward.core.forge.{ForgeApiAlg, ForgeRepoAlg}
import org.scalasteward.core.git.GitAlg
import org.scalasteward.core.repocache.RepoCacheAlg.RepositoryInactive
import org.scalasteward.core.repoconfig.RepoConfigAlg
import org.scalasteward.core.util.{dateTime, DateTimeAlg}
import org.scalasteward.core.util.DateTimeAlg
import org.scalasteward.core.util.dateTime.showDuration
import org.typelevel.log4cats.Logger
import scala.concurrent.duration.FiniteDuration
import scala.util.control.NoStackTrace

final class RepoCacheAlg[F[_]](config: Config)(implicit
Expand Down Expand Up @@ -89,14 +92,23 @@ final class RepoCacheAlg[F[_]](config: Config)(implicit
gitAlg.findFilesContaining(repo, dependency.version.value).map(DependencyInfo(dependency, _))

private[repocache] def throwIfInactive(data: RepoData): F[Unit] =
data.config.lastCommitMaxAge.traverse_ { maxAge =>
data.config.inactivityThreshold.traverse_ { threshold =>
dateTimeAlg.currentTimestamp.flatMap { now =>
val sinceLastCommit = data.cache.commitDate.until(now)
val isInactive = sinceLastCommit > maxAge
F.raiseWhen(isInactive) {
val msg = s"Skipping because last commit is older than ${dateTime.showDuration(maxAge)}"
new Throwable(msg) with NoStackTrace
}
val inactiveSince = data.cache.commitDate.until(now)
val isInactive = inactiveSince > threshold
F.raiseWhen(isInactive)(RepositoryInactive(data.repo, inactiveSince, threshold))
}
}
}

object RepoCacheAlg {
final case class RepositoryInactive(
repo: Repo,
inactiveSince: FiniteDuration,
threshold: FiniteDuration
) extends RuntimeException
with NoStackTrace {
override val getMessage: String =
s"${repo.show}, inactiveSince = ${showDuration(inactiveSince)}, threshold = ${showDuration(threshold)}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final case class RepoConfig(
private val reviewers: Option[List[String]] = None,
private val dependencyOverrides: Option[List[GroupRepoConfig]] = None,
signoffCommits: Option[Boolean] = None,
lastCommitMaxAge: Option[FiniteDuration] = None
inactivityThreshold: Option[FiniteDuration] = None
) {
def commitsOrDefault: CommitsConfig =
commits.getOrElse(CommitsConfig())
Expand Down Expand Up @@ -112,7 +112,8 @@ object RepoConfig {
reviewers = x.reviewers |+| y.reviewers,
dependencyOverrides = x.dependencyOverrides |+| y.dependencyOverrides,
signoffCommits = x.signoffCommits.orElse(y.signoffCommits),
lastCommitMaxAge = combineOptions(x.lastCommitMaxAge, y.lastCommitMaxAge)(_ max _)
inactivityThreshold =
combineOptions(x.inactivityThreshold, y.inactivityThreshold)(_ max _)
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.scalasteward.core.forge.github.Repository
import org.scalasteward.core.git.Branch
import org.scalasteward.core.mock.MockContext.context.{repoCacheAlg, repoConfigAlg, workspaceAlg}
import org.scalasteward.core.mock.{GitHubAuth, MockEff, MockEffOps, MockState}
import org.scalasteward.core.repocache.RepoCacheAlg.RepositoryInactive
import org.scalasteward.core.repoconfig.RepoConfig
import org.scalasteward.core.util.{intellijThisImportIsUsed, Timestamp}
import scala.concurrent.duration.*
Expand Down Expand Up @@ -60,7 +61,7 @@ class RepoCacheAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
assertIO(obtained, expected)
}

test("throwIfInactive: no maxAge") {
test("throwIfInactive: no threshold") {
val repo = Repo("repo-cache-alg", "test-1")
val cache = RepoCache(dummySha1, Timestamp(0L), Nil, None, None)
val config = RepoConfig.empty
Expand All @@ -69,23 +70,26 @@ class RepoCacheAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
assertIO(obtained, Right(()))
}

test("throwIfInactive: lastCommit < maxAge") {
test("throwIfInactive: inactiveSince < threshold") {
val repo = Repo("repo-cache-alg", "test-2")
val commitDate = Timestamp.fromLocalDateTime(LocalDateTime.now())
val cache = RepoCache(dummySha1, commitDate, Nil, None, None)
val config = RepoConfig(lastCommitMaxAge = Some(1.day))
val config = RepoConfig(inactivityThreshold = Some(1.day))
val data = RepoData(repo, cache, config)
val obtained = repoCacheAlg.throwIfInactive(data).runA(MockState.empty).attempt
assertIO(obtained, Right(()))
}

test("throwIfInactive: lastCommit > maxAge") {
test("throwIfInactive: inactiveSince > threshold") {
val repo = Repo("repo-cache-alg", "test-3")
val cache = RepoCache(dummySha1, Timestamp(0L), Nil, None, None)
val config = RepoConfig(lastCommitMaxAge = Some(1.day))
val config = RepoConfig(inactivityThreshold = Some(1.day))
val data = RepoData(repo, cache, config)
val obtained =
repoCacheAlg.throwIfInactive(data).runA(MockState.empty).attempt.map(_.leftMap(_.getMessage))
assertIO(obtained, Left("Skipping because last commit is older than 1d"))
val obtained = repoCacheAlg
.throwIfInactive(data)
.runA(MockState.empty)
.attemptNarrow[RepositoryInactive]
.map(_.leftMap(_.copy(inactiveSince = Duration.Zero)))
assertIO(obtained, Left(RepositoryInactive(repo, Duration.Zero, 1.day)))
}
}

0 comments on commit c7bdd22

Please sign in to comment.