-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from philippart-s/add-current-version-step
feat: Add currentVersion step support
- Loading branch information
Showing
11 changed files
with
388 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/io/jenkins/plugins/conventionalcommits/CurrentVersionStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package io.jenkins.plugins.conventionalcommits; | ||
|
||
import com.github.zafarkhaja.semver.Version; | ||
import com.google.common.collect.ImmutableSet; | ||
import hudson.Extension; | ||
import hudson.FilePath; | ||
import hudson.model.TaskListener; | ||
import io.jenkins.plugins.conventionalcommits.utils.CurrentVersion; | ||
import io.jenkins.plugins.conventionalcommits.utils.TagsHelper; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
import javax.annotation.Nonnull; | ||
import org.jenkinsci.plugins.workflow.steps.Step; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Step to get the current version of the project. | ||
* Example : | ||
* <code>def CURRENT_VERSION = currentVersion()</code> | ||
*/ | ||
public class CurrentVersionStep extends Step { | ||
|
||
@DataBoundConstructor | ||
public CurrentVersionStep() { | ||
// empty constructor, for now... | ||
} | ||
|
||
@Override | ||
public StepExecution start(StepContext stepContext) throws Exception { | ||
return new Execution(stepContext); | ||
} | ||
|
||
/** | ||
* 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> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Constructor with fields initialisation. | ||
* | ||
* @param context Jenkins context | ||
*/ | ||
protected Execution(@Nonnull StepContext context) { | ||
super(context); | ||
} | ||
|
||
/** | ||
* Entry point of the step. | ||
* | ||
* @return The current version of the project. | ||
* @throws Exception If errors occurs ;). | ||
*/ | ||
@Override | ||
protected String run() throws Exception { | ||
FilePath workspace = getContext().get(FilePath.class); | ||
if (workspace == null) { | ||
throw new IOException("no workspace"); | ||
} | ||
|
||
// 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); | ||
|
||
Version currentVersion = | ||
new CurrentVersion() | ||
.getCurrentVersion( | ||
dir, latestTag, getContext().get(TaskListener.class).getLogger()); | ||
|
||
return currentVersion.toString(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This Class implements the abstract class StepDescriptor. | ||
*/ | ||
@Extension | ||
public static class DescriptorImpl extends StepDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "determine the current version from the conventional commit history"; | ||
} | ||
|
||
@Override | ||
public Set<? extends Class<?>> getRequiredContext() { | ||
return ImmutableSet.of(TaskListener.class, FilePath.class); | ||
} | ||
|
||
@Override | ||
public String getFunctionName() { | ||
return "currentVersion"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/io/jenkins/plugins/conventionalcommits/process/ProcessUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.jenkins.plugins.conventionalcommits.process; | ||
|
||
import com.google.common.io.LineReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Class to execute some CLI commands. | ||
*/ | ||
public class ProcessUtil { | ||
|
||
/** | ||
* Reads data from stdout. | ||
* | ||
* @param in InputStream object. | ||
* @return read data. | ||
* @throws IOException If an error occur reading files. | ||
*/ | ||
private static String stdout(InputStream in) throws IOException { | ||
StringBuilder builder = new StringBuilder(); | ||
LineReader reader = new LineReader(new InputStreamReader(in, StandardCharsets.UTF_8)); | ||
while (true) { | ||
String line = reader.readLine(); | ||
if (line == null) { | ||
break; | ||
} | ||
builder.append(line); | ||
builder.append(System.getProperty("line.separator")); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* Execute a CLI command using ProcessBuilder. | ||
* | ||
* @param dir Directory where execute the command. | ||
* @param commandAndArgs Command and parameters of the command. | ||
* @return THe output of the command. | ||
* @throws IOException If an error occur accessing files. | ||
* @throws InterruptedException If the command is interrupted. | ||
*/ | ||
public static String execute(File dir, String... commandAndArgs) | ||
throws IOException, InterruptedException { | ||
ProcessBuilder builder = new ProcessBuilder().directory(dir).command(commandAndArgs); | ||
|
||
Process process = builder.start(); | ||
int exitCode = process.waitFor(); | ||
if (exitCode != 0) { | ||
String stderr = stdout(process.getErrorStream()); | ||
throw new IOException( | ||
"executing '" | ||
+ String.join(" ", commandAndArgs) | ||
+ "' failed in '" | ||
+ dir | ||
+ "' with exit code" | ||
+ exitCode | ||
+ " and error " | ||
+ stderr); | ||
} | ||
return stdout(process.getInputStream()); | ||
} | ||
} |
Oops, something went wrong.