diff --git a/pom.xml b/pom.xml index fecefbec1..f806c73c8 100644 --- a/pom.xml +++ b/pom.xml @@ -177,5 +177,56 @@ https://repo.jenkins-ci.org/public/ - + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.1 + + + validate + + check + + + + + + com.github.spotbugs + spotbugs-maven-plugin + 4.2.0 + + + + check + + + + + + org.jenkins-ci.tools + maven-hpi-plugin + 3.37 + true + + + com.diffplug.spotless + spotless-maven-plugin + 2.17.0 + + + + apply + + + + + + + + + + + diff --git a/src/main/java/com/nirima/jenkins/plugins/docker/listener/DockerQueueListener.java b/src/main/java/com/nirima/jenkins/plugins/docker/listener/DockerQueueListener.java index 541848199..4fd1b4c67 100644 --- a/src/main/java/com/nirima/jenkins/plugins/docker/listener/DockerQueueListener.java +++ b/src/main/java/com/nirima/jenkins/plugins/docker/listener/DockerQueueListener.java @@ -28,24 +28,37 @@ public class DockerQueueListener extends QueueListener { @Override - public void onEnterWaiting(WaitingItem wi) { + public void onEnterWaiting(final WaitingItem wi) { final DockerJobTemplateProperty jobTemplate = getJobTemplate(wi); if (jobTemplate != null) { - final DockerCloud cloud = DockerCloud.getCloudByName(jobTemplate.getCloudname()); + final DockerCloud cloud = + DockerCloud.getCloudByName(jobTemplate.getCloudname()); if (cloud != null) { final String uuid = UUID.randomUUID().toString(); - final DockerTemplate template = jobTemplate.getTemplate().cloneWithLabel(uuid); + final DockerTemplate template = + jobTemplate.getTemplate().cloneWithLabel(uuid); cloud.addJobTemplate(wi.getId(), template); wi.addAction(new DockerTemplateLabelAssignmentAction(uuid)); } } } - + /** + * Called when an item leaves the queue. + * Subclasses can override this method to provide custom behavior when an item is removed from the queue. + * + *

When overriding this method, ensure that you: + * 1. Call the superclass's `onLeft` method if needed, to preserve the default behavior. + * 2. Handle null checks for `jobTemplate` and `cloud` objects, as they can be `null`. + * 3. Avoid making any long-running or blocking operations in this method, as it is called within Jenkins' queue management thread. + * + * @param li the {@link LeftItem} that left the queue. This parameter should not be null. + */ @Override - public void onLeft(LeftItem li) { + public void onLeft(final LeftItem li) { final DockerJobTemplateProperty jobTemplate = getJobTemplate(li); if (jobTemplate != null) { - final DockerCloud cloud = DockerCloud.getCloudByName(jobTemplate.getCloudname()); + final DockerCloud cloud = + DockerCloud.getCloudByName(jobTemplate.getCloudname()); if (cloud != null) { cloud.removeJobTemplate(li.getId()); } @@ -59,15 +72,17 @@ public void onLeft(LeftItem li) { * @return If the item includes a template then the template will be returned. Otherwise null. */ @CheckForNull - private static DockerJobTemplateProperty getJobTemplate(Item item) { + private static DockerJobTemplateProperty getJobTemplate(final Item item) { if (item.task instanceof Project) { final Project project = (Project) item.task; - final DockerJobTemplateProperty p = project.getProperty(DockerJobTemplateProperty.class); + final DockerJobTemplateProperty p = project.getProperty( + DockerJobTemplateProperty.class); if (p != null) { return p; } // backward compatibility. DockerJobTemplateProperty used to be a nested object in DockerJobProperty - final DockerJobProperty property = project.getProperty(DockerJobProperty.class); + final DockerJobProperty property = project.getProperty( + DockerJobProperty.class); if (property != null) { return property.getDockerJobTemplate(); } @@ -75,7 +90,8 @@ private static DockerJobTemplateProperty getJobTemplate(Item item) { return null; } - private static class DockerTemplateLabelAssignmentAction extends InvisibleAction implements LabelAssignmentAction { + private static final class DockerTemplateLabelAssignmentAction extends InvisibleAction + implements LabelAssignmentAction { private final String uuid; private DockerTemplateLabelAssignmentAction(String uuid) { diff --git a/src/main/java/io/jenkins/docker/DockerTransientNode.java b/src/main/java/io/jenkins/docker/DockerTransientNode.java index cfb60780e..4c40c42de 100644 --- a/src/main/java/io/jenkins/docker/DockerTransientNode.java +++ b/src/main/java/io/jenkins/docker/DockerTransientNode.java @@ -172,7 +172,7 @@ public DockerComputer createComputer() { @Nullable @Override - public ProvisioningActivity.Id getId() { + public final ProvisioningActivity.Id getId() { return provisioningId; } @@ -184,7 +184,7 @@ private interface ILogger { @Override @Restricted(NoExternalUse.class) - public void _terminate(final TaskListener listener) { + public final void _terminate(final TaskListener listener) { final ILogger tl = createILoggerForTaskListener(listener); try { terminate(tl); diff --git a/src/main/java/io/jenkins/docker/connector/DockerComputerAttachConnector.java b/src/main/java/io/jenkins/docker/connector/DockerComputerAttachConnector.java index 104ce424f..dd830cfe3 100644 --- a/src/main/java/io/jenkins/docker/connector/DockerComputerAttachConnector.java +++ b/src/main/java/io/jenkins/docker/connector/DockerComputerAttachConnector.java @@ -186,7 +186,7 @@ enum ArgumentVariables { private final String name; private final String description; - ArgumentVariables(String name, String description) { + ArgumentVariables(final String name, final String description) { this.name = name; this.description = description; } diff --git a/src/main/java/io/jenkins/docker/package-info.java b/src/main/java/io/jenkins/docker/package-info.java new file mode 100644 index 000000000..73b053c9b --- /dev/null +++ b/src/main/java/io/jenkins/docker/package-info.java @@ -0,0 +1,8 @@ +/** + * This package contains classes related to the Jenkins pipeline integration + * for Docker, including the management of Docker nodes and steps within the pipeline. + * + *

The classes in this package manage Docker pipeline steps, node provisioning, + * and the execution of build jobs within Docker containers.

+ */ +package io.jenkins.docker; diff --git a/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java b/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java index b5b0fa45d..56bc669eb 100644 --- a/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java +++ b/src/main/java/io/jenkins/docker/pipeline/DockerNodeStepExecution.java @@ -186,7 +186,7 @@ private static DockerAPI defaultApi() { "Must either specify dockerHost/credentialsId, or define at least one Docker cloud"); } - private void invokeBody(DockerTransientNode node, TaskListener listener) { + private void invokeBody(final DockerTransientNode node, final TaskListener listener) { this.nodeName = node.getNodeName(); FilePath ws = null; Computer computer = null; @@ -256,7 +256,7 @@ public void stop(@NonNull Throwable cause) throws Exception { private static class Callback extends BodyExecutionCallback.TailCall { private final String nodeName; - public Callback(Node node) { + Callback(Node node) { this.nodeName = node.getNodeName(); }