Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds condition based on build description #129

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing POST/RequirePOST annotation Warning

Potential CSRF vulnerability: If BuildDescriptionDescriptor#doFillMatchingMethodItems connects to user-specified URLs, modifies state, or is expensive to run, it should be annotated with @POST or @RequirePOST

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing permission check Warning

Potential missing permission check in BuildDescriptionDescriptor#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";
}
}
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);
}
}
}
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>
Loading