Skip to content

Commit

Permalink
This improved version adds more logging, handles potential null value…
Browse files Browse the repository at this point in the history
…s, and uses the find() method for matching. This should make the method more robust.
  • Loading branch information
seahorse committed Oct 19, 2023
1 parent c3f5a18 commit ea44132
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,75 @@
* @author Kanstantsin Shautsou
*/
public class MessageExclusion extends GitSCMExtension {
/**
* Java Pattern for matching messages to be ignored.
*/
private String excludedMessage;
/**
* Java Pattern for matching messages to be ignored.
*/
private String excludedMessage;

private transient volatile Pattern excludedPattern;
private transient volatile Pattern excludedPattern;

@DataBoundConstructor
public MessageExclusion(String excludedMessage) { this.excludedMessage = excludedMessage; }
@DataBoundConstructor
public MessageExclusion(String excludedMessage) {
this.excludedMessage = excludedMessage;
}

@Override
public boolean requiresWorkspaceForPolling() { return true; }
@Override
public boolean requiresWorkspaceForPolling() {
return true;
}

public String getExcludedMessage() { return excludedMessage; }
public String getExcludedMessage() {
return excludedMessage;
}

@Override
@SuppressFBWarnings(value="NP_BOOLEAN_RETURN_NULL", justification="null used to indicate other extensions should decide")
@CheckForNull
public Boolean isRevExcluded(GitSCM scm, GitClient git, GitChangeSet commit, TaskListener listener, BuildData buildData) throws IOException, InterruptedException, GitException {
if (excludedPattern == null){
excludedPattern = Pattern.compile(excludedMessage);
}
String msg = commit.getComment();
if (excludedPattern.matcher(msg).matches()){
listener.getLogger().println("Ignored commit " + commit.getId() + ": Found excluded message: " + msg);
return true;
}
@Override
@SuppressFBWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "null used to indicate other extensions should decide")
@CheckForNull
public Boolean isRevExcluded(GitSCM scm, GitClient git, GitChangeSet commit, TaskListener listener, BuildData buildData) throws IOException, InterruptedException, GitException {
if (excludedMessage == null || excludedMessage.trim().isEmpty()) {

Check warning on line 53 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 53 is only partially covered, 2 branches are missing
listener.getLogger().println("Excluded message pattern is empty. No commits will be excluded based on message.");
return null; // No decision

Check warning on line 55 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 54-55 are not covered by tests
}

return null;
}
if (excludedPattern == null) {
try {
excludedPattern = Pattern.compile(excludedMessage);
} catch (PatternSyntaxException ex) {
listener.getLogger().println("Error compiling the excluded message pattern: " + ex.getMessage());
return null; // Pattern is invalid, so no decision

Check warning on line 63 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 61-63 are not covered by tests
}
}

@Extension
// No @Symbol annotation because message exclusion is done using a trait in Pipeline
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
String msg = commit.getComment();
if (msg == null) {

Check warning on line 68 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 68 is only partially covered, one branch is missing
listener.getLogger().println("Commit " + commit.getId() + " has a null message. It won't be excluded based on message.");
return null; // Commit message is null, so no decision

Check warning on line 70 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 69-70 are not covered by tests
}

public FormValidation doCheckExcludedMessage(@QueryParameter String value) {
try {
Pattern.compile(value);
} catch (PatternSyntaxException ex){
return FormValidation.error(ex.getMessage());
}
return FormValidation.ok();
}
if (excludedPattern.matcher(msg).find()) {

Check warning on line 73 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 73 is only partially covered, one branch is missing
listener.getLogger().println("Ignored commit " + commit.getId() + ": Found excluded message: " + msg);
return true; // Commit message contains the excluded pattern
}

@Override
public String getDisplayName() {
return "Polling ignores commits with certain messages";
}
}
return null; // No decision

Check warning on line 78 in src/main/java/hudson/plugins/git/extensions/impl/MessageExclusion.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 78 is not covered by tests
}

@Extension
// No @Symbol annotation because message exclusion is done using a trait in Pipeline
public static class DescriptorImpl extends GitSCMExtensionDescriptor {

public FormValidation doCheckExcludedMessage(@QueryParameter String value) {
try {
Pattern.compile(value);
} catch (PatternSyntaxException ex) {
return FormValidation.error(ex.getMessage());
}
return FormValidation.ok();
}

@Override
public String getDisplayName() {
return "Polling ignores commits with certain messages";
}
}
}

0 comments on commit ea44132

Please sign in to comment.