Skip to content

Commit

Permalink
implement flag overridenIsClean (#738)
Browse files Browse the repository at this point in the history
This flag is implemented similar to overriddenBranch and allows to set
the "isClean" flag for the repository. Which basically determines if the
working tree is dirty or clean.

One use-case is to speed up the computation when running the plugin
locally as the isClean-calculation is quite time consuming. Also it may
help during development of the version-rules.

This solves one part of issue #736
  • Loading branch information
balrok committed Apr 21, 2024
1 parent 610ddcf commit c7dcd21
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 1 deletion.
32 changes: 32 additions & 0 deletions docs/configuration/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,35 @@ All `axion-release-plugin` configuration options:
aheadOfRemote.set(false) // permanently disable ahead of remote check
}
}

All `axion-release-plugin` configuration flags:

- release.customKey
- see [Authorization](authorization.md)
- release.customKeyFile
- see [Authorization](authorization.md)
- release.customKeyPassword
- see [Authorization](authorization.md)
- release.customUsername
- see [Authorization](authorization.md) and [CI Servers](ci_servers.md)
- release.customPassword
- see [Authorization](authorization.md) and [CI Servers](ci_servers.md)
- release.pushTagsOnly
- see [Repository](repository.md) and [CI Servers](ci_servers.md)
- release.attachRemote
- see [CI Servers](ci_servers.md)
- release.overriddenBranchName
- possible values: any string like `develop`
- usually the plugin detects the branch name automatically. With this flag, the result can be overridden.
- see [CI Servers](ci_servers.md)
- release.overriddenIsClean
- default: not set = determine the `isClean`-state
- possible values: `true` or `false`
- usually the plugin performs a check if the working directory is clean. With this flag, the result of the check can be overridden.
- If you have a repository with a lot of files and do not use the isClean-feature, you may set this flag for a speed-up.
- release.disableSshAgent
- default: false
- do not use the ssh agent
- release.fetchTags
- default: false
- fetch tags from the remote repository
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ abstract class BaseExtension {
return providers.gradleProperty(name).forUseAtConfigurationTime()
}

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

