Skip to content

Commit

Permalink
[JENKINS-31831] Make Snippetizer UI be displayed in a top-level page.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed May 20, 2016
1 parent 891a1bd commit f395899
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 177 deletions.
Expand Up @@ -37,23 +37,12 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import jenkins.model.Jenkins;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.MultipleCompilationErrorsException;
import org.codehaus.groovy.control.ProcessingUnit;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.syntax.SyntaxException;
import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage;
Expand Down Expand Up @@ -119,8 +108,6 @@ public CpsFlowExecution create(FlowExecutionOwner owner, TaskListener listener,
@Extension
public static class DescriptorImpl extends FlowDefinitionDescriptor {

@Inject public Snippetizer snippetizer;

@Override
public String getDisplayName() {
return "Pipeline script";
Expand Down
Expand Up @@ -41,7 +41,6 @@
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.JOB;
Expand Down Expand Up @@ -133,8 +132,6 @@ private String getFilePathSuffix() {

@Extension public static class DescriptorImpl extends FlowDefinitionDescriptor {

@Inject public Snippetizer snippetizer;

@Override public String getDisplayName() {
return "Pipeline script from SCM";
}
Expand Down
67 changes: 41 additions & 26 deletions src/main/java/org/jenkinsci/plugins/workflow/cps/Snippetizer.java
Expand Up @@ -26,10 +26,15 @@

import hudson.Extension;
import hudson.Functions;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.DescriptorByNameOwner;
import hudson.model.InvisibleAction;
import hudson.model.Job;
import hudson.model.RootAction;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand All @@ -38,8 +43,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.lang.model.SourceVersion;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import jenkins.model.TransientActionFactory;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
Expand All @@ -51,13 +56,11 @@
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

/**
* Takes a {@link Step} as configured through the UI and tries to produce equivalent Groovy code.
* Render using: {@code <st:include it="${theSnippetizerInstance}" page="block.jelly"/>}
*/
@Extension public class Snippetizer implements RootAction {
@Extension public class Snippetizer implements RootAction, DescriptorByNameOwner {

static String object2Groovy(Object o) throws UnsupportedOperationException {
Class<? extends Object> clazz = o.getClass();
Expand Down Expand Up @@ -159,7 +162,7 @@ private static void render(StringBuilder b, Object value) {
}
}

private static final String ACTION_URL = "workflow-cps-snippetizer";
public static final String ACTION_URL = "pipeline-groovy";

@Override public String getUrlName() {
return ACTION_URL;
Expand All @@ -170,9 +173,14 @@ private static void render(StringBuilder b, Object value) {
}

@Override public String getDisplayName() {
// Do not want to add to main Jenkins sidepanel.
return null;
}

@Override public Descriptor getDescriptorByName(String id) {
return Jenkins.getActiveInstance().getDescriptorByName(id);
}

@Restricted(DoNotUse.class)
public Collection<? extends StepDescriptor> getStepDescriptors(boolean advanced) {
TreeSet<StepDescriptor> t = new TreeSet<StepDescriptor>(new StepDescriptorComparator());
Expand All @@ -190,14 +198,6 @@ public Iterable<GlobalVariable> getGlobalVariables() {
return GlobalVariable.ALL;
}

@Restricted(NoExternalUse.class)
public static final String HELP_URL = ACTION_URL + "/help";

@Restricted(DoNotUse.class)
public void doHelp(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveLocalizedFile(req, Snippetizer.class.getResource("Snippetizer/help.html"));
}

@Restricted(NoExternalUse.class)
public static final String GENERATE_URL = ACTION_URL + "/generateSnippet";

Expand Down Expand Up @@ -227,18 +227,6 @@ public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String
}
}

@Restricted(NoExternalUse.class)
public static final String GDSL_URL = ACTION_URL + "/gdsl";

@Restricted(NoExternalUse.class)
public static final String DSLD_URL = ACTION_URL + "/dsld";

@Restricted(NoExternalUse.class)
public static final String DSL_REF_URL = ACTION_URL + "/dslReference";

@Restricted(NoExternalUse.class)
public static final String DSL_HELP_URL = ACTION_URL + "/dslHelp";

private static class StepDescriptorComparator implements Comparator<StepDescriptor>, Serializable {
@Override
public int compare(StepDescriptor o1, StepDescriptor o2) {
Expand All @@ -247,4 +235,31 @@ public int compare(StepDescriptor o1, StepDescriptor o2) {
private static final long serialVersionUID = 1L;
}

@Restricted(DoNotUse.class)
@Extension public static class SidepanelAdder extends TransientActionFactory<Job> {

@Override public Class<Job> type() {
return Job.class;
}

@Override public Collection<? extends Action> createFor(Job target) {
// TODO probably want an API for FlowExecutionContainer or something
if (target.getClass().getName().equals("org.jenkinsci.plugins.workflow.job.WorkflowJob")) {
return Collections.singleton(new SidepanelAction());
} else {
return Collections.emptySet();
}
}

}

@Restricted(NoExternalUse.class)
public static class SidepanelAction extends /* has a custom action.jelly anyway */ InvisibleAction {

public static final String ACTION_URL = Snippetizer.ACTION_URL;

// TODO should retain the Job reference, and pass it along as context

}

}
Expand Up @@ -30,5 +30,4 @@
<f:entry field="sandbox">
<f:checkbox title="${%Use Groovy Sandbox}" default="true"/>
</f:entry>
<st:include it="${descriptor.snippetizer}" page="block.jelly"/>
</j:jelly>
@@ -1,6 +1,7 @@
<p>
Groovy script defining this Pipeline. A quick example:
</p>
<!-- TODO this content should be moved to Snippetizer -->
<pre>node {
sh 'echo hello world'
}</pre>
Expand Down
Expand Up @@ -29,5 +29,4 @@ THE SOFTWARE.
<f:entry field="scriptPath" title="${%Script Path}">
<f:textbox default="Jenkinsfile"/>
</f:entry>
<st:include it="${descriptor.snippetizer}" page="block.jelly"/>
</j:jelly>
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2016 CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout">
<div class="task"> <!-- TODO cannot use l:task since that does not support target -->
<a href="${rootURL}/${action.ACTION_URL}" class="task-icon-link" target="snippetizer">
<l:icon class="icon-help icon-md" style="width: 24px; height: 24px; margin: 2px;"/>
</a>
<st:nbsp/>
<a href="${rootURL}/${action.ACTION_URL}" class="task-link" target="snippetizer">
${%Pipeline Groovy}
</a>
</div>
</j:jelly>
Expand Up @@ -23,77 +23,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<!-- Deprecated, but kept for compatibility -->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form" xmlns:l="/lib/layout" xmlns:d="jelly:define" xmlns:local="local">
<d:taglib uri="local">
<d:tag name="listSteps">
<j:forEach var="stepDescriptor" items="${stepDescriptors}">
<f:dropdownListBlock title="${stepDescriptor.functionName}: ${stepDescriptor.displayName}" staplerClass="${stepDescriptor.clazz.name}" lazy="stepDescriptor,it">
<l:ajax>
<j:set var="instance" value="${null}"/>
<j:set var="descriptor" value="${stepDescriptor}"/>
<j:set var="help" value="${descriptor.helpFile}"/>
<j:if test="${help != null}">
<tr>
<td colspan="3"/>
<f:helpLink url="${help}"/>
</tr>
<f:helpArea/>
</j:if>
<st:include from="${descriptor}" page="${descriptor.configPage}" optional="true">
<f:block>
${%This step has not yet defined any visual configuration.}
</f:block>
</st:include>
</l:ajax>
</f:dropdownListBlock>
</j:forEach>
</d:tag>
</d:taglib>
<f:entry title="Pipeline DSL Reference" help="/${it.DSL_HELP_URL}"/>
<f:optionalBlock name="snippetizer" title="Snippet Generator" help="/${it.HELP_URL}">
<f:section title="Steps"/>
<!-- Similar to f:dropdownDescriptorSelector, but adds fallback content to block, and JENKINS-25130 adds per-selection help: -->
<f:dropdownList name="prototype" title="Sample Step">
<local:listSteps stepDescriptors="${it.getStepDescriptors(false)}"/>
<f:dropdownListBlock title="${%— Advanced/Deprecated —}"/>
<local:listSteps stepDescriptors="${it.getStepDescriptors(true)}"/>
</f:dropdownList>
<j:set var="id" value="${h.generateId()}"/>
<f:block>
<input type="button" value="Generate Groovy" onclick="handlePrototype_${id}(); return false" class="submit-button primary"/>
<f:textarea id="prototypeText_${id}" readonly="true" style="margin-top: 10px" />
<script>
function handlePrototype_${id}() {
buildFormTree(document.forms.config);
// TODO JSON.stringify fails in some circumstances: https://gist.github.com/jglick/70ec4b15c1f628fdf2e9 due to Array.prototype.toJSON
var json = Object.toJSON(JSON.parse(${configFormJS ?: 'document.forms.config'}.elements.json.value).${definitionJS ?: 'definition'}.snippetizer.prototype);
if (!json) {
return; // just a separator
}
new Ajax.Request('${rootURL}/${it.GENERATE_URL}', {
method: 'POST',
parameters: {json: json},
onSuccess: function(r) {
document.getElementById('prototypeText_${id}').value = r.responseText;
}
});
}
</script>
</f:block>
<f:section title="Global variables"/>
<f:dropdownList name="var" title="Variable">
<j:forEach var="var" items="${it.globalVariables}">
<f:dropdownListBlock title="${var.name}" lazy="var,it">
<l:ajax>
<f:block>
<st:include it="${var}" page="help" optional="true">
${%This variable has not supplied a description.}
</st:include>
</f:block>
</l:ajax>
</f:dropdownListBlock>
</j:forEach>
</f:dropdownList>
</f:optionalBlock>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:block>
<a href="${rootURL}/${it.ACTION_URL}" target="_blank">${%Pipeline Groovy Documentation}</a>
</f:block>
</j:jelly>

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit f395899

Please sign in to comment.