-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-46064] Validator for changelog regular expression
- Loading branch information
Showing
5 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
...va/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/WhenConditionalValidator.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,82 @@ | |||
/* | |||
* The MIT License | |||
* | |||
* Copyright (c) 2017, 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. | |||
* | |||
*/ | |||
|
|||
package org.jenkinsci.plugins.pipeline.modeldefinition.when.impl; | |||
|
|||
import hudson.Extension; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.Messages; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTArgumentList; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTNamedArgumentList; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPositionalArgumentList; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTSingleArgument; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTValue; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhenCondition; | |||
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.DeclarativeValidatorContributor; | |||
import org.jenkinsci.plugins.workflow.flow.FlowExecution; | |||
|
|||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nonnull; | |||
import java.util.List; | |||
import java.util.regex.Pattern; | |||
import java.util.regex.PatternSyntaxException; | |||
|
|||
@Extension | |||
public class WhenConditionalValidator extends DeclarativeValidatorContributor { | |||
|
|||
@CheckForNull | |||
@Override | |||
public String validateElement(@Nonnull ModelASTWhenCondition condition, @CheckForNull FlowExecution execution) { | |||
if (condition.getName().equals("changelog")) { | |||
String pattern = getPatternArgument(condition.getArgs()); | |||
if (pattern == null) { | |||
return Messages.WhenConditionalValidator_changelog_missingParameter(); | |||
} else { | |||
try { | |||
Pattern.compile(pattern); | |||
Pattern.compile(ChangeLogConditional.expandForMultiLine(pattern), Pattern.MULTILINE | Pattern.DOTALL); | |||
} catch (PatternSyntaxException e) { | |||
return Messages.WhenConditionalValidator_changelog_badPattern(pattern, e.getMessage()); | |||
} | |||
} | |||
} | |||
|
|||
return null; | |||
} | |||
|
|||
private String getPatternArgument(ModelASTArgumentList args) { | |||
if (args instanceof ModelASTSingleArgument) { | |||
return (String) ((ModelASTSingleArgument) args).getValue().getValue(); | |||
} else if (args instanceof ModelASTPositionalArgumentList) { | |||
final List<ModelASTValue> arguments = ((ModelASTPositionalArgumentList) args).getArguments(); | |||
if (!arguments.isEmpty()) { | |||
return (String) arguments.get(0).getValue(); | |||
} | |||
} else if (args instanceof ModelASTNamedArgumentList) { | |||
return (String) ((ModelASTNamedArgumentList) args).argListToMap().get("pattern"); | |||
} | |||
|
|||
return null; | |||
} | |||
} |
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
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
49 changes: 49 additions & 0 deletions
49
pipeline-model-definition/src/test/resources/when/changelog/badRegularExpression.groovy
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
|
|||
/* | |||
* The MIT License | |||
* | |||
* Copyright (c) 2017, 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. | |||
* | |||
*/ | |||
|
|||
pipeline { | |||
agent { | |||
label "here" | |||
} | |||
stages { | |||
stage("One") { | |||
steps { | |||
echo "Hello, you should not see this." | |||
} | |||
} | |||
stage("Two") { | |||
when { | |||
changelog '{"user_id" : 24}' | |||
} | |||
steps { | |||
script { | |||
echo "Dull World" | |||
} | |||
|
|||
} | |||
} | |||
} | |||
} |