protected Provider<Boolean> gradlePropertyPresent(String name) {
return gradleProperty(name).map({true})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abstract class RepositoryConfig extends BaseExtension {
private static final String RELEASE_PUSH_TAGS_ONLY_PROPERTY = 'release.pushTagsOnly'
private static final String ATTACH_REMOTE_PROPERTY = 'release.attachRemote'
private static final String OVERRIDDEN_BRANCH_NAME = 'release.overriddenBranchName'
private static final String OVERRIDDEN_IS_CLEAN = 'release.overriddenIsClean'
private static final String DISABLE_SSH_AGENT = 'release.disableSshAgent'
private static final String FETCH_TAGS_PROPERTY = 'release.fetchTags'

Expand Down Expand Up @@ -87,6 +88,10 @@ abstract class RepositoryConfig extends BaseExtension {
gradleProperty(OVERRIDDEN_BRANCH_NAME)
}

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

Provider<Boolean> disableSshAgent() {
gradlePropertyPresent(DISABLE_SSH_AGENT).orElse(false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ScmPropertiesFactory {
config.repository.attachRemote().isPresent(),
config.repository.attachRemote().getOrNull(),
config.repository.overriddenBranch().getOrNull(),
config.repository.overriddenIsClean().getOrNull(),
ScmIdentityFactory.create(config.repository, config.repository.disableSshAgent().get())
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.allegro.tech.build.axion.release.domain.scm;

import java.io.File;
import java.util.Optional;

public class ScmProperties {

Expand All @@ -12,6 +13,7 @@ public class ScmProperties {
private final boolean attachRemote;
private final String remoteUrl;
private final String overriddenBranchName;
private final Boolean overriddenIsClean;
private final ScmIdentity identity;

public ScmProperties(
Expand All @@ -23,6 +25,7 @@ public ScmProperties(
boolean attachRemote,
String remoteUrl,
String overriddenBranchName,
Boolean overriddenIsClean,
ScmIdentity identity
) {
this.type = type;
Expand All @@ -33,6 +36,7 @@ public ScmProperties(
this.attachRemote = attachRemote;
this.remoteUrl = remoteUrl;
this.overriddenBranchName = overriddenBranchName;
this.overriddenIsClean = overriddenIsClean;
this.identity = identity;
}

Expand Down Expand Up @@ -72,6 +76,10 @@ public String getOverriddenBranchName() {
return overriddenBranchName;
}

public Optional<Boolean> getOverriddenIsClean() {
return Optional.ofNullable(overriddenIsClean);
}

public final ScmIdentity getIdentity() {
return identity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private ScmPushResult verifyPushResults(Iterable<PushResult> pushResults) {

Optional<RemoteRefUpdate> failedRefUpdate = pushResult.getRemoteUpdates().stream().filter(ref ->
!ref.getStatus().equals(RemoteRefUpdate.Status.OK)
&& !ref.getStatus().equals(RemoteRefUpdate.Status.UP_TO_DATE)
&& !ref.getStatus().equals(RemoteRefUpdate.Status.UP_TO_DATE)
).findFirst();

boolean isSuccess = !failedRefUpdate.isPresent();
Expand Down Expand Up @@ -504,8 +504,16 @@ public boolean remoteAttached(final String remoteName) {
return config.getSubsections("remote").stream().anyMatch(n -> n.equals(remoteName));
}

/**
* @return true when there are uncommitted changes. This means: not clean
*/
@Override
public boolean checkUncommittedChanges() {
Optional<Boolean> overriddenIsClean = properties.getOverriddenIsClean();
if (overriddenIsClean.isPresent()) {
// the logic to get the isClean-property is inverted
return !overriddenIsClean.get();
}
try {
return !jgitRepository.status().call().isClean();
} catch (GitAPIException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ScmPropertiesBuilder {

private String type = 'git'
private String overriddenBranchName
private Boolean overriddenIsClean = null

private ScmPropertiesBuilder(File directory) {
this.directory = directory
Expand All @@ -20,6 +21,11 @@ class ScmPropertiesBuilder {
return this
}

ScmPropertiesBuilder withOverriddenIsClean(Boolean overriddenIsClean) {
this.overriddenIsClean = overriddenIsClean
return this
}

ScmProperties build() {
return new ScmProperties(
type,
Expand All @@ -30,6 +36,7 @@ class ScmPropertiesBuilder {
false,
null,
overriddenBranchName,
overriddenIsClean,
ScmIdentity.defaultIdentityWithoutAgents()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.gradle.testfixtures.ProjectBuilder
import pl.allegro.tech.build.axion.release.domain.scm.*
import spock.lang.Specification

import java.nio.file.Files
import java.nio.file.Path
import java.util.regex.Pattern

import static java.util.regex.Pattern.compile
Expand Down Expand Up @@ -296,6 +298,43 @@ class GitRepositoryTest extends Specification {
position.branch == 'HEAD'
}


def "should respect overriddenIsClean-flag"(boolean expectedIsClean, Boolean overridenIsCleanFlag, boolean dirtyRepository) {
given:
File repositoryDir = File.createTempDir('axion-release', 'tmp')
def scmProperties = scmProperties(repositoryDir)
.withOverriddenIsClean(overridenIsCleanFlag)
.build()
Map repositories = GitProjectBuilder.gitProject(repositoryDir, remoteRepositoryDir).usingProperties(scmProperties).build()

Grgit rawRepository = repositories[Grgit]
GitRepository repository = repositories[GitRepository]

String headCommitId = rawRepository.repository.jgit.repository.resolve(Constants.HEAD).name()
rawRepository.repository.jgit.checkout().setName(headCommitId).call()
when:
def dirtyFile = Path.of(repositoryDir.path).resolve("dirty-file")
if (dirtyRepository && !Files.exists(dirtyFile)) {
Files.createFile(dirtyFile)
}
if (!dirtyRepository && Files.exists(dirtyFile)) {
Files.delete(Path.of(repositoryDir.path).resolve("dirty-file"));
}
ScmPosition position = repository.currentPosition()

then:
position.isClean == expectedIsClean

where:
expectedIsClean | overridenIsCleanFlag | dirtyRepository
false | false | false
true | true | false
false | false | true
true | true | true
true | null | false
false | null | true
}

def "should provide current branch name from overriddenBranchName when in detached state and overriddenBranchName is set"() {
given:
File repositoryDir = File.createTempDir('axion-release', 'tmp')
Expand Down

0 comments on commit c7dcd21

Please sign in to comment.