@@ -0,0 +1,3499 @@
+package net.nemerosa.ontrack.jenkins;%0A%0Aimport com.fasterxml.jackson.databind.JsonNode;%0Aimport hudson.Extension;%0Aimport hudson.Launcher;%0Aimport hudson.model.*;%0Aimport hudson.tasks.BuildStepDescriptor;%0Aimport hudson.tasks.Builder;%0Aimport org.kohsuke.stapler.DataBoundConstructor;%0A%0Aimport java.io.IOException;%0Aimport java.io.PrintStream;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0Aimport net.nemerosa.ontrack.jenkins.support.client.OntrackClient;%0A%0A/**%0A * Plug-in that allows to inject the last build name of a branch into%0A * an environment variable.%0A */%0Apublic class OntrackLastBuildStep extends Builder %7B%0A%0A    private final String project;%0A    private final String branch;%0A    private final String variable;%0A%0A    @DataBoundConstructor%0A    public OntrackLastBuildStep(String project, String branch, String variable) %7B%0A        this.project = project;%0A        this.branch = branch;%0A        this.variable = variable;%0A    %7D%0A%0A    public String getProject() %7B%0A        return project;%0A    %7D%0A%0A    public String getBranch() %7B%0A        return branch;%0A    %7D%0A%0A    public String getVariable() %7B%0A        return variable;%0A    %7D%0A%0A    @Override%0A    public boolean perform(AbstractBuild%3C?, ?%3E theBuild, Launcher launcher, BuildListener listener) throws InterruptedException, IOException %7B%0A        final String actualProject = OntrackPluginSupport.expand(project, theBuild, listener);%0A        final String actualBranch = OntrackPluginSupport.expand(branch, theBuild, listener);%0A        JsonNode lastBuild = getBuild(System.out, actualProject, actualBranch);%0A        // Found%0A        if (lastBuild != null) %7B%0A            String name = lastBuild.path(%22name%22).textValue();%0A            listener.getLogger().format(%22Found build '%25s' for branch '%25s' and project '%25s'%25n%22, name, actualBranch, actualProject);%0A            theBuild.addAction(new ParametersAction(new StringParameterValue(variable, name)));%0A        %7D else %7B%0A            listener.getLogger().format(%22Could not find any build for branch '%25s' and project '%25s'%25n%22, actualBranch, actualProject);%0A            theBuild.setResult(Result.FAILURE);%0A        %7D%0A        // OK%0A        return true;%0A    %7D%0A    %0A    /**%0A     * Get last build with a certain promotion level on a branch%0A     * @param logger The logger%0A     * @param project The project%0A     * @param branch The branch%0A     * @param promotionLevel The promotion level%0A     * @return The build or null of no build found.%0A     * @throws IOException%0A     */%0A    private JsonNode getBuild(PrintStream logger, final String project, final String branch) throws IOException %7B%0A        Map%3CString, Object%3E filter = new LinkedHashMap%3CString, Object%3E();%0A        filter.put(%22count%22, 1);%0A        JsonNode branchBuildView = OntrackClient.forBranch(logger, project, branch)%0A                .on(%22_view%22, %22/net.nemerosa.ontrack.service.StandardBuildFilterProvider%22, filter)%0A                .get();%0A        JsonNode buildViews = branchBuildView.get(%22buildViews%22);%0A        if (buildViews.size() %3E 0) %7B%0A            // Gets the first build that complies%0A            return buildViews.get(0).get(%22build%22);%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A    @Extension%0A    public static class OntrackLastBuildStepDescription extends BuildStepDescriptor%3CBuilder%3E %7B%0A%0A%0A        @Override%0A        public boolean isApplicable(Class%3C? extends AbstractProject%3E jobType) %7B%0A            return true;%0A        %7D%0A%0A        @Override%0A        public String getDisplayName() %7B%0A            return %22Ontrack: Last build%22;%0A        %7D%0A    %7D%0A%7D%0A

Allow jenkins to get last build for Ontrack project/branch.