Skip to content

Commit

Permalink
Integration Tests and implementation for manipulating versions for fo…
Browse files Browse the repository at this point in the history
…rced deployment of OTHER branches (long-running POCs).
  • Loading branch information
bvarner committed Aug 14, 2018
1 parent 990128d commit 1d32484
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 10 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,22 @@ the artifacts built by the first job into a jboss application server.
* If the detached HEAD commit resolves to a single branch type, it uses that branch name.
3. If the first two methods fail, the plugin attempts to resolve `${env.GIT_BRANCH}`.

## To Debug the plugin (replicating a test-case but without being run from jUnit)
You can 'bootstrap' the plugin into your local repository and get the test project stubbed by running:
`mvn -Dmaven.test.skip=true install`

Then, change directories:
`cd target/test-classes/project-stub`

From there, you'll need to supply the required environment variables or commandline arguments to `mvnDebug`:
```
export GIT_BRANCH=origin/feature/mybranch-foo-bar
mvnDebug -Dstub.project.version=5.0.0-SNAPSHOT -DotherBranchDeploy=semver -DallowGitflowPluginSnapshot=true deploy
```
You can then connect a remote debugger and step through the plugin code.

## Building with IntelliJ IDEA notes
### To Debug Tests:
### To Debug Test Code:
Configure the Maven commandline to include
`-DforkMode=never` You will likely get warnings when you run maven with this argument.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
@Parameter(defaultValue = "${repositorySystemSession}", required = true)
RepositorySystemSession session;

@Parameter(property = "gitflow.force.other.deploy", defaultValue = "false", required = true)
String forceOtherDeploy;

