Skip to content

Commit

Permalink
Capture additional metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Oct 10, 2024
1 parent ea359e5 commit 5b41564
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import hudson.model.BuildStepListener;
import hudson.tasks.BuildStep;
import hudson.tasks.junit.JUnitResultArchiver;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

import javax.inject.Inject;
import java.io.IOException;
Expand All @@ -29,7 +31,7 @@ public void finished(AbstractBuild build, BuildStep bs, BuildListener listener,
// 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());
ingester.slurp(build.getRootDir(), new PropsBuilder<>(build));
}
} catch (IOException e) {
// Priority #1: Do no harm to people's build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.jenkins.plugins.launchable;

import hudson.Extension;
import hudson.model.Queue;
import org.jenkinsci.plugins.workflow.flow.GraphListener;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import javax.inject.Inject;
import java.io.IOException;
Expand All @@ -18,8 +20,11 @@ public class GraphListenerImpl implements GraphListener {
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());
Queue.Executable owner = node.getExecution().getOwner().getExecutable();
if (owner instanceof WorkflowRun) {// another defensive check just to be safe
WorkflowRun run = (WorkflowRun) owner;
ingester.slurp(run.getRootDir(), new PropsBuilder<>(run));
}
}
} catch (IOException e) {
// Priority #1: Do no harm to people's build
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/jenkins/plugins/launchable/Ingester.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.plugins.launchable;

import hudson.Extension;
import hudson.tasks.junit.TestResult;
import hudson.util.Secret;
import hudson.util.TextFile;
import jenkins.model.GlobalConfiguration;
Expand Down Expand Up @@ -28,11 +29,18 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
return super.configure(req, json);
}

/*package*/ void slurp(File dir) throws IOException {
/**
* @param dir
* Directory that's supposed to contain test results. See {@link TestResult} for the data format.
* @param properties
* Additional contextual data to submit along with the test results.
*/
/*package*/ void slurp(File dir, PropsBuilder<?> properties) 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(properties.build().toString());
System.out.println(new TextFile(report).read());
}
}
33 changes: 33 additions & 0 deletions src/main/java/io/jenkins/plugins/launchable/PropsBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.jenkins.plugins.launchable;

import hudson.model.Run;
import net.sf.json.JSONObject;

/**
* Extensive properties builder for a Run object.
*/
class PropsBuilder<B extends Run<?,?>> {
protected final B run;

public PropsBuilder(B run) {
this.run = run;
}

public JSONObject build() {
return new JSONObject()
.accumulate("job", buildJobProperties())
.accumulate("build", buildRunProperties());
}

protected JSONObject buildRunProperties() {
return new JSONObject()
.accumulate("number", run.getNumber())
.accumulate("displayName", run.getDisplayName());
}

protected JSONObject buildJobProperties() {
return new JSONObject()
.accumulate("fullName", run.getParent().getFullName())
.accumulate("type", run.getParent().getClass().getName());
}
}

0 comments on commit 5b41564

Please sign in to comment.