diff --git a/docs/README.adoc b/docs/README.adoc
index 3881326ff..5b7566ea5 100644
--- a/docs/README.adoc
+++ b/docs/README.adoc
@@ -144,6 +144,8 @@ contents should follow this syntax:
----
p=some/project
b^**/master/*
+t~.*
+h~.*
f~.*\.txt
p=some/other/project
b^**
@@ -153,6 +155,8 @@ Explanation:
p for project +
b for branch +
+t for topic +
+h for hashtags +
f for file +
= for plain syntax +
^ for ANT style syntax +
diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessor.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessor.java
index 5460b7a90..0c0009764 100644
--- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessor.java
+++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessor.java
@@ -28,6 +28,7 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.CompareType;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.FilePath;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject;
+import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Hashtag;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Topic;
import org.kohsuke.accmod.Restricted;
@@ -63,6 +64,7 @@ public final class GerritDynamicUrlProcessor {
private static final String SHORTNAME_PROJECT = "p";
private static final String SHORTNAME_BRANCH = "b";
private static final String SHORTNAME_TOPIC = "t";
+ private static final String SHORTNAME_HASHTAG = "h";
private static final String SHORTNAME_FILE = "f";
private static final String SHORTNAME_FORBIDDEN_FILE = "o";
private static final int SOCKET_READ_TIMEOUT = 10000;
@@ -89,6 +91,7 @@ private static Pattern buildLinePattern() {
+ SHORTNAME_PROJECT
+ "|" + SHORTNAME_BRANCH
+ "|" + SHORTNAME_TOPIC
+ + "|" + SHORTNAME_HASHTAG
+ "|" + SHORTNAME_FILE
+ "|" + SHORTNAME_FORBIDDEN_FILE
+ ")";
@@ -141,6 +144,7 @@ private static List readAndParseTriggerConfig(BufferedReader read
List dynamicGerritProjects = new ArrayList();
List branches = null;
List topics = null;
+ List hashtags = null;
List filePaths = null;
List forbiddenFilePaths = null;
GerritProject dynamicGerritProject = null;
@@ -192,9 +196,11 @@ private static List readAndParseTriggerConfig(BufferedReader read
branches = new ArrayList();
topics = new ArrayList();
+ hashtags = new ArrayList<>();
filePaths = new ArrayList();
forbiddenFilePaths = new ArrayList();
- dynamicGerritProject = new GerritProject(type, text, branches, topics, filePaths, forbiddenFilePaths, false);
+ dynamicGerritProject = new GerritProject(type, text, branches, topics,
+ filePaths, forbiddenFilePaths, false);
} else if (SHORTNAME_BRANCH.equals(item)) { // Branch
if (branches == null) {
throw new ParseException("Line " + lineNr + ": attempt to use 'Branch' before 'Project'", lineNr);
@@ -223,6 +229,13 @@ private static List readAndParseTriggerConfig(BufferedReader read
FilePath filePath = new FilePath(type, text);
forbiddenFilePaths.add(filePath);
dynamicGerritProject.setForbiddenFilePaths(forbiddenFilePaths);
+ } else if (SHORTNAME_HASHTAG.equals(item)) {
+ if (hashtags == null) {
+ throw new ParseException("Line " + lineNr + ": attempt to use 'hashtag' before 'Project'", lineNr);
+ }
+ Hashtag hashtag = new Hashtag(type, text);
+ hashtags.add(hashtag);
+ dynamicGerritProject.setHashtags(hashtags);
}
}
diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java
index ef4947c3e..3b599168c 100644
--- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java
+++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java
@@ -953,12 +953,12 @@ private boolean isChangeInteresting(Change change, GerritProject project, Gerrit
|| (project.getForbiddenFilePaths() != null && project.getForbiddenFilePaths().size() > 0));
if (isFileTriggerEnabled() && containsFilePathsOrForbiddenFilePaths) {
- if (project.isInteresting(change.getProject(), change.getBranch(), change.getTopic(),
+ if (project.isInteresting(change,
() -> change.getFiles(gerritQueryHandler))) {
shouldTrigger = true;
}
} else {
- if (project.isInteresting(change.getProject(), change.getBranch(), change.getTopic())) {
+ if (project.isInteresting(change)) {
shouldTrigger = true;
}
}
@@ -1167,9 +1167,10 @@ public boolean isInteresting(GerritTriggeredEvent event) {
}
} else if (event instanceof RefUpdated) {
RefUpdated refUpdated = (RefUpdated)event;
- if (p.isInteresting(refUpdated.getRefUpdate().getProject(),
- refUpdated.getRefUpdate().getRefName(),
- null)) {
+ Change change = new Change();
+ change.setProject(refUpdated.getRefUpdate().getProject());
+ change.setBranch(refUpdated.getRefUpdate().getRefName());
+ if (p.isInteresting(change)) {
logger.trace("According to {} the event is interesting; event: {}", p, event);
return true;
}
diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParameters.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParameters.java
index dbd37cab1..427331352 100644
--- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParameters.java
+++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParameters.java
@@ -40,6 +40,7 @@
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeRestored;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.CommentAdded;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.RefUpdated;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.TopicChanged;
import hudson.model.Job;
@@ -58,6 +59,7 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
+import java.util.stream.Collectors;
/**
* The parameters to add to a build.
@@ -253,7 +255,19 @@ public enum GerritTriggerParameters {
/**
* Updated approvals.
*/
- GERRIT_EVENT_UPDATED_APPROVALS;
+ GERRIT_EVENT_UPDATED_APPROVALS,
+ /**
+ * Hashtags posted to Gerrit in a change based event.
+ */
+ GERRIT_HASHTAGS,
+ /**
+ * Hashtags removed to Gerrit in a hashtags-changed event.
+ */
+ GERRIT_REMOVED_HASHTAGS,
+ /**
+ * Hashtags added to Gerrit in a hashtags-changed event.
+ */
+ GERRIT_ADDED_HASHTAGS;
private static final Logger logger = LoggerFactory.getLogger(GerritTriggerParameters.class);
@@ -408,6 +422,65 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Job p
parameters, String.valueOf(((java.lang.Object)gerritEvent).hashCode()), escapeQuotes);
if (gerritEvent instanceof ChangeBasedEvent) {
ChangeBasedEvent event = (ChangeBasedEvent)gerritEvent;
+ setOrCreateParametersForChangeBasedEvent(event, parameters, escapeQuotes, nameAndEmailParameterMode,
+ changeSubjectMode, project, commitMessageMode, commentTextMode);
+ } else if (gerritEvent instanceof RefUpdated) {
+ RefUpdated event = (RefUpdated)gerritEvent;
+ GERRIT_REFNAME.setOrCreateStringParameterValue(
+ parameters, event.getRefUpdate().getRefName(), escapeQuotes);
+ GERRIT_PROJECT.setOrCreateStringParameterValue(
+ parameters, event.getRefUpdate().getProject(), escapeQuotes);
+ GERRIT_OLDREV.setOrCreateStringParameterValue(
+ parameters, event.getRefUpdate().getOldRev(), escapeQuotes);
+ GERRIT_NEWREV.setOrCreateStringParameterValue(
+ parameters, event.getRefUpdate().getNewRev(), escapeQuotes);
+ }
+ Account account = gerritEvent.getAccount();
+ if (account != null) {
+ nameAndEmailParameterMode.setOrCreateParameterValue(GERRIT_EVENT_ACCOUNT, parameters,
+ getNameAndEmail(account), ParameterMode.PlainMode.STRING, escapeQuotes);
+ GERRIT_EVENT_ACCOUNT_NAME.setOrCreateStringParameterValue(
+ parameters, getName(account), escapeQuotes);
+ GERRIT_EVENT_ACCOUNT_EMAIL.setOrCreateStringParameterValue(
+ parameters, getEmail(account), escapeQuotes);
+ }
+ Provider provider = gerritEvent.getProvider();
+ if (provider != null) {
+ GERRIT_NAME.setOrCreateStringParameterValue(
+ parameters, provider.getName(), escapeQuotes);
+ GERRIT_HOST.setOrCreateStringParameterValue(
+ parameters, provider.getHost(), escapeQuotes);
+ GERRIT_PORT.setOrCreateStringParameterValue(
+ parameters, provider.getPort(), escapeQuotes);
+ GERRIT_SCHEME.setOrCreateStringParameterValue(
+ parameters, provider.getScheme(), escapeQuotes);
+ GERRIT_VERSION.setOrCreateStringParameterValue(
+ parameters, provider.getVersion(), escapeQuotes);
+ }
+ }
+
+ /**
+ * To avoid setOrCreateParameters too long,
+ * move change based event related function to separate method.
+ * Set or Create parameters for change based event.
+ *
+ * @param event ChangeBasedEvent
+ * @param parameters jenkins job parameters
+ * @param escapeQuotes do escape quotes or not
+ * @param nameAndEmailParameterMode mode for name and email
+ * @param changeSubjectMode mode for change subject
+ * @param project jenkins job
+ * @param commitMessageMode mode for commit message
+ * @param commentTextMode mode for comment text
+ */
+ private static void setOrCreateParametersForChangeBasedEvent(ChangeBasedEvent event,
+ List parameters,
+ boolean escapeQuotes,
+ ParameterMode nameAndEmailParameterMode,
+ ParameterMode changeSubjectMode,
+ Job project,
+ ParameterMode commitMessageMode,
+ ParameterMode commentTextMode) {
GERRIT_CHANGE_WIP_STATE.setOrCreateStringParameterValue(
parameters, String.valueOf(event.getChange().isWip()), escapeQuotes);
GERRIT_CHANGE_PRIVATE_STATE.setOrCreateStringParameterValue(
@@ -420,6 +493,8 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Job p
parameters, event.getChange().getNumber(), escapeQuotes);
GERRIT_CHANGE_ID.setOrCreateStringParameterValue(
parameters, event.getChange().getId(), escapeQuotes);
+ GERRIT_HASHTAGS.setOrCreateStringParameterValue(
+ parameters, String.join(",", event.getChange().getHashtags()), escapeQuotes);
String pNumber = null;
if (null != event.getPatchSet()) {
pNumber = event.getPatchSet().getNumber();
@@ -498,40 +573,15 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Job p
parameters, comment, ParameterMode.PlainMode.TEXT, escapeQuotes);
}
GERRIT_EVENT_UPDATED_APPROVALS.setOrCreateStringParameterValue(parameters,
- getUpdatedApprovals((CommentAdded)event), false);
+ getUpdatedApprovals((CommentAdded)event), false);
}
- } else if (gerritEvent instanceof RefUpdated) {
- RefUpdated event = (RefUpdated)gerritEvent;
- GERRIT_REFNAME.setOrCreateStringParameterValue(
- parameters, event.getRefUpdate().getRefName(), escapeQuotes);
- GERRIT_PROJECT.setOrCreateStringParameterValue(
- parameters, event.getRefUpdate().getProject(), escapeQuotes);
- GERRIT_OLDREV.setOrCreateStringParameterValue(
- parameters, event.getRefUpdate().getOldRev(), escapeQuotes);
- GERRIT_NEWREV.setOrCreateStringParameterValue(
- parameters, event.getRefUpdate().getNewRev(), escapeQuotes);
- }
- Account account = gerritEvent.getAccount();
- if (account != null) {
- nameAndEmailParameterMode.setOrCreateParameterValue(GERRIT_EVENT_ACCOUNT, parameters,
- getNameAndEmail(account), ParameterMode.PlainMode.STRING, escapeQuotes);
- GERRIT_EVENT_ACCOUNT_NAME.setOrCreateStringParameterValue(
- parameters, getName(account), escapeQuotes);
- GERRIT_EVENT_ACCOUNT_EMAIL.setOrCreateStringParameterValue(
- parameters, getEmail(account), escapeQuotes);
- }
- Provider provider = gerritEvent.getProvider();
- if (provider != null) {
- GERRIT_NAME.setOrCreateStringParameterValue(
- parameters, provider.getName(), escapeQuotes);
- GERRIT_HOST.setOrCreateStringParameterValue(
- parameters, provider.getHost(), escapeQuotes);
- GERRIT_PORT.setOrCreateStringParameterValue(
- parameters, provider.getPort(), escapeQuotes);
- GERRIT_SCHEME.setOrCreateStringParameterValue(
- parameters, provider.getScheme(), escapeQuotes);
- GERRIT_VERSION.setOrCreateStringParameterValue(
- parameters, provider.getVersion(), escapeQuotes);
+ if (event instanceof HashtagsChanged) {
+ String addedHashtags = ((HashtagsChanged)event).getAddedHashtags().stream()
+ .collect(Collectors.joining(","));
+ String removedHashtags = ((HashtagsChanged)event).getRemovedHashtags().stream()
+ .collect(Collectors.joining(","));
+ GERRIT_ADDED_HASHTAGS.setOrCreateStringParameterValue(parameters, addedHashtags, escapeQuotes);
+ GERRIT_REMOVED_HASHTAGS.setOrCreateStringParameterValue(parameters, removedHashtags, escapeQuotes);
}
}
diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java
index acd64be9a..9f09a53bd 100644
--- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java
+++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java
@@ -42,10 +42,12 @@
import jenkins.model.Jenkins;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
+import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
+import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
/**
* Base settings for one matcher rule of a Gerrit project.
@@ -64,6 +66,7 @@ public class GerritProject implements Describable {
private List branches;
private List filePaths;
private List topics;
+ private List hashtags;
private List forbiddenFilePaths;
private boolean disableStrictForbiddenFileVerification;
@@ -199,6 +202,23 @@ public void setTopics(List topics) {
this.topics = topics;
}
+ /**
+ *
+ * @return the hashtags-rules
+ */
+ public List getHashtags() {
+ return hashtags;
+ }
+
+ /**
+ * The list of the hashtags-rules.
+ * @param hashtags the hashtags-rules
+ */
+ @DataBoundSetter
+ public void setHashtags(List hashtags) {
+ this.hashtags = hashtags;
+ }
+
/**
* The list of the forbidden file-path rules.
* @return the forbidden file-path rules.
@@ -223,6 +243,7 @@ public void setForbiddenFilePaths(List forbiddenFilePaths) {
* @param files a closure which returns the list of files in the change.
* @return true is the rules match.
*/
+ @Deprecated
public boolean isInteresting(String project, String branch, String topic, Supplier> files) {
if (isInteresting(project, branch, topic)) {
return isInterestingFile(files.get());
@@ -237,11 +258,38 @@ public boolean isInteresting(String project, String branch, String topic, Suppli
* @param topic the topic.
* @return true is the rules match.
*/
+ @Deprecated
public boolean isInteresting(String project, String branch, String topic) {
- if (compareType.matches(pattern, project)) {
+ Change change = new Change();
+ change.setProject(project);
+ change.setBranch(branch);
+ change.setTopic(topic);
+ return isInteresting(change);
+ }
+
+ /**
+ * Compares the project, branch and files to see if the rules specified is a match.
+ * @param change gerrit change info.
+ * @param files a closure which returns the list of files in the change.
+ * @return true is the rules match.
+ */
+ public boolean isInteresting(Change change, Supplier> files) {
+ if (isInteresting(change)) {
+ return isInterestingFile(files.get());
+ }
+ return false;
+ }
+
+ /**
+ * Compares the project and branch to see if the rules specified is a match.
+ * @param change gerrit change info.
+ * @return true is the rules match.
+ */
+ public boolean isInteresting(Change change) {
+ if (compareType.matches(pattern, change.getProject())) {
for (Branch b : branches) {
- if (b.isInteresting(branch)) {
- return isInterestingTopic(topic);
+ if (b.isInteresting(change.getBranch())) {
+ return isInterestingTopic(change.getTopic()) && isInterestingHashtags(change.getHashtags());
}
}
}
@@ -266,6 +314,19 @@ private boolean isInterestingTopic(String topic) {
return true;
}
+ /**
+ * Compare tags to see if the rules specified is a match.
+ *
+ * @param tags the tags in change.
+ * @return true if the rules match or no rules.
+ */
+ private boolean isInterestingHashtags(List tags) {
+ if (this.hashtags != null && this.hashtags.size() > 0) {
+ return this.hashtags.stream().anyMatch(h-> h.isInteresting(tags));
+ }
+ return true;
+ }
+
/**
* Compare files to see if the rules specified is a match.
*
diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Hashtag.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Hashtag.java
new file mode 100644
index 000000000..a947e5d24
--- /dev/null
+++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Hashtag.java
@@ -0,0 +1,113 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2013 rinrinne. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;
+
+import hudson.Extension;
+import hudson.model.AbstractDescribableImpl;
+import hudson.model.Descriptor;
+import org.kohsuke.stapler.DataBoundConstructor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represents a rule for triggering on a hashtag of a GerritProject.
+ */
+public class Hashtag extends AbstractDescribableImpl {
+
+ private CompareType compareType;
+ private String pattern;
+
+ /**
+ * Default empty constructor.
+ */
+ public Hashtag() {
+ }
+
+ /**
+ * Default DataBound constructor.
+ * @param compareType the CompareType to use when comparing with the pattern.
+ * @param pattern the pattern to match on.
+ */
+ @DataBoundConstructor
+ public Hashtag(CompareType compareType, String pattern) {
+ this.compareType = compareType;
+ this.pattern = pattern;
+ }
+
+ /**
+ * The CompareType used.
+ * @return the CompareType
+ */
+ public CompareType getCompareType() {
+ return compareType;
+ }
+
+ /**
+ * The CompareType used.
+ * @param compareType the compareType.
+ */
+ public void setCompareType(CompareType compareType) {
+ this.compareType = compareType;
+ }
+
+ /**
+ * The pattern to match on.
+ * @return the pattern
+ */
+ public String getPattern() {
+ return pattern;
+ }
+
+ /**
+ * The pattern to match on.
+ * @param pattern the pattern.
+ */
+ public void setPattern(String pattern) {
+ this.pattern = pattern;
+ }
+
+ /**
+ * Tells if the given topic are matched by this rule.
+ * @param hashtags the hashtags in change.
+ * @return true if the hashtags match.
+ */
+ public boolean isInteresting(List hashtags) {
+ if (hashtags == null) {
+ hashtags = new ArrayList<>();
+ }
+ return hashtags.stream().anyMatch(hashtag-> compareType.matches(pattern, hashtag));
+ }
+
+ /**
+ * The Descriptor for the hashtag.
+ */
+ @Extension
+ public static class DescriptorImpl extends Descriptor {
+ @Override
+ public String getDisplayName() {
+ return "";
+ }
+ }
+}
diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent.java
new file mode 100644
index 000000000..0e41568af
--- /dev/null
+++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent.java
@@ -0,0 +1,125 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2012 Intel, Inc. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events;
+
+import com.sonyericsson.hudson.plugins.gerrit.trigger.Messages;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
+
+import hudson.Extension;
+import org.jenkinsci.Symbol;
+import org.kohsuke.stapler.DataBoundConstructor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.regex.Pattern;
+
+/**
+ * An event configuration that causes the build to be triggered
+ * when a hashTag changed which matching a configurable RegEx.
+ */
+public class PluginHashtagsChangedEvent extends PluginGerritEvent implements Serializable {
+
+ /**
+ * The descriptor for PluginHashtagChangedEvent.
+ */
+ @Extension
+ @Symbol("hashtagChanged")
+ public static class PluginHashtagsChangedEventDescriptor extends PluginGerritEventDescriptor {
+ /**
+ * Display name in jenkins page for PluginHashtagChangedEvent.
+ *
+ * @return display name in jenkins page.
+ */
+ @Override
+ public String getDisplayName() {
+ return Messages.HashtagChangedDisplayName();
+ }
+ }
+
+ private static final long serialVersionUID = -7504226521891459396L;
+
+ private String hashtagChanged;
+
+ private static final Logger logger = LoggerFactory.getLogger(PluginHashtagsChangedEvent.class);
+
+ /**
+ * Empty Default constructor for Serializer.
+ */
+ public PluginHashtagsChangedEvent() {
+
+ }
+
+ /**
+ * Standard dataBoundConstructor for configuration.
+ *
+ * @param hashtagChanged String contains a regular expression to match changed hasgtag.
+ */
+ @DataBoundConstructor
+ public PluginHashtagsChangedEvent(String hashtagChanged) {
+ this.hashtagChanged = hashtagChanged;
+ }
+
+ /**
+ * Get a regular expression to show in jenkins page.
+ *
+ * @return String contains a regular expression to match changed hasgtag.
+ */
+ public String getHashtagChanged() {
+ return this.hashtagChanged;
+ }
+
+ /**
+ * Get class for watched gerrit event.
+ *
+ * @return Hashtag changed event class
+ */
+ @Override
+ public Class getCorrespondingEventClass() {
+ return HashtagsChanged.class;
+ }
+
+
+ @Override
+ public boolean shouldTriggerOn(GerritTriggeredEvent event) {
+ if (event instanceof HashtagsChanged) {
+ return match((HashtagsChanged)event);
+ }
+ return false;
+ }
+
+ /**
+ * Check if changed hashtag match pattern.
+ * @param event gerrit hashtag changed event
+ * @return match or not.
+ */
+ private boolean match(HashtagsChanged event) {
+ Pattern p = Pattern.compile(hashtagChanged);
+ logger.debug("Hashtags after change {}, added hashtags {}, removed hashtags {}",
+ event.getHashtags(), event.getAddedHashtags(), event.getRemovedHashtags());
+ return event.getAddedHashtags().stream().anyMatch(tag -> p.matcher(tag).find())
+ || event.getRemovedHashtags().stream().anyMatch(tag -> p.matcher(tag).find());
+ }
+}
diff --git a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages.properties b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages.properties
index fb260ff79..1e28b7049 100644
--- a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages.properties
+++ b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages.properties
@@ -100,6 +100,8 @@ RefUpdatedDisplayName=\
Ref Updated
ChangeAbandonedDisplayName=\
Change Abandoned
+HashtagChangedDisplayName=\
+ Hashtag Changed
TopicChangedDisplayName=\
Topic changed
ChangeMergedDisplayName=\
diff --git a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages_ja.properties b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages_ja.properties
index 27db24a24..821170307 100644
--- a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages_ja.properties
+++ b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/Messages_ja.properties
@@ -94,6 +94,8 @@ RefUpdatedDisplayName=\
Ref Updated
ChangeAbandonedDisplayName=\
Change Abandoned
+HashtagChangedDisplayName=\
+ Hashtag Changed
ChangeMergedDisplayName=\
Change Merged
ChangeRestoredDisplayName=\
diff --git a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger/config.jelly b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger/config.jelly
index 50e1e4fb7..0de103729 100644
--- a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger/config.jelly
+++ b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger/config.jelly
@@ -285,6 +285,18 @@
+
+
+
+
+
+
diff --git a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent/config.jelly b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent/config.jelly
new file mode 100644
index 000000000..39ea61e7f
--- /dev/null
+++ b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent/config.jelly
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
diff --git a/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent/config_ja.properties b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent/config_ja.properties
new file mode 100644
index 000000000..2d8936e92
--- /dev/null
+++ b/src/main/resources/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/events/PluginHashtagsChangedEvent/config_ja.properties
@@ -0,0 +1,2 @@
+Value=\
+ \u5024
diff --git a/src/main/webapp/trigger/help-DynamicTriggerConfiguration.html b/src/main/webapp/trigger/help-DynamicTriggerConfiguration.html
index 36fbd8203..62539374e 100644
--- a/src/main/webapp/trigger/help-DynamicTriggerConfiguration.html
+++ b/src/main/webapp/trigger/help-DynamicTriggerConfiguration.html
@@ -15,6 +15,7 @@
p=some/project
b^**/master/*
t~.*
+h~.*
f~\.txt$
p=some/other/project
b^**
@@ -23,10 +24,11 @@
p for project
b for branch
t for topic
+ h for hashtags
f for file
o for forbidden file
= for plain syntax
^ for ANT style syntax
~ for regexp syntax
- Branch, topic, file and forbidden file lines are assumed to be part of the closest preceding project line.
+ Branch, topic, hashtags, file and forbidden file lines are assumed to be part of the closest preceding project line.
diff --git a/src/main/webapp/trigger/help-DynamicTriggerConfiguration_ja.html b/src/main/webapp/trigger/help-DynamicTriggerConfiguration_ja.html
index f0affd4fe..f6ed5c8e1 100644
--- a/src/main/webapp/trigger/help-DynamicTriggerConfiguration_ja.html
+++ b/src/main/webapp/trigger/help-DynamicTriggerConfiguration_ja.html
@@ -12,6 +12,7 @@
createMockGerritProjectList() {
GerritProject project = mock(GerritProject.class);
when(project.getCompareType()).thenReturn(CompareType.PLAIN);
- when(project.isInteresting(anyString(), anyString(), anyString())).thenReturn(true);
+ when(project.isInteresting(any(Change.class))).thenReturn(true);
projectList.add(project);
return projectList;
}
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessorTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessorTest.java
new file mode 100644
index 000000000..0280a9b09
--- /dev/null
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritDynamicUrlProcessorTest.java
@@ -0,0 +1,185 @@
+package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger;
+
+import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.CompareType;
+import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.text.ParseException;
+import java.util.List;
+
+import static java.lang.System.lineSeparator;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+/**
+ * Test for {@link GerritDynamicUrlProcessor}.
+ */
+public class GerritDynamicUrlProcessorTest {
+
+ private static final String PROJECT = "project";
+ private static final String BRANCH = "branch";
+ private static final String TOPIC = "topic";
+ private static final String HASHTAG = "hashtag";
+ private static final String FILE = "file";
+ private static final String FORBIDDEN_FILE = "forbidden_file";
+
+ /**
+ * Test {@link GerritDynamicUrlProcessor#fetch(String)}}
+ *
+ * Should throw {@link MalformedURLException} when url is null or empty.
+ */
+ @Test
+ public void testGerritTriggerConfigUrlIsNull() {
+ assertThrows(MalformedURLException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(null);
+ });
+ assertThrows(MalformedURLException.class, () -> {
+ GerritDynamicUrlProcessor.fetch("");
+ });
+ }
+
+ /**
+ * Test {@link GerritDynamicUrlProcessor#fetch(String)} with full options.
+ *
+ * @throws IOException if so.
+ * @throws ParseException if so.
+ */
+ @Test
+ public void testGerritTriggerConfigUrlFullOptions() throws IOException, ParseException {
+ StringBuilder configContent = new StringBuilder();
+ configContent.append(newLine("# comment"))
+ .append(newLine("p~" + PROJECT))
+ .append(newLine("; comment"))
+ .append(newLine("b~" + BRANCH))
+ .append(newLine(" "))
+ .append(newLine("t~" + TOPIC))
+ .append(newLine("h~" + HASHTAG))
+ .append(newLine("f~" + FILE))
+ .append(newLine("o~" + FORBIDDEN_FILE))
+ .append(newLine("p=p1"))
+ .append(newLine("b=b1"));
+
+ List projects = GerritDynamicUrlProcessor.fetch(
+ generateDynamicTriggerConfigFile("testGerritTriggerConfigUrlFullOptions", configContent.toString())
+ );
+
+ assertEquals(2, projects.size());
+ assertEquals(CompareType.REG_EXP, projects.get(0).getCompareType());
+ assertEquals(PROJECT, projects.get(0).getPattern());
+ assertEquals(CompareType.REG_EXP, projects.get(0).getBranches().get(0).getCompareType());
+ assertEquals(BRANCH, projects.get(0).getBranches().get(0).getPattern());
+ assertEquals(CompareType.REG_EXP, projects.get(0).getTopics().get(0).getCompareType());
+ assertEquals(TOPIC, projects.get(0).getTopics().get(0).getPattern());
+ assertEquals(CompareType.REG_EXP, projects.get(0).getHashtags().get(0).getCompareType());
+ assertEquals(HASHTAG, projects.get(0).getHashtags().get(0).getPattern());
+ assertEquals(CompareType.REG_EXP, projects.get(0).getFilePaths().get(0).getCompareType());
+ assertEquals(FILE, projects.get(0).getFilePaths().get(0).getPattern());
+ assertEquals(CompareType.REG_EXP, projects.get(0).getForbiddenFilePaths().get(0).getCompareType());
+ assertEquals(FORBIDDEN_FILE, projects.get(0).getForbiddenFilePaths().get(0).getPattern());
+ assertEquals(CompareType.PLAIN, projects.get(1).getCompareType());
+ assertEquals("p1", projects.get(1).getPattern());
+ assertEquals(CompareType.PLAIN, projects.get(1).getBranches().get(0).getCompareType());
+ assertEquals("b1", projects.get(1).getBranches().get(0).getPattern());
+ }
+
+ /**
+ * Test {@link GerritDynamicUrlProcessor#fetch(String)} with wrong content.
+ *
+ * @throws IOException if so.
+ * @throws ParseException if so.
+ */
+ @Test
+ public void testGerritTriggerConfigUrlWithWrongContent() throws IOException, ParseException {
+ String filename = "testGerritTriggerConfigUrlWithWrongContent";
+ //Error pattern
+ StringBuilder configContent = new StringBuilder();
+ configContent.append(newLine("p~" + PROJECT))
+ .append(newLine("a~advance"));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent.toString()));
+ });
+
+ //Error format
+ StringBuilder configContent1 = new StringBuilder();
+ configContent1.append(newLine("p " + PROJECT));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent1.toString()));
+ });
+
+ // branch before project
+ StringBuilder configContent2 = new StringBuilder();
+ configContent2.append(newLine("b~" + BRANCH))
+ .append(newLine("p~" + PROJECT));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent2.toString()));
+ });
+
+ // topic before project
+ StringBuilder configContent3 = new StringBuilder();
+ configContent3.append(newLine("t~" + TOPIC))
+ .append(newLine("p~" + PROJECT));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent3.toString()));
+ });
+
+
+ // hashtag before project
+ StringBuilder configContent4 = new StringBuilder();
+ configContent4.append(newLine("h~" + HASHTAG))
+ .append(newLine("p~" + PROJECT));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent4.toString()));
+ });
+
+
+ // file before project
+ StringBuilder configContent5 = new StringBuilder();
+ configContent5.append(newLine("f~" + FILE))
+ .append(newLine("p~" + PROJECT));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent5.toString()));
+ });
+
+
+ // forbidden file before project
+ StringBuilder configContent6 = new StringBuilder();
+ configContent6.append(newLine("o~" + FORBIDDEN_FILE))
+ .append(newLine("p~" + PROJECT));
+ assertThrows(ParseException.class, () -> {
+ GerritDynamicUrlProcessor.fetch(generateDynamicTriggerConfigFile(filename, configContent6.toString()));
+ });
+
+ }
+
+ /**
+ * Generate a file with dynamic trigger config content.
+ *
+ * @param filename dynamic trigger config file.
+ * @param fileContent dynamic trigger config content.
+ * @return file path
+ * @throws IOException if so.
+ */
+ private String generateDynamicTriggerConfigFile(String filename, String fileContent) throws IOException {
+ File file = File.createTempFile(filename, "txt");
+ FileWriter fw = new FileWriter(file);
+ fw.write(fileContent);
+ fw.close();
+ URI uri = file.toURI();
+ return uri.toURL().toString();
+ }
+
+ /**
+ * return string start with a new line
+ * @param str input string
+ * @return string start with a new line
+ */
+ private static String newLine(String str) {
+ return lineSeparator() + str;
+ }
+}
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParametersTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParametersTest.java
index 11e71fe5f..1ab11c08b 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParametersTest.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerParametersTest.java
@@ -29,6 +29,7 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.CommentAdded;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;
import hudson.model.AbstractProject;
import hudson.model.ParameterValue;
@@ -44,6 +45,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
+import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -178,6 +180,37 @@ public void testGetUpdatedApprovals() {
}
+ // CS IGNORE LineLength FOR NEXT 3 LINES. REASON: JavaDoc.
+ /**
+ * Tests {@link GerritTriggerParameters#setOrCreateParameters(com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent, hudson.model.Job, List)}.
+ * The {@link GerritTriggerParameters#GERRIT_CHANGE_URL} should contain the base url from the first server.
+ *
+ * @throws Exception if so
+ */
+ @Test
+ public void testHashtagsInParameter() throws Exception {
+ HashtagsChanged hashtagsChanged = Setup.createHashtagsChanged();
+ AbstractProject project = j.createFreeStyleProject();
+ GerritTrigger trigger = Setup.createDefaultTrigger(project);
+ trigger.setServerName(GerritServer.ANY_SERVER);
+ LinkedList parameters = new LinkedList();
+ GerritTriggerParameters.setOrCreateParameters(hashtagsChanged, project, parameters);
+ StringParameterValue hashtags = findParameter(GerritTriggerParameters.GERRIT_HASHTAGS, parameters);
+ StringParameterValue addedHashtags = findParameter(GerritTriggerParameters.GERRIT_ADDED_HASHTAGS, parameters);
+ StringParameterValue removedHashtags = findParameter(GerritTriggerParameters.GERRIT_REMOVED_HASHTAGS,
+ parameters);
+
+ assertNotNull(hashtags);
+ assertNotNull(addedHashtags);
+ assertNotNull(removedHashtags);
+ assertEquals(hashtagsChanged.getHashtags().stream()
+ .collect(Collectors.joining(",")), hashtags.getValue());
+ assertEquals(hashtagsChanged.getAddedHashtags().stream()
+ .collect(Collectors.joining(",")), addedHashtags.getValue());
+ assertEquals(hashtagsChanged.getRemovedHashtags().stream()
+ .collect(Collectors.joining(",")), removedHashtags.getValue());
+ }
+
/**
* Finds the given parameter in the list.
*
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java
index 09245bb38..fd990f7e5 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java
@@ -118,10 +118,8 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertFalse;
-import static org.mockito.AdditionalMatchers.or;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doReturn;
@@ -793,7 +791,7 @@ public void testGerritEvent() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), or(isNull(), any(String.class)));
+ doReturn(true).when(gP).isInteresting(any(Change.class));
when(gP.getFilePaths()).thenReturn(null);
@@ -875,7 +873,7 @@ public void testGerritEventNotInteresting() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- doReturn(false).when(gP).isInteresting(any(String.class), any(String.class), any(String.class));
+ doReturn(false).when(gP).isInteresting(any(Change.class));
when(gP.getFilePaths()).thenReturn(null);
GerritTrigger trigger = Setup.createDefaultTrigger(project);
@@ -920,7 +918,7 @@ public void testGerritEventManualEvent() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), or(isNull(), any(String.class)));
+ doReturn(true).when(gP).isInteresting(any(Change.class));
when(gP.getFilePaths()).thenReturn(null);
GerritTrigger trigger = Setup.createDefaultTrigger(project);
@@ -1071,8 +1069,7 @@ public void testGerritEventSilentMode() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- when(gP.isInteresting(any(String.class), any(String.class), or(isNull(), any(String.class))))
- .thenReturn(true);
+ when(gP.isInteresting(any(Change.class))).thenReturn(true);
//doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class));
when(gP.getFilePaths()).thenReturn(null);
@@ -1105,7 +1102,7 @@ public void testGerritEventManualEventSilentMode() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), or(isNull(), any(String.class)));
+ doReturn(true).when(gP).isInteresting(any(Change.class));
when(gP.getFilePaths()).thenReturn(null);
GerritTrigger trigger = Setup.createDefaultTrigger(project);
@@ -1814,7 +1811,7 @@ public void testGerritEventPrivateStateChanged() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), or(isNull(), any(String.class)));
+ doReturn(true).when(gP).isInteresting(any(Change.class));
when(gP.getFilePaths()).thenReturn(null);
@@ -1851,7 +1848,7 @@ public void testGerritEventWipStateChanged() {
runListenerMockedStatic.when(ToGerritRunListener::getInstance).thenReturn(listener);
GerritProject gP = mock(GerritProject.class);
- doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), or(isNull(), any(String.class)));
+ doReturn(true).when(gP).isInteresting(any(Change.class));
when(gP.getFilePaths()).thenReturn(null);
GerritTrigger trigger = Setup.createDefaultTrigger(project);
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesTest.java
index 29c585132..b4941962b 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesTest.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesTest.java
@@ -23,6 +23,8 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;
+import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
+
import org.junit.Test;
import java.util.LinkedList;
@@ -58,11 +60,14 @@ public void testFileRemoved() {
files.add("hide.txt");
GerritProject config = new GerritProject(
CompareType.PLAIN, "project1", branches, topics, filePaths, forbiddenFilePaths, true);
- config.isInteresting("project1", "master", null, () -> files);
+ Change change = new Change();
+ change.setProject("project1");
+ change.setBranch("master");
+ config.isInteresting(change, () -> files);
filePaths.add(new FilePath(CompareType.PLAIN, "test.txt"));
config = new GerritProject(
CompareType.PLAIN, "project2", branches, topics, filePaths, null, false);
-
- assertTrue(config.isInteresting("project2", "master", null, () -> files));
+ change.setProject("project2");
+ assertTrue(config.isInteresting(change, () -> files));
}
}
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesWithMagicalFileNamesTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesWithMagicalFileNamesTest.java
index 1b579cac3..791b5d44b 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesWithMagicalFileNamesTest.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectForbiddenFilesWithMagicalFileNamesTest.java
@@ -23,6 +23,8 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;
+import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
+
import org.junit.Test;
import java.util.LinkedList;
@@ -60,7 +62,9 @@ public void testMagicalFileNamesDoNotMakeChangeInteresting() {
files.add("README.md");
GerritProject project = new GerritProject(
CompareType.PLAIN, "project1", branches, topics, filePaths, forbiddenFilePaths, true);
-
- assertFalse(project.isInteresting("project1", "master", null, () -> files));
+ Change change = new Change();
+ change.setProject("project1");
+ change.setBranch("master");
+ assertFalse(project.isInteresting(change, () -> files));
}
}
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java
index 1d797825b..a4d08fb32 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java
@@ -23,6 +23,8 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;
+import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
+
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -52,14 +54,12 @@ public GerritProjectInterestingTest(InterestingScenario scenario) {
}
/**
- * Tests {@link GerritProject#isInteresting(String, String, String)}.
+ * Tests {@link GerritProject#isInteresting(Change change)}.
*/
@Test
public void testInteresting() {
assertEquals(scenario.expected, scenario.config.isInteresting(
- scenario.project,
- scenario.branch,
- scenario.topic));
+ scenario.change));
}
/**
@@ -73,49 +73,59 @@ public static Collection getParameters() {
List branches = new LinkedList();
List topics = new LinkedList();
+ List hashtags = new LinkedList<>();
Branch branch = new Branch(CompareType.PLAIN, "master");
branches.add(branch);
- GerritProject config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
- parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null, true)});
+ GerritProject config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null,
+ null, true), });
branches = new LinkedList();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
- "project", "origin/master", null, true), });
+ "project", "origin/master", null, null, true), });
branches = new LinkedList();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
- parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null, true)});
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null,
+ null, true), });
branches = new LinkedList();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
branch = new Branch(CompareType.REG_EXP, "feature/.*master");
branches.add(branch);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
- parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null, true)});
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null,
+ null, true), });
branches = new LinkedList();
branch = new Branch(CompareType.PLAIN, "olstorp");
branches.add(branch);
branch = new Branch(CompareType.REG_EXP, "feature/.*master");
branches.add(branch);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
- "project", "feature/mymaster", null, true), });
+ "project", "feature/mymaster", null, null, true), });
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
- "project", "Olstorp", null, true), });
+ "project", "Olstorp", null, null, true), });
branches = new LinkedList();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
- config = new GerritProject(CompareType.ANT, "vendor/**/project", branches, topics, null, null, false);
+ config = new GerritProject(CompareType.ANT, "vendor/**/project", branches, topics,
+ null, null, false);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
- "vendor/semc/master/project", "origin/master", null, true), });
+ "vendor/semc/master/project", "origin/master", null, null, true), });
branches = new LinkedList();
branch = new Branch(CompareType.PLAIN, "master");
@@ -123,22 +133,80 @@ public static Collection getParameters() {
topics = new LinkedList();
Topic topic = new Topic(CompareType.PLAIN, "topic");
topics.add(topic);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
- parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", "topic", true)});
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", "topic",
+ null, true), });
topics = new LinkedList();
topic = new Topic(CompareType.ANT, "**/topic");
topics.add(topic);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
- "project", "master", "team/topic", true), });
+ "project", "master", "team/topic", null, true), });
topics = new LinkedList();
topic = new Topic(CompareType.REG_EXP, ".*_topic");
topics.add(topic);
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null, false);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, null, false);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config,
+ "project", "master", "team-wolf_topic", null, true), });
+
+ hashtags = new LinkedList<>();
+ Hashtag hashtag = new Hashtag(CompareType.PLAIN, "hashtag");
+ hashtags.add(hashtag);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, null,
+ null, null, false);
+ config.setHashtags(hashtags);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config,
+ "project", "master", null, List.of("hashtag"), true), });
+
+ hashtags = new LinkedList<>();
+ hashtag = new Hashtag(CompareType.ANT, "**/hashtag");
+ hashtags.add(hashtag);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, null,
+ null, null, false);
+ config.setHashtags(hashtags);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config,
+ "project", "master", null, List.of("test/hashtag"), true), });
+
+ hashtags = new LinkedList<>();
+ hashtag = new Hashtag(CompareType.REG_EXP, ".*_hashtag");
+ hashtags.add(hashtag);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, null,
+ null, null, false);
+ config.setHashtags(hashtags);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config,
+ "project", "master", null, List.of("test_hashtag"), true), });
+
+ hashtags = new LinkedList<>();
+ hashtag = new Hashtag(CompareType.REG_EXP, "hash.*tag");
+ hashtags.add(hashtag);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, null,
+ null, null, false);
+ config.setHashtags(hashtags);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config,
+ "project", "master", null, List.of("test_hashtag"), false), });
+
+ hashtags = new LinkedList<>();
+ hashtag = new Hashtag(CompareType.ANT, "hash.*tag");
+ hashtags.add(hashtag);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, null,
+ null, null, false);
+ config.setHashtags(hashtags);
+ parameters.add(new InterestingScenario[]{new InterestingScenario(config,
+ "project", "master", null, List.of("test_hashtag"), false), });
+
+ hashtags = new LinkedList<>();
+ hashtag = new Hashtag(CompareType.PLAIN, "hash.*tag");
+ hashtags.add(hashtag);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, null,
+ null, null, false);
+ config.setHashtags(hashtags);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
- "project", "master", "team-wolf_topic", true), });
+ "project", "master", null, List.of("test_hashtag"), false), });
return parameters;
}
@@ -149,9 +217,7 @@ public static Collection getParameters() {
public static class InterestingScenario {
GerritProject config;
- String project;
- String branch;
- String topic;
+ Change change;
boolean expected;
/**
@@ -160,17 +226,21 @@ public static class InterestingScenario {
* @param project project
* @param branch branch
* @param topic topic
+ * @param hashtags hashtags
* @param expected expected
*/
public InterestingScenario(GerritProject config,
String project,
String branch,
String topic,
+ List hashtags,
boolean expected) {
this.config = config;
- this.project = project;
- this.branch = branch;
- this.topic = topic;
+ this.change = new Change();
+ this.change.setProject(project);
+ this.change.setBranch(branch);
+ this.change.setTopic(topic);
+ this.change.setHashtags(hashtags);
this.expected = expected;
}
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java
index 21bf7e022..ef33a3095 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java
@@ -23,6 +23,8 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;
+import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
+
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -53,12 +55,12 @@ public GerritProjectWithFilesInterestingTest(InterestingScenarioWithFiles scenar
}
/**
- * Tests {@link GerritProject#isInteresting(String, String, String, java.util.function.Supplier)}.
+ * Tests {@link GerritProject#isInteresting(Change, java.util.function.Supplier)}.
*/
@Test
public void testInteresting() {
assertEquals(scenarioWithFiles.expected, scenarioWithFiles.config.isInteresting(
- scenarioWithFiles.project, scenarioWithFiles.branch, scenarioWithFiles.topic,
+ scenarioWithFiles.change,
scenarioWithFiles::getFiles));
assertEquals(scenarioWithFiles.fileCheckOccurred, scenarioWithFiles.fileCheckNeeded);
}
@@ -131,7 +133,8 @@ private static void addParametersForFileMatchTests(LinkedList forbiddenFilePaths = new LinkedList();
forbiddenFilePaths.add(new FilePath(CompareType.PLAIN, "test2.txt"));
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, filePaths, forbiddenFilePaths, false);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ filePaths, forbiddenFilePaths, false);
files = new LinkedList();
files.add("test.txt");
files.add("test2.txt");
@@ -147,7 +150,8 @@ private static void addParametersForFileMatchTests(LinkedList();
forbiddenFilePaths.add(new FilePath(CompareType.PLAIN, "test2.txt"));
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, filePaths, forbiddenFilePaths, true);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ filePaths, forbiddenFilePaths, true);
files = new LinkedList();
files.add("test.txt");
files.add("test2.txt");
@@ -160,7 +164,8 @@ private static void addParametersForFileMatchTests(LinkedList();
forbiddenFilePaths = new LinkedList();
forbiddenFilePaths.add(new FilePath(CompareType.PLAIN, "test2.txt"));
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, forbiddenFilePaths, false);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, forbiddenFilePaths, false);
files = new LinkedList();
files.add("test.txt");
files.add("test2.txt");
@@ -174,7 +179,8 @@ private static void addParametersForFileMatchTests(LinkedList();
forbiddenFilePaths = new LinkedList();
forbiddenFilePaths.add(new FilePath(CompareType.PLAIN, "test2.txt"));
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, forbiddenFilePaths, true);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, forbiddenFilePaths, true);
files = new LinkedList();
files.add("test.txt");
files.add("test2.txt");
@@ -188,7 +194,8 @@ private static void addParametersForFileMatchTests(LinkedList();
forbiddenFilePaths = new LinkedList();
forbiddenFilePaths.add(new FilePath(CompareType.PLAIN, "test2.txt"));
- config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, forbiddenFilePaths, true);
+ config = new GerritProject(CompareType.PLAIN, "project", branches, topics,
+ null, forbiddenFilePaths, true);
files = new LinkedList();
files.add("test2.txt");
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
@@ -203,7 +210,8 @@ private static void addParametersForFileMatchTests(LinkedList();
files.add("tests/test.txt");
files.add("tests/test2.txt");
- config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics, filePaths, forbiddenFilePaths,
+ config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics,
+ filePaths, forbiddenFilePaths,
false);
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
config, "projectNumber5", "feature/mymaster", null, files, true, false), });
@@ -215,7 +223,8 @@ private static void addParametersForFileMatchTests(LinkedList();
forbiddenFilePaths.add(new FilePath(CompareType.ANT, "**/*skip*"));
config = new GerritProject(
- CompareType.ANT, "vendor/**/project", branches, topics, filePaths, forbiddenFilePaths, false);
+ CompareType.ANT, "vendor/**/project", branches, topics,
+ filePaths, forbiddenFilePaths, false);
files = new LinkedList();
files.add("resources/test.xml");
files.add("files/skip.txt");
@@ -256,9 +265,7 @@ private static void addParametersForBypassFileMatchTests(LinkedList files;
boolean fileCheckNeeded;
@@ -282,9 +289,10 @@ public InterestingScenarioWithFiles(GerritProject config,
boolean fileCheckNeeded,
boolean expected) {
this.config = config;
- this.project = project;
- this.branch = branch;
- this.topic = topic;
+ this.change = new Change();
+ change.setProject(project);
+ change.setBranch(branch);
+ change.setTopic(topic);
this.files = files;
this.fileCheckNeeded = fileCheckNeeded;
this.fileCheckOccurred = false;
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/event/PluginHashtagsChangedEventTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/event/PluginHashtagsChangedEventTest.java
new file mode 100644
index 000000000..5b465fb58
--- /dev/null
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/event/PluginHashtagsChangedEventTest.java
@@ -0,0 +1,60 @@
+package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.event;
+
+import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginHashtagsChangedEvent;
+import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.TopicChanged;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for {@link PluginHashtagsChangedEvent}.
+ */
+public class PluginHashtagsChangedEventTest {
+
+ /**
+ * Tests that it should fire on all type of patchset.
+ */
+ @Test
+ public void shouldFireOnHashtagsChanged() {
+ PluginHashtagsChangedEvent pluginHashtagsChangedEvent =
+ new PluginHashtagsChangedEvent(".*Hashtag.*");
+ HashtagsChanged hashtagsChanged = Setup.createHashtagsChanged();
+
+ //should fire on hashtags changed.
+ assertTrue(pluginHashtagsChangedEvent.shouldTriggerOn(hashtagsChanged));
+
+ TopicChanged topicChanged = Setup.createTopicChanged();
+
+ //should not fire on topic changed.
+ assertFalse(pluginHashtagsChangedEvent.shouldTriggerOn(topicChanged));
+ }
+
+ /**
+ * Tests that it should not fire on draft patchset when they are excluded.
+ */
+ @Test
+ public void hashtagsRegExCheck() {
+ PluginHashtagsChangedEvent pluginHashtagsChangedEvent =
+ new PluginHashtagsChangedEvent("mHashtags.*");
+ HashtagsChanged hashtagsChanged = Setup.createHashtagsChanged();
+
+ //should not fire on pattern not match.
+ assertFalse(pluginHashtagsChangedEvent.shouldTriggerOn(hashtagsChanged));
+
+ PluginHashtagsChangedEvent pluginHashtagsChangedEvent1 =
+ new PluginHashtagsChangedEvent("aHashtag.*");
+
+ //should fire on pattern match.
+ assertTrue(pluginHashtagsChangedEvent1.shouldTriggerOn(hashtagsChanged));
+
+ PluginHashtagsChangedEvent pluginHashtagsChangedEvent2 =
+ new PluginHashtagsChangedEvent("");
+
+ //should fire on pattern empty.
+ assertTrue(pluginHashtagsChangedEvent2.shouldTriggerOn(hashtagsChanged));
+ }
+}
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/mock/Setup.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/mock/Setup.java
index 738fe9cee..e53a08f8f 100644
--- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/mock/Setup.java
+++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/mock/Setup.java
@@ -49,6 +49,7 @@
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeRestored;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.CommentAdded;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.DraftPublished;
+import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PrivateStateChanged;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.RefReplicated;
@@ -67,6 +68,7 @@
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import jenkins.model.Jenkins;
+import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
@@ -75,6 +77,7 @@
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
@@ -88,6 +91,7 @@
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.BRANCH;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CHANGE;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.EMAIL;
+import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.HASHTAGS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.ID;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.NAME;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.NUMBER;
@@ -215,6 +219,8 @@ public static PatchsetCreated createPatchsetCreated(String serverName,
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -262,6 +268,8 @@ public static DraftPublished createDraftPublished() {
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -288,6 +296,8 @@ public static ChangeAbandoned createChangeAbandoned() {
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -316,6 +326,8 @@ public static TopicChanged createTopicChanged() {
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
change.setTopic("new-topic");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -368,6 +380,8 @@ public static ChangeMerged createChangeMerged() {
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -399,6 +413,8 @@ public static ChangeMerged createChangeMergedWithPatchSetDate(String serverName,
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -433,6 +449,8 @@ public static DraftPublished createDraftPublishedWithPatchSetDate(String serverN
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -460,6 +478,8 @@ public static ChangeRestored createChangeRestored() {
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -489,6 +509,8 @@ public static CommentAdded createCommentAdded() {
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
event.setProvider(new Provider(PluginImpl.DEFAULT_SERVER_NAME, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
PatchSet patch = new PatchSet();
@@ -505,6 +527,47 @@ public static CommentAdded createCommentAdded() {
return event;
}
+ /**
+ * Gives you a HashtagsChanged mock.
+ * @return HashtagsChanged mock.
+ */
+ public static HashtagsChanged createHashtagsChanged() {
+ HashtagsChanged event = new HashtagsChanged();
+ Change change = new Change();
+ change.setBranch("branch");
+ change.setId("Iddaaddaa123456789");
+ change.setNumber("1000");
+ Account account = new Account();
+ account.setEmail("email@domain.com");
+ account.setName("Name");
+ change.setOwner(account);
+ change.setProject("project");
+ change.setSubject("subject");
+ change.setUrl("http://gerrit/1000");
+ List hashTags = List.of("hashtag1", "hashtag2");
+ change.setHashtags(hashTags);
+ event.setChange(change);
+ event.setProvider(new Provider(PluginImpl.DEFAULT_SERVER_NAME, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
+ PatchSet patch = new PatchSet();
+ patch.setNumber("1");
+ patch.setRevision("9999");
+ event.setPatchset(patch);
+
+ JSONObject jsonObject = new JSONObject();
+ List addedHashtags = new ArrayList<>();
+ addedHashtags.add("aHashtag1");
+ addedHashtags.add("aHashtag2");
+ List removedHashtags = new ArrayList<>();
+ removedHashtags.add("rHashtag1");
+ removedHashtags.add("rHashtag2");
+ jsonObject.put("hashtags", hashTags);
+ jsonObject.put("added", JSONArray.fromObject(addedHashtags));
+ jsonObject.put("removed", JSONArray.fromObject(removedHashtags));
+ event.fromJson(jsonObject);
+ event.setEventCreatedOn("1418133772");
+ return event;
+ }
+
/**
* Gives you a PatchsetCreated mock as a JSON object.
* @return PatchsetCreated mock.
@@ -524,6 +587,9 @@ public static JSONObject createPatchsetCreatedJson() {
change.put(SUBJECT, "subject");
change.put(URL, "http://gerrit/1000");
change.put(OWNER, account);
+ JSONArray hashtags = new JSONArray();
+ hashtags.add("hashtag");
+ change.put(HASHTAGS, hashtags);
event.put(CHANGE, change);
@@ -556,6 +622,8 @@ public static ManualPatchsetCreated createManualPatchsetCreated() {
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -925,6 +993,8 @@ public static PrivateStateChanged createPrivateStateChanged(String serverName,
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
@@ -959,6 +1029,8 @@ public static WipStateChanged createWipStateChanged(String serverName,
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
+ List hashtags = List.of("hashtag");
+ change.setHashtags(hashtags);
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");