Search in sources :

Example 6 with Transaction

use of net.nemerosa.ontrack.tx.Transaction in project ontrack by nemerosa.

the class SVNChangeLogServiceImpl method getChangeLogRevisions.

@Override
@Transactional
public SVNChangeLogRevisions getChangeLogRevisions(SVNChangeLog changeLog) {
    // Reference
    Collection<SVNChangeLogReference> references = changeLog.getChangeLogReferences();
    // No difference?
    if (references.isEmpty()) {
        return SVNChangeLogRevisions.none();
    }
    // SVN transaction
    try (Transaction ignored = transactionService.start()) {
        List<SVNChangeLogRevision> revisions = new ArrayList<>();
        for (SVNChangeLogReference reference : references) {
            if (!reference.isNone()) {
                SVNRepository repository = changeLog.getRepository();
                // List of log entries
                SVNLogEntryCollector logEntryCollector = new SVNLogEntryCollector();
                // SVN change log
                svnClient.log(repository, SVNUtils.toURL(repository.getUrl(reference.getPath())), SVNRevision.create(reference.getEnd()), SVNRevision.create(reference.getStart() + 1), SVNRevision.create(reference.getEnd()), // Stops on copy
                true, // No path discovering (yet)
                false, // no limit
                0L, // Includes merged revisions
                true, logEntryCollector);
                // Loops through all SVN log entries, taking the merged revisions into account
                int level = 0;
                for (SVNLogEntry svnEntry : logEntryCollector.getEntries()) {
                    long revision = svnEntry.getRevision();
                    if (SVNRevision.isValidRevisionNumber(revision)) {
                        // Conversion
                        SVNChangeLogRevision entry = createChangeLogRevision(repository, reference.getPath(), level, svnEntry);
                        // Adds it to the list
                        revisions.add(entry);
                        // New parent?
                        if (svnEntry.hasChildren()) {
                            level++;
                        }
                    } else {
                        level--;
                    }
                }
            }
        }
        // Sorting the revisions
        Collections.sort(revisions, // From the newest revision to the oldest
        (o1, o2) -> Long.compare(o2.getRevision(), o1.getRevision()));
        // OK
        return new SVNChangeLogRevisions(revisions);
    }
}
Also used : SVNLogEntryCollector(net.nemerosa.ontrack.extension.svn.support.SVNLogEntryCollector) Transaction(net.nemerosa.ontrack.tx.Transaction) SVNLogEntry(org.tmatesoft.svn.core.SVNLogEntry) SVNRepository(net.nemerosa.ontrack.extension.svn.db.SVNRepository) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Transaction

use of net.nemerosa.ontrack.tx.Transaction in project ontrack by nemerosa.

the class SVNChangeLogServiceImpl method getChangeLogIssues.

@Override
@Transactional
public SVNChangeLogIssues getChangeLogIssues(SVNChangeLog changeLog) {
    // Revisions must have been loaded first
    if (changeLog.getRevisions() == null) {
        changeLog.withRevisions(getChangeLogRevisions(changeLog));
    }
    // In a transaction
    try (Transaction ignored = transactionService.start()) {
        // Repository
        SVNRepository repository = changeLog.getRepository();
        // Index of issues, sorted by keys
        Map<String, SVNChangeLogIssue> issues = new TreeMap<>();
        // For all revisions in this revision log
        for (SVNChangeLogRevision changeLogRevision : changeLog.getRevisions().getList()) {
            long revision = changeLogRevision.getRevision();
            collectIssuesForRevision(repository, issues, revision);
        }
        // List of issues
        List<SVNChangeLogIssue> issuesList = new ArrayList<>(issues.values());
        // Validations
        validateIssues(issuesList, changeLog);
        // Issues link
        IssueServiceConfigurationRepresentation issueServiceConfiguration = null;
        String allIssuesLink = "";
        ConfiguredIssueService configuredIssueService = repository.getConfiguredIssueService();
        if (configuredIssueService != null) {
            issueServiceConfiguration = configuredIssueService.getIssueServiceConfigurationRepresentation();
            allIssuesLink = configuredIssueService.getLinkForAllIssues(issuesList.stream().map(SVNChangeLogIssue::getIssue).collect(Collectors.toList()));
        }
        // OK
        return new SVNChangeLogIssues(allIssuesLink, issueServiceConfiguration, issuesList);
    }
}
Also used : IssueServiceConfigurationRepresentation(net.nemerosa.ontrack.extension.issues.model.IssueServiceConfigurationRepresentation) ConfiguredIssueService(net.nemerosa.ontrack.extension.issues.model.ConfiguredIssueService) Transaction(net.nemerosa.ontrack.tx.Transaction) SVNRepository(net.nemerosa.ontrack.extension.svn.db.SVNRepository) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Transaction

use of net.nemerosa.ontrack.tx.Transaction in project ontrack by nemerosa.

the class SVNConfigurationServiceImpl method validate.

