|
| 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 | + |
| 26 | +package org.jenkinsci.plugins.pipeline.modeldefinition.when.impl; |
| 27 | + |
| 28 | +import hudson.EnvVars; |
| 29 | +import hudson.Extension; |
| 30 | +import hudson.util.ListBoxModel; |
| 31 | +import jenkins.scm.api.SCMHead; |
| 32 | +import jenkins.scm.api.SCMHeadOrigin; |
| 33 | +import jenkins.scm.api.metadata.ContributorMetadataAction; |
| 34 | +import jenkins.scm.api.metadata.ObjectMetadataAction; |
| 35 | +import jenkins.scm.api.mixin.ChangeRequestSCMHead; |
| 36 | +import jenkins.scm.api.mixin.ChangeRequestSCMHead2; |
| 37 | +import org.codehaus.groovy.ast.expr.Expression; |
| 38 | +import org.jenkinsci.Symbol; |
| 39 | +import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhenContent; |
| 40 | +import org.jenkinsci.plugins.pipeline.modeldefinition.parser.ASTParserUtils; |
| 41 | +import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional; |
| 42 | +import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalDescriptor; |
| 43 | +import org.jenkinsci.plugins.pipeline.modeldefinition.when.utils.Comparator; |
| 44 | +import org.jenkinsci.plugins.pipeline.modeldefinition.when.utils.EnvironmentNames; |
| 45 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 46 | +import org.kohsuke.stapler.DataBoundSetter; |
| 47 | + |
| 48 | +import javax.annotation.CheckForNull; |
| 49 | + |
| 50 | +import static org.apache.commons.lang.StringUtils.isNotEmpty; |
| 51 | +import static org.jenkinsci.plugins.pipeline.modeldefinition.when.utils.EnvironmentNames.*; |
| 52 | + |
| 53 | +public class ChangeRequestConditional extends DeclarativeStageConditional<ChangeRequestConditional> { |
| 54 | + |
| 55 | + private String id; |
| 56 | + private String target; |
| 57 | + private String branch; |
| 58 | + private String fork; |
| 59 | + private String url; |
| 60 | + private String title; |
| 61 | + private String author; |
| 62 | + private String authorDisplayName; |
| 63 | + private String authorEmail; |
| 64 | + private String comparator; |
| 65 | + |
| 66 | + @DataBoundConstructor |
| 67 | + public ChangeRequestConditional() { |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * CHANGE_ID matching. |
| 72 | + * @return id |
| 73 | + * @see ChangeRequestSCMHead#getId() |
| 74 | + */ |
| 75 | + public String getId() { |
| 76 | + return id; |
| 77 | + } |
| 78 | + |
| 79 | + @DataBoundSetter |
| 80 | + public void setId(String id) { |
| 81 | + this.id = id; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * CHANGE_TARGET matching. |
| 86 | + * @return target |
| 87 | + * @see SCMHead#getName() |
| 88 | + */ |
| 89 | + public String getTarget() { |
| 90 | + return target; |
| 91 | + } |
| 92 | + |
| 93 | + @DataBoundSetter |
| 94 | + public void setTarget(String target) { |
| 95 | + this.target = target; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * CHANGE_BRANCH matching. |
| 100 | + * @return target |
| 101 | + * @see ChangeRequestSCMHead2#getOriginName() |
| 102 | + */ |
| 103 | + public String getBranch() { |
| 104 | + return branch; |
| 105 | + } |
| 106 | + |
| 107 | + @DataBoundSetter |
| 108 | + public void setBranch(String branch) { |
| 109 | + this.branch = branch; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * CHANGE_FORK matching. |
| 114 | + * @return fork |
| 115 | + * @see SCMHeadOrigin.Fork#getName() |
| 116 | + */ |
| 117 | + public String getFork() { |
| 118 | + return fork; |
| 119 | + } |
| 120 | + |
| 121 | + @DataBoundSetter |
| 122 | + public void setFork(String fork) { |
| 123 | + this.fork = fork; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * CHANGE_URL. |
| 128 | + * |
| 129 | + * Ant style matching doesn't work on url strings due to it being meant for regular paths |
| 130 | + * and all those extra characters conflicts. |
| 131 | + * |
| 132 | + * @return url |
| 133 | + * @see ObjectMetadataAction#getObjectUrl() |
| 134 | + */ |
| 135 | + public String getUrl() { |
| 136 | + return url; |
| 137 | + } |
| 138 | + |
| 139 | + @DataBoundSetter |
| 140 | + public void setUrl(String url) { |
| 141 | + this.url = url; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * CHANGE_TITLE matching. |
| 146 | + * @return title |
| 147 | + * @see ObjectMetadataAction#getObjectDisplayName() |
| 148 | + */ |
| 149 | + public String getTitle() { |
| 150 | + return title; |
| 151 | + } |
| 152 | + |
| 153 | + @DataBoundSetter |
| 154 | + public void setTitle(String title) { |
| 155 | + this.title = title; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * CHANGE_AUTHOR matching. |
| 160 | + * @return authorDisplayName |
| 161 | + * @see ContributorMetadataAction#getContributor() |
| 162 | + */ |
| 163 | + public String getAuthor() { |
| 164 | + return author; |
| 165 | + } |
| 166 | + |
| 167 | + @DataBoundSetter |
| 168 | + public void setAuthor(String author) { |
| 169 | + this.author = author; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * CHANGE_AUTHOR_DISPLAY_NAME matching. |
| 174 | + * @return authorDisplayName |
| 175 | + * @see ContributorMetadataAction#getContributorDisplayName() |
| 176 | + */ |
| 177 | + public String getAuthorDisplayName() { |
| 178 | + return authorDisplayName; |
| 179 | + } |
| 180 | + |
| 181 | + @DataBoundSetter |
| 182 | + public void setAuthorDisplayName(String authorDisplayName) { |
| 183 | + this.authorDisplayName = authorDisplayName; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * CHANGE_AUTHOR_EMAIL matching. |
| 188 | + * @return authorEmail |
| 189 | + * @see ContributorMetadataAction#getContributorEmail() |
| 190 | + */ |
| 191 | + public String getAuthorEmail() { |
| 192 | + return authorEmail; |
| 193 | + } |
| 194 | + |
| 195 | + @DataBoundSetter |
| 196 | + public void setAuthorEmail(String authorEmail) { |
| 197 | + this.authorEmail = authorEmail; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * The name of the {@link Comparator} to use. |
| 202 | + * Default is {@link Comparator#EQUALS} |
| 203 | + * |
| 204 | + * @return the name of the comparator, or null if default. |
| 205 | + */ |
| 206 | + public String getComparator() { |
| 207 | + return comparator; |
| 208 | + } |
| 209 | + |
| 210 | + @DataBoundSetter |
| 211 | + public void setComparator(String comparator) { |
| 212 | + Comparator c = Comparator.get(comparator, null); |
| 213 | + //TODO validation |
| 214 | + if (c != null) { |
| 215 | + this.comparator = c.name(); |
| 216 | + } else { |
| 217 | + this.comparator = null; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + public boolean matches(EnvVars vars) { |
| 222 | + if (!CHANGE_ID.exists(vars)) { |
| 223 | + return false; //Not a change |
| 224 | + } |
| 225 | + Comparator c = Comparator.get(this.comparator, Comparator.EQUALS); |
| 226 | + try { |
| 227 | + check(id, c, CHANGE_ID, vars); |
| 228 | + check(target, c, CHANGE_TARGET, vars); |
| 229 | + check(branch, c, CHANGE_BRANCH, vars); |
| 230 | + check(fork, c, CHANGE_FORK, vars); |
| 231 | + check(title, c, CHANGE_TITLE, vars); |
| 232 | + check(author, c, CHANGE_AUTHOR, vars); |
| 233 | + check(authorDisplayName, c, CHANGE_AUTHOR_DISPLAY_NAME, vars); |
| 234 | + check(authorEmail, c, CHANGE_AUTHOR_EMAIL, vars); |
| 235 | + return true; |
| 236 | + } catch (AssertionError e) { |
| 237 | + return false; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + private void check(String pattern, Comparator comparator, EnvironmentNames varName, EnvVars vars) { |
| 242 | + if (isNotEmpty(pattern)) { |
| 243 | + if (!comparator.compare(pattern, varName.get(vars))) { |
| 244 | + throw new AssertionError(varName.name() + "("+varName.get(vars)+")" + " does not match " + pattern); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + @Extension |
| 250 | + @Symbol("changeRequest") |
| 251 | + public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<ChangeRequestConditional> { |
| 252 | + @Override |
| 253 | + public Expression transformToRuntimeAST(@CheckForNull ModelASTWhenContent original) { |
| 254 | + return ASTParserUtils.transformWhenContentToRuntimeAST(original); |
| 255 | + } |
| 256 | + |
| 257 | + public ListBoxModel doFillComparatorItems() { |
| 258 | + return Comparator.getSelectOptions(true, Comparator.EQUALS); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments