Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c70d2fe
commit d295b67
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/main/java/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...in/resources/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
13 changes: 13 additions & 0 deletions
13
...sources/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType/help-content.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |