Skip to content

Commit

Permalink
Merge pull request #13 from jenkinsci/listen-to-checkout
Browse files Browse the repository at this point in the history
Listen to checkout instead of on started.
  • Loading branch information
XiongKezhi authored Aug 21, 2020
2 parents 18d0b21 + cbeae0e commit 350f0f4
Showing 1 changed file with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.jenkins.plugins.checks;

import edu.umd.cs.findbugs.annotations.NonNull;

import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.*;

import hudson.model.listeners.RunListener;
import hudson.model.listeners.SCMListener;
import hudson.model.queue.QueueListener;
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder;
import io.jenkins.plugins.checks.api.ChecksPublisher;
import io.jenkins.plugins.checks.api.ChecksPublisherFactory;
import io.jenkins.plugins.checks.api.ChecksStatus;

import java.io.File;

/**
* A publisher which publishes different statuses through the checks API based on the stage of the {@link Queue.Item}
* or {@link Run}.
Expand All @@ -32,53 +36,72 @@ private static void publish(final ChecksPublisher publisher, final ChecksStatus
/**
* {@inheritDoc}
*
* <p>
* Listens to the queue and publishes checks in "queued" state for entering items.
* </p>
*/
@Extension
public static class JobScheduledListener extends QueueListener {
/**
* {@inheritDoc}
*
* <p>
* When a job enters queue, creates the check on "queued".
* </p>
*/
@Override
public void onEnterWaiting(final Queue.WaitingItem wi) {
if (!(wi.task instanceof Job)) {
if (!(wi.task instanceof Job)) {
return;
}

publish(ChecksPublisherFactory.fromJob((Job)wi.task, null),
ChecksStatus.QUEUED, ChecksConclusion.NONE);
publish(ChecksPublisherFactory.fromJob((Job) wi.task, TaskListener.NULL), ChecksStatus.QUEUED,
ChecksConclusion.NONE);
}
}

/**
* {@inheritDoc}
*
* Listens to the run and publishes checks for started and completed run.
* <p>
* Listens to the SCM checkout and publishes checks.
* </p>
*/
@Extension
public static class JobStartedListener extends RunListener<Run<?, ?>> {
public static class JobCheckoutListener extends SCMListener {
/**
* {@inheritDoc}
*
* When a job starts, updates the check to "in progress".
* <p>
* When checkout finished, update the check to "in progress".
* </p>
*/
@Override
public void onStarted(final Run run, final TaskListener listener) {
publish(ChecksPublisherFactory.fromRun(run, listener),
ChecksStatus.IN_PROGRESS, ChecksConclusion.NONE);
public void onCheckout(final Run<?, ?> run, final SCM scm, final FilePath workspace,
final TaskListener listener, @Nullable final File changelogFile,
@Nullable final SCMRevisionState pollingBaseline) {
publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.IN_PROGRESS, ChecksConclusion.NONE);
}
}

/**
* {@inheritDoc}
*
* <p>
* Listens to the run and publishes checks.
* </p>
*/
@Extension
public static class JobCompletedListener extends RunListener<Run<?, ?>> {
/**
* {@inheritDoc}
*
* <p>
* When a job completes, completes the check.
* </p>
*/
@Override
public void onCompleted(final Run run, @NonNull final TaskListener listener) {
publish(ChecksPublisherFactory.fromRun(run, listener),
ChecksStatus.COMPLETED, extractConclusion(run));
public void onCompleted(final Run run, @Nullable final TaskListener listener) {
publish(ChecksPublisherFactory.fromRun(run, listener), ChecksStatus.COMPLETED, extractConclusion(run));
}

private ChecksConclusion extractConclusion(final Run<?, ?> run) {
Expand Down

0 comments on commit 350f0f4

Please sign in to comment.