Search in sources :

Example 1 with GitRepositoryClient

use of net.nemerosa.ontrack.git.GitRepositoryClient in project ontrack by nemerosa.

the class GitServiceImpl method buildSync.

protected <T> void buildSync(Branch branch, GitBranchConfiguration branchConfiguration, JobRunListener listener) {
    listener.message("Git build/tag sync for %s/%s", branch.getProject().getName(), branch.getName());
    GitConfiguration configuration = branchConfiguration.getConfiguration();
    // Gets the branch Git client
    GitRepositoryClient gitClient = gitRepositoryClientFactory.getClient(configuration.getGitRepository());
    // Link
    @SuppressWarnings("unchecked") IndexableBuildGitCommitLink<T> link = (IndexableBuildGitCommitLink<T>) branchConfiguration.getBuildCommitLink().getLink();
    @SuppressWarnings("unchecked") T linkData = (T) branchConfiguration.getBuildCommitLink().getData();
    // Configuration for the sync
    Property<GitBranchConfigurationProperty> confProperty = propertyService.getProperty(branch, GitBranchConfigurationPropertyType.class);
    boolean override = !confProperty.isEmpty() && confProperty.getValue().isOverride();
    // Makes sure of synchronization
    listener.message("Synchronizing before importing");
    syncAndWait(configuration);
    // Gets the list of tags
    listener.message("Getting list of tags");
    Collection<GitTag> tags = gitClient.getTags();
    // Creates the builds
    listener.message("Creating builds from tags");
    for (GitTag tag : tags) {
        String tagName = tag.getName();
        // Filters the tags according to the branch tag pattern
        link.getBuildNameFromTagName(tagName, linkData).ifPresent(buildNameCandidate -> {
            String buildName = NameDescription.escapeName(buildNameCandidate);
            listener.message(format("Build %s from tag %s", buildName, tagName));
            // Existing build?
            boolean createBuild;
            Optional<Build> build = structureService.findBuildByName(branch.getProject().getName(), branch.getName(), buildName);
            if (build.isPresent()) {
                if (override) {
                    // Deletes the build
                    listener.message("Deleting existing build %s", buildName);
                    structureService.deleteBuild(build.get().getId());
                    createBuild = true;
                } else {
                    // Keeps the build
                    listener.message("Build %s already exists", buildName);
                    createBuild = false;
                }
            } else {
                createBuild = true;
            }
            // Actual creation
            if (createBuild) {
                listener.message("Creating build %s from tag %s", buildName, tagName);
                structureService.newBuild(Build.of(branch, new NameDescription(buildName, "Imported from Git tag " + tagName), securityService.getCurrentSignature().withTime(tag.getTime())));
            }
        });
    }
}
Also used : GitBranchConfigurationProperty(net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty) GitRepositoryClient(net.nemerosa.ontrack.git.GitRepositoryClient)

Example 2 with GitRepositoryClient

use of net.nemerosa.ontrack.git.GitRepositoryClient in project ontrack by nemerosa.

the class GitServiceImpl method getOntrackGitCommitInfo.

