use of net.nemerosa.ontrack.extension.svn.db.SVNRepository in project ontrack by nemerosa.
the class SVNInfoServiceImpl method getIssueInfo.
@Override
public OntrackSVNIssueInfo getIssueInfo(String configurationName, String issueKey) {
// Repository
SVNRepository repository = svnService.getRepository(configurationName);
// Issue service
ConfiguredIssueService configuredIssueService = repository.getConfiguredIssueService();
if (configuredIssueService == null) {
// No issue service configured
return OntrackSVNIssueInfo.empty(repository.getConfiguration());
}
// Gets the details about the issue
Issue issue = configuredIssueService.getIssue(issueKey);
// For each configured branch
Map<String, BranchRevision> branchRevisions = new HashMap<>();
svnService.forEachConfiguredBranch(config -> Objects.equals(configurationName, config.getConfiguration().getName()), (branch, branchConfig) -> {
String branchPath = branchConfig.getCuredBranchPath();
// List of linked issues
Collection<String> linkedIssues = configuredIssueService.getLinkedIssues(branch.getProject(), issue).stream().map(Issue::getKey).collect(Collectors.toList());
// Gets the last raw revision on this branch
issueRevisionDao.findLastRevisionByIssuesAndBranch(repository.getId(), linkedIssues, branchPath).ifPresent(revision -> branchRevisions.put(branchPath, new BranchRevision(branchPath, revision, false)));
});
// Until all revisions are complete in respect of their merges...
while (!BranchRevision.areComplete(branchRevisions.values())) {
// Gets the incomplete revisions
Collection<BranchRevision> incompleteRevisions = branchRevisions.values().stream().filter(br -> !br.isComplete()).collect(Collectors.toList());
// For each of them, gets the list of revisions it was merged to
incompleteRevisions.forEach(br -> {
List<Long> merges = revisionDao.getMergesForRevision(repository.getId(), br.getRevision());
// Marks the current revision as complete
branchRevisions.put(br.getPath(), br.complete());
// Gets the revision info for each merged revision
List<TRevision> revisions = merges.stream().map(r -> revisionDao.get(repository.getId(), r)).collect(Collectors.toList());
// For each revision path, compares with current stored revision
revisions.forEach(t -> {
String branch = t.getBranch();
// Existing branch revision?
BranchRevision existingBranchRevision = branchRevisions.get(branch);
if (existingBranchRevision == null || t.getRevision() > existingBranchRevision.getRevision()) {
branchRevisions.put(branch, new BranchRevision(branch, t.getRevision(), true));
}
});
});
}
// We now have the last revision for this issue on each branch...
List<OntrackSVNIssueRevisionInfo> issueRevisionInfos = new ArrayList<>();
branchRevisions.values().forEach(br -> {
// Loads the revision info
SVNRevisionInfo basicInfo = svnService.getRevisionInfo(repository, br.getRevision());
SVNChangeLogRevision changeLogRevision = svnService.createChangeLogRevision(repository, basicInfo);
// Info to collect
OntrackSVNIssueRevisionInfo issueRevisionInfo = OntrackSVNIssueRevisionInfo.of(changeLogRevision);
// Gets the branch from the branch path
AtomicReference<Branch> rBranch = new AtomicReference<>();
svnService.forEachConfiguredBranch(config -> Objects.equals(configurationName, config.getConfiguration().getName()), (candidate, branchConfig) -> {
String branchPath = branchConfig.getCuredBranchPath();
if (Objects.equals(br.getPath(), branchPath)) {
rBranch.set(candidate);
}
});
Branch branch = rBranch.get();
if (branch != null) {
// Collects branch info
SCMIssueCommitBranchInfo branchInfo = SCMIssueCommitBranchInfo.of(branch);
// Gets the first copy event on this path after this revision
SVNLocation firstCopy = svnService.getFirstCopyAfter(repository, basicInfo.toLocation());
// Identifies a possible build given the path/revision and the first copy
Optional<Build> buildAfterCommit = lookupBuild(basicInfo.toLocation(), firstCopy, branch);
branchInfo = scmService.getBranchInfo(buildAfterCommit, branchInfo);
// OK
issueRevisionInfo.add(branchInfo);
}
// OK
issueRevisionInfos.add(issueRevisionInfo);
});
// Gets the list of revisions & their basic info (order from latest to oldest)
List<SVNChangeLogRevision> revisions = svnService.getRevisionsForIssueKey(repository, issueKey).stream().map(revision -> svnService.createChangeLogRevision(repository, svnService.getRevisionInfo(repository, revision))).collect(Collectors.toList());
// OK
return new OntrackSVNIssueInfo(repository.getConfiguration(), repository.getConfiguredIssueService().getIssueServiceConfigurationRepresentation(), issue, issueRevisionInfos, revisions);
}
use of net.nemerosa.ontrack.extension.svn.db.SVNRepository in project ontrack by nemerosa.
the class SVNSyncServiceImpl method sync.
protected void sync(Branch branch, JobRunListener runListener) {
// Number of created builds
AtomicInteger createdBuilds = new AtomicInteger();
// Gets the configuration property
SVNSyncProperty syncProperty = propertyService.getProperty(branch, SVNSyncPropertyType.class).getValue();
// Gets the project configurations for SVN
SVNProjectConfigurationProperty projectConfigurationProperty = propertyService.getProperty(branch.getProject(), SVNProjectConfigurationPropertyType.class).getValue();
// Gets the branch configurations for SVN
SVNBranchConfigurationProperty branchConfigurationProperty = propertyService.getProperty(branch, SVNBranchConfigurationPropertyType.class).getValue();
// SVN repository configuration
SVNRepository repository = svnService.getRepository(projectConfigurationProperty.getConfiguration().getName());
// Link
ConfiguredBuildSvnRevisionLink<?> revisionLink = buildSvnRevisionLinkService.getConfiguredBuildSvnRevisionLink(branchConfigurationProperty.getBuildRevisionLink());
// Gets the base path
svnService.getBasePath(repository, branchConfigurationProperty.getCuredBranchPath()).ifPresent(basePath -> {
// Tags path
String tagsPath = basePath + "/tags";
// Gets the list of tags from the copy events, filtering them
List<TCopyEvent> copies = eventDao.findCopies(// In this repository
repository.getId(), // from path...
branchConfigurationProperty.getCuredBranchPath(), // to path with prefix...
tagsPath, // filter the target path with...
(copyEvent) -> getBuildNameFromPath(tagsPath, revisionLink, copyEvent.copyToLocation()).isPresent());
// Creates the builds (in a transaction)
for (TCopyEvent copy : copies) {
Optional<Build> build = transactionTemplate.execute(status -> createBuild(tagsPath, syncProperty, branch, copy, revisionLink, repository));
// Completes the information collection (build created)
if (build.isPresent()) {
int count = createdBuilds.incrementAndGet();
runListener.message("Running build synchronisation from SVN for branch %s/%s: %d build(s) created", branch.getProject().getName(), branch.getName(), count);
}
}
});
}
use of net.nemerosa.ontrack.extension.svn.db.SVNRepository 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.extension.svn.db.SVNRepository 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.extension.svn.db.SVNRepository 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());
}
}
Aggregations