RepoService.kt

/*
  Copyright (C) 2026 Sber

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package org.archtool.service

import org.archtool.configuration.VcsProperties
import org.archtool.data.entity.RepoEntity
import org.archtool.data.model.RepoInfoModel
import org.archtool.vcs.provider.VcsRepoLinkFormat
import org.archtool.repository.RepoRepository
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.Instant
import java.util.UUID

@Service
class RepoService(
    private val repoRepository: RepoRepository,
    private val vcsProperties: VcsProperties,
    private val vcsRepoLinkFormat: VcsRepoLinkFormat,
) {
    private val log: Logger = LoggerFactory.getLogger(javaClass)

    @Transactional
    suspend fun saveRepo(repoInfo: RepoInfoModel, sequence: Long, expirationTime: Instant) {
        val storageKey = vcsRepoLinkFormat.storageKey(repoInfo.projectKey, repoInfo.slug)
        val repoEntity = RepoEntity(
            repoId = UUID.randomUUID(),
            slug = storageKey.slug,
            projectKey = storageKey.projectKey,
            name = repoInfo.name,
            description = repoInfo.description,
            cloneLink = repoInfo.cloneLink,
            webLink = repoInfo.webLink,
            sequence = sequence,
            lastUpdateTime = Instant.now(),
            creationTime = Instant.now(),
            expirationTime = expirationTime,
            defaultPermission = repoInfo.defaultPermission,
            vcsProvider = vcsProperties.provider,
            new = true
        )
        repoRepository.save(repoEntity)
        log.trace(
            "Сохранен репозиторий {}/{} в БД (sequence={})",
            repoInfo.projectKey,
            repoInfo.slug,
            sequence
        )
    }

    @Transactional
    suspend fun checkRepositoriesExistence(
        sequence: Long,
        repoStrings: List<String>,
    ): Boolean {
        val vcsProvider = vcsProperties.provider
        for (repoString in repoStrings) {
            val storageKey = vcsRepoLinkFormat.parseRepo(repoString)
            if (storageKey == null) {
                log.warn("Неверный формат строки репозитория: {}", repoString)
                return false
            }
            val exists = repoRepository.existsBySequenceAndVcsProviderAndProjectKeyAndSlug(
                sequence,
                vcsProvider,
                storageKey.projectKey,
                storageKey.slug,
            )
            if (!exists) {
                log.trace(
                    "Репозиторий {}/{} не найден в БД (sequence={})",
                    storageKey.projectKey,
                    storageKey.slug,
                    sequence,
                )
                return false
            }
        }
        return true
    }

    @Transactional
    suspend fun getAllReposForUpdate(sequence: Long): List<RepoInfoModel> {
        return repoRepository.findAllBySequenceAndVcsProvider(sequence, vcsProperties.provider).map { entity ->
            RepoInfoModel(
                slug = entity.slug,
                projectKey = entity.projectKey,
                name = entity.name,
                description = entity.description,
                cloneLink = entity.cloneLink,
                webLink = entity.webLink,
                defaultPermission = entity.defaultPermission
            )
        }
    }
}