<commit_before>package net.nemerosa.ontrack.extension.git.service

import net.nemerosa.ontrack.common.FutureUtils
import net.nemerosa.ontrack.common.asOptional
import net.nemerosa.ontrack.common.getOrNull
import net.nemerosa.ontrack.extension.api.model.BuildDiffRequest
import net.nemerosa.ontrack.extension.api.model.BuildDiffRequestDifferenceProjectException
import net.nemerosa.ontrack.extension.git.GitConfigProperties
import net.nemerosa.ontrack.extension.git.branching.BranchingModelService
import net.nemerosa.ontrack.extension.git.model.*
import net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty
import net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationPropertyType
import net.nemerosa.ontrack.extension.git.repository.GitRepositoryHelper
import net.nemerosa.ontrack.extension.git.support.NoGitCommitPropertyException
import net.nemerosa.ontrack.extension.issues.model.ConfiguredIssueService
import net.nemerosa.ontrack.extension.issues.model.Issue
import net.nemerosa.ontrack.extension.issues.model.IssueServiceNotConfiguredException
import net.nemerosa.ontrack.extension.scm.model.SCMBuildView
import net.nemerosa.ontrack.extension.scm.model.SCMChangeLogFileChangeType
import net.nemerosa.ontrack.extension.scm.model.SCMPathInfo
import net.nemerosa.ontrack.extension.scm.service.AbstractSCMChangeLogService
import net.nemerosa.ontrack.extension.scm.service.SCMUtilsService
import net.nemerosa.ontrack.git.GitRepositoryClient
import net.nemerosa.ontrack.git.GitRepositoryClientFactory
import net.nemerosa.ontrack.git.exceptions.GitRepositorySyncException
import net.nemerosa.ontrack.git.model.*
import net.nemerosa.ontrack.job.*
import net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier
import net.nemerosa.ontrack.model.Ack
import net.nemerosa.ontrack.model.security.ProjectConfig
import net.nemerosa.ontrack.model.security.SecurityService
import net.nemerosa.ontrack.model.structure.*
import net.nemerosa.ontrack.model.support.*
import net.nemerosa.ontrack.tx.TransactionService
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.support.TransactionTemplate
import java.lang.String.format
import java.time.Duration
import java.util.*
import java.util.concurrent.Future
import java.util.function.BiConsumer
import java.util.stream.Stream

