-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds condition based on build description (#129)
Request #128
- Loading branch information
1 parent
30db1db
commit 3c860bd
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...epanik/jenkins/buildhistorymanager/descriptors/conditions/BuildDescriptionDescriptor.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.conditions; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Descriptor; | ||
import hudson.util.ListBoxModel; | ||
import org.jenkinsci.Symbol; | ||
import pl.damianszczepanik.jenkins.buildhistorymanager.Messages; | ||
import pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions.BuildDescriptionCondition; | ||
import pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions.BuildDescriptionCondition.MatchingMethodType; | ||
import pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions.Condition; | ||
|
||
/** | ||
* Descriptor implementation needed to render UI for {@link BuildDescriptionCondition}. | ||
* | ||
* @author Damian Szczepanik (damianszczepanik@github) | ||
*/ | ||
@Extension | ||
@Symbol("BuildDescription") | ||
public class BuildDescriptionDescriptor extends Descriptor<Condition> { | ||
|
||
public BuildDescriptionDescriptor() { | ||
super(BuildDescriptionCondition.class); | ||
} | ||
|
||
// names must refer to the field name | ||
public ListBoxModel doFillMatchingMethodItems() { | ||
return new ListBoxModel( | ||
// default option should be listed first | ||
new ListBoxModel.Option(Messages.matchingMethodType_EQUALS(), MatchingMethodType.EQUALS.name()), | ||
new ListBoxModel.Option(Messages.matchingMethodType_CONTAINS(), MatchingMethodType.CONTAINS.name()), | ||
new ListBoxModel.Option(Messages.matchingMethodType_MATCHES(), MatchingMethodType.MATCHES.name()) | ||
); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Build description"; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildDescriptionCondition.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions; | ||
|
||
import hudson.Util; | ||
import hudson.model.Run; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import pl.damianszczepanik.jenkins.buildhistorymanager.model.RuleConfiguration; | ||
|
||
/** | ||
* Matches builds that description is equal, contains or matches given pattern. | ||
* | ||
* @author Damian Szczepanik (damianszczepanik@github) | ||
*/ | ||
public class BuildDescriptionCondition extends Condition { | ||
|
||
/** | ||
* Matches method for the pattern | ||
*/ | ||
public enum MatchingMethodType { | ||
EQUALS, | ||
CONTAINS, | ||
MATCHES; | ||
} | ||
|
||
private String pattern; | ||
|
||
private MatchingMethodType matchingMethod = MatchingMethodType.EQUALS; | ||
|
||
@DataBoundConstructor | ||
public BuildDescriptionCondition() { | ||
// Jenkins stapler requires to have public constructor with @DataBoundConstructor | ||
} | ||
|
||
public String getPattern() { | ||
return pattern; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setPattern(String pattern) { | ||
this.pattern = Util.fixNull(pattern); | ||
} | ||
|
||
public String getMatchingMethod() { | ||
return matchingMethod.name(); | ||
} | ||
|
||
@DataBoundSetter | ||
public void setMatchingMethod(String matchingMethod) { | ||
this.matchingMethod = MatchingMethodType.valueOf(matchingMethod); | ||
} | ||
|
||
@Override | ||
public boolean matches(Run<?, ?> run, RuleConfiguration configuration) { | ||
// by default build description is null | ||
String buildDescription = Util.fixNull(run.getDescription()); | ||
|
||
switch (matchingMethod) { | ||
case CONTAINS: | ||
return buildDescription.contains(pattern); | ||
case MATCHES: | ||
return buildDescription.matches(pattern); | ||
case EQUALS: | ||
default: // any unspecified value should be fallback to default one -> equals | ||
return buildDescription.equals(pattern); | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
...panik/jenkins/buildhistorymanager/model/conditions/BuildDescriptionCondition/config.jelly
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | ||
|
||
<f:entry | ||
title="Description matching method" | ||
field="matchingMethod"> | ||
<f:select/> | ||
</f:entry> | ||
<f:entry title="Pattern" field="pattern"> | ||
<f:textbox/> | ||
</f:entry> | ||
|
||
</j:jelly> |
12 changes: 12 additions & 0 deletions
12
...czepanik/jenkins/buildhistorymanager/model/conditions/BuildDescriptionCondition/help.html
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<p><b>Description</b></p> | ||
<p>Matches builds that description is equal, contains or matches given pattern.</p> | ||
|
||
<p><b>Use cases</b></p> | ||
<p>Helpful when build description contains text that can be used for decision what to do with the build.</p> | ||
|
||
<p><b>Warning!</b></p> | ||
<p>Leaving <i>pattern</i> field empty makes this condition valid for all builds that have undefined or empty build what | ||
is default value for new created builds.</p> | ||
<p>Methods <i>equals</i> and <i>contains</i> expect to have plain text pattern. To define proper regular expression | ||
pattern for matching type follow <a href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html">the | ||
guideline</a>.</p> |