diff --git a/src/integration/groovy/pl/allegro/tech/build/axion/release/BaseIntegrationTest.groovy b/src/integration/groovy/pl/allegro/tech/build/axion/release/BaseIntegrationTest.groovy index 771ed37e..c0fc5bad 100644 --- a/src/integration/groovy/pl/allegro/tech/build/axion/release/BaseIntegrationTest.groovy +++ b/src/integration/groovy/pl/allegro/tech/build/axion/release/BaseIntegrationTest.groovy @@ -37,6 +37,7 @@ class BaseIntegrationTest extends RepositoryBasedTest { return GradleRunner.create() .withProjectDir(temporaryFolder) .withPluginClasspath() + .forwardOutput() } BuildResult runGradle(String... arguments) { diff --git a/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy b/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy index 24ce14aa..626c7a04 100644 --- a/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy +++ b/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy @@ -154,8 +154,8 @@ class SimpleIntegrationTest extends BaseIntegrationTest { def releaseResult = runGradle('release', '-Prelease.version=1.0.0', '-Prelease.localOnly', '-Prelease.disableChecks') then: - releaseResult.task(':release').outcome == TaskOutcome.SUCCESS - releaseResult.output.contains('Release step skipped since \'releaseOnlyOnReleaseBranches\' option is set, and \'master\' was not in \'releaseBranchNames\' list [develop,release]') + releaseResult.task(':release').outcome == TaskOutcome.SKIPPED + releaseResult.task(':verifyRelease').outcome == TaskOutcome.SKIPPED } def "should skip release when releaseOnlyOnReleaseBranches is set by gradle task property and current branch is not on releaseBranchNames list"() { @@ -166,8 +166,8 @@ class SimpleIntegrationTest extends BaseIntegrationTest { def releaseResult = runGradle('release', '-Prelease.releaseOnlyOnReleaseBranches', '-Prelease.releaseBranchNames=develop,release', '-Prelease.version=1.0.0', '-Prelease.localOnly', '-Prelease.disableChecks') then: - releaseResult.task(':release').outcome == TaskOutcome.SUCCESS - releaseResult.output.contains('Release step skipped since \'releaseOnlyOnReleaseBranches\' option is set, and \'master\' was not in \'releaseBranchNames\' list [develop,release]') + releaseResult.task(':release').outcome == TaskOutcome.SKIPPED + releaseResult.task(':verifyRelease').outcome == TaskOutcome.SKIPPED } def "should not skip release when releaseOnlyOnReleaseBranches is true when on master branch (default releaseBranches list)"() { diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/CreateReleaseTask.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/CreateReleaseTask.groovy index 0ba97aa5..774626af 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/CreateReleaseTask.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/CreateReleaseTask.groovy @@ -10,11 +10,6 @@ abstract class CreateReleaseTask extends BaseAxionTask { void release() { VersionResolutionContext context = resolutionContext() Releaser releaser = context.releaser() - ReleaseBranchesConfiguration releaseBranchesConfiguration = new ReleaseBranchesConfiguration( - context.scmService().isReleaseOnlyOnReleaseBranches(), - context.repository().currentPosition().getBranch(), - context.scmService().getReleaseBranchNames() - ) - releaser.release(context.rules(), releaseBranchesConfiguration) + releaser.release(context.rules()) } } diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseBranchesConfiguration.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseBranchesConfiguration.groovy deleted file mode 100644 index d4a8ab7f..00000000 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseBranchesConfiguration.groovy +++ /dev/null @@ -1,29 +0,0 @@ -package pl.allegro.tech.build.axion.release - -class ReleaseBranchesConfiguration { - private final boolean releaseOnlyOnReleaseBranches - private final String currentBranch - private final Set releaseBranchNames - - ReleaseBranchesConfiguration( - boolean releaseOnlyOnReleaseBranches, - String currentBranch, - Set releaseBranchNames - ) { - this.releaseOnlyOnReleaseBranches = releaseOnlyOnReleaseBranches - this.currentBranch = currentBranch - this.releaseBranchNames = releaseBranchNames - } - - boolean shouldRelease() { - return releaseOnlyOnReleaseBranches && !releaseBranchNames.contains(currentBranch) - } - - String getCurrentBranch() { - return currentBranch - } - - Set getReleaseBranchNames() { - return releaseBranchNames - } -} diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/ReleasePlugin.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/ReleasePlugin.groovy index 91f28c1d..0ea42002 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/ReleasePlugin.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/ReleasePlugin.groovy @@ -4,6 +4,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import pl.allegro.tech.build.axion.release.domain.SnapshotDependenciesChecker import pl.allegro.tech.build.axion.release.domain.VersionConfig +import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext import pl.allegro.tech.build.axion.release.util.FileLoader abstract class ReleasePlugin implements Plugin { @@ -63,5 +64,36 @@ abstract class ReleasePlugin implements Plugin { group = 'Help' description = 'Prints current project version extracted from SCM.' } + + maybeDisableReleaseTasks(project, versionConfig) + } + + private static void maybeDisableReleaseTasks(Project project, VersionConfig versionConfig) { + project.afterEvaluate { + def context = VersionResolutionContext.create(versionConfig, project.layout.projectDirectory) + def releaseOnlyOnReleaseBranches = context.scmService().isReleaseOnlyOnReleaseBranches() + def releaseBranchNames = context.scmService().getReleaseBranchNames() + def currentBranch = context.repository().currentPosition().getBranch() + + def shouldSkipRelease = releaseOnlyOnReleaseBranches && !releaseBranchNames.contains(currentBranch) + + if (shouldSkipRelease) { + disableReleaseTasks(currentBranch, releaseBranchNames, project) + } + } + } + + private static void disableReleaseTasks(String currentBranch, Set releaseBranchNames, Project project) { + String message = String.format( + "Release will be skipped since 'releaseOnlyOnReleaseBranches' option is set, and '%s' was not in 'releaseBranchNames' list [%s]", + currentBranch, + String.join(",", releaseBranchNames) + ) + project.logger.lifecycle(message); + + List tasksToDisable = [RELEASE_TASK, CREATE_RELEASE_TASK, PUSH_RELEASE_TASK, VERIFY_RELEASE_TASK] + project.tasks + .matching { it.name in tasksToDisable } + .configureEach { it.enabled = false } } } diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseTask.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseTask.groovy index 702e27fc..05f93945 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseTask.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseTask.groovy @@ -16,13 +16,8 @@ abstract class ReleaseTask extends BaseAxionTask { void release() { VersionResolutionContext context = resolutionContext() Releaser releaser = context.releaser() - ReleaseBranchesConfiguration releaseBranchesConfiguration = new ReleaseBranchesConfiguration( - context.scmService().isReleaseOnlyOnReleaseBranches(), - context.repository().currentPosition().getBranch(), - context.scmService().getReleaseBranchNames() - ) - ScmPushResult result = releaser.releaseAndPush(context.rules(), releaseBranchesConfiguration) + ScmPushResult result = releaser.releaseAndPush(context.rules()) if (result.outcome === ScmPushResultOutcome.FAILED) { def status = result.failureStatus diff --git a/src/main/java/pl/allegro/tech/build/axion/release/domain/Releaser.java b/src/main/java/pl/allegro/tech/build/axion/release/domain/Releaser.java index c70dd4d3..9e06bedc 100644 --- a/src/main/java/pl/allegro/tech/build/axion/release/domain/Releaser.java +++ b/src/main/java/pl/allegro/tech/build/axion/release/domain/Releaser.java @@ -3,7 +3,6 @@ import com.github.zafarkhaja.semver.Version; import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; -import pl.allegro.tech.build.axion.release.ReleaseBranchesConfiguration; import pl.allegro.tech.build.axion.release.domain.hooks.ReleaseHooksRunner; import pl.allegro.tech.build.axion.release.domain.properties.Properties; import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResult; @@ -25,16 +24,7 @@ public Releaser(VersionService versionService, ScmService repository, ReleaseHoo this.hooksRunner = hooksRunner; } - public Optional release(Properties properties, ReleaseBranchesConfiguration releaseBranchesConfiguration) { - if (releaseBranchesConfiguration.shouldRelease()) { - String message = String.format( - "Release step skipped since 'releaseOnlyOnReleaseBranches' option is set, and '%s' was not in 'releaseBranchNames' list [%s]", - releaseBranchesConfiguration.getCurrentBranch(), - String.join(",", releaseBranchesConfiguration.getReleaseBranchNames()) - ); - logger.quiet(message); - return Optional.empty(); - } + public Optional release(Properties properties) { VersionContext versionContext = versionService.currentVersion( properties.getVersion(), properties.getTag(), properties.getNextVersion() @@ -58,8 +48,8 @@ public Optional release(Properties properties, ReleaseBranchesConfigurat } } - public ScmPushResult releaseAndPush(Properties rules, ReleaseBranchesConfiguration releaseBranchesConfiguration) { - Optional releasedTagName = release(rules, releaseBranchesConfiguration); + public ScmPushResult releaseAndPush(Properties rules) { + Optional releasedTagName = release(rules); if (releasedTagName.isEmpty()) { return new ScmPushResult(ScmPushResultOutcome.SKIPPED, Optional.empty(), Optional.empty()); diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/ReleaserTest.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/ReleaserTest.groovy index a16853b9..b85f7305 100644 --- a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/ReleaserTest.groovy +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/ReleaserTest.groovy @@ -1,6 +1,6 @@ package pl.allegro.tech.build.axion.release.domain -import pl.allegro.tech.build.axion.release.ReleaseBranchesConfiguration + import pl.allegro.tech.build.axion.release.RepositoryBasedTest import pl.allegro.tech.build.axion.release.domain.hooks.ReleaseHooksRunner import pl.allegro.tech.build.axion.release.domain.properties.Properties @@ -30,7 +30,7 @@ class ReleaserTest extends RepositoryBasedTest { .build() when: - releaser.release(rules, new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(rules) then: currentVersion() == '2.0.0' @@ -41,7 +41,7 @@ class ReleaserTest extends RepositoryBasedTest { repository.tag(fullPrefix() + '1.0.0') when: - releaser.release(context.rules(), new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(context.rules()) then: currentVersion() == '1.0.0' @@ -55,7 +55,7 @@ class ReleaserTest extends RepositoryBasedTest { .build() when: - releaser.release(rules, new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(rules) then: currentVersion() == '3.0.0-rc4' @@ -66,7 +66,7 @@ class ReleaserTest extends RepositoryBasedTest { repository.tag(fullPrefix() + '3.0.0-rc4') when: - releaser.release(context.rules(), new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(context.rules()) then: currentVersion() == '3.0.0-rc4' @@ -78,7 +78,7 @@ class ReleaserTest extends RepositoryBasedTest { repository.commit(['*'], 'make is snapshot') when: - releaser.release(context.rules(), new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(context.rules()) then: currentVersion() == '3.0.1' @@ -92,7 +92,7 @@ class ReleaserTest extends RepositoryBasedTest { .build() when: - releaser.release(rules, new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(rules) then: currentVersion() == '3.0.0' @@ -108,39 +108,10 @@ class ReleaserTest extends RepositoryBasedTest { .build() when: - releaser.release(rules, new ReleaseBranchesConfiguration(false, 'main', ['main'] as Set)) + releaser.release(rules) then: currentVersion() == '3.2.0' repository.lastLogMessages(1) == ['release version: 3.2.0'] } - - def "should do release when releaseOnlyOnReleaseBranches option is set and current branch is in releaseBranchNames list"() { - given: - repository.tag(fullPrefix() + '1.0.0') - Properties rules = properties() - .withVersionRules(versionProperties().forceVersion('1.0.1').build()) - .withHooksRules(hooksProperties().withCommitHook().build()) - .build() - - when: - releaser.release(rules, new ReleaseBranchesConfiguration(true, 'main', ['main'] as Set)) - - then: - currentVersion() == '1.0.1' - } - - def "should skip release when current branch is not in releaseBranchNames list"() { - given: - repository.tag(fullPrefix() + '1.0.0') - Properties rules = properties() - .withHooksRules(hooksProperties().withCommitHook().build()) - .build() - - when: - releaser.release(rules, new ReleaseBranchesConfiguration(true, 'feature/branch', ['main'] as Set)) - - then: - currentVersion() == '1.0.0' - } }