@Service
@Transactional
class GitServiceImpl(
    structureService: StructureService,
    propertyService: PropertyService,
    private val jobScheduler: JobScheduler,
    private val securityService: SecurityService,
    private val transactionService: TransactionService,
    private val applicationLogService: ApplicationLogService,
    private val gitRepositoryClientFactory: GitRepositoryClientFactory,
    private val buildGitCommitLinkService: BuildGitCommitLinkService,
    private val gitConfigurators: Collection<GitConfigurator>,
    private val scmService: SCMUtilsService,
    private val gitRepositoryHelper: GitRepositoryHelper,
    private val branchingModelService: BranchingModelService,
    private val entityDataService: EntityDataService,
    private val gitConfigProperties: GitConfigProperties,
    private val gitPullRequestCache: DefaultGitPullRequestCache,
    transactionManager: PlatformTransactionManager
) : AbstractSCMChangeLogService<GitConfiguration, GitBuildInfo, GitChangeLogIssue>(structureService, propertyService), GitService, JobOrchestratorSupplier {

    private val logger = LoggerFactory.getLogger(GitService::class.java)

    private val transactionTemplate = TransactionTemplate(transactionManager)

    override fun forEachConfiguredProject(consumer: BiConsumer<Project, GitConfiguration>) {
        structureService.projectList
                .forEach { project ->
                    val configuration = getProjectConfiguration(project)
                    if (configuration != null) {
                        consumer.accept(project, configuration)
                    }
                }
    }

    override fun forEachConfiguredBranch(consumer: BiConsumer<Branch, GitBranchConfiguration>) {
        for (project in structureService.projectList) {
            forEachConfiguredBranchInProject(project, consumer::accept)
        }
    }

    override fun forEachConfiguredBranchInProject(project: Project, consumer: (Branch, GitBranchConfiguration) -> Unit) {
        structureService.getBranchesForProject(project.id)
                .forEach { branch ->
                    val configuration = getBranchConfiguration(branch)
                    if (configuration != null) {
                        consumer(branch, configuration)
                    }
                }
    }

    override fun collectJobRegistrations(): Stream<JobRegistration> {
        val jobs = ArrayList<JobRegistration>()
        // Indexation of repositories, based on projects actually linked
        forEachConfiguredProject(BiConsumer { _, configuration -> jobs.add(getGitIndexationJobRegistration(configuration)) })
        // Synchronisation of branch builds with tags when applicable
        forEachConfiguredBranch(BiConsumer { branch, branchConfiguration ->
            // Build/tag sync job
            if (branchConfiguration.buildTagInterval > 0 && branchConfiguration.buildCommitLink?.link is IndexableBuildGitCommitLink<*>) {
                jobs.add(
                        JobRegistration.of(createBuildSyncJob(branch))
                                .everyMinutes(branchConfiguration.buildTagInterval.toLong())
                )
            }
        })
        // OK
        return jobs.stream()
    }

    override fun isBranchConfiguredForGit(branch: Branch): Boolean {
        return getBranchConfiguration(branch) != null
    }
<commit_msg>#1030 Not running the indexation for disabled branches & projects<commit_after>package net.nemerosa.ontrack.extension.git.service

import net.nemerosa.ontrack.common.FutureUtils
import net.nemerosa.ontrack.common.asOptional
import net.nemerosa.ontrack.common.getOrNull
import net.nemerosa.ontrack.extension.api.model.BuildDiffRequest
import net.nemerosa.ontrack.extension.api.model.BuildDiffRequestDifferenceProjectException
import net.nemerosa.ontrack.extension.git.GitConfigProperties
import net.nemerosa.ontrack.extension.git.branching.BranchingModelService
import net.nemerosa.ontrack.extension.git.model.*
import net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty
import net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationPropertyType
import net.nemerosa.ontrack.extension.git.repository.GitRepositoryHelper
import net.nemerosa.ontrack.extension.git.support.NoGitCommitPropertyException
import net.nemerosa.ontrack.extension.issues.model.ConfiguredIssueService
import net.nemerosa.ontrack.extension.issues.model.Issue
import net.nemerosa.ontrack.extension.issues.model.IssueServiceNotConfiguredException
import net.nemerosa.ontrack.extension.scm.model.SCMBuildView
import net.nemerosa.ontrack.extension.scm.model.SCMChangeLogFileChangeType
import net.nemerosa.ontrack.extension.scm.model.SCMPathInfo
import net.nemerosa.ontrack.extension.scm.service.AbstractSCMChangeLogService
import net.nemerosa.ontrack.extension.scm.service.SCMUtilsService
import net.nemerosa.ontrack.git.GitRepositoryClient
import net.nemerosa.ontrack.git.GitRepositoryClientFactory
import net.nemerosa.ontrack.git.exceptions.GitRepositorySyncException
import net.nemerosa.ontrack.git.model.*
import net.nemerosa.ontrack.job.*
import net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier
import net.nemerosa.ontrack.model.Ack
import net.nemerosa.ontrack.model.security.ProjectConfig
import net.nemerosa.ontrack.model.security.SecurityService
import net.nemerosa.ontrack.model.structure.*
import net.nemerosa.ontrack.model.support.*
import net.nemerosa.ontrack.tx.TransactionService
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.support.TransactionTemplate
import java.lang.String.format
import java.time.Duration
import java.util.*
import java.util.concurrent.Future
import java.util.function.BiConsumer
import java.util.stream.Stream

@Service
@Transactional
class GitServiceImpl(
    structureService: StructureService,
    propertyService: PropertyService,
    private val jobScheduler: JobScheduler,
    private val securityService: SecurityService,
    private val transactionService: TransactionService,
    private val applicationLogService: ApplicationLogService,
    private val gitRepositoryClientFactory: GitRepositoryClientFactory,
    private val buildGitCommitLinkService: BuildGitCommitLinkService,
    private val gitConfigurators: Collection<GitConfigurator>,
    private val scmService: SCMUtilsService,
    private val gitRepositoryHelper: GitRepositoryHelper,
    private val branchingModelService: BranchingModelService,
    private val entityDataService: EntityDataService,
    private val gitConfigProperties: GitConfigProperties,
    private val gitPullRequestCache: DefaultGitPullRequestCache,
    transactionManager: PlatformTransactionManager
) : AbstractSCMChangeLogService<GitConfiguration, GitBuildInfo, GitChangeLogIssue>(structureService, propertyService), GitService, JobOrchestratorSupplier {

    private val logger = LoggerFactory.getLogger(GitService::class.java)

    private val transactionTemplate = TransactionTemplate(transactionManager)

    override fun forEachConfiguredProject(consumer: BiConsumer<Project, GitConfiguration>) {
        structureService.projectList
                .forEach { project ->
                    val configuration = getProjectConfiguration(project)
                    if (configuration != null) {
                        consumer.accept(project, configuration)
                    }
                }
    }

    override fun forEachConfiguredBranch(consumer: BiConsumer<Branch, GitBranchConfiguration>) {
        for (project in structureService.projectList) {
            forEachConfiguredBranchInProject(project, consumer::accept)
        }
    }

    override fun forEachConfiguredBranchInProject(project: Project, consumer: (Branch, GitBranchConfiguration) -> Unit) {
        structureService.getBranchesForProject(project.id)
                .forEach { branch ->
                    val configuration = getBranchConfiguration(branch)
                    if (configuration != null) {
                        consumer(branch, configuration)
                    }
                }
    }

    override fun collectJobRegistrations(): Stream<JobRegistration> {
        val jobs = ArrayList<JobRegistration>()
        // Indexation of repositories, based on projects actually linked
        forEachConfiguredProject(BiConsumer { project, configuration ->
            if (!project.isDisabled) {
                jobs.add(getGitIndexationJobRegistration(configuration))
            }
        })
        // Synchronisation of branch builds with tags when applicable
        forEachConfiguredBranch(BiConsumer { branch, branchConfiguration ->
            if (!branch.isDisabled && !branch.project.isDisabled) {
                // Build/tag sync job
                if (branchConfiguration.buildTagInterval > 0 && branchConfiguration.buildCommitLink?.link is IndexableBuildGitCommitLink<*>) {
                    jobs.add(
                        JobRegistration.of(createBuildSyncJob(branch))
                            .everyMinutes(branchConfiguration.buildTagInterval.toLong())
                    )
                }
            }
        })
        // OK
        return jobs.stream()
    }

    override fun isBranchConfiguredForGit(branch: Branch): Boolean {
        return getBranchConfiguration(branch) != null
    }

{'fraction_non_alphanumeric': 0.07385658754445455, 'fraction_numerical': 0.0008270614506657845, 'mean_word_length': 4.017427385892116, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264632', 'n_tokens_mistral': 3111, 'n_tokens_neox': 3037, 'n_words': 542}