Skip to content

Commit

Permalink
Merge pull request #94 from mikebro/master
Browse files Browse the repository at this point in the history
Add support for Token Macro tm pipeline step
  • Loading branch information
rsandell authored Feb 4, 2019
2 parents 221ac5a + a2d10a2 commit 1421971
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 95 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.10</version>
<version>2.2</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.sonyericsson.jenkins.plugins.bfa.model.FailureCauseMatrixBuildAction;
import com.sonyericsson.jenkins.plugins.bfa.sod.ScanOnDemandTask;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
Expand Down Expand Up @@ -94,15 +96,32 @@ public boolean acceptsMacroName(final String macroName) {
public String evaluate(final AbstractBuild<?, ?> build, final TaskListener listener, final String macroName)
throws MacroEvaluationException, IOException, InterruptedException {

return evaluate(build);
}

@Override
public String evaluate(final Run<?, ?> run, final FilePath workspace, final TaskListener listener,
final String macroName)
throws MacroEvaluationException, IOException, InterruptedException {

return evaluate(run);
}

/**
* @param run The run to analyze
* @return The results of the build failure analyzer
*/
private String evaluate(final Run<?, ?> run) {

// Scan the build now.
new ScanOnDemandTask(build).run();
new ScanOnDemandTask(run).run();

final FailureCauseBuildAction action = build.getAction(FailureCauseBuildAction.class);
final FailureCauseBuildAction action = run.getAction(FailureCauseBuildAction.class);
if (action != null) {
return renderer.render(action);
}

final FailureCauseMatrixBuildAction matrixAction = build.getAction(FailureCauseMatrixBuildAction.class);
final FailureCauseMatrixBuildAction matrixAction = run.getAction(FailureCauseMatrixBuildAction.class);
if (matrixAction != null) {
return renderer.render(matrixAction);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.sonyericsson.jenkins.plugins.bfa.tokens;

import hudson.FilePath;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertEquals;

/**
* Tests that the plugin is compatible with the token macro pipeline step.
*/
public class PipelineTokenTest {
/**
* The Jenkins Rule.
*/
@Rule
//CS IGNORE VisibilityModifier FOR NEXT 1 LINES. REASON: Jenkins Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

//CS IGNORE LineLength FOR NEXT 11 LINES. REASON: Test data.
private static final String DECLARATIVE_PIPELINE =
"pipeline {\n"
+ " agent any\n"
+ " stages {\n"
+ " stage(\"Run declarative bfa\") {\n"
+ " steps {\n"
+ " writeFile file: 'bfa.log', text: tm('''${BUILD_FAILURE_ANALYZER, noFailureText=\"No errors found - Declarative\"}''')\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n";

//CS IGNORE LineLength FOR NEXT 6 LINES. REASON: Test data.
private static final String SCRIPTED_PIPELINE =
"node {\n"
+ " stage(\"Run scripted bfa\") {\n"
+ " writeFile file: 'bfa.log', text: tm('''${BUILD_FAILURE_ANALYZER, noFailureText=\"No errors found - Scripted\"}''')\n"
+ " }\n"
+ "}\n";

/**
* Tests that the plugin is run by the tm pipeline step in a declarative pipeline by writing the result to a file.
*
* @throws Exception If necessary
*/
@Test
public void declarativePipelineTokenMacro() throws Exception {
WorkflowJob project = jenkinsRule.createProject(WorkflowJob.class);
project.setDefinition(new CpsFlowDefinition(DECLARATIVE_PIPELINE, true));

final WorkflowRun build = jenkinsRule.buildAndAssertSuccess(project);
final FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(project);
final FilePath bfaLog = workspace.child("bfa.log");

final String bfaLogText = bfaLog.readToString();

assertEquals("No errors found - Declarative", bfaLogText);
}

/**
* Tests that the plugin is run by the tm pipeline step in a scripted pipeline by writing the result to a file.
*
* @throws Exception If necessary
*/
@Test
public void scriptedPipelineTokenMacro() throws Exception {
WorkflowJob project = jenkinsRule.createProject(WorkflowJob.class);
project.setDefinition(new CpsFlowDefinition(SCRIPTED_PIPELINE, true));

final WorkflowRun build = jenkinsRule.buildAndAssertSuccess(project);
final FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(project);
final FilePath bfaLog = workspace.child("bfa.log");

final String bfaLogText = bfaLog.readToString();

assertEquals("No errors found - Scripted", bfaLogText);
}
}
Loading

0 comments on commit 1421971

Please sign in to comment.