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);
}
}
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);
}
}
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());
}
}
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();
}
}
}
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);
}
}
Aggregations