Search in sources :

Example 1 with ScmException

use of com.itemis.maven.plugins.unleash.scm.ScmException in project unleash-maven-plugin by shillner.

the class ScmPomVersionsMergeClient method mergeParentVersions.

private void mergeParentVersions(Model local, Model remote, Model base, Model result) throws ScmException {
    String localParentVersion = local.getParent() != null ? local.getParent().getVersion() : null;
    String remoteParentVersion = remote.getParent() != null ? remote.getParent().getVersion() : null;
    String baseParentVersion = base.getParent() != null ? base.getParent().getVersion() : null;
    boolean remoteParentRemoved = remoteParentVersion == null && baseParentVersion != null;
    boolean remoteParentAdded = remoteParentVersion != null && baseParentVersion == null;
    boolean remoteParentVersionChanged = !Objects.equal(remoteParentVersion, baseParentVersion);
    boolean localParentRemoved = localParentVersion == null && baseParentVersion != null;
    boolean localParentAdded = localParentVersion != null && baseParentVersion == null;
    boolean localParentVersionChanged = !Objects.equal(localParentVersion, baseParentVersion);
    if (localParentAdded) {
        // if locally added the base had no parent (remote remove and change is not relevant)
        if (remoteParentAdded) {
            if (Objects.equal(local.getParent().getArtifactId(), remote.getParent().getArtifactId()) && Objects.equal(local.getParent().getGroupId(), remote.getParent().getGroupId())) {
                if (MavenVersionUtil.isNewerVersion(local.getParent().getVersion(), remote.getParent().getVersion())) {
                    result.setParent(local.getParent());
                }
            } else {
                throw new ScmException(ScmOperation.MERGE, "Could not merge local and remote POM parent changes since both versions added different parent artifacts.");
            }
        } else {
            result.setParent(local.getParent());
        }
    } else if (localParentRemoved) {
        // if locally removed the base had a parent (remote add is not relevant and remote remove is ok)
        if (remoteParentVersionChanged) {
            throw new ScmException(ScmOperation.MERGE, "Could not merge POM parent version conflicts since in the local POM the parent had been removed and in the remote POM the parent had been changed.");
        } else {
            result.getParent().setVersion(localParentVersion);
        }
    } else if (localParentVersionChanged) {
        // if locally changed the base had a parent (remote add is not relevant)
        if (remoteParentVersionChanged) {
            if (Objects.equal(local.getParent().getArtifactId(), remote.getParent().getArtifactId()) && Objects.equal(local.getParent().getGroupId(), remote.getParent().getGroupId())) {
                if (MavenVersionUtil.isNewerVersion(local.getParent().getVersion(), remote.getParent().getVersion())) {
                    result.setParent(local.getParent());
                }
            } else {
                throw new ScmException(ScmOperation.MERGE, "Could not merge local and remote POM parent changes since both versions are referencing different parent artifacts.");
            }
        } else if (remoteParentRemoved) {
            throw new ScmException(ScmOperation.MERGE, "Could not merge POM parent version conflicts since in the local POM the parent had been updated while in the remote POM the parent had been removed.");
        } else {
            result.getParent().setVersion(localParentVersion);
        }
    }
}
Also used : ScmException(com.itemis.maven.plugins.unleash.scm.ScmException)

Example 2 with ScmException

use of com.itemis.maven.plugins.unleash.scm.ScmException in project unleash-maven-plugin by shillner.

the class ScmPomVersionsMergeClient method merge.

@Override
public void merge(InputStream local, InputStream remote, InputStream base, OutputStream result) throws ScmException {
    Optional<Model> localModel = loadModel(local);
    if (!localModel.isPresent()) {
        // TODO implement merge of other files!
        throw new ScmException(ScmOperation.MERGE, "Unable to merge non-POM changes.");
    }
    byte[] remoteData = null;
    try {
        remoteData = ByteStreams.toByteArray(remote);
    } catch (IOException e) {
        throw new ScmException(ScmOperation.MERGE, "Unable to read remote content!", e);
    } finally {
        Closeables.closeQuietly(remote);
    }
    Optional<Model> remoteModel = loadModel(new ByteArrayInputStream(remoteData));
    Optional<Model> baseModel = loadModel(base);
    Model resultModel = loadModel(new ByteArrayInputStream(remoteData)).get();
    mergeVersions(localModel.get(), remoteModel.get(), baseModel.get(), resultModel);
    mergeParentVersions(localModel.get(), remoteModel.get(), baseModel.get(), resultModel);
    try {
        Document document = PomUtil.parsePOM(new ByteArrayInputStream(remoteData));
        PomUtil.setProjectVersion(resultModel, document, resultModel.getVersion());
        if (resultModel.getParent() != null) {
            PomUtil.setParentVersion(resultModel, document, resultModel.getParent().getVersion());
        }
        PomUtil.writePOM(document, result, false);
    } catch (Throwable t) {
        throw new ScmException(ScmOperation.MERGE, "Could not serialize merged POM!", t);
    }
}
Also used : ScmException(com.itemis.maven.plugins.unleash.scm.ScmException) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.apache.maven.model.Model) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 3 with ScmException

