Skip to content

Commit

Permalink
Preliminary pipeline support
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Oct 9, 2024
1 parent c36108d commit ea359e5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@
import hudson.tasks.junit.JUnitResultArchiver;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This gets invoked when classic "freestyle" project runs its step
*/
@Extension
public class BuildStepListenerImpl extends BuildStepListener {
@Inject
Configuration configuration;
Ingester ingester;

public void started(AbstractBuild build, BuildStep bs, BuildListener listener) {
// noop
}

public void finished(AbstractBuild build, BuildStep bs, BuildListener listener, boolean canContinue) {
if (bs instanceof JUnitResultArchiver) {
File report = new File(build.getRootDir(), "junitResult.xml");
if (!report.exists()) return; // be defensive just in case

// TODO
System.out.println("Sending "+report+" to Launchable");
try {
// if somebody configured JUnit result archiving step we hit here,
// so we just slurp the result unprocessed to the server side.
if (bs instanceof JUnitResultArchiver) {
ingester.slurp(build.getRootDir());
}
} catch (IOException e) {
// Priority #1: Do no harm to people's build
LOGGER.log(Level.WARNING, "Failed to send JUnit result to Launchable", e);
}
}

private static final Logger LOGGER = Logger.getLogger("io.jenkins.plugins.launchable.BuildStepListenerImpl");
}
31 changes: 31 additions & 0 deletions src/main/java/io/jenkins/plugins/launchable/GraphListenerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.jenkins.plugins.launchable;

import hudson.Extension;
import org.jenkinsci.plugins.workflow.flow.GraphListener;
import org.jenkinsci.plugins.workflow.graph.FlowNode;

import javax.inject.Inject;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

@Extension
public class GraphListenerImpl implements GraphListener {
@Inject
Ingester ingester;

@Override
public void onNewHead(FlowNode node) {
try {
if (node.getDisplayFunctionName().equals("junit")) {// TODO: probably not the most robust check
// owner is actually WorkflowRun
ingester.slurp(node.getExecution().getOwner().getRootDir());
}
} catch (IOException e) {
// Priority #1: Do no harm to people's build
LOGGER.log(Level.WARNING, "Failed to send JUnit result to Launchable", e);
}
}

private static final Logger LOGGER = Logger.getLogger("io.jenkins.plugins.launchable.GraphListenerImpl");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import hudson.Extension;
import hudson.util.Secret;
import hudson.util.TextFile;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

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

@Extension
public class Configuration extends GlobalConfiguration {
public class Ingester extends GlobalConfiguration {
private Secret apiKey;

public Secret getApiKey() {
Expand All @@ -23,4 +27,12 @@ public void setApiKey(Secret apiKey) {
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
return super.configure(req, json);
}

/*package*/ void slurp(File dir) throws IOException {
File report = new File(dir,"junitResult.xml");
if (!report.exists()) return; // be defensive just in case

System.out.println("Sending "+report+" to Launchable");
System.out.println(new TextFile(report).read());
}
}

0 comments on commit ea359e5

Please sign in to comment.