Skip to content

Commit

Permalink
Migrate to AbstractStatusChecksProperties class. (#52)
Browse files Browse the repository at this point in the history
* Deprecate the StatusChecksProperties interface and add AbstractStatusChecksProperties class insead.

* Rename isSkip method to isSkipped.

* Clean up
  • Loading branch information
XiongKezhi authored Dec 21, 2020
1 parent 6113479 commit 3ca4e19
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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<StatusChecksProperties> 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()
Expand Down Expand Up @@ -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<StatusChecksProperties> 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));
}
}
}
}
Expand All @@ -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<StatusChecksProperties> 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));
}
}
}
}
Expand All @@ -126,11 +153,20 @@ public static class JobCompletedListener extends RunListener<Run<?, ?>> {
*/
@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<StatusChecksProperties> 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));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3ca4e19

Please sign in to comment.