Skip to content
Permalink
Browse files
Fix JENKINS-14056
  • Loading branch information
gboissinot committed Jun 17, 2012
1 parent c70d2fe commit d295b675c16fabc939f3453e7cc79c1498c8f681
Showing 3 changed files with 111 additions and 0 deletions.
@@ -0,0 +1,76 @@
package org.jenkinsci.plugins.sharedobjects.type;

import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.model.AbstractBuild;
import org.jenkinsci.plugins.sharedobjects.MultipleSharedObjectType;
import org.jenkinsci.plugins.sharedobjects.SharedObjectException;
import org.jenkinsci.plugins.sharedobjects.SharedObjectType;
import org.jenkinsci.plugins.sharedobjects.SharedObjectTypeDescriptor;
import org.jenkinsci.plugins.sharedobjects.service.SharedObjectLogger;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.HashMap;
import java.util.Map;

/**
* @author Gregory Boissinot
*/
public class GroovySharedObjectType extends MultipleSharedObjectType {

private String content;

@DataBoundConstructor
public GroovySharedObjectType(String name, String profiles, String content) {
super(name, profiles);
this.content = content;
}

@SuppressWarnings("unused")
public String getContent() {
return content;
}

@Override
public Map<String, String> getEnvVars(AbstractBuild build, SharedObjectLogger logger) throws SharedObjectException {
if (content == null) {
return new HashMap<String, String>();
}

if (content.trim().length() == 0) {
return new HashMap<String, String>();
}

logger.info(String.format("Evaluation the following Groovy script content: \n%s\n", content));
GroovyShell shell = new GroovyShell();
Object groovyResult = shell.evaluate(content);
if (groovyResult != null && !(groovyResult instanceof Map)) {
throw new SharedObjectException("The evaluated Groovy script must return a Map object.");
}

Map<String, String> result = new HashMap<String, String>();
if (groovyResult == null) {
return result;
}

for (Map.Entry entry : (((Map<Object, Object>) groovyResult).entrySet())) {
result.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
return result;

}

@Extension
public static class GroovySharedObjectTypeDescriptor extends SharedObjectTypeDescriptor {

@Override
public String getDisplayName() {
return "Groovy script";
}

@Override
public Class<? extends SharedObjectType> getType() {
return GroovySharedObjectType.class;
}
}
}
@@ -0,0 +1,22 @@
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">

<f:entry title="${%Type}">
<label>"${descriptor.displayName}"</label>
</f:entry>

<f:entry title="${%Name}"
help="/descriptor/org.jenkinsci.plugins.sharedobjects.type.PublicFilePathSharedObjectType/help/name">
<f:textbox name="name" value="${instance.name}"/>
</f:entry>

<f:entry title="${%Profiles}"
help="/descriptor/org.jenkinsci.plugins.sharedobjects.type.PublicFilePathSharedObjectType/help/profiles">
<f:textbox name="profiles" value="${instance.profiles}"/>
</f:entry>

<f:entry title="${%Groovy script}"
help="/descriptor/org.jenkinsci.plugins.sharedobjects.type.GroovySharedObjectType/help/content">
<f:textarea name="content" value="${instance.content}"/>
</f:entry>

</j:jelly>
@@ -0,0 +1,13 @@
<div>
<p>
Evaluate a Groovy script and inject a map result.<br/>
The groovy script must return a map Java object.<br/>
<br/>
For example, the following script injects the SOMEVAR environment variable with the 'someValue' value:<br/>
<i>
def map = [SOMEVAR: "someValue"] <br/>
return map <br/>
} <br/>
</i>
</p>
</div>

0 comments on commit d295b67

Please sign in to comment.