Skip to content

Commit 1d32484

Browse files
committed
Integration Tests and implementation for manipulating versions for forced deployment of OTHER branches (long-running POCs).
1 parent 990128d commit 1d32484

File tree

5 files changed

+163
-10
lines changed

5 files changed

+163
-10
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,22 @@ the artifacts built by the first job into a jboss application server.
371371
* If the detached HEAD commit resolves to a single branch type, it uses that branch name.
372372
3. If the first two methods fail, the plugin attempts to resolve `${env.GIT_BRANCH}`.
373373

374+
## To Debug the plugin (replicating a test-case but without being run from jUnit)
375+
You can 'bootstrap' the plugin into your local repository and get the test project stubbed by running:
376+
`mvn -Dmaven.test.skip=true install`
377+
378+
Then, change directories:
379+
`cd target/test-classes/project-stub`
380+
381+
From there, you'll need to supply the required environment variables or commandline arguments to `mvnDebug`:
382+
```
383+
export GIT_BRANCH=origin/feature/mybranch-foo-bar
384+
mvnDebug -Dstub.project.version=5.0.0-SNAPSHOT -DotherBranchDeploy=semver -DallowGitflowPluginSnapshot=true deploy
385+
```
386+
You can then connect a remote debugger and step through the plugin code.
387+
374388
## Building with IntelliJ IDEA notes
375-
### To Debug Tests:
389+
### To Debug Test Code:
376390
Configure the Maven commandline to include
377391
`-DforkMode=never` You will likely get warnings when you run maven with this argument.
378392

src/main/java/com/e_gineering/maven/gitflowhelper/AbstractGitflowBasedRepositoryMojo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
8484
@Parameter(defaultValue = "${repositorySystemSession}", required = true)
8585
RepositorySystemSession session;
8686

87+
@Parameter(property = "gitflow.force.other.deploy", defaultValue = "false", required = true)
88+
String forceOtherDeploy;
89+
8790
@Component
8891
private EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory;
8992

src/main/java/com/e_gineering/maven/gitflowhelper/AttachDeployedArtifactsMojo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
3333
getLog().info("Attaching artifacts from snapshot repository...");
3434
attachExistingArtifacts(snapshotDeploymentRepository, true);
3535
break;
36+
}
37+
case OTHER: {
38+
3639
}
3740
default: {
3841
getLog().info("Attaching Artifacts from local repository...");

src/main/java/com/e_gineering/maven/gitflowhelper/RetargetDeployMojo.java

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.e_gineering.maven.gitflowhelper;
22

3+
import org.apache.maven.artifact.Artifact;
4+
import org.apache.maven.artifact.ArtifactUtils;
35
import org.apache.maven.model.DistributionManagement;
46
import org.apache.maven.plugin.MojoExecutionException;
57
import org.apache.maven.plugin.MojoFailureException;
@@ -20,31 +22,96 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
2022
switch (gitBranchInfo.getType()) {
2123
case SUPPORT:
2224
case MASTER: {
23-
getLog().info("Setting release artifact repository to: [" + releaseDeploymentRepository + "]");
24-
project.setSnapshotArtifactRepository(null);
25-
project.setReleaseArtifactRepository(getDeploymentRepository(releaseDeploymentRepository));
25+
setTargetRelease();
2626
break;
2727
}
2828
case RELEASE:
2929
case HOTFIX: {
30-
getLog().info("Setting release artifact repository to: [" + stageDeploymentRepository + "]");
31-
project.setSnapshotArtifactRepository(null);
32-
project.setReleaseArtifactRepository(getDeploymentRepository(stageDeploymentRepository));
30+
setTargetStage();
3331
break;
3432
}
3533
case DEVELOPMENT: {
36-
getLog().info("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]");
37-
project.setSnapshotArtifactRepository(getDeploymentRepository(snapshotDeploymentRepository));
38-
project.setReleaseArtifactRepository(null);
34+
setTargetSnapshots();
3935
break;
4036
}
37+
case OTHER: {
38+
// Other branches never target release, but may target stage for non-SNAPSHOT artifacts.
39+
// For this reason, "overwrite" is considered _highly_ dangerous.
40+
if (!"false".equalsIgnoreCase(forceOtherDeploy)) {
41+
// Setup the target based on the base project version.
42+
if (ArtifactUtils.isSnapshot(project.getVersion())) {
43+
setTargetSnapshots();
44+
} else {
45+
setTargetStage();
46+
}
47+
48+
// Monkey with things to do our semVer magic.
49+
if ("semVer".equalsIgnoreCase(forceOtherDeploy)) {
50+
if (ArtifactUtils.isSnapshot(project.getVersion())) {
51+
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.");
52+
getLog().info("The gitflow-helper-maven-plugin will inject the build metadata preceding the -SNAPSHOT, allowing for snapshot deployments of this branch.");
53+
}
54+
String branchName = gitBranchInfo.getName();
55+
String semVerAddition = "+" + branchName.replaceAll("[^0-9^A-Z^a-z^-^.]", "-");
56+
57+
updateArtifactVersion(project.getArtifact(), semVerAddition);
58+
for (Artifact a : project.getAttachedArtifacts()) {
59+
updateArtifactVersion(a, semVerAddition);
60+
}
61+
62+
getLog().info("Artifact versions updated with semVer build metadata: " + semVerAddition);
63+
}
64+
if ("overwrite".equalsIgnoreCase(forceOtherDeploy)) {
65+
getLog().warn("DANGER! DANGER, WILL ROBINSON!");
66+
getLog().warn("Deployment of this build will OVERWRITE Deployment of " + project.getVersion() + " in the targeted repository.");
67+
getLog().warn("THIS IS NOT RECOMMENDED.");
68+
}
69+
break;
70+
}
71+
}
4172
default: {
4273
unsetRepos();
4374
break;
4475
}
4576
}
4677
}
4778

