package hudson.plugins.git.extensions.impl;

import hudson.Extension;
import hudson.model.TaskListener;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import hudson.plugins.git.util.BuildData;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.regex.Pattern;

/**
 * {@link GitSCMExtension} that ignores commits with specific messages.
 *
 * @author Kanstantsin Shautsou
 */
public class MessageExclusion extends GitSCMExtension {
	/**
	 * Java Pattern for matching messages to be ignored.
	 */
	private String excludedMessage;

	private transient volatile Pattern excludedPattern;

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

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

	public String getExcludedMessage() { return excludedMessage; }

	@Override
	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;
		}

		return null;
	}

	@Extension
	public static class DescriptorImpl extends GitSCMExtensionDescriptor {
		@Override
		public String getDisplayName() {
			return "Polling ignores commits with certain messages";
		}
	}
}

@@ -371,16 +371,51 @@
 ldData;%0A
+import hudson.util.FormValidation;%0A
 import o
@@ -504,16 +504,59 @@
 tructor;
+%0Aimport org.kohsuke.stapler.QueryParameter;
 %0A%0Aimport
@@ -608,16 +608,63 @@
 Pattern;
+%0Aimport java.util.regex.PatternSyntaxException;
 %0A%0A/**%0A *
@@ -1831,16 +1831,264 @@
 iptor %7B%0A
+%0A%09%09public FormValidation doCheckExcludedMessage(@QueryParameter String value) %7B%0A%09%09%09try %7B%0A%09%09%09%09Pattern.compile(value);%0A%09%09%09%7D catch (PatternSyntaxException ex)%7B%0A%09%09%09%09return FormValidation.error(ex.getMessage());%0A%09%09%09%7D%0A%09%09%09return FormValidation.ok();%0A%09%09%7D%0A%0A
 %09%09@Overr

Check user pattern for MessageExclusion extension