@Component
private EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
getLog().info("Attaching artifacts from snapshot repository...");
attachExistingArtifacts(snapshotDeploymentRepository, true);
break;
}
case OTHER: {

}
default: {
getLog().info("Attaching Artifacts from local repository...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.e_gineering.maven.gitflowhelper;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -20,31 +22,96 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
switch (gitBranchInfo.getType()) {
case SUPPORT:
case MASTER: {
getLog().info("Setting release artifact repository to: [" + releaseDeploymentRepository + "]");
project.setSnapshotArtifactRepository(null);
project.setReleaseArtifactRepository(getDeploymentRepository(releaseDeploymentRepository));
setTargetRelease();
break;
}
case RELEASE:
case HOTFIX: {
getLog().info("Setting release artifact repository to: [" + stageDeploymentRepository + "]");
project.setSnapshotArtifactRepository(null);
project.setReleaseArtifactRepository(getDeploymentRepository(stageDeploymentRepository));
setTargetStage();
break;
}
case DEVELOPMENT: {
getLog().info("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]");
project.setSnapshotArtifactRepository(getDeploymentRepository(snapshotDeploymentRepository));
project.setReleaseArtifactRepository(null);
setTargetSnapshots();
break;
}
case OTHER: {
// Other branches never target release, but may target stage for non-SNAPSHOT artifacts.
// For this reason, "overwrite" is considered _highly_ dangerous.
if (!"false".equalsIgnoreCase(forceOtherDeploy)) {
// Setup the target based on the base project version.
if (ArtifactUtils.isSnapshot(project.getVersion())) {
setTargetSnapshots();
} else {
setTargetStage();
}

// Monkey with things to do our semVer magic.
if ("semVer".equalsIgnoreCase(forceOtherDeploy)) {
if (ArtifactUtils.isSnapshot(project.getVersion())) {
getLog().warn("Maven -SNAPSHOT builds break semVer standards, in that -SNAPSHOT must be the _last_ poriton of a maven version. In semVer, the pre-release status is supposed to come before the build meta-data.");
getLog().info("The gitflow-helper-maven-plugin will inject the build metadata preceding the -SNAPSHOT, allowing for snapshot deployments of this branch.");
}
String branchName = gitBranchInfo.getName();
String semVerAddition = "+" + branchName.replaceAll("[^0-9^A-Z^a-z^-^.]", "-");

updateArtifactVersion(project.getArtifact(), semVerAddition);
for (Artifact a : project.getAttachedArtifacts()) {
updateArtifactVersion(a, semVerAddition);
}

getLog().info("Artifact versions updated with semVer build metadata: " + semVerAddition);
}
if ("overwrite".equalsIgnoreCase(forceOtherDeploy)) {
getLog().warn("DANGER! DANGER, WILL ROBINSON!");
getLog().warn("Deployment of this build will OVERWRITE Deployment of " + project.getVersion() + " in the targeted repository.");
getLog().warn("THIS IS NOT RECOMMENDED.");
}
break;
}
}
default: {
unsetRepos();
break;
}
}
}

private void updateArtifactVersion(final Artifact a, final String semVerAdditon) {
String baseVersion = a.getBaseVersion();
String version = a.getVersion();

String semBaseVersion = baseVersion + semVerAdditon;
String semVersion = version + semVerAdditon;

if (semBaseVersion.contains("-SNAPSHOT")) {
semBaseVersion = semBaseVersion.replace("-SNAPSHOT", "") + "-SNAPSHOT";
}
if (semVersion.contains("-SNAPSHOT")) {
semVersion = semVersion.replace("-SNAPSHOT", "") + "-SNAPSHOT";
}
a.setBaseVersion(semBaseVersion);
a.setVersion(semVersion);
}


private void setTargetSnapshots() throws MojoExecutionException, MojoFailureException {
getLog().info("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]");
project.setSnapshotArtifactRepository(getDeploymentRepository(snapshotDeploymentRepository));
project.setReleaseArtifactRepository(null);
}

private void setTargetStage() throws MojoExecutionException, MojoFailureException {
getLog().info("Setting release artifact repository to: [" + stageDeploymentRepository + "]");
project.setSnapshotArtifactRepository(null);
project.setReleaseArtifactRepository(getDeploymentRepository(stageDeploymentRepository));
}

private void setTargetRelease() throws MojoExecutionException, MojoFailureException {
getLog().info("Setting release artifact repository to: [" + releaseDeploymentRepository + "]");
project.setSnapshotArtifactRepository(null);
project.setReleaseArtifactRepository(getDeploymentRepository(releaseDeploymentRepository));
}

private void unsetRepos() {
getLog().info("Un-Setting artifact repositories.");
project.setSnapshotArtifactRepository(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.e_gineering.maven.gitflowhelper;

import org.apache.maven.it.Verifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class OtherBranchIT extends AbstractIntegrationTest {
@Test
public void featureSnapshotSemVer() throws Exception {
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch", "5.0.0-SNAPSHOT");
try {
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=semver");
verifier.executeGoal("deploy");

verifier.verifyTextInLog("Artifact versions updated with semVer build metadata: +origin-feature-my-feature-branch");
verifier.verifyErrorFreeLog();
} finally {
verifier.resetStreams();
}
}

@Test
public void featureSnapshotOverwrite() throws Exception {
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch", "5.0.0-SNAPSHOT");
try {
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=overwrite");
verifier.executeGoal("deploy");

verifier.verifyTextInLog("DANGER! DANGER, WILL ROBINSON!");
verifier.verifyErrorFreeLog();
} finally {
verifier.resetStreams();
}
}

@Test
public void featureSemVer() throws Exception {
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch.with.other.identifiers", "5.0.1");
try {
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=semver");
verifier.executeGoal("deploy");

verifier.verifyTextInLog("Artifact versions updated with semVer build metadata: +origin-feature-my-feature-branch.with.other.identifiers");
verifier.verifyErrorFreeLog();
} finally {
verifier.resetStreams();
}
}

@Test
public void featureOverwrite() throws Exception {
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch", "5.0.1");
try {
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=overwrite");
verifier.executeGoal("deploy");

verifier.verifyTextInLog("DANGER! DANGER, WILL ROBINSON!");
verifier.verifyErrorFreeLog();
} finally {
verifier.resetStreams();
}
}

}

0 comments on commit 1d32484

Please sign in to comment.