Skip to content

Commit

Permalink
[FIXED JENKINS-14759] Job created by posting config.xml to /createIte…
Browse files Browse the repository at this point in the history
…m does not set GitHub webhook.
  • Loading branch information
jglick committed Sep 6, 2012
1 parent 4557a43 commit 597ea1e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -55,6 +55,9 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=bug>
Job created by posting <code>config.xml</code> to <code>/createItem</code> does not set GitHub webhook.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-14759">issue 14759</a>)
<li class=bug>
“Took…on master” shown for a build which ran on a slave which was since deleted.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-15042">issue 15042</a>)
Expand Down
10 changes: 7 additions & 3 deletions core/src/main/java/hudson/model/AbstractItem.java
Expand Up @@ -34,7 +34,6 @@
import hudson.cli.declarative.CLIResolver;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.SaveableListener;
import hudson.search.SearchIndexBuilder;
import hudson.security.AccessControlled;
import hudson.security.Permission;
import hudson.security.ACL;
Expand Down Expand Up @@ -586,7 +585,12 @@ public void updateByXml(Source source) throws IOException {

// try to reflect the changes by reloading
new XmlFile(Items.XSTREAM, out.getTemporaryFile()).unmarshal(this);
onLoad(getParent(), getRootDir().getName());
Items.updatingByXml.set(true);
try {
onLoad(getParent(), getRootDir().getName());
} finally {
Items.updatingByXml.set(false);
}
Jenkins.getInstance().rebuildDependencyGraph();

// if everything went well, commit this new version
Expand All @@ -596,7 +600,7 @@ public void updateByXml(Source source) throws IOException {
out.abort(); // don't leave anything behind
}
}


/* (non-Javadoc)
* @see hudson.model.AbstractModelObject#getSearchName()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -283,7 +283,7 @@ public R create(File dir) throws IOException {
OldDataMonitor.report(this, "1.28");
}
for (Trigger t : triggers)
t.start(this,false);
t.start(this, Items.updatingByXml.get());
if(scm==null)
scm = new NullSCM(); // perhaps it was pointing to a plugin that no longer exists.

Expand Down
15 changes: 13 additions & 2 deletions core/src/main/java/hudson/model/ItemGroupMixIn.java
Expand Up @@ -203,7 +203,12 @@ public synchronized <T extends TopLevelItem> T copy(T src, String name) throws I
Util.copyFile(Items.getConfigFile(src).getFile(),Items.getConfigFile(result).getFile());

// reload from the new config
result = (T)Items.load(parent,result.getRootDir());
Items.updatingByXml.set(true);
try {
result = (T)Items.load(parent,result.getRootDir());
} finally {
Items.updatingByXml.set(false);
}
result.onCopiedFrom(src);

add(result);
Expand All @@ -224,7 +229,13 @@ public synchronized TopLevelItem createProjectFromXML(String name, InputStream x
IOUtils.copy(xml,configXml);

// load it
TopLevelItem result = (TopLevelItem)Items.load(parent,configXml.getParentFile());
TopLevelItem result;
Items.updatingByXml.set(true);
try {
result = (TopLevelItem)Items.load(parent,configXml.getParentFile());
} finally {
Items.updatingByXml.set(false);
}
add(result);

ItemListener.fireOnCreated(result);
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/hudson/model/Items.java
Expand Up @@ -30,6 +30,7 @@
import hudson.matrix.MatrixConfiguration;
import hudson.XmlFile;
import hudson.matrix.Axis;
import hudson.triggers.Trigger;
import hudson.util.DescriptorList;
import hudson.util.XStream2;
import jenkins.model.Jenkins;
Expand All @@ -53,6 +54,17 @@ public class Items {
*/
public static final List<TopLevelItemDescriptor> LIST = (List)new DescriptorList<TopLevelItem>(TopLevelItem.class);

/**
* Used to behave differently when loading posted configuration as opposed to persisted configuration.
* @see Trigger#start
* @since 1.482
*/
static final ThreadLocal<Boolean> updatingByXml = new ThreadLocal<Boolean>() {
@Override protected Boolean initialValue() {
return false;
}
};

