From efe60a87e425f82a5fa52e01062617182ace3f55 Mon Sep 17 00:00:00 2001 From: Kanstantsin Shautsou Date: Tue, 4 Oct 2016 17:05:22 +0300 Subject: [PATCH] Attempt to make verifications with expandableTextbox Signed-off-by: Kanstantsin Shautsou --- src/main/resources/rebel.xml | 9 + .../.rebel-remote.xml.bak | 4 + yet-another-docker-plugin/pom.xml | 7 + .../kostyasha/yad/DockerManagementLink.java | 177 ++++++++++++++++++ .../yad/DockerManagementLink/global.groovy | 0 .../yad/DockerManagementLink/index.groovy | 22 +++ .../kostyasha/yad/DockerSlave/config.groovy | 3 + .../DockerCreateContainer/config.groovy | 11 +- .../resources/lib/yad/expandableTextbox.jelly | 57 ++++++ .../expandableTextbox/expandableTextbox.js | 14 ++ .../src/main/resources/lib/yad/taglib | 0 .../src/main/resources/rebel.xml | 4 +- .../src/main/webapp/js/yad.js | 14 ++ yet-another-docker-plugin/yad2.log | 0 yet-another-docker-plugin/yad2.log.lck | 0 15 files changed, 318 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/rebel.xml create mode 100644 yet-another-docker-plugin/.rebel-remote.xml.bak create mode 100644 yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/DockerManagementLink.java create mode 100644 yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/global.groovy create mode 100644 yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/index.groovy create mode 100644 yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox.jelly create mode 100644 yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox/expandableTextbox.js create mode 100644 yet-another-docker-plugin/src/main/resources/lib/yad/taglib create mode 100644 yet-another-docker-plugin/src/main/webapp/js/yad.js create mode 100644 yet-another-docker-plugin/yad2.log create mode 100644 yet-another-docker-plugin/yad2.log.lck diff --git a/src/main/resources/rebel.xml b/src/main/resources/rebel.xml new file mode 100644 index 00000000..d4f3b858 --- /dev/null +++ b/src/main/resources/rebel.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/yet-another-docker-plugin/.rebel-remote.xml.bak b/yet-another-docker-plugin/.rebel-remote.xml.bak new file mode 100644 index 00000000..32626dbf --- /dev/null +++ b/yet-another-docker-plugin/.rebel-remote.xml.bak @@ -0,0 +1,4 @@ + + + yet-another-docker-plugin + diff --git a/yet-another-docker-plugin/pom.xml b/yet-another-docker-plugin/pom.xml index 582db8a3..0870c4db 100644 --- a/yet-another-docker-plugin/pom.xml +++ b/yet-another-docker-plugin/pom.xml @@ -120,6 +120,13 @@ + + + + generate-taglib-interface + + + diff --git a/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/DockerManagementLink.java b/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/DockerManagementLink.java new file mode 100644 index 00000000..86816a6a --- /dev/null +++ b/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/DockerManagementLink.java @@ -0,0 +1,177 @@ +package com.github.kostyasha.yad; + +import com.cloudbees.plugins.credentials.GlobalCredentialsConfiguration; +import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.model.Link; +import com.google.common.base.Predicate; +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.BulkChange; +import hudson.Extension; +import hudson.Functions; +import hudson.model.Describable; +import hudson.model.Descriptor; +import hudson.model.ManagementLink; +import hudson.util.FormApply; +import hudson.util.FormValidation; +import jenkins.model.GlobalConfigurationCategory; +import jenkins.model.Jenkins; +import net.sf.json.JSONObject; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.HttpResponse; +import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.interceptor.RequirePOST; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import javax.servlet.ServletException; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import static com.github.kostyasha.yad.utils.BindUtils.joinToStr; +import static com.github.kostyasha.yad.utils.BindUtils.splitAndFilterEmpty; +import static java.util.Objects.isNull; + +/** + * @author Kanstantsin Shautsou + */ +@Extension +public class DockerManagementLink extends ManagementLink implements Describable { + + public static final Predicate FILTER = input -> input instanceof GlobalCredentialsConfiguration.Category; + + @Override + public String getIconFileName() { + return "clipboard.png"; + } + + @Override + public String getDisplayName() { + return "display name"; + } + + @Override + public String getUrlName() { + return "yad"; + } + + @CheckForNull + private List links = Collections.singletonList("sdfsdfsdf"); + + public DockerManagementLink() { + } + + // links + @Nonnull + public List getLinks() { + return isNull(links) ? Collections.EMPTY_LIST : links; + } + + public void setLinks(List links) { + this.links = links; + } + + public String getLinksString() { + return joinToStr(getLinks()); + } + + @DataBoundSetter + public void setLinksString(String devicesString) { + setLinks(splitAndFilterEmpty(devicesString)); + } + + + @RequirePOST + @NonNull + @Restricted(NoExternalUse.class) + @SuppressWarnings("unused") // stapler web method binding + public synchronized HttpResponse doConfigure(@NonNull StaplerRequest req) throws IOException, ServletException, + Descriptor.FormException { + Jenkins jenkins = Jenkins.getActiveInstance(); + jenkins.checkPermission(Jenkins.ADMINISTER); + // logically this change starts from Jenkins + BulkChange bc = new BulkChange(jenkins); + try { + boolean result = configure(req, req.getSubmittedForm()); +// LOGGER.log(Level.FINE, "credentials configuration saved: " + result); + jenkins.save(); + return FormApply + .success(result ? req.getContextPath() + "/manage" : req.getContextPath() + "/" + getUrlName()); + } finally { + bc.commit(); + } + } + + /** + * Performs the configuration. + * + * @param req the request. + * @param json the JSON object. + * @return {@code false} to keep the client in the same config page. + * @throws Descriptor.FormException if something goes wrong. + */ + private boolean configure(StaplerRequest req, JSONObject json) throws Descriptor.FormException { + // for compatibility reasons, the actual value is stored in Jenkins + Jenkins j = Jenkins.getActiveInstance(); + j.checkPermission(Jenkins.ADMINISTER); + + // persist all the provider configs + boolean result = true; + for (Descriptor d : Functions.getSortedDescriptorsForGlobalConfig(FILTER)) { + result &= configureDescriptor(req, json, d); + } + + return result; + } + + /** + * Performs the configuration of a specific {@link Descriptor}. + * + * @param req the request. + * @param json the JSON object. + * @param d the {@link Descriptor}. + * @return {@code false} to keep the client in the same config page. + * @throws Descriptor.FormException if something goes wrong. + */ + private boolean configureDescriptor(StaplerRequest req, JSONObject json, Descriptor d) throws + Descriptor.FormException { + // collapse the structure to remain backward compatible with the JSON structure before 1. + String name = d.getJsonSafeClassName(); + JSONObject js = json.has(name) ? json.getJSONObject(name) : new JSONObject(); + // if it doesn't have the property, the method returns invalid null object. + json.putAll(js); + return d.configure(req, js); + } + + @SuppressWarnings("unchecked") + @Override + public Descriptor getDescriptor() { + return Jenkins.getActiveInstance().getDescriptorOrDie(getClass()); + } + + /** + * Our {@link Descriptor}. + */ + @Extension + public static final class DescriptorImpl extends Descriptor { + @Override + public String getDisplayName() { + return "display bane"; + } + + public FormValidation doCheckLinksString(@QueryParameter String linksString) { + final List links = splitAndFilterEmpty(linksString); + for (String linkString : links) { + try { + Link.parse(linkString); + } catch (Exception ex) { + return FormValidation.error("Bad link configuration", ex); + } + } + + return FormValidation.ok(); + } + } +} diff --git a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/global.groovy b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/global.groovy new file mode 100644 index 00000000..e69de29b diff --git a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/index.groovy b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/index.groovy new file mode 100644 index 00000000..19387559 --- /dev/null +++ b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerManagementLink/index.groovy @@ -0,0 +1,22 @@ +package com.github.kostyasha.yad.DockerManagementLink + +import com.github.kostyasha.yad.DockerManagementLink +import lib.FormTagLib +import lib.LayoutTagLib +import lib.YadTagLib + +def l = namespace(LayoutTagLib) +def st = namespace("jelly:stapler") +def f = namespace(FormTagLib); +def yad = namespace(YadTagLib) + +l.layout(permission: app.ADMINISTER) { + l.header(title: my.displayName) + l.main_panel { + h1(my.displayName) + f.entry(title: _("Links")) { + it = new DockerManagementLink() + yad.expandableTextbox(field: "linksString") + } + } +} \ No newline at end of file diff --git a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerSlave/config.groovy b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerSlave/config.groovy index bf2b16a6..3491c163 100644 --- a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerSlave/config.groovy +++ b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/DockerSlave/config.groovy @@ -1,8 +1,11 @@ package com.github.kostyasha.yad.DockerSlave import lib.FormTagLib +import lib.YadTagLib def f = namespace(FormTagLib); +def yad = namespace(YadTagLib) + f.entry(title: _("Ctest"), field: "containerId") { f.readOnlyTextbox() diff --git a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy index 37051669..81323be8 100644 --- a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy +++ b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy @@ -2,14 +2,16 @@ package com.github.kostyasha.yad.commons.DockerCreateContainer import lib.FormTagLib import com.github.kostyasha.yad.commons.DockerCreateContainer +import lib.YadTagLib def f = namespace(FormTagLib); +def yad = namespace(YadTagLib) if (instance == null) { instance = new DockerCreateContainer() } -f.advanced(title: _("Container settings"), align: "left") { +f.advanced(title: _("Create Container settings"), align: "left") { f.entry(title: _("Docker Command"), field: "command") { f.textbox() } @@ -83,6 +85,11 @@ f.advanced(title: _("Container settings"), align: "left") { } f.entry(title: _("Links"), field: "linksString") { - f.expandableTextbox() +// f.textarea() + yad.expandableTextbox() +//// checkUrl: "${rootURL}/com.github.kostyasha.yad.commons.DockerCreateContainer/checkLinksString?linksString=" + +//// escape('Load rules are mandatory') + +//// ";value='+escape(this.value)" +// ) } } diff --git a/yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox.jelly b/yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox.jelly new file mode 100644 index 00000000..e4e48286 --- /dev/null +++ b/yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox.jelly @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + ${customizedFields.add(name)} + + + +
+ + +
+
+
+
diff --git a/yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox/expandableTextbox.js b/yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox/expandableTextbox.js new file mode 100644 index 00000000..d811a0f5 --- /dev/null +++ b/yet-another-docker-plugin/src/main/resources/lib/yad/expandableTextbox/expandableTextbox.js @@ -0,0 +1,14 @@ +function expandTextAreaYAD(button, id) { + button.style.display = "none"; + var field = button.parentNode.previousSibling.children[0]; + var value = field.value.replace(/ +/g, '\n'); + + var n = button; + while (n.tagName != "TABLE") { + n = n.parentNode; + } + + n.parentNode.innerHTML = + ""; + layoutUpdateCallback.call(); +} diff --git a/yet-another-docker-plugin/src/main/resources/lib/yad/taglib b/yet-another-docker-plugin/src/main/resources/lib/yad/taglib new file mode 100644 index 00000000..e69de29b diff --git a/yet-another-docker-plugin/src/main/resources/rebel.xml b/yet-another-docker-plugin/src/main/resources/rebel.xml index 74c3dec3..434f53d4 100644 --- a/yet-another-docker-plugin/src/main/resources/rebel.xml +++ b/yet-another-docker-plugin/src/main/resources/rebel.xml @@ -2,8 +2,8 @@ - - + + diff --git a/yet-another-docker-plugin/src/main/webapp/js/yad.js b/yet-another-docker-plugin/src/main/webapp/js/yad.js new file mode 100644 index 00000000..fd3703d5 --- /dev/null +++ b/yet-another-docker-plugin/src/main/webapp/js/yad.js @@ -0,0 +1,14 @@ +function expandTextArea2(button, id) { + button.style.display = "none"; + var field = button.parentNode.previousSibling.children[0]; + var value = field.value.replace(/ +/g, '\n'); + + var n = button; + while (n.tagName != "TABLE") { + n = n.parentNode; + } + + n.parentNode.innerHTML = + ""; + layoutUpdateCallback.call(); +} diff --git a/yet-another-docker-plugin/yad2.log b/yet-another-docker-plugin/yad2.log new file mode 100644 index 00000000..e69de29b diff --git a/yet-another-docker-plugin/yad2.log.lck b/yet-another-docker-plugin/yad2.log.lck new file mode 100644 index 00000000..e69de29b