use of com.itemis.maven.plugins.unleash.scm.ScmException in project unleash-maven-plugin by shillner.

the class TagScm method rollback.

@RollbackOnError
private void rollback() throws MojoExecutionException {
    this.log.info("Rollback of SCM tag creation and POM modifications due to a processing exception.");
    String scmTagName = this.metadata.getScmTagName();
    StringBuilder deleteTagMessageBuilder = new StringBuilder("Deletion of tag '").append(scmTagName).append("' due to release rollback.");
    if (StringUtils.isNotBlank(this.scmMessagePrefix)) {
        deleteTagMessageBuilder.insert(0, this.scmMessagePrefix);
    }
    DeleteTagRequest.Builder deleteTagRequestBuilder = DeleteTagRequest.builder().message(deleteTagMessageBuilder.toString()).tagName(scmTagName);
    if (this.scmProvider.hasTag(scmTagName)) {
        deleteTagRequestBuilder.push();
    }
    if (!this.tagWasPresent) {
        this.log.debug("\tDeleting scm tag '" + scmTagName + "' since the release build failed.");
        this.scmProvider.deleteTag(deleteTagRequestBuilder.build());
    } else {
        this.log.debug("\tSkipping deletion of SCM tag '" + scmTagName + "' since the tag was already present before the release build was triggered.");
    }
    if (this.commitBeforeTagging) {
        StringBuilder revertCommitsMessageBuilder = new StringBuilder("Reversion of failed release build (step: tag SCM).");
        if (StringUtils.isNotBlank(this.scmMessagePrefix)) {
            revertCommitsMessageBuilder.insert(0, this.scmMessagePrefix);
        }
        String fromRevision = MoreObjects.firstNonNull(this.metadata.getScmRevisionAfterTag(), this.scmProvider.getLocalRevision());
        RevertCommitsRequest revertCommitsRequest = RevertCommitsRequest.builder().fromRevision(fromRevision).toRevision(this.metadata.getScmRevisionBeforeTag()).message(revertCommitsMessageBuilder.toString()).merge().mergeClient(new ScmPomVersionsMergeClient()).build();
        this.scmProvider.revertCommits(revertCommitsRequest);
        try {
            this.scmProvider.push(PushRequest.builder().build());
        } catch (ScmException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    for (MavenProject project : this.reactorProjects) {
        Document document = this.cachedPOMs.get(ProjectToCoordinates.EMPTY_VERSION.apply(project));
        if (document != null) {
            try {
                PomUtil.writePOM(document, project);
            } catch (Throwable t) {
                throw new MojoExecutionException("Could not revert SCM connection adaption after a failed release build. Tried to reset tag connection URL to initial state.", t);
            }
        }
    }
}
Also used : ScmException(com.itemis.maven.plugins.unleash.scm.ScmException) DeleteTagRequest(com.itemis.maven.plugins.unleash.scm.requests.DeleteTagRequest) ScmPomVersionsMergeClient(com.itemis.maven.plugins.unleash.util.scm.ScmPomVersionsMergeClient) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenProject(org.apache.maven.project.MavenProject) RevertCommitsRequest(com.itemis.maven.plugins.unleash.scm.requests.RevertCommitsRequest) ProjectToString(com.itemis.maven.plugins.unleash.util.functions.ProjectToString) Document(org.w3c.dom.Document) RollbackOnError(com.itemis.maven.plugins.cdi.annotations.RollbackOnError)

Aggregations

ScmException (com.itemis.maven.plugins.unleash.scm.ScmException)3 Document (org.w3c.dom.Document)2 RollbackOnError (com.itemis.maven.plugins.cdi.annotations.RollbackOnError)1 DeleteTagRequest (com.itemis.maven.plugins.unleash.scm.requests.DeleteTagRequest)1 RevertCommitsRequest (com.itemis.maven.plugins.unleash.scm.requests.RevertCommitsRequest)1 ProjectToString (com.itemis.maven.plugins.unleash.util.functions.ProjectToString)1 ScmPomVersionsMergeClient (com.itemis.maven.plugins.unleash.util.scm.ScmPomVersionsMergeClient)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Model (org.apache.maven.model.Model)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenProject (org.apache.maven.project.MavenProject)1