Skip to content

Commit

Permalink
Fix JENKINS-32012
Browse files Browse the repository at this point in the history
Add removeNewlines parameter.
Fix issue when job description is not set (null)
  • Loading branch information
slide committed Dec 10, 2015
1 parent 3cef2c6 commit 22a5aa7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Expand Up @@ -13,6 +13,9 @@ public class JobDescriptionMacro extends DataBoundTokenMacro {

public static final String MACRO_NAME = "JOB_DESCRIPTION";

@Parameter
public Boolean removeNewlines = false;

@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals(MACRO_NAME);
Expand All @@ -21,6 +24,14 @@ public boolean acceptsMacroName(String macroName) {
@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
return build.getParent().getDescription();
String val = build.getParent().getDescription();
if(val != null) {
if (removeNewlines) {
val = val.replaceAll("[\\n\\r]", " ");
}
} else {
val = "";
}
return val;
}
}
@@ -0,0 +1,54 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.jenkinsci.plugins.tokenmacro.impl;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.util.StreamTaskListener;
import jenkins.model.JenkinsLocationConfiguration;
import static junit.framework.TestCase.assertEquals;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

/**
*
* @author acearl
*/
public class JobDescriptionMacroTest {
@Rule
public final JenkinsRule j = new JenkinsRule();

@Test
public void testNoDescription() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("noDescription");
FreeStyleBuild b = p.scheduleBuild2(0).get();

assertEquals("",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${JOB_DESCRIPTION}"));
}

@Test
public void testSimple() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("testSimple");
p.setDescription("This is the description");
FreeStyleBuild b = p.scheduleBuild2(0).get();

assertEquals("This is the description",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${JOB_DESCRIPTION}"));
}

@Issue("JENKINS-32012")
@Test
public void testRemoveNewlines() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("testRemoveNewlines");
p.setDescription("This is a description\nwith a newline");
FreeStyleBuild b = p.scheduleBuild2(0).get();

assertEquals("This is a description with a newline", TokenMacro.expand(b, StreamTaskListener.fromStdout(), "${JOB_DESCRIPTION, removeNewlines=true}"));
}
}

0 comments on commit 22a5aa7

Please sign in to comment.