diff --git a/src/main/java/io/jenkins/plugins/checks/status/AbstractStatusChecksProperties.java b/src/main/java/io/jenkins/plugins/checks/status/AbstractStatusChecksProperties.java new file mode 100644 index 00000000..fecd4d02 --- /dev/null +++ b/src/main/java/io/jenkins/plugins/checks/status/AbstractStatusChecksProperties.java @@ -0,0 +1,55 @@ +package io.jenkins.plugins.checks.status; + +import hudson.ExtensionPoint; +import hudson.model.Job; + +/** + * Extension points for implementations to provide status checks properties. + * + * When no implementations is provided for a job, a {@link DefaultStatusCheckProperties} will be used. + */ +public abstract class AbstractStatusChecksProperties implements ExtensionPoint { + /** + * Returns whether the implementation is applicable for the {@code job}. + * + * @param job + * A jenkins job. + * @return true if applicable + */ + public abstract boolean isApplicable(Job job); + + /** + * Returns the name of the status check. + * + * @param job + * A jenkins job. + * @return the name of the status check + */ + public abstract String getName(Job job); + + /** + * Returns whether to skip publishing status checks. + * + * @param job + * A jenkins job. + * @return true if skip + */ + public abstract boolean isSkipped(Job job); +} + +class DefaultStatusCheckProperties extends AbstractStatusChecksProperties { + @Override + public boolean isApplicable(final Job job) { + return false; + } + + @Override + public String getName(final Job job) { + return "Jenkins"; + } + + @Override + public boolean isSkipped(final Job job) { + return true; + } +} diff --git a/src/main/java/io/jenkins/plugins/checks/status/BuildStatusChecksPublisher.java b/src/main/java/io/jenkins/plugins/checks/status/BuildStatusChecksPublisher.java index 338ff845..66fb9510 100644 --- a/src/main/java/io/jenkins/plugins/checks/status/BuildStatusChecksPublisher.java +++ b/src/main/java/io/jenkins/plugins/checks/status/BuildStatusChecksPublisher.java @@ -1,6 +1,7 @@ package io.jenkins.plugins.checks.status; import java.io.File; +import java.util.Optional; import edu.umd.cs.findbugs.annotations.CheckForNull; @@ -30,7 +31,7 @@ */ public final class BuildStatusChecksPublisher { private static final JenkinsFacade JENKINS = new JenkinsFacade(); - private static final StatusChecksProperties DEFAULT_PROPERTIES = new DefaultStatusCheckProperties(); + private static final AbstractStatusChecksProperties DEFAULT_PROPERTIES = new DefaultStatusCheckProperties(); private static void publish(final ChecksPublisher publisher, final ChecksStatus status, final ChecksConclusion conclusion, final String name) { @@ -41,8 +42,16 @@ private static void publish(final ChecksPublisher publisher, final ChecksStatus .build()); } - private static StatusChecksProperties findProperties(final Job job) { + @Deprecated + private static Optional findDeprecatedProperties(final Job job) { return JENKINS.getExtensionsFor(StatusChecksProperties.class) + .stream() + .filter(p -> p.isApplicable(job)) + .findFirst(); + } + + private static AbstractStatusChecksProperties findProperties(final Job job) { + return JENKINS.getExtensionsFor(AbstractStatusChecksProperties.class) .stream() .filter(p -> p.isApplicable(job)) .findFirst() @@ -72,10 +81,19 @@ public void onEnterWaiting(final Queue.WaitingItem wi) { } final Job job = (Job)wi.task; - final StatusChecksProperties properties = findProperties(job); - if (!properties.isSkip(job)) { - publish(ChecksPublisherFactory.fromJob(job, TaskListener.NULL), ChecksStatus.QUEUED, - ChecksConclusion.NONE, properties.getName(job)); + Optional deprecatedProperties = findDeprecatedProperties(job); + if (deprecatedProperties.isPresent()) { + if (!deprecatedProperties.get().isSkip(job)) { + publish(ChecksPublisherFactory.fromJob(job, TaskListener.NULL), ChecksStatus.QUEUED, + ChecksConclusion.NONE, deprecatedProperties.get().getName(job)); + } + } + else { + final AbstractStatusChecksProperties properties = findProperties(job); + if (!properties.isSkipped(job)) { + publish(ChecksPublisherFactory.fromJob(job, TaskListener.NULL), ChecksStatus.QUEUED, + ChecksConclusion.NONE, properties.getName(job)); + } } } } @@ -99,11 +117,20 @@ public static class JobCheckoutListener extends SCMListener { public void onCheckout(final Run run, final SCM scm, final FilePath workspace, final TaskListener listener, @CheckForNull final File changelogFile, @CheckForNull final SCMRevisionState pollingBaseline) { - final StatusChecksProperties properties = findProperties(run.getParent()); - - if (!properties.isSkip(run.getParent())) { - publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.IN_PROGRESS, ChecksConclusion.NONE, - properties.getName(run.getParent())); + final Job job = run.getParent(); + final Optional deprecatedProperties = findDeprecatedProperties(job); + if (deprecatedProperties.isPresent()) { + if (!deprecatedProperties.get().isSkip(job)) { + publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.IN_PROGRESS, + ChecksConclusion.NONE, deprecatedProperties.get().getName(job)); + } + } + else { + final AbstractStatusChecksProperties properties = findProperties(job); + if (!properties.isSkipped(job)) { + publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.IN_PROGRESS, + ChecksConclusion.NONE, properties.getName(job)); + } } } } @@ -126,11 +153,20 @@ public static class JobCompletedListener extends RunListener> { */ @Override public void onCompleted(final Run run, @CheckForNull final TaskListener listener) { - final StatusChecksProperties properties = findProperties(run.getParent()); - - if (!properties.isSkip(run.getParent())) { - publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.COMPLETED, extractConclusion(run), - properties.getName(run.getParent())); + final Job job = run.getParent(); + final Optional deprecatedProperties = findDeprecatedProperties(job); + if (deprecatedProperties.isPresent()) { + if (!deprecatedProperties.get().isSkip(job)) { + publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.COMPLETED, + extractConclusion(run), deprecatedProperties.get().getName(job)); + } + } + else { + final AbstractStatusChecksProperties properties = findProperties(job); + if (!properties.isSkipped(job)) { + publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.COMPLETED, + extractConclusion(run), properties.getName(job)); + } } } diff --git a/src/main/java/io/jenkins/plugins/checks/status/StatusChecksProperties.java b/src/main/java/io/jenkins/plugins/checks/status/StatusChecksProperties.java index 66f90646..c6f789db 100644 --- a/src/main/java/io/jenkins/plugins/checks/status/StatusChecksProperties.java +++ b/src/main/java/io/jenkins/plugins/checks/status/StatusChecksProperties.java @@ -7,7 +7,10 @@ * Properties that controls status checks. * * When no implementations is provided for a job, a {@link DefaultStatusCheckProperties} will be used. + * + * @deprecated The interface is incompatible for future changes, use {@link AbstractStatusChecksProperties} instead */ +@Deprecated public interface StatusChecksProperties extends ExtensionPoint { /** * Returns if the implementation is applicable for the {@code job}. @@ -36,20 +39,3 @@ public interface StatusChecksProperties extends ExtensionPoint { */ boolean isSkip(Job job); } - -class DefaultStatusCheckProperties implements StatusChecksProperties { - @Override - public boolean isApplicable(final Job job) { - return false; - } - - @Override - public String getName(final Job job) { - return "Jenkins"; - } - - @Override - public boolean isSkip(final Job job) { - return true; - } -}