private OntrackGitCommitInfo getOntrackGitCommitInfo(String commit) {
    // Reference data
    AtomicReference<GitCommit> theCommit = new AtomicReference<>();
    AtomicReference<GitConfiguration> theConfiguration = new AtomicReference<>();
    // Data to collect
    Collection<BuildView> buildViews = new ArrayList<>();
    Collection<BranchStatusView> branchStatusViews = new ArrayList<>();
    // For all configured branches
    forEachConfiguredBranch((branch, branchConfiguration) -> {
        GitConfiguration configuration = branchConfiguration.getConfiguration();
        // Gets the client client for this branch
        GitRepositoryClient gitClient = gitRepositoryClientFactory.getClient(configuration.getGitRepository());
        // Scan for this commit in this branch
        AtomicReference<RevCommit> revCommitRef = new AtomicReference<>();
        gitClient.scanCommits(branchConfiguration.getBranch(), revCommit -> {
            String commitId = gitClient.getId(revCommit);
            if (StringUtils.equals(commit, commitId)) {
                revCommitRef.set(revCommit);
                return true;
            } else {
                return false;
            }
        });
        // If present...
        RevCommit revCommit = revCommitRef.get();
        if (revCommit != null) {
            // Reference
            if (theCommit.get() == null) {
                theCommit.set(gitClient.toCommit(revCommit));
                theConfiguration.set(configuration);
            }
            // Gets the earliest build on this branch that contains this commit
            getEarliestBuildAfterCommit(commit, branch, branchConfiguration, gitClient).ifPresent(build -> {
                // Gets the build view
                BuildView buildView = structureService.getBuildView(build, true);
                // Adds it to the list
                buildViews.add(buildView);
                // Collects the promotions for the branch
                branchStatusViews.add(structureService.getEarliestPromotionsAfterBuild(build));
            });
        }
    });
    // OK
    if (theCommit.get() != null) {
        String commitLink = theConfiguration.get().getCommitLink();
        List<? extends MessageAnnotator> messageAnnotators = getMessageAnnotators(theConfiguration.get());
        return new OntrackGitCommitInfo(toUICommit(commitLink, messageAnnotators, theCommit.get()), buildViews, branchStatusViews);
    } else {
        throw new GitCommitNotFoundException(commit);
    }
}
Also used : SCMBuildView(net.nemerosa.ontrack.extension.scm.model.SCMBuildView) GitRepositoryClient(net.nemerosa.ontrack.git.GitRepositoryClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with GitRepositoryClient

use of net.nemerosa.ontrack.git.GitRepositoryClient in project ontrack by nemerosa.

the class GitServiceImpl method diff.

@Override
public String diff(GitChangeLog changeLog, List<String> patterns) {
    // Gets the client client for this configuration`
    GitRepositoryClient gitClient = getGitRepositoryClient(changeLog.getProject());
    // Path predicate
    Predicate<String> pathFilter = scmService.getPathFilter(patterns);
    // Gets the build boundaries
    Build buildFrom = changeLog.getFrom().getBuild();
    Build buildTo = changeLog.getTo().getBuild();
    // Commit boundaries
    String commitFrom = getCommitFromBuild(buildFrom);
    String commitTo = getCommitFromBuild(buildTo);
    // Gets the diff
    return gitClient.unifiedDiff(commitFrom, commitTo, pathFilter);
}
Also used : GitRepositoryClient(net.nemerosa.ontrack.git.GitRepositoryClient)

Example 4 with GitRepositoryClient

use of net.nemerosa.ontrack.git.GitRepositoryClient in project ontrack by nemerosa.

the class GitServiceImpl method collectIssueCommitInfos.

private List<OntrackGitIssueCommitInfo> collectIssueCommitInfos(Project project, Issue issue) {
    // Index of commit infos
    Map<String, OntrackGitIssueCommitInfo> commitInfos = new LinkedHashMap<>();
    // For all configured branches
    forEachConfiguredBranch((branch, branchConfiguration) -> {
        // Filter per project
        if (branch.projectId() != project.id()) {
            return;
        }
        // Gets the branch configuration
        GitConfiguration configuration = branchConfiguration.getConfiguration();
        // Gets the Git client for this project
        GitRepositoryClient client = gitRepositoryClientFactory.getClient(configuration.getGitRepository());
        // Issue service
        ConfiguredIssueService configuredIssueService = configuration.getConfiguredIssueService().orElse(null);
        if (configuredIssueService != null) {
            // List of commits for this branch
            List<RevCommit> revCommits = new ArrayList<>();
            // Scanning this branch's repository for the commit
            client.scanCommits(branchConfiguration.getBranch(), revCommit -> {
                String message = revCommit.getFullMessage();
                Set<String> keys = configuredIssueService.extractIssueKeysFromMessage(message);
                // Gets all linked issues
                boolean matching = configuredIssueService.getLinkedIssues(branch.getProject(), issue).stream().map(Issue::getKey).anyMatch(key -> configuredIssueService.containsIssueKey(key, keys));
                if (matching) {
                    // We have a commit for this branch!
                    revCommits.add(revCommit);
                }
                // Scanning all commits
                return false;
            });
            // If at least one commit
            if (revCommits.size() > 0) {
                // Gets the last commit (which is the first in the list)
                RevCommit revCommit = revCommits.get(0);
                // Commit explained (independent from the branch)
                GitCommit commit = client.toCommit(revCommit);
                String commitId = commit.getId();
                // Gets any existing commit info
                OntrackGitIssueCommitInfo commitInfo = commitInfos.get(commitId);
                // If not defined, creates an entry
                if (commitInfo == null) {
                    // UI commit (independent from the branch)
                    GitUICommit uiCommit = toUICommit(configuration.getCommitLink(), getMessageAnnotators(configuration), commit);
                    // Commit info
                    commitInfo = OntrackGitIssueCommitInfo.of(uiCommit);
                    // Indexation
                    commitInfos.put(commitId, commitInfo);
                }
                // Collects branch info
                SCMIssueCommitBranchInfo branchInfo = SCMIssueCommitBranchInfo.of(branch);
                // Gets the last build for this branch
                Optional<Build> buildAfterCommit = getEarliestBuildAfterCommit(commitId, branch, branchConfiguration, client);
                branchInfo = scmService.getBranchInfo(buildAfterCommit, branchInfo);
                // Adds the info
                commitInfo.add(branchInfo);
            }
        }
    });
    // OK
    return Lists.newArrayList(commitInfos.values());
}
Also used : ConfiguredIssueService(net.nemerosa.ontrack.extension.issues.model.ConfiguredIssueService) GitRepositoryClient(net.nemerosa.ontrack.git.GitRepositoryClient) RevCommit(org.eclipse.jgit.revwalk.RevCommit) SCMIssueCommitBranchInfo(net.nemerosa.ontrack.extension.scm.model.SCMIssueCommitBranchInfo)

Example 5 with GitRepositoryClient

use of net.nemerosa.ontrack.git.GitRepositoryClient in project ontrack by nemerosa.

the class GitServiceImpl method getChangeLogFiles.

@Override
public GitChangeLogFiles getChangeLogFiles(GitChangeLog changeLog) {
    // Gets the configuration
    GitConfiguration configuration = getRequiredProjectConfiguration(changeLog.getProject());
    // Gets the client for this project
    GitRepositoryClient client = gitRepositoryClientFactory.getClient(configuration.getGitRepository());
    // Gets the build boundaries
    Build buildFrom = changeLog.getFrom().getBuild();
    Build buildTo = changeLog.getTo().getBuild();
    // Commit boundaries
    String commitFrom = getCommitFromBuild(buildFrom);
    String commitTo = getCommitFromBuild(buildTo);
    // Diff
    final GitDiff diff = client.diff(commitFrom, commitTo);
    // File change links
    String fileChangeLinkFormat = configuration.getFileAtCommitLink();
    // OK
    return new GitChangeLogFiles(diff.getEntries().stream().map(entry -> toChangeLogFile(entry).withUrl(getDiffUrl(diff, entry, fileChangeLinkFormat))).collect(Collectors.toList()));
}
Also used : GitRepositoryClient(net.nemerosa.ontrack.git.GitRepositoryClient)

Aggregations

GitRepositoryClient (net.nemerosa.ontrack.git.GitRepositoryClient)13 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 GitBranchConfigurationProperty (net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty)1 ConfiguredIssueService (net.nemerosa.ontrack.extension.issues.model.ConfiguredIssueService)1 SCMBuildView (net.nemerosa.ontrack.extension.scm.model.SCMBuildView)1 SCMIssueCommitBranchInfo (net.nemerosa.ontrack.extension.scm.model.SCMIssueCommitBranchInfo)1