Skip to content

Commit

Permalink
Merge pull request #72 from ADI10HERO/checkstyle-fixes-3
Browse files Browse the repository at this point in the history
fix: correct checkstyle errors
  • Loading branch information
garethjevans authored Jul 19, 2021
2 parents 626ad7e + d0e202b commit 9aad51a
Show file tree
Hide file tree
Showing 20 changed files with 191 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.utils.LogUtils;

import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;

/** Class to process conventional commit messages to get next version. */
public class ConventionalCommits {

LogUtils logger = new LogUtils();
Expand All @@ -15,6 +15,13 @@ private List<String> filterMergeCommits(List<String> commits) {
return commits.stream().filter(s -> !s.startsWith("Merge")).collect(Collectors.toList());
}

/**
* Return the next semantic version.
*
* @param in The current semantic version.
* @param commits List of commit messages from the last tag.
* @return The next calculated version (based on Semver).
*/
public Version nextVersion(Version in, List<String> commits) {
List<String> filtered = filterMergeCommits(commits);
List<String> breaking =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import hudson.FilePath;
import hudson.model.TaskListener;
import io.jenkins.plugins.conventionalcommits.utils.CurrentVersion;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -21,7 +16,16 @@
import java.util.Arrays;
import java.util.List;
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;
import org.kohsuke.stapler.DataBoundSetter;

/** Base class of the plugin. */
public class NextVersionStep extends Step {

private String outputFormat;
Expand Down Expand Up @@ -53,6 +57,13 @@ private static String execute(File dir, String... commandAndArgs)
return stdout(process.getInputStream());
}

/**
* Reads data from stdout.
*
* @param in InputStream object.
* @return read data.
* @throws IOException If an error occur reading files.
*/
public static String stdout(InputStream in) throws IOException {
StringBuilder builder = new StringBuilder();
LineReader reader = new LineReader(new InputStreamReader(in, StandardCharsets.UTF_8));
Expand Down Expand Up @@ -82,6 +93,7 @@ public StepExecution start(StepContext stepContext) throws Exception {
return new Execution(outputFormat, startTag, stepContext);
}

/** This class extends Step Execution class, contains the run method. */
public static class Execution extends SynchronousStepExecution<String> {

private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -150,6 +162,7 @@ protected String run() throws Exception {
}
}

/** This Class implements the abstract class StepDescriptor. */
@Extension
public static class DescriptorImpl extends StepDescriptor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
import java.io.Serializable;
import java.util.Objects;

/**
* DTO to represent an helm chart's file.
*/
/** DTO to represent an helm chart's file. */
public class HelmChart implements Serializable {

// Version attribute
private String version;
// Version attribute
private String version;

public String getVersion() {
return version;
}
public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}
public void setVersion(String version) {
this.version = version;
}

@Override
public boolean equals(Object anotherObject) {
if (this == anotherObject) return true;
if (anotherObject == null || getClass() != anotherObject.getClass()) return false;
HelmChart helmChart = (HelmChart) anotherObject;
return Objects.equals(version, helmChart.version);
@Override
public boolean equals(Object anotherObject) {
if (this == anotherObject) {
return true;
}

@Override
public int hashCode() {
return Objects.hash(version);
if (anotherObject == null || getClass() != anotherObject.getClass()) {
return false;
}


HelmChart helmChart = (HelmChart) anotherObject;
return Objects.equals(version, helmChart.version);
}

@Override
public int hashCode() {
return Objects.hash(version);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.jenkins.plugins.conventionalcommits.dto;

import com.moandjiezana.toml.Toml;
import java.io.File;

/** DTO to represent a pyproject.toml file. */
public class PyProjectToml {

/**
* Return the next version of the version attribute.
*
* @param tomlFilePath The path to the pyproject.toml file.
* @return Current Version of the project mentioned in the pyproject.toml file.
*/
public String getVersion(String tomlFilePath) {
Toml tomlFile = new Toml().read(new File(tomlFilePath));

Toml project = tomlFile.getTable("project");
return project.getString("version");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.jenkins.plugins.conventionalcommits.process;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.IOUtils;

/** This class runs a given command using ProcessBuilder. */
public class DefaultProcessHelper implements ProcessHelper {

@Override
Expand All @@ -22,5 +22,4 @@ public String runProcessBuilder(File directory, List<String> command)

return results;
}
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.List;

/** Interface intended to run CLI commands using ProcessBuilder. */
public interface ProcessHelper {
String runProcessBuilder(File directory, List<String> command)
throws IOException, InterruptedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.process.DefaultProcessHelper;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;

import java.io.File;
import java.io.IOException;

/** This class focus on getting the current version (latest release version) of a project. */
public class CurrentVersion {

private ProcessHelper processHelper;
Expand All @@ -19,16 +19,29 @@ private Version getCurrentVersionTag(String latestTag) {
return Version.valueOf(latestTag.isEmpty() ? "0.0.0" : latestTag);
}

/**
* Return the next version of the version attribute.
*
* @param directory The project's directory.
* @param latestTag The last tagged version.
* @return The current version (based on Semver).
* @throws IOException If an error occur reading files.
* @throws InterruptedException If an error occurs while executing command using processHelper.
*/
public Version getCurrentVersion(File directory, String latestTag)
throws IOException, InterruptedException {

Version currentVersion;
ProjectType projectType = ProjectTypeFactory.getProjectType(directory);

if (projectType != null) {
if (processHelper == null) processHelper = new DefaultProcessHelper();
if (processHelper == null) {
processHelper = new DefaultProcessHelper();
}
currentVersion = projectType.getCurrentVersion(directory, processHelper);
} else currentVersion = getCurrentVersionTag(latestTag);
} else {
currentVersion = getCurrentVersionTag(latestTag);
}

return currentVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/** Represent a Gradle project type (i.e with a build.gradle file). */
public class GradleProjectType extends ProjectType {

public boolean check(File directory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@
import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.dto.HelmChart;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
* Represent an Helm project type (i.e with a Chart.yaml file).
*/
/** Represent an Helm project type (i.e with a Chart.yaml file). */
public class HelmProjectType extends ProjectType {
// Helm chart's file name
private static final String CHART_YAML_NAME = "Chart.yaml";
// Helm chart's file name
private static final String CHART_YAML_NAME = "Chart.yaml";

/**
* To know if the project is an Helm project type.
*
* @param directory The directory to check. <b>Mandatory</b>
* @return true if a Chart.yaml file is found.
*/
@Override
public boolean check(File directory) {
Objects.requireNonNull(directory);
return new File(directory.getAbsoluteFile() + File.separator + CHART_YAML_NAME).exists();
}
/**
* To know if the project is an Helm project type.
*
* @param directory The directory to check. <b>Mandatory</b>
* @return true if a Chart.yaml file is found.
*/
@Override
public boolean check(File directory) {
Objects.requireNonNull(directory);
return new File(directory.getAbsoluteFile() + File.separator + CHART_YAML_NAME).exists();
}

/**
* Return the next version of the version attribute.
*
* @param directory The project's directory.
* @param processHelper Not used.
* @return The next calculated version (based on Semver).
* @throws IOException If an error occur reading files.
*/
@Override
public Version getCurrentVersion(File directory, ProcessHelper processHelper) throws IOException {
Objects.requireNonNull(directory);
/**
* Return the next version of the version attribute.
*
* @param directory The project's directory.
* @param processHelper Not used.
* @return The next calculated version (based on Semver).
* @throws IOException If an error occur reading files.
*/
@Override
public Version getCurrentVersion(File directory, ProcessHelper processHelper) throws IOException {
Objects.requireNonNull(directory);

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

// Convert Chart.yaml to a DTO
HelmChart chart = mapper.readValue(new File(directory.getAbsoluteFile() + File.separator + CHART_YAML_NAME), HelmChart.class);
return Version.valueOf(chart.getVersion());
}
// Convert Chart.yaml to a DTO
HelmChart chart =
mapper.readValue(
new File(directory.getAbsoluteFile() + File.separator + CHART_YAML_NAME),
HelmChart.class);
return Version.valueOf(chart.getVersion());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.jenkins.plugins.conventionalcommits.utils;

import io.jenkins.plugins.conventionalcommits.ConventionalCommits;

import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

/** Handles Logging. */
public class LogUtils {

private static final Logger LOGGER = Logger.getLogger(ConventionalCommits.class.getName());
Expand All @@ -24,19 +24,29 @@ private void afterLogging(Level loggerLevel, Level consoleLevel) {
consoleHandler.setLevel(consoleLevel);
}

/**
* Logs to console.
*
* @param initialLogLevel Log Level before the current log.
* @param initialConsoleLevel ConsoleHandler's level before the current log.
* @param requiredLogLevel Log Level at which current log is to be written.
* @param requiredConsoleLevel ConsoleHandler's level for the current log string.
* @param revertAfterLogging If True, revert to initial log and console level after logging the
* current log string.
* @param message Log message.
*/
public void log(
Level initialLogLevel,
Level initialConsoleLevel,
Level requiredLogLevel,
Level requiredConsoleLevel,
boolean revertAfterLogging,
String message) {
/*
revertAfterLogging (boolean): revert to initial log and console level after logging the current log string
*/

beforeLogging(requiredLogLevel, requiredConsoleLevel);
LOGGER.log(requiredLogLevel, message);
if (revertAfterLogging) afterLogging(initialLogLevel, initialConsoleLevel);
if (revertAfterLogging) {
afterLogging(initialLogLevel, initialConsoleLevel);
}
}
}
Loading

0 comments on commit 9aad51a

Please sign in to comment.