Search in sources :

Example 1 with GitBranchConfigurationProperty

use of net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty 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 GitBranchConfigurationProperty

use of net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty in project ontrack by nemerosa.

the class BuildGitCommitLinkMigrationAction method migrateBranchConfigurations.

private void migrateBranchConfigurations(Connection connection) throws SQLException, IOException {
    // For all branches...
    try (PreparedStatement psBranches = connection.prepareStatement("SELECT ID FROM BRANCHES")) {
        try (ResultSet rsBranches = psBranches.executeQuery()) {
            while (rsBranches.next()) {
                int branchId = rsBranches.getInt("ID");
                // For all the Git branch configurations
                try (PreparedStatement psProperty = connection.prepareStatement("SELECT * FROM PROPERTIES WHERE TYPE = ? AND BRANCH = ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
                    psProperty.setString(1, GitBranchConfigurationPropertyType.class.getName());
                    psProperty.setInt(2, branchId);
                    try (ResultSet rsProperty = psProperty.executeQuery()) {
                        while (rsProperty.next()) {
                            logger.info("Migrating Branch Git configuration: {}", branchId);
                            // Configuration as JSON
                            String json = rsProperty.getString("JSON");
                            // Parses the configuration
                            JsonNode node = objectMapper.readTree(json);
                            // Gets any existing tag pattern
                            String tagPattern = JsonUtils.get(node, "tagPattern", "*");
                            // Creates the commit link
                            ServiceConfiguration buildCommitLink;
                            if ("*".equals(tagPattern)) {
                                buildCommitLink = TagBuildNameGitCommitLink.DEFAULT.toServiceConfiguration();
                            } else {
                                buildCommitLink = new ServiceConfiguration("tagPattern", JsonUtils.object().with("pattern", tagPattern).end());
                            }
                            // Creates the new property
                            GitBranchConfigurationProperty property = new GitBranchConfigurationProperty(JsonUtils.get(node, "branch"), buildCommitLink, JsonUtils.getBoolean(node, "override"), JsonUtils.getInt(node, "buildTagInterval"));
                            // Saves the data back
                            rsProperty.updateString("json", objectMapper.writeValueAsString(property));
                            rsProperty.updateRow();
                        }
                    }
                }
            }
        }
    }
}
Also used : GitBranchConfigurationPropertyType(net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationPropertyType) ServiceConfiguration(net.nemerosa.ontrack.model.structure.ServiceConfiguration) GitBranchConfigurationProperty(net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

GitBranchConfigurationProperty (net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationProperty)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 GitBranchConfigurationPropertyType (net.nemerosa.ontrack.extension.git.property.GitBranchConfigurationPropertyType)1 GitRepositoryClient (net.nemerosa.ontrack.git.GitRepositoryClient)1 ServiceConfiguration (net.nemerosa.ontrack.model.structure.ServiceConfiguration)1