Skip to content

Commit

Permalink
Port ProjectAction over to LastBuildAction
Browse files Browse the repository at this point in the history
This commit removes the "Project" action from being exposed directly on
the Publisher, and instead exposes it via the "Build" action, by
implementing the SimpleBuildSte.LastBuildAction.

This is necessary in order for Project actions to be used with Pipeline
jobs, because Jenkins doesn't know ahead of time what steps / actions
will be triggered during the Pipeline.  Implementing LastBuildAction
makes Project actions discoverable once the Pipeline has been run
at least once.
  • Loading branch information
cprice404 committed May 18, 2016
1 parent 288041c commit 34d811a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
28 changes: 27 additions & 1 deletion src/main/java/io/gatling/jenkins/GatlingBuildAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,39 @@
import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import jenkins.tasks.SimpleBuildStep;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GatlingBuildAction implements Action {
/**
* An Action to add to a project/job's build/run page in the UI.
*
* Note that in order to be compatible with the new Jenkins Pipeline jobs,
* Actions that should be added to the project/job's page directly must be added
* by implementing the SimpleBuildStep.LastBuildAction interface, and encapsulating
* the project actions in the build action via the getProjectActions method.
*
* This is necessary and now preferred to the old approach of defining getProjectAction
* directly on the Publisher, because for a Pipeline job, Jenkins doesn't know ahead
* of time what actions will be triggered, and will never call the Publisher.getProjectAction
* method. Attaching it as a LastBuildAction means that it is discoverable once
* the Pipeline job has been run once.
*/
public class GatlingBuildAction implements Action, SimpleBuildStep.LastBuildAction {

private final Run<?, ?> build;
private final List<BuildSimulation> simulations;
private final List<GatlingProjectAction> projectActions;

public GatlingBuildAction(Run<?, ?> build, List<BuildSimulation> sims) {
this.build = build;
this.simulations = sims;

List<GatlingProjectAction> projectActions = new ArrayList<>();
projectActions.add(new GatlingProjectAction(build.getParent()));
this.projectActions = projectActions;
}

public Run<?, ?> getBuild() {
Expand Down Expand Up @@ -80,4 +102,8 @@ private BuildSimulation getSimulation(String simulationName) {
return null;
}

@Override
public Collection<? extends Action> getProjectActions() {
return this.projectActions;
}
}
18 changes: 8 additions & 10 deletions src/main/java/io/gatling/jenkins/GatlingProjectAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import static io.gatling.jenkins.PluginConstants.*;

import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.*;

import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -30,9 +28,9 @@

public class GatlingProjectAction implements Action {

private final AbstractProject<?, ?> project;
private final Job<?, ?> project;

public GatlingProjectAction(AbstractProject<?, ?> project) {
public GatlingProjectAction(Job<?, ?> project) {
this.project = project;
}

Expand All @@ -48,12 +46,12 @@ public String getUrlName() {
return URL_NAME;
}

public AbstractProject<?, ?> getProject() {
public Job<?, ?> getProject() {
return project;
}

public boolean isVisible() {
for (AbstractBuild<?, ?> build : getProject().getBuilds()) {
for (Run<?, ?> build : getProject().getBuilds()) {
GatlingBuildAction gatlingBuildAction = build.getAction(GatlingBuildAction.class);
if (gatlingBuildAction != null) {
return true;
Expand Down Expand Up @@ -98,10 +96,10 @@ public Long getValue(RequestReport requestReport) {
};
}

public Map<AbstractBuild<?, ?>, List<String>> getReports() {
Map<AbstractBuild<?, ?>, List<String>> reports = new LinkedHashMap<AbstractBuild<?, ?>, List<String>>();
public Map<Run<?, ?>, List<String>> getReports() {
Map<Run<?, ?>, List<String>> reports = new LinkedHashMap<Run<?, ?>, List<String>>();

for (AbstractBuild<?, ?> build : project.getBuilds()) {
for (Run<?, ?> build : project.getBuilds()) {
GatlingBuildAction action = build.getAction(GatlingBuildAction.class);
if (action != null) {
List<String> simNames = new ArrayList<String>();
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/gatling/jenkins/GatlingPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}

@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
return new GatlingProjectAction(project);
}

private List<BuildSimulation> saveFullReports(FilePath workspace, File rootDir) throws IOException, InterruptedException {
FilePath[] files = workspace.list("**/global_stats.json");
List<FilePath> reportFolders = new ArrayList<FilePath>();
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/io/gatling/jenkins/chart/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package io.gatling.jenkins.chart;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;

import java.io.IOException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.model.Job;
import hudson.model.Run;
import io.gatling.jenkins.GatlingBuildAction;
import io.gatling.jenkins.BuildSimulation;
import io.gatling.jenkins.RequestReport;
Expand All @@ -36,9 +35,9 @@ public abstract class Graph<Y extends Number> {

private final ObjectMapper mapper = new ObjectMapper();

public Graph(AbstractProject<?, ?> project, int maxBuildsToDisplay) {
public Graph(Job<?, ?> project, int maxBuildsToDisplay) {
int numberOfBuild = 0;
for (AbstractBuild<?, ?> build : project.getBuilds()) {
for (Run<?, ?> build : project.getBuilds()) {
GatlingBuildAction action = build.getAction(GatlingBuildAction.class);

if (action != null) {
Expand Down

0 comments on commit 34d811a

Please sign in to comment.