Skip to content

Instantly share code, notes, and snippets.

@oleg-nenashev
Last active December 17, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleg-nenashev/5590176 to your computer and use it in GitHub Desktop.
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
/**
* 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 <nenashev@synopsys.com>
*/
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;
}
}
@jglick
Copy link

jglick commented May 16, 2013

You probably want to use Ancestor rather than manually parsing the request URI!

@oleg-nenashev
Copy link
Author

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