/**
* Returns all the registered {@link TopLevelItemDescriptor}s.
*/
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/triggers/Trigger.java
Expand Up @@ -80,7 +80,7 @@ public abstract class Trigger<J extends Item> implements Describable<Trigger<?>>
* @param project
* given so that the persisted form of this object won't have to have a back pointer.
* @param newInstance
* True if this is a newly created trigger first attached to the {@link Project}.
* True if this may be a newly created trigger first attached to the {@link Project} (generally if the project is being created or configured).
* False if this is invoked for a {@link Project} loaded from disk.
*/
public void start(J project, boolean newInstance) {
Expand Down
2 changes: 2 additions & 0 deletions test/pom.xml
Expand Up @@ -235,6 +235,7 @@ THE SOFTWARE.
<includes>
<include>**/UseRecipesWithJenkinsRuleTest.java</include>
<include>hudson/model/CauseTest.java</include>
<include>hudson/triggers/TriggerStartTest.java</include>
</includes>
</configuration>
</plugin>
Expand Down Expand Up @@ -279,6 +280,7 @@ THE SOFTWARE.
<excludes>
<exclude>org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest.class</exclude>
<exclude>hudson/model/CauseTest.class</exclude>
<exclude>hudson/triggers/TriggerStartTest.class</exclude>
</excludes>
</configuration>
</execution>
Expand Down
138 changes: 138 additions & 0 deletions test/src/test/java/hudson/triggers/TriggerStartTest.java
@@ -0,0 +1,138 @@
/*
* The MIT License
*
* Copyright 2012 Jesse Glick.
*
* 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 hudson.triggers;

import hudson.model.AbstractProject;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.Items;
import java.io.ByteArrayInputStream;
import java.io.ObjectStreamException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;

@Bug(14759)
public class TriggerStartTest {

@Rule public JenkinsRule j = new JenkinsRule();

@Test public void loadCallsStartFalse() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
MockTrigger t = new MockTrigger();
p.addTrigger(t);
p.save();
p = (FreeStyleProject) Items.load(p.getParent(), p.getRootDir());
t = p.getTrigger(MockTrigger.class);
assertNotNull(t);
assertEquals("[false]", t.calls.toString());
}

@Test public void submitCallsStartTrue() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
MockTrigger t = new MockTrigger();
p.addTrigger(t);
p.save();
p = j.configRoundtrip(p);
t = p.getTrigger(MockTrigger.class);
assertNotNull(t);
assertEquals("[true]", t.calls.toString());
}

@Test public void updateByXmlCallsStartTrue() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
String xml = p.getConfigFile().asString();
xml = xml.replace(" <triggers class=\"vector\"/>\n", triggersSection());
Source newXML = new StreamSource(new StringReader(xml));
p.updateByXml(newXML);
MockTrigger t = p.getTrigger(MockTrigger.class);
assertNotNull(t);
assertEquals("[true]", t.calls.toString());
}

@Test public void createProjectFromXmlCallsStartTrue() throws Exception {
FreeStyleProject p = (FreeStyleProject) j.jenkins.createProjectFromXML("whatever", new ByteArrayInputStream(("<project>\n <builders/>\n <publishers/>\n <buildWrappers/>\n" + triggersSection() + "</project>").getBytes()));
MockTrigger t = p.getTrigger(MockTrigger.class);
assertNotNull(t);
assertEquals("[true]", t.calls.toString());
}

@Test public void copyCallsStartTrue() throws Exception {
AbstractProject<?,?> p = j.createFreeStyleProject();
MockTrigger t = new MockTrigger();
p.addTrigger(t);
p.save();
p = j.jenkins.copy(p, "nue");
t = p.getTrigger(MockTrigger.class);
assertNotNull(t);
assertEquals("[true]", t.calls.toString());
}

private String triggersSection() {
String tagname = MockTrigger.class.getName().replace("$", "_-");
return " <triggers class=\"vector\">\n <" + tagname + ">\n <spec/>\n </" + tagname + ">\n </triggers>\n";
}

public static class MockTrigger extends Trigger<Item> {

public transient List<Boolean> calls = new ArrayList<Boolean>();

@DataBoundConstructor public MockTrigger() {}

@Override public void start(Item project, boolean newInstance) {
super.start(project, newInstance);
calls.add(newInstance);
}

@Override protected Object readResolve() throws ObjectStreamException {
calls = new ArrayList<Boolean>();
return super.readResolve();
}

@TestExtension
public static class DescriptorImpl extends TriggerDescriptor {

@Override public boolean isApplicable(Item item) {
return true;
}

@Override public String getDisplayName() {
return "mock trigger";
}

}

}

}

0 comments on commit 597ea1e

Please sign in to comment.