Skip to content

Commit

Permalink
fix: prevent watching from applying to all jobs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slide committed Nov 13, 2024
1 parent e23cec3 commit 2e2df96
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ public void setMatrixTriggerMode(MatrixTriggerMode matrixTriggerMode) {
@NonNull
@Override
public Collection<? extends Action> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public class EmailExtWatchAction implements Action {
*/
public static class UserProperty extends hudson.model.UserProperty {
private List<EmailTrigger> triggers = new ArrayList<>();
private String projectName;

public UserProperty(List<EmailTrigger> triggers) {
public UserProperty(String projectId, List<EmailTrigger> triggers) {
if (triggers != null) {
this.triggers = Collections.unmodifiableList(triggers);
}
this.projectName = projectId;
}

@Exported
Expand All @@ -51,6 +53,14 @@ private void clearTriggers() {
triggers = Collections.emptyList();
}

public String getProjectName() {
return projectName;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}

Check warning on line 62 in src/main/java/hudson/plugins/emailext/watching/EmailExtWatchAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 57-62 are not covered by tests

@Extension
public static final class DescriptorImpl extends UserPropertyDescriptor {

Expand All @@ -66,20 +76,20 @@ public String getDisplayName() {

@Override
public UserProperty newInstance(User user) {
return new UserProperty(null);
return new UserProperty("", null);
}

@NonNull
@Override
public UserProperty newInstance(StaplerRequest req, @NonNull JSONObject json) throws FormException {
List<EmailTrigger> 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;
Expand Down Expand Up @@ -114,15 +124,14 @@ public boolean isWatching() {
}

public List<EmailTrigger> getTriggers() {
List<EmailTrigger> 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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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));

Check warning on line 216 in src/main/java/hudson/plugins/emailext/watching/EmailExtWatchAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 87-216 are not covered by tests
}
}
rsp.sendRedirect(project.getAbsoluteUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,42 @@ public List<String> 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;
}
}
}

Check warning on line 73 in src/main/java/hudson/plugins/emailext/watching/EmailExtWatchJobProperty.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 36-73 are not covered by tests
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e2df96

Please sign in to comment.