Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Updated code per review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mockitoguy committed Nov 12, 2016
1 parent 49b8965 commit 2b14666
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
* - if one of the steps fails, further steps are not executed
* - onlyIf {} predicate can be used to cleanly abort the release process based on predicate
*
* Each step can have one post-step task, either a 'rollback' or 'cleanup'.
*
* Rollback tasks:
* - executed only when one the release tasks fails, otherwise they are skipped
* - executed in reverse order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class ReleaseWorkflowExtension implements ReleaseWorkflow {

final List<Task> steps = new ArrayList<Task>();
Task previousStep;
Task previousRollback;
final List<Task> rollbacks = new ArrayList<Task>();
Task previousPostStep;
final List<Task> postSteps = new ArrayList<Task>();
private final Project project;
private final List<Callable<Boolean>> allowers = new ArrayList<Callable<Boolean>>();

Expand Down Expand Up @@ -69,43 +69,43 @@ private void addStep(final Task task, Map<String, Task> config) {
return; //no rollback/cleanup configured
}

Task rollback = stepConfig.getRollback();
if (rollback != null) {
//rollbacks only run when one of the steps fails, by default we assume they don't fail
Task postStep = stepConfig.getRollback();
if (postStep != null) {
//postSteps only run when one of the steps fails, by default we assume they don't fail
if (!project.hasProperty("dryRun")) { //accommodate testing
rollback.setEnabled(false);
postStep.setEnabled(false);
}
} else {
rollback = stepConfig.getCleanup();
postStep = stepConfig.getCleanup();
//cleanups run even if the release is successful
}

//populate main rollbacks list
rollbacks.add(rollback);
//populate main postSteps list
postSteps.add(postStep);

//rollback must run after every main task
rollback.mustRunAfter(steps);
postStep.mustRunAfter(steps);

//rollbacks need to have order between themselves
if (previousRollback != null) {
previousRollback.mustRunAfter(rollback);
//postSteps need to have order between themselves
if (previousPostStep != null) {
previousPostStep.mustRunAfter(postStep);
}
previousRollback = rollback;
previousPostStep = postStep;

//rollbacks finalize release steps
task.finalizedBy(rollback);
//postSteps finalize release steps
task.finalizedBy(postStep);

//rollbacks only run when their main task did not fail
//postSteps only run when their main task did not fail
// when main task fails, there is nothing to rollback
rollback.onlyIf(new Spec<Task>() {
postStep.onlyIf(new Spec<Task>() {
public boolean isSatisfiedBy(Task t) {
return task.getState().getFailure() == null;
}
});

//only run rollback if it is allowed
for (Callable<Boolean> allower : allowers) {
rollback.onlyIf(new SpecAdapter(allower));
postStep.onlyIf(new SpecAdapter(allower));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@ public void apply(final Project project) {
project.getTasks().create("release");
final ReleaseWorkflowExtension ext = project.getExtensions().create("releaseWorkflow", ReleaseWorkflowExtension.class, project);

//setup listener, so that the rollbacks are only executed if one of the main tasks fail
project.getGradle().addListener(new TaskExecutionListener() {
public void beforeExecute(Task task) {}
public void afterExecute(Task task, TaskState taskState) {
//when one of the main step tasks fails, enable all rollback tasks
if (taskState.getFailure() != null && ext.steps.contains(task)) {
for (Task rollback : ext.rollbacks) {
rollback.setEnabled(true);
}
}
}
});
//setup listener, so that the postSteps are only executed if one of the main tasks fail
project.getGradle().addListener(new PostStepTaskEnabler(ext));

//TODO very implicit, it needs to go to some tools for release, for example, "releaseTools.git"
GitTool gitTool = Git.gitTool(Exec.getProcessRunner(project.getProjectDir()));
project.getExtensions().getExtraProperties().set("gitTool", gitTool);
}

private static class PostStepTaskEnabler implements TaskExecutionListener {
private final ReleaseWorkflowExtension ext;

PostStepTaskEnabler(ReleaseWorkflowExtension ext) {
this.ext = ext;
}

public void beforeExecute(Task task) {}

public void afterExecute(Task task, TaskState taskState) {
//when one of the main step tasks fails, enable all rollback tasks
if (taskState.getFailure() != null && ext.steps.contains(task)) {
for (Task rollback : ext.postSteps) {
rollback.setEnabled(true);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ private static void validateConfig(Map<String, Task> config) {
throw new GradleException("Step configuration can only have one setting either 'rollback' or 'cleanup'. " +
"Please refer to the documentation.");
}
String key = config.keySet().iterator().next();
if (!(config.get(key) instanceof Task)) {
throw new GradleException("Step configuration '" + key + "' must refer to a Gradle task but it is: '" +
config.get(key) + "'. Please refer to the documentation.");
Map.Entry<String, Task> entry = config.entrySet().iterator().next();
if (!(entry.getValue() instanceof Task)) {
throw new GradleException("Step configuration '" + entry.getKey() + "' must refer to a Gradle task but it is: '" +
entry.getValue() + "'. Please refer to the documentation.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class GradleSpecification extends Specification {
BuildResult pass(String... args) {
try {
GradleRunner.create()
.withProjectDir(projectDir.root)
.withArguments(args)
.build()
.withProjectDir(projectDir.root)
.withArguments(args)
.build()
} catch (Exception e) {
println " ---- build.gradle ---- \n" + buildFile.text + "\n ------------------------"
throw e
Expand All @@ -54,9 +54,9 @@ class GradleSpecification extends Specification {
BuildResult fail(String... args) {
try {
GradleRunner.create()
.withProjectDir(projectDir.root)
.withArguments(args)
.buildAndFail()
.withProjectDir(projectDir.root)
.withArguments(args)
.buildAndFail()
} catch (Exception e) {
println " ---- build.gradle ---- \n" + buildFile.text + "\n ------------------------"
throw e
Expand Down

0 comments on commit 2b14666

Please sign in to comment.