79+
private void updateArtifactVersion(final Artifact a, final String semVerAdditon) {
80+
String baseVersion = a.getBaseVersion();
81+
String version = a.getVersion();
82+
83+
String semBaseVersion = baseVersion + semVerAdditon;
84+
String semVersion = version + semVerAdditon;
85+
86+
if (semBaseVersion.contains("-SNAPSHOT")) {
87+
semBaseVersion = semBaseVersion.replace("-SNAPSHOT", "") + "-SNAPSHOT";
88+
}
89+
if (semVersion.contains("-SNAPSHOT")) {
90+
semVersion = semVersion.replace("-SNAPSHOT", "") + "-SNAPSHOT";
91+
}
92+
a.setBaseVersion(semBaseVersion);
93+
a.setVersion(semVersion);
94+
}
95+
96+
97+
private void setTargetSnapshots() throws MojoExecutionException, MojoFailureException {
98+
getLog().info("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]");
99+
project.setSnapshotArtifactRepository(getDeploymentRepository(snapshotDeploymentRepository));
100+
project.setReleaseArtifactRepository(null);
101+
}
102+
103+
private void setTargetStage() throws MojoExecutionException, MojoFailureException {
104+
getLog().info("Setting release artifact repository to: [" + stageDeploymentRepository + "]");
105+
project.setSnapshotArtifactRepository(null);
106+
project.setReleaseArtifactRepository(getDeploymentRepository(stageDeploymentRepository));
107+
}
108+
109+
private void setTargetRelease() throws MojoExecutionException, MojoFailureException {
110+
getLog().info("Setting release artifact repository to: [" + releaseDeploymentRepository + "]");
111+
project.setSnapshotArtifactRepository(null);
112+
project.setReleaseArtifactRepository(getDeploymentRepository(releaseDeploymentRepository));
113+
}
114+
48115
private void unsetRepos() {
49116
getLog().info("Un-Setting artifact repositories.");
50117
project.setSnapshotArtifactRepository(null);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.e_gineering.maven.gitflowhelper;
2+
3+
import org.apache.maven.it.Verifier;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.BlockJUnit4ClassRunner;
7+
8+
@RunWith(BlockJUnit4ClassRunner.class)
9+
public class OtherBranchIT extends AbstractIntegrationTest {
10+
@Test
11+
public void featureSnapshotSemVer() throws Exception {
12+
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch", "5.0.0-SNAPSHOT");
13+
try {
14+
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=semver");
15+
verifier.executeGoal("deploy");
16+
17+
verifier.verifyTextInLog("Artifact versions updated with semVer build metadata: +origin-feature-my-feature-branch");
18+
verifier.verifyErrorFreeLog();
19+
} finally {
20+
verifier.resetStreams();
21+
}
22+
}
23+
24+
@Test
25+
public void featureSnapshotOverwrite() throws Exception {
26+
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch", "5.0.0-SNAPSHOT");
27+
try {
28+
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=overwrite");
29+
verifier.executeGoal("deploy");
30+
31+
verifier.verifyTextInLog("DANGER! DANGER, WILL ROBINSON!");
32+
verifier.verifyErrorFreeLog();
33+
} finally {
34+
verifier.resetStreams();
35+
}
36+
}
37+
38+
@Test
39+
public void featureSemVer() throws Exception {
40+
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch.with.other.identifiers", "5.0.1");
41+
try {
42+
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=semver");
43+
verifier.executeGoal("deploy");
44+
45+
verifier.verifyTextInLog("Artifact versions updated with semVer build metadata: +origin-feature-my-feature-branch.with.other.identifiers");
46+
verifier.verifyErrorFreeLog();
47+
} finally {
48+
verifier.resetStreams();
49+
}
50+
}
51+
52+
@Test
53+
public void featureOverwrite() throws Exception {
54+
Verifier verifier = createVerifier("/project-stub", "origin/feature/my-feature-branch", "5.0.1");
55+
try {
56+
verifier.getCliOptions().add("-Dgitflow.force.other.deploy=overwrite");
57+
verifier.executeGoal("deploy");
58+
59+
verifier.verifyTextInLog("DANGER! DANGER, WILL ROBINSON!");
60+
verifier.verifyErrorFreeLog();
61+
} finally {
62+
verifier.resetStreams();
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)