Last active
December 17, 2015 09:49
-
-
Save oleg-nenashev/5590176 to your computer and use it in GitHub Desktop.
[Jenkins] Sample function, which gets Node from Jenkins according to StaplerRequest configuration request.
Function can be used as workaround for null "node" property at hudson.slaves.NodeProperty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets Node, which is being configured by StaplerRequest | |
* @param req StaplerRequest (ex, from NodePropertyDescriptor::newInstance()) | |
* @return Instance of the node, which is being configured (or null) | |
* | |
* @author Oleg Nenashev <[email protected]> | |
*/ | |
private static Node getNodePropertyOwner(StaplerRequest req) | |
{ | |
List<Ancestor> ancestors = req.getAncestors(); | |
if (ancestors.isEmpty()) | |
{ | |
assert false : "StaplerRequest is empty"; | |
return null; | |
} | |
Object node = ancestors.get(ancestors.size()-1).getObject(); | |
if (SlaveComputer.class.isAssignableFrom(node.getClass())) | |
{ | |
return ((SlaveComputer)node).getNode(); | |
} | |
else | |
{ | |
assert false : "StaplerRequest should have Node ancestor"; | |
return null; | |
} | |
} |
Yes, Jesse is definitely right.
Previously I've decided to avoid such approach, because I've expected that StaplerRequest::getAncestors() generates list after the first request. BTW, list is available at the start of NodePropertyDescriptor::newInstance()
Fixed code...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably want to use
Ancestor
rather than manually parsing the request URI!