From dae17374aa76c3afa9a8d775954e05def00d3072 Mon Sep 17 00:00:00 2001 From: Markus Winter Date: Mon, 27 May 2024 05:14:14 +0200 Subject: [PATCH] [JENKINS-64553] fix ItemListener not called after updating job via REST API or CLI (#9304) [JENKINS-64553] call ItemListener after updating job via cli when a job is updated via REST API or CLI, the ItemListener.fireOnUpdated is now called to inform about the change. --- .../main/java/hudson/model/AbstractItem.java | 1 + .../model/listeners/ItemListenerTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/src/main/java/hudson/model/AbstractItem.java b/core/src/main/java/hudson/model/AbstractItem.java index 0649bdb33b10..5bd1d6a02d79 100644 --- a/core/src/main/java/hudson/model/AbstractItem.java +++ b/core/src/main/java/hudson/model/AbstractItem.java @@ -937,6 +937,7 @@ public void updateByXml(Source source) throws IOException { // if everything went well, commit this new version out.commit(); SaveableListener.fireOnChange(this, getConfigFile()); + ItemListener.fireOnUpdated(this); } finally { out.abort(); // don't leave anything behind diff --git a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java index 6209790f1a97..b7147d3ebfe5 100644 --- a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java +++ b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java @@ -35,6 +35,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; /** @@ -58,6 +59,12 @@ public void setUp() { @Override public void onCopied(Item src, Item item) { events.append('Y'); } + + @Override + public void onUpdated(Item item) { + events.append('U'); + } + }; ItemListener.all().add(0, listener); } @@ -71,4 +78,18 @@ public void onCreatedViaCLI() { assertNotNull("job should be created: " + result, j.jenkins.getItem("testJob")); assertEquals("onCreated event should be triggered: " + result, "C", events.toString()); } + + @Issue("JENKINS-64553") + @Test + public void onUpdatedViaCLI() { + CLICommandInvoker.Result result = new CLICommandInvoker(j, "create-job"). + withStdin(new ByteArrayInputStream("".getBytes(Charset.defaultCharset()))). + invokeWithArgs("testJob"); + assertThat(result, CLICommandInvoker.Matcher.succeeded()); + result = new CLICommandInvoker(j, "update-job"). + withStdin(new ByteArrayInputStream("".getBytes(Charset.defaultCharset()))). + invokeWithArgs("testJob"); + assertThat(result, CLICommandInvoker.Matcher.succeeded()); + assertEquals("onUpdated event should be triggered: " + result, "CU", events.toString()); + } }