Skip to content

Commit

Permalink
Disable release tasks if release should be skipped (#813)
Browse files Browse the repository at this point in the history
* Disable release tasks if release should be skipped
* Fix Groovy existential issues
  • Loading branch information
radoslaw-panuszewski committed Sep 5, 2024
1 parent 4af8913 commit 8d6fc88
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BaseIntegrationTest extends RepositoryBasedTest {
return GradleRunner.create()
.withProjectDir(temporaryFolder)
.withPluginClasspath()
.forwardOutput()
}

BuildResult runGradle(String... arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Expand All @@ -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)"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
Expand Down Expand Up @@ -63,5 +64,36 @@ abstract class ReleasePlugin implements Plugin<Project> {
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<String> 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<String> tasksToDisable = [RELEASE_TASK, CREATE_RELEASE_TASK, PUSH_RELEASE_TASK, VERIFY_RELEASE_TASK]
project.tasks
.matching { it.name in tasksToDisable }
.configureEach { it.enabled = false }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,16 +24,7 @@ public Releaser(VersionService versionService, ScmService repository, ReleaseHoo
this.hooksRunner = hooksRunner;
}

public Optional<String> 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<String> release(Properties properties) {

VersionContext versionContext = versionService.currentVersion(
properties.getVersion(), properties.getTag(), properties.getNextVersion()
Expand All @@ -58,8 +48,8 @@ public Optional<String> release(Properties properties, ReleaseBranchesConfigurat
}
}

public ScmPushResult releaseAndPush(Properties rules, ReleaseBranchesConfiguration releaseBranchesConfiguration) {
Optional<String> releasedTagName = release(rules, releaseBranchesConfiguration);
public ScmPushResult releaseAndPush(Properties rules) {
Optional<String> releasedTagName = release(rules);

if (releasedTagName.isEmpty()) {
return new ScmPushResult(ScmPushResultOutcome.SKIPPED, Optional.empty(), Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
}
}

0 comments on commit 8d6fc88

Please sign in to comment.