@Override
protected ConnectionResult validate(SVNConfiguration configuration) {
    // No trailing slash
    String url = configuration.getUrl();
    if (StringUtils.endsWith(url, "/")) {
        throw new SVNURLFormatException("The Subversion URL must not end with a slash: %s", url);
    }
    try (Transaction ignored = transactionService.start()) {
        // Creates a repository
        SVNRepository repository = SVNRepository.of(0, configuration, null);
        // Configuration URL
        SVNURL svnurl = SVNUtils.toURL(configuration.getUrl());
        // Connection to the root
        if (!svnClient.exists(repository, svnurl, SVNRevision.HEAD)) {
            return ConnectionResult.error(configuration.getUrl() + " does not exist.");
        }
        // Gets base info
        SVNInfo info = svnClient.getInfo(repository, svnurl, SVNRevision.HEAD);
        // Checks the repository root
        if (!Objects.equals(info.getRepositoryRootURL(), svnurl)) {
            return ConnectionResult.error(configuration.getUrl() + " must be the root of the repository.");
        }
        // OK
        return ConnectionResult.ok();
    } catch (Exception ex) {
        return ConnectionResult.error(ex.getMessage());
    }
}
Also used : Transaction(net.nemerosa.ontrack.tx.Transaction) SVNURL(org.tmatesoft.svn.core.SVNURL) SVNURLFormatException(net.nemerosa.ontrack.extension.svn.model.SVNURLFormatException) SVNRepository(net.nemerosa.ontrack.extension.svn.db.SVNRepository) SVNInfo(org.tmatesoft.svn.core.wc.SVNInfo) ConfigurationValidationException(net.nemerosa.ontrack.model.support.ConfigurationValidationException) SVNURLFormatException(net.nemerosa.ontrack.extension.svn.model.SVNURLFormatException)

Example 9 with Transaction

use of net.nemerosa.ontrack.tx.Transaction in project ontrack by nemerosa.

the class SVNHealthIndicator method getHealth.

@Override
protected Health getHealth(SVNConfiguration config) {
    // Just gets the latest revision
    SVNRepository repository = svnService.getRepository(config.getName());
    try (Transaction ignored = transactionService.start()) {
        SVNURL url = SVNUtils.toURL(repository.getConfiguration().getUrl());
        Health.Builder builder = Health.unknown();
        try {
            long repositoryRevision = svnClient.getRepositoryRevision(repository, url);
            return builder.up().withDetail("revision", repositoryRevision).build();
        } catch (Exception ex) {
            return builder.down(ex).build();
        }
    }
}
Also used : Transaction(net.nemerosa.ontrack.tx.Transaction) Health(org.springframework.boot.actuate.health.Health) SVNURL(org.tmatesoft.svn.core.SVNURL) SVNRepository(net.nemerosa.ontrack.extension.svn.db.SVNRepository)

Example 10 with Transaction

use of net.nemerosa.ontrack.tx.Transaction in project ontrack by nemerosa.

the class IndexationServiceImpl method index.

/**
 * Indexation of a range in a thread for one repository - since it is called by a single thread executor, we can
 * be sure that only one call of this method is running at one time for one given repository.
 */
protected void index(SVNRepository repository, long from, long to, JobRunListener runListener) {
    // Ordering
    if (from > to) {
        long t = from;
        from = to;
        to = t;
    }
    // Range
    long min = from;
    long max = to;
    // Opens a transaction
    try (Transaction ignored = transactionService.start()) {
        // SVN URL
        SVNURL url = SVNUtils.toURL(repository.getConfiguration().getUrl());
        // Filters the revision range using the repository configuration
        long startRevision = repository.getConfiguration().getIndexationStart();
        from = Math.max(startRevision, from);
        // Filters the revision range using the SVN repository
        long repositoryRevision = svnClient.getRepositoryRevision(repository, url);
        to = Math.min(to, repositoryRevision);
        // Final check of range
        if (from > to) {
            throw new IllegalArgumentException(String.format("Cannot index range from %d to %d", from, to));
        }
        // Log
        logger.info("[svn-indexation] Repository={}, Range: {}-{}", repository.getId(), from, to);
        // SVN range
        SVNRevision fromRevision = SVNRevision.create(from);
        SVNRevision toRevision = SVNRevision.create(to);
        // Calls the indexer, including merge revisions
        IndexationHandler handler = new IndexationHandler(repository, revision -> runListener.message("Indexation on %s is running (%d to %d - at %d - %d%%)", repository.getConfiguration().getName(), min, max, revision, Math.round(100.0 * (revision - min + 1) / (max - min + 1))));
        svnClient.log(repository, url, SVNRevision.HEAD, fromRevision, toRevision, true, true, 0, false, handler);
    }
}
Also used : Transaction(net.nemerosa.ontrack.tx.Transaction) SVNRevision(org.tmatesoft.svn.core.wc.SVNRevision)

Aggregations

Transaction (net.nemerosa.ontrack.tx.Transaction)15 SVNRepository (net.nemerosa.ontrack.extension.svn.db.SVNRepository)7 Transactional (org.springframework.transaction.annotation.Transactional)5 ConfiguredIssueService (net.nemerosa.ontrack.extension.issues.model.ConfiguredIssueService)3 Issue (net.nemerosa.ontrack.extension.issues.model.Issue)3 IssueServiceConfigurationRepresentation (net.nemerosa.ontrack.extension.issues.model.IssueServiceConfigurationRepresentation)3 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 SVNBranchConfigurationProperty (net.nemerosa.ontrack.extension.svn.property.SVNBranchConfigurationProperty)2 SVNProjectConfigurationProperty (net.nemerosa.ontrack.extension.svn.property.SVNProjectConfigurationProperty)2 SVNLogEntryCollector (net.nemerosa.ontrack.extension.svn.support.SVNLogEntryCollector)2 TransactionService (net.nemerosa.ontrack.tx.TransactionService)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 SVNLogEntry (org.tmatesoft.svn.core.SVNLogEntry)2 SVNURL (org.tmatesoft.svn.core.SVNURL)2 SVNRevision (org.tmatesoft.svn.core.wc.SVNRevision)2 Sets (com.google.common.collect.Sets)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 String.format (java.lang.String.format)1