Skip to content

Commit

Permalink
release skipping logic moved to Releaser class
Browse files Browse the repository at this point in the history
  • Loading branch information
bgalek committed Aug 26, 2024
1 parent 04a746d commit 6c62618
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BaseIntegrationTest extends RepositoryBasedTest {
"""
project.version = scmVersion.version
scmVersion.ignoreGlobalGitConfig = true
""")
}

Expand All @@ -48,6 +49,7 @@ class BaseIntegrationTest extends RepositoryBasedTest {
try {
return gradle().withArguments(args).build()
}

finally {
def ccDir = new File(temporaryFolder, "build/reports/configuration-cache")
if (ccDir.exists() && ccDir.isDirectory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import static pl.allegro.tech.build.axion.release.TagPrefixConf.fullPrefix
class SimpleIntegrationTest extends BaseIntegrationTest {

@Rule
EnvironmentVariables environmentVariablesRule = new EnvironmentVariables();
EnvironmentVariables environmentVariablesRule = new EnvironmentVariables()

def "should return default version on calling currentVersion task on vanilla repo"() {
given:
Expand Down Expand Up @@ -155,7 +155,7 @@ class SimpleIntegrationTest extends BaseIntegrationTest {

then:
releaseResult.task(':release').outcome == TaskOutcome.SUCCESS
releaseResult.output.contains('Release step skipped since \'releaseOnlyOnDefaultBranches\' option is set, and \'master\' was not in \'releaseBranchNames\' list [develop, release]')
releaseResult.output.contains('Release step skipped since \'releaseOnlyOnDefaultBranches\' option is set, and \'master\' was not in \'releaseBranchNames\' list [develop,release]')
}

def "should skip release when releaseOnlyOnReleaseBranches is set by gradle task property and current branch is not on releaseBranchNames list"() {
Expand All @@ -167,7 +167,7 @@ class SimpleIntegrationTest extends BaseIntegrationTest {

then:
releaseResult.task(':release').outcome == TaskOutcome.SUCCESS
releaseResult.output.contains('Release step skipped since \'releaseOnlyOnDefaultBranches\' option is set, and \'master\' was not in \'releaseBranchNames\' list [develop, release]')
releaseResult.output.contains('Release step skipped since \'releaseOnlyOnDefaultBranches\' option is set, and \'master\' was not in \'releaseBranchNames\' list [develop,release]')
}

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
@@ -1,6 +1,5 @@
package pl.allegro.tech.build.axion.release


import org.gradle.api.tasks.TaskAction
import pl.allegro.tech.build.axion.release.domain.Releaser
import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext
Expand All @@ -11,6 +10,11 @@ abstract class CreateReleaseTask extends BaseAxionTask {
void release() {
VersionResolutionContext context = resolutionContext()
Releaser releaser = context.releaser()
releaser.release(context.rules())
def scmService = context.scmService()
def repository = context.repository()
def releaseBranchNames = scmService.getReleaseBranchNames()
def currentBranch = repository.currentPosition().getBranch()
def isReleaseOnlyOnDefaultBranches = scmService.isReleaseOnlyOnDefaultBranches()
releaser.release(context.rules(), isReleaseOnlyOnDefaultBranches, currentBranch, releaseBranchNames)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ abstract class ReleaseTask extends BaseAxionTask {
VersionResolutionContext context = resolutionContext()
Releaser releaser = context.releaser()
ScmService scmService = context.scmService()

if (scmService.isReleaseOnlyOnDefaultBranches()) {
def releaseBranchNames = scmService.getReleaseBranchNames()
def currentBranch = context.repository().currentPosition().getBranch()
if (!releaseBranchNames.contains(currentBranch)) {
logger.quiet("Release step skipped since 'releaseOnlyOnDefaultBranches' option is set, and '${currentBranch}' was not in 'releaseBranchNames' list [${releaseBranchNames.join(', ')}]")
return
}
}

ScmPushResult result = releaser.releaseAndPush(context.rules())
def releaseBranchNames = scmService.getReleaseBranchNames()
def currentBranch = context.repository().currentPosition().getBranch()
def isReleaseOnlyOnDefaultBranches = scmService.isReleaseOnlyOnDefaultBranches()
ScmPushResult result = releaser.releaseAndPush(context.rules(), isReleaseOnlyOnDefaultBranches, currentBranch, releaseBranchNames)

if (!result.success) {
def status = result.failureStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ abstract class BaseExtension {
GradleVersion.current()
}

protected Provider<Set<String>> gradleSetProperty(String name) {
protected Provider<Set<String>> gradlePropertyAsSet(String name) {
return gradleProperty(name).map({ it.tokenize(',') as Set })
}

protected Provider<Boolean> gradlePropertyBoolean(String name) {
protected Provider<Boolean> gradlePropertyAsBoolean(String name) {
return gradleProperty(name).map(Boolean::valueOf)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ abstract class RepositoryConfig extends BaseExtension {
}

Provider<Boolean> overriddenIsClean() {
gradlePropertyBoolean(OVERRIDDEN_IS_CLEAN)
gradlePropertyAsBoolean(OVERRIDDEN_IS_CLEAN)
}

Provider<Boolean> disableSshAgent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ abstract class VersionConfig extends BaseExtension {
getIgnoreUncommittedChanges().convention(true)
getUseHighestVersion().convention(false)
getUnshallowRepoOnCI().convention(false)
getReleaseBranchNames().convention(gradleSetProperty(RELEASE_BRANCH_NAMES_PROPERTY).orElse(['master', 'main'] as Set))
getIgnoreGlobalGitConfig().convention(false)
getReleaseBranchNames().convention(gradlePropertyAsSet(RELEASE_BRANCH_NAMES_PROPERTY).orElse(['master', 'main'] as Set))
getReleaseOnlyOnReleaseBranches().convention(gradlePropertyPresent(RELEASE_ONLY_ON_RELEASE_BRANCHES_PROPERTY).orElse(false))
getReleaseBranchPattern().convention(Pattern.compile('^' + defaultPrefix() + '(/.*)?$'))
getSanitizeVersion().convention(true)
Expand Down Expand Up @@ -89,6 +90,9 @@ abstract class VersionConfig extends BaseExtension {
@Internal
abstract Property<Boolean> getReleaseOnlyOnReleaseBranches()

@Internal
abstract Property<Boolean> getIgnoreGlobalGitConfig()

@Internal
@Incubating
abstract Property<Boolean> getUnshallowRepoOnCI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ScmPropertiesFactory {
ScmIdentityFactory.create(config.repository, config.repository.disableSshAgent().get()),
config.getUnshallowRepoOnCI().get(),
config.getReleaseBranchNames().get(),
config.getReleaseOnlyOnReleaseBranches().get()
config.getReleaseOnlyOnReleaseBranches().get(),
config.getIgnoreGlobalGitConfig().get()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pl.allegro.tech.build.axion.release.domain.scm.ScmService;

import java.util.Optional;
import java.util.Set;

public class Releaser {

Expand All @@ -23,10 +24,20 @@ public Releaser(VersionService versionService, ScmService repository, ReleaseHoo
this.hooksRunner = hooksRunner;
}

public Optional<String> release(Properties properties) {
public Optional<String> release(Properties properties,
boolean isReleaseOnlyOnDefaultBranches,
String currentBranch,
Set<String> releaseBranchNames) {
if (isReleaseOnlyOnDefaultBranches && !releaseBranchNames.contains(currentBranch)) {
String message = String.format("Release step skipped since 'releaseOnlyOnDefaultBranches' option is set, and '%s' was not in 'releaseBranchNames' list [%s]", currentBranch, String.join(",", releaseBranchNames));
logger.quiet(message);
return Optional.empty();
}

VersionContext versionContext = versionService.currentVersion(
properties.getVersion(), properties.getTag(), properties.getNextVersion()
);

Version version = versionContext.getVersion();

if (versionContext.isSnapshot()) {
Expand All @@ -43,11 +54,17 @@ public Optional<String> release(Properties properties) {
logger.quiet("Working on released version " + version + ", nothing to release");
return Optional.empty();
}

}

public ScmPushResult releaseAndPush(Properties rules) {
Optional<String> releasedTagName = release(rules);
public ScmPushResult releaseAndPush(Properties rules,
boolean isReleaseOnlyOnDefaultBranches,
String currentBranch,
Set<String> releaseBranchNames) {
Optional<String> releasedTagName = release(rules, isReleaseOnlyOnDefaultBranches, currentBranch, releaseBranchNames);

if (releasedTagName.isEmpty()) {
return new ScmPushResult(true, Optional.empty(), Optional.empty());
}

ScmPushResult result = pushRelease();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ScmProperties {
private final Boolean unshallowRepoOnCI;
private final Set<String> releaseBranchNames;
private final boolean releaseOnlyOnReleaseBranches;
private final boolean ignoreGlobalGitConfig;

public ScmProperties(
String type,
Expand All @@ -33,7 +34,8 @@ public ScmProperties(
ScmIdentity identity,
Boolean unshallowRepoOnCI,
Set<String> releaseBranchNames,
boolean releaseOnlyOnReleaseBranches
boolean releaseOnlyOnReleaseBranches,
boolean ignoreGlobalGitConfig
) {
this.type = type;
this.directory = directory;
Expand All @@ -48,6 +50,7 @@ public ScmProperties(
this.unshallowRepoOnCI = unshallowRepoOnCI;
this.releaseBranchNames = releaseBranchNames;
this.releaseOnlyOnReleaseBranches = releaseOnlyOnReleaseBranches;
this.ignoreGlobalGitConfig = ignoreGlobalGitConfig;
}

public ScmPushOptions pushOptions() {
Expand Down Expand Up @@ -105,4 +108,8 @@ public Set<String> getReleaseBranchNames() {
public boolean isReleaseOnlyOnReleaseBranches() {
return releaseOnlyOnReleaseBranches;
}

public boolean isIgnoreGlobalGitConfig() {
return ignoreGlobalGitConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class GitRepository implements ScmRepository {
private final ScmProperties properties;

public GitRepository(ScmProperties properties) {
SystemReader.setInstance(new SystemReaderWithoutSystemConfig());

SystemReader.setInstance(new SystemReaderWithoutSystemConfig(properties.isIgnoreGlobalGitConfig()));
try {
this.repositoryDir = properties.getDirectory();
this.jgitRepository = Git.open(repositoryDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;

public class SystemReaderWithoutSystemConfig extends SystemReader
{
public class SystemReaderWithoutSystemConfig extends SystemReader {
private static final SystemReader DefaultSystemReader = SystemReader.getInstance();
private final boolean ignoreUserSettings;

public SystemReaderWithoutSystemConfig(boolean ignoreUserSettings) {
super();
this.ignoreUserSettings = ignoreUserSettings;
}

@Override
public String getenv(String variable)
{
public String getenv(String variable) {
return DefaultSystemReader.getenv(variable);
}

@Override
public String getHostname()
{
public String getHostname() {
return DefaultSystemReader.getHostname();
}

@Override
public String getProperty(String key)
{
public String getProperty(String key) {
return DefaultSystemReader.getProperty(key);
}

@Override
public long getCurrentTime()
{
public long getCurrentTime() {
return DefaultSystemReader.getCurrentTime();
}

@Override
public int getTimezone(long when)
{
public int getTimezone(long when) {
return DefaultSystemReader.getTimezone(when);
}

@Override
public FileBasedConfig openUserConfig(Config parent, FS fs)
{
public FileBasedConfig openUserConfig(Config parent, FS fs) {
if (ignoreUserSettings) return new EmptyFileBasedConfig(parent, fs);
return DefaultSystemReader.openUserConfig(parent, fs);
}

Expand All @@ -54,20 +54,22 @@ public FileBasedConfig openJGitConfig(Config parent, FS fs) {
// This resolves issues with Gradle being unable to save configuration cache
// Based on https://stackoverflow.com/a/59110721
@Override
public FileBasedConfig openSystemConfig(Config parent, FS fs)
{
return new FileBasedConfig(parent, null, fs)
{
@Override
public void load()
{
}
public FileBasedConfig openSystemConfig(Config parent, FS fs) {
return new EmptyFileBasedConfig(parent, fs);
}

private static class EmptyFileBasedConfig extends FileBasedConfig {
public EmptyFileBasedConfig(Config parent, FS fs) {
super(parent, null, fs);
}

@Override
public void load() {
}

@Override
public boolean isOutdated()
{
return false;
}
};
@Override
public boolean isOutdated() {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pl.allegro.tech.build.axion.release

import org.ajoberstar.grgit.Grgit
import org.eclipse.jgit.util.SystemReader
import pl.allegro.tech.build.axion.release.domain.LocalOnlyResolver
import pl.allegro.tech.build.axion.release.domain.properties.PropertiesBuilder
import pl.allegro.tech.build.axion.release.domain.scm.ScmProperties
Expand All @@ -25,9 +24,6 @@ class RepositoryBasedTest extends Specification {
String defaultBranch

void setup() {
// let's make sure, not to use system wide user settings in tests
setupLocalGitConfiguration()

def rawRepository = Grgit.init(dir: temporaryFolder)
defaultBranch = rawRepository.branch.current().name

Expand All @@ -46,17 +42,6 @@ class RepositoryBasedTest extends Specification {
repository.commit(['*'], 'initial commit')
}

static void setupLocalGitConfiguration() {
File testGitConfig = File.createTempFile("axion-test-git-config", ".tmp")
testGitConfig.with {
append "[user]\n"
append "\tname = Axion Test User\n"
append "\temail = [email protected]\n"
deleteOnExit()
}
SystemReader.setInstance(new TestConfigSystemReader(testGitConfig))
}

protected String currentVersion() {
return context.versionService().currentDecoratedVersion(
context.rules().version,
Expand Down
Loading

0 comments on commit 6c62618

Please sign in to comment.