fun indexRepository()

in app/src/main/kotlin/io/klibs/app/indexing/GitHubIndexingService.kt [207:259]


    fun indexRepository(ownerLogin: String, name: String): ScmRepositoryEntity? {
        val entity = scmRepositoryRepository.findByName(ownerLogin, name)
        if (entity != null) return entity

        logger.debug("Indexing a GitHub repository: $ownerLogin/$name")
        val repo = gitHubIntegration.getRepository(ownerLogin, name) ?: return null

        if (repo.hasBeenRelocated(originalOwnerLogin = ownerLogin, originalName = name)) {
            logger.debug("The indexed repository has been relocated: {}", repo)

            val relocatedEntity = scmRepositoryRepository.findByNativeId(repo.nativeId)
            if (relocatedEntity != null) return relocatedEntity
        }

        val ownerEntity = indexOwner(repo.owner)
        val readmeContent = fetchReadmeContent(repo)
        val license = gitHubIntegration.getLicense(repo.nativeId)

        val persistedEntity = scmRepositoryRepository.upsert(
            ScmRepositoryEntity(
                nativeId = repo.nativeId,
                name = repo.name,
                description = repo.description,
                defaultBranch = repo.defaultBranch,
                createdTs = repo.createdAt,
                ownerId = ownerEntity.idNotNull,
                ownerType = ownerEntity.type,
                ownerLogin = ownerEntity.login,
                homepage = repo.homepage,
                hasGhPages = repo.hasGhPages,
                hasIssues = repo.hasIssues,
                hasWiki = repo.hasWiki,
                hasReadme = readmeContent?.markdown?.isNotBlank() == true,
                licenseKey = license?.key,
                licenseName = license?.name,
                stars = repo.stars,
                openIssues = repo.openIssues,
                lastActivityTs = repo.lastActivity,
                updatedAtTs = Instant.now(),
                minimizedReadme = readmeContent?.minimized
            )
        )

        if (readmeContent != null) {
            readmeService.writeReadmeFiles(
                scmRepositoryId = persistedEntity.idNotNull,
                mdContent = readmeContent.markdown,
                htmlContent = readmeContent.html
            )
        }

        return persistedEntity
    }