Skip to content

Commit

Permalink
fix: 🐛 remote execution (#180)
Browse files Browse the repository at this point in the history
* fix: 🐛  remote execution

* 📝 code format

* ✅ checkstyle
  • Loading branch information
philippart-s authored Mar 19, 2022
1 parent afd6553 commit e38c906
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Step to get the current version of the project.
* Example :
* Step to get the current version of the project. Example :
* <code>def CURRENT_VERSION = currentVersion()</code>
*/
public class CurrentVersionStep extends Step {
Expand All @@ -36,8 +35,8 @@ public StepExecution start(StepContext stepContext) throws Exception {
}

/**
* This class extends Step Execution class, contains the run method.
* This is the main entry point of the step.
* This class extends Step Execution class, contains the run method. This is the main entry point
* of the step.
*/
public static class Execution extends SynchronousStepExecution<String> {

Expand Down Expand Up @@ -66,19 +65,13 @@ protected String run() throws Exception {
}

// if the workspace is remote then lets make a local copy
if (workspace.isRemote()) {
throw new IOException("workspace.isRemote(), not entirely sure what to do here...");
} else {
File dir = new File(workspace.getRemote());
String latestTag = TagsHelper.getLatestTag(getContext(), dir, false);
File dir = new File(workspace.getRemote());
String latestTag = TagsHelper.getLatestTag(getContext(), dir, false);

Version currentVersion =
new CurrentVersion()
.getCurrentVersion(
dir, latestTag, getContext().get(TaskListener.class).getLogger());
Version currentVersion = new CurrentVersion().getCurrentVersion(dir, latestTag,
getContext().get(TaskListener.class).getLogger());

return currentVersion.toString();
}
return currentVersion.toString();
}
}

Expand All @@ -103,4 +96,4 @@ public String getFunctionName() {
return "currentVersion";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,8 @@ public void setNonAnnotatedTag(boolean nonAnnotatedTag) {

@Override
public StepExecution start(StepContext stepContext) throws Exception {
return new Execution(
outputFormat,
startTag,
buildMetadata,
writeVersion,
preRelease,
preservePreRelease,
incrementPreRelease,
nonAnnotatedTag,
stepContext);
return new Execution(outputFormat, startTag, buildMetadata, writeVersion, preRelease,
preservePreRelease, incrementPreRelease, nonAnnotatedTag, stepContext);
}

/**
Expand All @@ -142,74 +134,59 @@ public static class Execution extends SynchronousStepExecution<String> {

private static final long serialVersionUID = 1L;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
private final transient String outputFormat;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
private final transient String startTag;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
private final transient String buildMetadata;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
private final transient boolean writeVersion;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
// Pre release information to add to the next version
private final transient String preRelease;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
// True to preserve in the next version the prerelease information (if set)
private final transient boolean preservePreRelease;

@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
// True to increment prerelease information instead of the version itself
private final transient boolean incrementPreRelease;

// True if annotated tags are supported
@SuppressFBWarnings(
value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
justification = "Only used when starting.")
private final transient boolean nonAnnotatedTag;


/**
* Constructor with fields initialisation.
*
* @param outputFormat Output format for the next version
* @param startTag Git tag
* @param buildMetadata Add meta date to the version.
* @param writeVersion Should write the new version in the file.
* @param preRelease Pre release information to add
* @param preservePreRelease Keep existing prerelease information or not
* @param outputFormat Output format for the next version
* @param startTag Git tag
* @param buildMetadata Add meta date to the version.
* @param writeVersion Should write the new version in the file.
* @param preRelease Pre release information to add
* @param preservePreRelease Keep existing prerelease information or not
* @param incrementPreRelease Increment prerelease information or not
* @param nonAnnotatedTag Should use or non annotated tags
* @param context Jenkins context
* @param nonAnnotatedTag Should use or non annotated tags
* @param context Jenkins context
*/
protected Execution(
String outputFormat,
String startTag,
String buildMetadata,
boolean writeVersion,
String preRelease,
boolean preservePreRelease,
boolean incrementPreRelease,
boolean nonAnnotatedTag,
@Nonnull StepContext context) {
protected Execution(String outputFormat, String startTag, String buildMetadata,
boolean writeVersion, String preRelease, boolean preservePreRelease,
boolean incrementPreRelease, boolean nonAnnotatedTag, @Nonnull StepContext context) {
super(context);
this.outputFormat = outputFormat;
this.startTag = startTag;
Expand All @@ -229,67 +206,61 @@ protected String run() throws Exception {
}

// if the workspace is remote then lets make a local copy
if (workspace.isRemote()) {
throw new IOException("workspace.isRemote(), not entirely sure what to do here...");
} else {
File dir = new File(workspace.getRemote());
String latestTag = getLatestTag(getContext(), dir, nonAnnotatedTag);
File dir = new File(workspace.getRemote());
String latestTag = getLatestTag(getContext(), dir, nonAnnotatedTag);

Version currentVersion =
new CurrentVersion()
.getCurrentVersion(
dir, latestTag, getContext().get(TaskListener.class).getLogger());
Version currentVersion = new CurrentVersion().getCurrentVersion(dir, latestTag,
getContext().get(TaskListener.class).getLogger());

String commitMessagesString = null;
if (latestTag.isEmpty()) {
commitMessagesString = execute(dir, "git", "log", "--pretty=format:%s").trim();
} else {
// FIXME get a list of commits between 'this' and the tag
// git log --pretty=format:%s tag..HEAD
commitMessagesString =
execute(dir, "git", "log", "--pretty=format:%s", latestTag + "..HEAD").trim();
}
String commitMessagesString = null;
if (latestTag.isEmpty()) {
commitMessagesString = execute(dir, "git", "log", "--pretty=format:%s").trim();
} else {
// FIXME get a list of commits between 'this' and the tag
// git log --pretty=format:%s tag..HEAD
commitMessagesString =
execute(dir, "git", "log", "--pretty=format:%s", latestTag + "..HEAD").trim();
}

List<String> commitHistory = Arrays.asList(commitMessagesString.split("\n"));
List<String> commitHistory = Arrays.asList(commitMessagesString.split("\n"));

Version nextVersion;
if (!incrementPreRelease || StringUtils.isEmpty(currentVersion.getPreReleaseVersion())) {
// based on the commit list, determine how to bump the version
nextVersion = new ConventionalCommits().nextVersion(currentVersion, commitHistory);
} else {
nextVersion = currentVersion.incrementPreReleaseVersion();
}
Version nextVersion;
if (!incrementPreRelease || StringUtils.isEmpty(currentVersion.getPreReleaseVersion())) {
// based on the commit list, determine how to bump the version
nextVersion = new ConventionalCommits().nextVersion(currentVersion, commitHistory);
} else {
nextVersion = currentVersion.incrementPreReleaseVersion();
}

if (StringUtils.isNotBlank(buildMetadata)) {
nextVersion = nextVersion.setBuildMetadata(buildMetadata);
}
if (StringUtils.isNotBlank(buildMetadata)) {
nextVersion = nextVersion.setBuildMetadata(buildMetadata);
}

// Keep (or not) the pre-release information only if incrementPreRelease is not set
if (!incrementPreRelease && StringUtils.isNotBlank(currentVersion.getPreReleaseVersion())) {
if (preservePreRelease) {
nextVersion = nextVersion.setPreReleaseVersion(currentVersion.getPreReleaseVersion());
} else {
if (!StringUtils.isNotBlank(preRelease)) {
nextVersion = Version.valueOf(currentVersion.getNormalVersion());
}
// Keep (or not) the pre-release information only if incrementPreRelease is not set
if (!incrementPreRelease && StringUtils.isNotBlank(currentVersion.getPreReleaseVersion())) {
if (preservePreRelease) {
nextVersion = nextVersion.setPreReleaseVersion(currentVersion.getPreReleaseVersion());
} else {
if (!StringUtils.isNotBlank(preRelease)) {
nextVersion = Version.valueOf(currentVersion.getNormalVersion());
}
}
}

// If pre-release information, add it
if (StringUtils.isNotBlank(preRelease)) {
nextVersion = nextVersion.setPreReleaseVersion(preRelease);
}

getContext().get(TaskListener.class).getLogger().println(nextVersion);
// If pre-release information, add it
if (StringUtils.isNotBlank(preRelease)) {
nextVersion = nextVersion.setPreReleaseVersion(preRelease);
}

if (writeVersion) {
WriteVersion writer = new WriteVersion();
String writeLog = writer.write(nextVersion, dir);
getContext().get(TaskListener.class).getLogger().println(writeLog);
}
getContext().get(TaskListener.class).getLogger().println(nextVersion);

return nextVersion.toString();
if (writeVersion) {
WriteVersion writer = new WriteVersion();
String writeLog = writer.write(nextVersion, dir);
getContext().get(TaskListener.class).getLogger().println(writeLog);
}

return nextVersion.toString();
}
}

Expand Down

0 comments on commit e38c906

Please sign in to comment.