Skip to content

Commit

Permalink
ScmPushResultOutcome introduced instead of boolean value, fixes bug w…
Browse files Browse the repository at this point in the history
…hen GITHUB_OUTPUT should be empty when release task was skipped
  • Loading branch information
bgalek committed Aug 27, 2024
1 parent b84cd97 commit f0c575d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ JDK11+ & Gradle 7+ required.

```kotlin
plugins {
id("pl.allegro.tech.build.axion-release") version "1.18.6"
id("pl.allegro.tech.build.axion-release") version "1.18.7"
}

version = scmVersion.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import pl.allegro.tech.build.axion.release.domain.scm.ScmIdentity
import pl.allegro.tech.build.axion.release.domain.scm.ScmPropertiesBuilder
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushOptions
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResult
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResultOutcome
import pl.allegro.tech.build.axion.release.infrastructure.git.GitRepository
import pl.allegro.tech.build.axion.release.infrastructure.git.SshConnector
import spock.lang.Shared
Expand Down Expand Up @@ -59,7 +60,7 @@ class RemoteRejectionTest extends Specification {
ScmPushResult result = repository.push(keyIdentity, new ScmPushOptions('origin', false), true)

then:
!result.success
result.outcome == ScmPushResultOutcome.FAILED
result.remoteMessage.get().contains("I reject this push!")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.domain.scm.ScmPushResult
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResultOutcome
import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext

abstract class PushReleaseTask extends BaseAxionTask {
Expand All @@ -14,7 +15,7 @@ abstract class PushReleaseTask extends BaseAxionTask {
Releaser releaser = context.releaser()
ScmPushResult result = releaser.pushRelease()

if (!result.success) {
if (result.outcome === ScmPushResultOutcome.FAILED) {
def message = result.remoteMessage.orElse("Unknown error during push")
logger.error("remote message: ${message}")
throw new ReleaseFailedException(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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.domain.scm.ScmPushResult
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResultOutcome
import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext

import java.nio.file.Files
Expand All @@ -20,22 +21,25 @@ abstract class ReleaseTask extends BaseAxionTask {
context.repository().currentPosition().getBranch(),
context.scmService().getReleaseBranchNames()
)

ScmPushResult result = releaser.releaseAndPush(context.rules(), releaseBranchesConfiguration)

if (!result.success) {
if (result.outcome === ScmPushResultOutcome.FAILED) {
def status = result.failureStatus
def message = result.remoteMessage.orElse("Unknown error during push")
logger.error("remote status: ${status}")
logger.error("remote message: ${message}")
throw new ReleaseFailedException("Status: ${status}\nMessage: ${message}")
}

if (System.getenv().containsKey('GITHUB_ACTIONS')) {
Files.write(
Paths.get(System.getenv('GITHUB_OUTPUT')),
"released-version=${versionConfig.uncached.decoratedVersion}\n".getBytes(),
StandardOpenOption.APPEND
)
if (result.outcome === ScmPushResultOutcome.SUCCESS) {
if (System.getenv().containsKey('GITHUB_ACTIONS')) {
Files.write(
Paths.get(System.getenv('GITHUB_OUTPUT')),
"released-version=${versionConfig.uncached.decoratedVersion}\n".getBytes(),

Check warning on line 39 in src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseTask.groovy

View check run for this annotation

Codecov / codecov/patch

src/main/groovy/pl/allegro/tech/build/axion/release/ReleaseTask.groovy#L37-L39

Added lines #L37 - L39 were not covered by tests
StandardOpenOption.APPEND
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NoOpRepository implements ScmRepository {
@Override
ScmPushResult push(ScmIdentity identity, ScmPushOptions pushOptions) {
log("pushing to remote: ${pushOptions.remote}")
return new ScmPushResult(true, Optional.empty(), Optional.empty())
return new ScmPushResult(ScmPushResultOutcome.SUCCESS, Optional.empty(), Optional.empty())
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResultOutcome;
import pl.allegro.tech.build.axion.release.domain.scm.ScmService;

import java.util.Optional;
Expand Down Expand Up @@ -61,12 +62,12 @@ public ScmPushResult releaseAndPush(Properties rules, ReleaseBranchesConfigurati
Optional<String> releasedTagName = release(rules, releaseBranchesConfiguration);

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

Check warning on line 65 in src/main/java/pl/allegro/tech/build/axion/release/domain/Releaser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/pl/allegro/tech/build/axion/release/domain/Releaser.java#L65

Added line #L65 was not covered by tests
}

ScmPushResult result = pushRelease();

if (!result.isSuccess()) {
if (result.getOutcome().equals(ScmPushResultOutcome.FAILED)) {
releasedTagName.ifPresent(this::rollbackRelease);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

public class ScmPushResult {

private final boolean success;
private final ScmPushResultOutcome outcome;

private final Optional<RemoteRefUpdate.Status> failureCause;

private final Optional<String> remoteMessage;

public ScmPushResult(boolean success, Optional<RemoteRefUpdate.Status> failureCause, Optional<String> remoteMessage) {
this.success = success;
public ScmPushResult(ScmPushResultOutcome outcome,
Optional<RemoteRefUpdate.Status> failureCause,
Optional<String> remoteMessage) {
this.outcome = outcome;
this.failureCause = failureCause;
this.remoteMessage = remoteMessage;
}

public boolean isSuccess() {
return success;
public ScmPushResultOutcome getOutcome() {
return outcome;
}

public Optional<RemoteRefUpdate.Status> getFailureStatus() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.allegro.tech.build.axion.release.domain.scm;

public enum ScmPushResultOutcome {
SUCCESS, SKIPPED, FAILED
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void dropTag(String tagName) {
public ScmPushResult push() {
if (localOnlyResolver.localOnly(this.remoteAttached())) {
logger.quiet("Changes made to local repository only");
return new ScmPushResult(true, Optional.of(RemoteRefUpdate.Status.NOT_ATTEMPTED), Optional.empty());
return new ScmPushResult(ScmPushResultOutcome.SUCCESS, Optional.of(RemoteRefUpdate.Status.NOT_ATTEMPTED), Optional.empty());
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ public ScmPushResult push(ScmIdentity identity, ScmPushOptions pushOptions, bool
if (!pushOptions.isPushTagsOnly()) {
PushCommand command = pushCommand(identity, pushOptions.getRemote(), all);
ScmPushResult result = verifyPushResults(callPush(command));
if (!result.isSuccess()) {
if (result.getOutcome().equals(ScmPushResultOutcome.FAILED)) {
return result;
}

}

// and again for tags
Expand All @@ -182,12 +181,12 @@ private ScmPushResult verifyPushResults(Iterable<PushResult> pushResults) {
&& !ref.getStatus().equals(RemoteRefUpdate.Status.UP_TO_DATE)
).findFirst();

boolean isSuccess = !failedRefUpdate.isPresent();
boolean isSuccess = failedRefUpdate.isEmpty();
Optional<RemoteRefUpdate.Status> failureCause = isSuccess ?
Optional.empty() : Optional.of(failedRefUpdate.get().getStatus());

return new ScmPushResult(
isSuccess,
isSuccess ? ScmPushResultOutcome.SUCCESS : ScmPushResultOutcome.FAILED,
failureCause,
Optional.ofNullable(pushResult.getMessages())
);
Expand Down

0 comments on commit f0c575d

Please sign in to comment.