|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018, CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.jenkinsci.plugins.workflow.cps; |
| 26 | + |
| 27 | +import hudson.Extension; |
| 28 | +import hudson.ExtensionPoint; |
| 29 | +import hudson.model.Item; |
| 30 | +import hudson.model.Job; |
| 31 | +import org.kohsuke.stapler.Stapler; |
| 32 | +import org.kohsuke.stapler.StaplerRequest; |
| 33 | + |
| 34 | +import javax.annotation.Nonnull; |
| 35 | + |
| 36 | +import java.net.URI; |
| 37 | +import java.net.URISyntaxException; |
| 38 | +import java.util.logging.Level; |
| 39 | +import java.util.logging.Logger; |
| 40 | + |
| 41 | +import static org.jenkinsci.plugins.workflow.cps.Snippetizer.ACTION_URL; |
| 42 | + |
| 43 | +/** |
| 44 | + * A link that will show up on the side panel of the snippet generator and other similar pages. |
| 45 | + * Display order is determined by extension ordinal - highest ordinal first. |
| 46 | + * |
| 47 | + * @author Andrew Bayer |
| 48 | + */ |
| 49 | +public abstract class SnippetizerLink implements ExtensionPoint { |
| 50 | + private static final Logger LOGGER = Logger.getLogger(SnippetizerLink.class.getName()); |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the URL this link should point to, which will be used by {@link #getDisplayUrl()}. If this is not absolute, |
| 54 | + * {@link #getDisplayUrl()} will link to this within the current context. |
| 55 | + */ |
| 56 | + @Nonnull |
| 57 | + public abstract String getUrl(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Get the actual URL to use in sidepanel.jelly. If {@link #getUrl()} is not absolute, this will try to get the |
| 61 | + * current Job context and return a url starting with that job's {@link Job#getUrl()} appended with {@link #getUrl()}. |
| 62 | + */ |
| 63 | + @Nonnull |
| 64 | + public final String getDisplayUrl() { |
| 65 | + String u = getUrl(); |
| 66 | + |
| 67 | + try { |
| 68 | + if (new URI(u).isAbsolute()) { |
| 69 | + return u; |
| 70 | + } |
| 71 | + } catch (URISyntaxException e) { |
| 72 | + LOGGER.log(Level.WARNING, "Failed to parse URL for " + u, e); |
| 73 | + return ""; |
| 74 | + } |
| 75 | + |
| 76 | + StaplerRequest req = Stapler.getCurrentRequest(); |
| 77 | + if (req == null) { |
| 78 | + return u; |
| 79 | + } |
| 80 | + |
| 81 | + Item i = req.findAncestorObject(Item.class); |
| 82 | + |
| 83 | + StringBuilder toAppend = new StringBuilder(); |
| 84 | + |
| 85 | + toAppend.append(req.getContextPath()); |
| 86 | + |
| 87 | + if (!req.getContextPath().endsWith("/")) { |
| 88 | + toAppend.append("/"); |
| 89 | + } |
| 90 | + |
| 91 | + if (i == null) { |
| 92 | + toAppend.append(u); |
| 93 | + } else { |
| 94 | + toAppend.append(i.getUrl()); |
| 95 | + |
| 96 | + if (!i.getUrl().endsWith("/")) { |
| 97 | + toAppend.append("/"); |
| 98 | + } |
| 99 | + |
| 100 | + toAppend.append(u); |
| 101 | + } |
| 102 | + |
| 103 | + return toAppend.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the icon information for the link. |
| 108 | + */ |
| 109 | + @Nonnull |
| 110 | + public String getIcon() { |
| 111 | + return "icon-help icon-md"; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get the display name for the link. |
| 116 | + */ |
| 117 | + @Nonnull |
| 118 | + public abstract String getDisplayName(); |
| 119 | + |
| 120 | + /** |
| 121 | + * Check whether the link should target a new window - this defaults to false; |
| 122 | + */ |
| 123 | + public boolean inNewWindow() { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + @Extension(ordinal = 1000L) |
| 128 | + public static class GeneratorLink extends SnippetizerLink { |
| 129 | + @Override |
| 130 | + @Nonnull |
| 131 | + public String getUrl() { |
| 132 | + return ACTION_URL; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + @Nonnull |
| 137 | + public String getIcon() { |
| 138 | + return "icon-gear2 icon-md"; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + @Nonnull |
| 143 | + public String getDisplayName() { |
| 144 | + return Messages.SnippetizerLink_GeneratorLink_displayName(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Extension(ordinal = 900L) |
| 149 | + public static class StepReferenceLink extends SnippetizerLink { |
| 150 | + @Override |
| 151 | + @Nonnull |
| 152 | + public String getUrl() { |
| 153 | + return ACTION_URL + "/html"; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + @Nonnull |
| 158 | + public String getDisplayName() { |
| 159 | + return Messages.SnippetizerLink_StepReferenceLink_displayName(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Extension(ordinal = 800L) |
| 164 | + public static class GlobalsReferenceLink extends SnippetizerLink { |
| 165 | + @Override |
| 166 | + @Nonnull |
| 167 | + public String getUrl() { |
| 168 | + return ACTION_URL + "/globals"; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + @Nonnull |
| 173 | + public String getDisplayName() { |
| 174 | + return Messages.SnippetizerLink_GlobalsReferenceLink_displayName(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Extension(ordinal = 700L) |
| 179 | + public static class OnlineDocsLink extends SnippetizerLink { |
| 180 | + @Override |
| 181 | + @Nonnull |
| 182 | + public String getUrl() { |
| 183 | + return "https://jenkins.io/doc/pipeline/"; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + @Nonnull |
| 188 | + public String getDisplayName() { |
| 189 | + return Messages.SnippetizerLink_OnlineDocsLink_displayName(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public boolean inNewWindow() { |
| 194 | + return true; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Extension(ordinal = 600L) |
| 199 | + public static class GDSLLink extends SnippetizerLink { |
| 200 | + @Override |
| 201 | + @Nonnull |
| 202 | + public String getUrl() { |
| 203 | + return ACTION_URL + "/gdsl"; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + @Nonnull |
| 208 | + public String getDisplayName() { |
| 209 | + return Messages.SnippetizerLink_GDSLLink_displayName(); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public boolean inNewWindow() { |
| 214 | + return true; |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments