Skip to content

Commit

Permalink
Adds condition based on build description (#129)
Browse files Browse the repository at this point in the history
Request #128
  • Loading branch information
damianszczepanik authored Jun 26, 2024
1 parent 30db1db commit 3c860bd
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
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())

Check warning on line 31 in src/main/java/pl/damianszczepanik/jenkins/buildhistorymanager/descriptors/conditions/BuildDescriptionDescriptor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 27-31 are not covered by tests
);
}

@Override
public String getDisplayName() {
return "Build description";
}
}
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);

Check warning on line 64 in src/main/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildDescriptionCondition.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 19-64 are not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
notInteger=Provided value is not an integer
# === RULE
role.notValidMatchAtMost=Provide integer value or -1 if the rule execution should not be limited
# === FIELDS
matchingMethodType.EQUALS=Equals pattern
matchingMethodType.CONTAINS=Contains pattern
matchingMethodType.MATCHES=Matches pattern (regular expression)
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>
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>

0 comments on commit 3c860bd

Please sign in to comment.