def loadProjects()

in auditlog/src/main/scala/com/gerritforge/analytics/auditlog/broadcast/GerritProjects.scala [119:161]


  def loadProjects(
      gerritConnectivity: GerritConnectivity,
      gerritUrl: String
  ): Try[GerritProjects] = {
    val PAGE_SIZE = 500
    logger.debug(s"Loading gerrit projects...")

    val baseUrl =
      s"""$gerritUrl/projects/?n=$PAGE_SIZE&query=state%3Aactive%20OR%20state%3Aread-only"""

    @tailrec
    def loopThroughPages(
        more: Boolean,
        triedAcc: Try[GerritProjects] = Success(empty)
    ): Try[GerritProjects] = {
      if (!more)
        triedAcc
      else {
        val acc = triedAcc.get

        val url              = baseUrl + s"&S=${acc.projects.size}"
        val accountsJsonPage = gerritConnectivity.getContentFromApi(url).dropGerritPrefix.mkString

        logger.debug(s"Getting gerrit projects - start: ${acc.projects.size}")

        val pageInfo = Try(parse(accountsJsonPage)).map { jsMapProjects =>
          val thisPageProjects =
            jsMapProjects.extract[List[GerritProject]].map(prj => prj.name -> prj)
          (
            thisPageProjects.size == PAGE_SIZE,
            acc.copy(projects = acc.projects ++ thisPageProjects)
          )
        }

        pageInfo match {
          case Success((newMore, newGerritProjects)) =>
            loopThroughPages(newMore, Success(newGerritProjects))
          case Failure(exception) => loopThroughPages(more = false, Failure(exception))
        }
      }
    }
    loopThroughPages(more = true)
  }