From 2e2df968338ef3cbcf1bf289d84dbea339faae7e Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Tue, 12 Nov 2024 17:05:12 -0700 Subject: [PATCH] fix: prevent watching from applying to all jobs Fixes the watching feature so that when a user watches a given job, it only applies to that job. Previously, when a job was watched, the project was not stored in the user property, so it would end up watching all jobs. --- .../emailext/ExtendedEmailPublisher.java | 6 ++- .../watching/EmailExtWatchAction.java | 32 +++++++++------ .../watching/EmailExtWatchJobProperty.java | 40 +++++++++++-------- .../watching/EmailExtWatchAction/index.groovy | 2 +- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java b/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java index 7d312c5c0..52259bbce 100644 --- a/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java +++ b/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java @@ -368,7 +368,11 @@ public void setMatrixTriggerMode(MatrixTriggerMode matrixTriggerMode) { @NonNull @Override public Collection getProjectActions(AbstractProject project) { - return Collections.singletonList(new EmailExtWatchAction(project)); + EmailExtWatchAction action = project.getAction(EmailExtWatchAction.class); + if (action == null) { + action = new EmailExtWatchAction(project); + } + return Collections.singletonList(action); } public void debug(PrintStream p, String format, Object... args) { diff --git a/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchAction.java b/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchAction.java index 61654a2be..778407746 100644 --- a/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchAction.java +++ b/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchAction.java @@ -35,11 +35,13 @@ public class EmailExtWatchAction implements Action { */ public static class UserProperty extends hudson.model.UserProperty { private List triggers = new ArrayList<>(); + private String projectName; - public UserProperty(List triggers) { + public UserProperty(String projectId, List triggers) { if (triggers != null) { this.triggers = Collections.unmodifiableList(triggers); } + this.projectName = projectId; } @Exported @@ -51,6 +53,14 @@ private void clearTriggers() { triggers = Collections.emptyList(); } + public String getProjectName() { + return projectName; + } + + public void setProjectName(String projectName) { + this.projectName = projectName; + } + @Extension public static final class DescriptorImpl extends UserPropertyDescriptor { @@ -66,7 +76,7 @@ public String getDisplayName() { @Override public UserProperty newInstance(User user) { - return new UserProperty(null); + return new UserProperty("", null); } @NonNull @@ -74,12 +84,12 @@ public UserProperty newInstance(User user) { public UserProperty newInstance(StaplerRequest req, @NonNull JSONObject json) throws FormException { List triggers = req != null ? req.bindJSONToList(EmailTrigger.class, json) : Collections.emptyList(); - return new UserProperty(triggers); + return new UserProperty("", triggers); } } } - private AbstractProject project; + private final AbstractProject project; public EmailExtWatchAction(AbstractProject project) { this.project = project; @@ -114,15 +124,14 @@ public boolean isWatching() { } public List getTriggers() { - List triggers = null; User current = User.current(); if (current != null) { UserProperty p = current.getProperty(UserProperty.class); - if (p != null) { - triggers = p.getTriggers(); + if (p != null && p.getProjectName().equalsIgnoreCase(project.getFullName())) { + return p.getTriggers(); } } - return triggers; + return null; } public EmailExtWatchJobProperty getJobProperty() throws IOException { @@ -135,12 +144,11 @@ public EmailExtWatchJobProperty getJobProperty() throws IOException { } public Mailer.UserProperty getMailerProperty() { - Mailer.UserProperty prop = null; User current = User.current(); if (current != null) { - prop = current.getProperty(Mailer.UserProperty.class); + return current.getProperty(Mailer.UserProperty.class); } - return prop; + return null; } public ExtendedEmailPublisher getPublisher() { @@ -205,7 +213,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOExc } startWatching(); - user.addProperty(new UserProperty(triggers)); + user.addProperty(new UserProperty(project.getFullName(), triggers)); } } rsp.sendRedirect(project.getAbsoluteUrl()); diff --git a/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchJobProperty.java b/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchJobProperty.java index d26c5f1a2..41348ee0b 100644 --- a/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchJobProperty.java +++ b/src/main/java/hudson/plugins/emailext/watching/EmailExtWatchJobProperty.java @@ -33,36 +33,42 @@ public List getWatchers() { public void addWatcher(User user) { String existing = null; - for (String u : watchers) { - if (u.compareTo(user.getId()) == 0) { - existing = u; - break; + synchronized (watchers) { + for (String u : watchers) { + if (u.compareTo(user.getId()) == 0) { + existing = u; + break; + } } - } - if (existing == null) { - watchers.add(user.getId()); + if (existing == null) { + watchers.add(user.getId()); + } } } public void removeWatcher(User user) { String remove = null; - for (String u : watchers) { - if (u.compareTo(user.getId()) == 0) { - remove = u; - break; + synchronized (watchers) { + for (String u : watchers) { + if (u.compareTo(user.getId()) == 0) { + remove = u; + break; + } } - } - if (remove != null) { - watchers.remove(user.getId()); + if (remove != null) { + watchers.remove(user.getId()); + } } } public boolean isWatching(User user) { - for (String u : watchers) { - if (u.compareTo(user.getId()) == 0) { - return true; + synchronized (watchers) { + for (String u : watchers) { + if (u.compareTo(user.getId()) == 0) { + return true; + } } } return false; diff --git a/src/main/resources/hudson/plugins/emailext/watching/EmailExtWatchAction/index.groovy b/src/main/resources/hudson/plugins/emailext/watching/EmailExtWatchAction/index.groovy index 092f3c064..50514a750 100644 --- a/src/main/resources/hudson/plugins/emailext/watching/EmailExtWatchAction/index.groovy +++ b/src/main/resources/hudson/plugins/emailext/watching/EmailExtWatchAction/index.groovy @@ -45,7 +45,7 @@ l.layout(norefresh: true) { f.entry(title: _("Content Token Reference"), help: descriptor.getHelpFile('tokens')) def triggers = hudson.plugins.emailext.plugins.EmailTrigger.all().findAll { t -> t.isWatchable() } - def configuredTriggers = (my != null && my.triggers.size() > 0) ? my.triggers : [] + def configuredTriggers = (my != null && my?.triggers?.size() > 0) ? my.triggers : [] // do we want to filter the triggers somehow so that only some show up? showSendTo = false