def loadAccounts()

in auditlog/src/main/scala/com/gerritforge/analytics/auditlog/broadcast/GerritUserIdentifiers.scala [56:99]


  def loadAccounts(
      gerritConnectivity: GerritConnectivity,
      gerritUrl: String
  ): Try[GerritUserIdentifiers] = {

    logger.debug(s"Loading gerrit accounts...")

    val baseUrl = s"""$gerritUrl/accounts/?q=name:""&o=details"""

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

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

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

        val pageInfo = Try(parse(accountsJsonPage)).map { jsListAccounts =>
          val more = (jsListAccounts \ "_more_accounts").extractOrElse(default = false)

          val thisPageAccounts = jsListAccounts
            .extract[List[GerritAccount]]
            .map(ga => ga.accountId -> ga.getIdentifier)
            .toMap

          (more, acc.copy(accounts = acc.accounts ++ thisPageAccounts))
        }

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