fun updateRepo()

in app/src/main/kotlin/io/klibs/app/indexing/GitHubIndexingService.kt [76:129]


    fun updateRepo(repoToUpdate: ScmRepositoryEntity): ScmRepositoryEntity {
        val ghRepo =
            gitHubIntegration.getRepository(repoToUpdate.nativeId) ?: gitHubIntegration.getRepository(
                repoToUpdate.ownerLogin,
                repoToUpdate.name
            )
        if (ghRepo == null) {
            // TODO disable indexing at all, remove / hide the project
            scmRepositoryRepository.setUpdatedAt(repoToUpdate.idNotNull, Instant.now()).also { require(it) }
            logger.warn("Unable to find the GH repository for update: $repoToUpdate. Skipping it.")
            return repoToUpdate.copy(updatedAtTs = Instant.now())
        }

        val ownerId = updateRepositoryOwnerIfChanged(repoToUpdate, ghRepo)

        val hasReadme = updateReadme(repoToUpdate, ghRepo)
        val license = gitHubIntegration.getLicense(ghRepo.nativeId)

        val scmRepositoryEntity = repoToUpdate.copy(
            nativeId = ghRepo.nativeId,
            name = ghRepo.name,
            description = ghRepo.description,
            defaultBranch = ghRepo.defaultBranch,
            ownerId = ownerId,
            homepage = ghRepo.homepage,
            hasGhPages = ghRepo.hasGhPages,
            hasIssues = ghRepo.hasIssues,
            hasWiki = ghRepo.hasWiki,
            hasReadme = hasReadme,
            licenseKey = license?.key,
            licenseName = license?.name,
            stars = ghRepo.stars,
            openIssues = ghRepo.openIssues,
            lastActivityTs = ghRepo.lastActivity,
            minimizedReadme = repoToUpdate.minimizedReadme
        )

        logger.info("Updating ${ghRepo.owner}/${ghRepo.name}")
        val persistedRepo = if (scmRepositoryRepository.findByName(ghRepo.owner, ghRepo.name) != null) {
            scmRepositoryRepository.update(scmRepositoryEntity)
        } else {
            scmRepositoryRepository.upsert(scmRepositoryEntity)
        }

        // After repository is persisted, fetch and update GitHub tags for the linked project (if any)
        try {
            val projectEntity = projectRepository.findByScmRepoId(persistedRepo.idNotNull)
            updateGithubTagsForProject(projectEntity, persistedRepo)
        } catch (e: Exception) {
            logger.error("Failed to update GitHub tags for repoId=${persistedRepo.idNotNull}", e)
        }

        return persistedRepo
    }